Compare commits

...

5 Commits

Author SHA1 Message Date
Malo
e8e8a05a8b
Merge ccabbe5bec4ea62fb05747154b0027f70ab3c811 into 627f5b9d4f39f91affc529f276bb500d42887dae 2025-07-18 01:28:19 +08:00
Lachlan Kermode
627f5b9d4f
Add show rule for smallcaps in HTML (#6600) 2025-07-17 16:09:13 +00:00
Malo
ccabbe5bec Add test 2025-07-12 14:30:29 +01:00
Malo
b374aa131b Update tests 2025-07-12 14:20:11 +01:00
Malo
c69c07fab5 Fix HTML smartquotes 2025-07-12 14:20:02 +01:00
8 changed files with 84 additions and 15 deletions

View File

@ -1,3 +1,5 @@
use crate::fragment::html_fragment;
use crate::{attr, tag, FrameElem, HtmlElem, HtmlElement, HtmlFrame, HtmlNode};
use typst_library::diag::{warning, SourceResult};
use typst_library::engine::Engine;
use typst_library::foundations::{Content, StyleChain, Target, TargetElem};
@ -5,10 +7,10 @@ use typst_library::introspection::{SplitLocator, TagElem};
use typst_library::layout::{Abs, Axes, BlockBody, BlockElem, BoxElem, Region, Size};
use typst_library::model::ParElem;
use typst_library::routines::Pair;
use typst_library::text::{LinebreakElem, SmartQuoteElem, SpaceElem, TextElem};
use crate::fragment::html_fragment;
use crate::{attr, tag, FrameElem, HtmlElem, HtmlElement, HtmlFrame, HtmlNode};
use typst_library::text::{
is_default_ignorable, LinebreakElem, SmartQuoteElem, SmartQuoter, SmartQuotes,
SpaceElem, TextElem,
};
/// Converts realized content into HTML nodes.
pub fn convert_to_nodes<'a>(
@ -16,9 +18,10 @@ pub fn convert_to_nodes<'a>(
locator: &mut SplitLocator,
children: impl IntoIterator<Item = Pair<'a>>,
) -> SourceResult<Vec<HtmlNode>> {
let mut quoter = SmartQuoter::new();
let mut output = Vec::new();
for (child, styles) in children {
handle(engine, child, locator, styles, &mut output)?;
handle(engine, child, locator, styles, &mut quoter, &mut output)?;
}
Ok(output)
}
@ -29,6 +32,7 @@ fn handle(
child: &Content,
locator: &mut SplitLocator,
styles: StyleChain,
quoter: &mut SmartQuoter,
output: &mut Vec<HtmlNode>,
) -> SourceResult<()> {
if let Some(elem) = child.to_packed::<TagElem>() {
@ -95,10 +99,20 @@ fn handle(
} else if let Some(elem) = child.to_packed::<LinebreakElem>() {
output.push(HtmlElement::new(tag::br).spanned(elem.span()).into());
} else if let Some(elem) = child.to_packed::<SmartQuoteElem>() {
output.push(HtmlNode::text(
if elem.double.get(styles) { '"' } else { '\'' },
child.span(),
));
let double = elem.double.get(styles);
if elem.enabled.get(styles) {
let before = last_char(output);
let quotes = SmartQuotes::get(
elem.quotes.get_ref(styles),
styles.get(TextElem::lang),
styles.get(TextElem::region),
elem.alternative.get(styles),
);
let quote = quoter.quote(before, &quotes, double);
output.push(HtmlNode::text(quote, child.span()));
} else {
output.push(HtmlNode::text(if double { '"' } else { '\'' }, child.span()));
}
} else if let Some(elem) = child.to_packed::<FrameElem>() {
let locator = locator.next(&elem.span());
let style = TargetElem::target.set(Target::Paged).wrap();
@ -120,6 +134,20 @@ fn handle(
Ok(())
}
/// Returns the last non-default ignorable character from the passed nodes.
fn last_char(nodes: &[HtmlNode]) -> Option<char> {
for node in nodes.iter().rev() {
if let Some(c) = match node {
HtmlNode::Text(s, _) => s.chars().rev().find(|&c| !is_default_ignorable(c)),
HtmlNode::Element(e) => last_char(&e.children),
_ => None,
} {
return Some(c);
}
}
None
}
/// Checks whether the given element is an inline-level HTML element.
pub fn is_inline(elem: &Content) -> bool {
elem.to_packed::<HtmlElem>()

View File

@ -14,8 +14,8 @@ use typst_library::model::{
RefElem, StrongElem, TableCell, TableElem, TermsElem,
};
use typst_library::text::{
HighlightElem, LinebreakElem, OverlineElem, RawElem, RawLine, SpaceElem, StrikeElem,
SubElem, SuperElem, UnderlineElem,
HighlightElem, LinebreakElem, OverlineElem, RawElem, RawLine, SmallcapsElem,
SpaceElem, StrikeElem, SubElem, SuperElem, UnderlineElem,
};
use typst_library::visualize::ImageElem;
@ -47,6 +47,7 @@ pub fn register(rules: &mut NativeRuleMap) {
rules.register(Html, OVERLINE_RULE);
rules.register(Html, STRIKE_RULE);
rules.register(Html, HIGHLIGHT_RULE);
rules.register(Html, SMALLCAPS_RULE);
rules.register(Html, RAW_RULE);
rules.register(Html, RAW_LINE_RULE);
@ -390,6 +391,20 @@ const STRIKE_RULE: ShowFn<StrikeElem> =
const HIGHLIGHT_RULE: ShowFn<HighlightElem> =
|elem, _, _| Ok(HtmlElem::new(tag::mark).with_body(Some(elem.body.clone())).pack());
const SMALLCAPS_RULE: ShowFn<SmallcapsElem> = |elem, _, styles| {
Ok(HtmlElem::new(tag::span)
.with_attr(
attr::style,
if elem.all.get(styles) {
"font-variant-caps: all-small-caps"
} else {
"font-variant-caps: small-caps"
},
)
.with_body(Some(elem.body.clone()))
.pack())
};
const RAW_RULE: ShowFn<RawElem> = |elem, _, styles| {
let lines = elem.lines.as_deref().unwrap_or_default();

View File

@ -6,8 +6,8 @@
</head>
<body>
<h2>Heading is no paragraph</h2>
<p>I'm a paragraph.</p>
<div>I'm not.</div>
<p>Im a paragraph.</p>
<div>Im not.</div>
<div>
<p>We are two.</p>
<p>So we are paragraphs.</p>

View File

@ -5,6 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>When you said that “he surely meant that she intended to say “I'm sorry””, I was quite confused.</p>
<p>When you said that “he surely meant that she intended to say “Im sorry””, I was quite confused.</p>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p><span style="font-variant-caps: small-caps">Test 012</span><br><span style="font-variant-caps: all-small-caps">Test 012</span></p>
</body>
</html>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>When you said that “he surely meant that she intended to say “Im sorry””, I was quite confused.</p>
<p><span style="display: inline-block;">box</span></p>
</body>
</html>

View File

@ -11,6 +11,6 @@
#show smallcaps: set text(fill: red)
#smallcaps[Smallcaps]
--- smallcaps-all ---
--- smallcaps-all render html ---
#smallcaps(all: false)[Test 012] \
#smallcaps(all: true)[Test 012]

View File

@ -172,3 +172,8 @@ Some people's thought on this would be #[#set smartquote(enabled: false); "stran
--- issue-5146-smartquotes-after-equations ---
$i$'s $i$ 's
--- smartquotes-html html ---
When you said that "he surely meant that 'she intended to say "I'm sorry"'", I was quite confused.
'#box[box]'