mirror of
https://github.com/typst/typst
synced 2025-08-23 03:04:14 +08:00
Compare commits
7 Commits
d3caedd813
...
e8ce894ee7
Author | SHA1 | Date | |
---|---|---|---|
|
e8ce894ee7 | ||
|
9311f6f08e | ||
|
7420ec972f | ||
|
5dd5771df0 | ||
|
04fd0acaca | ||
|
6a1d6c08e2 | ||
|
35809387f8 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -413,7 +413,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "codex"
|
||||
version = "0.1.1"
|
||||
source = "git+https://github.com/typst/codex?rev=56eb217#56eb2172fc0670f4c1c8b79a63d11f9354e5babe"
|
||||
source = "git+https://github.com/typst/codex?rev=a5428cb#a5428cb9c81a41354d44b44dbd5a16a710bbd928"
|
||||
|
||||
[[package]]
|
||||
name = "color-print"
|
||||
|
@ -47,7 +47,7 @@ clap = { version = "4.4", features = ["derive", "env", "wrap_help"] }
|
||||
clap_complete = "4.2.1"
|
||||
clap_mangen = "0.2.10"
|
||||
codespan-reporting = "0.11"
|
||||
codex = { git = "https://github.com/typst/codex", rev = "56eb217" }
|
||||
codex = { git = "https://github.com/typst/codex", rev = "a5428cb" }
|
||||
color-print = "0.3.6"
|
||||
comemo = "0.4"
|
||||
csv = "1"
|
||||
|
@ -205,7 +205,9 @@ impl Eval for ast::Label<'_> {
|
||||
type Output = Value;
|
||||
|
||||
fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
|
||||
Ok(Value::Label(Label::new(PicoStr::intern(self.get()))))
|
||||
Ok(Value::Label(
|
||||
Label::new(PicoStr::intern(self.get())).expect("unexpected empty label"),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,7 +215,8 @@ impl Eval for ast::Ref<'_> {
|
||||
type Output = Content;
|
||||
|
||||
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
|
||||
let target = Label::new(PicoStr::intern(self.target()));
|
||||
let target = Label::new(PicoStr::intern(self.target()))
|
||||
.expect("unexpected empty reference");
|
||||
let mut elem = RefElem::new(target);
|
||||
if let Some(supplement) = self.supplement() {
|
||||
elem.push_supplement(Smart::Custom(Some(Supplement::Content(
|
||||
|
@ -3,9 +3,8 @@ use std::fmt::Write;
|
||||
use typst_library::diag::{bail, At, SourceResult, StrResult};
|
||||
use typst_library::foundations::Repr;
|
||||
use typst_library::html::{
|
||||
attr, charsets, tag, HtmlDocument, HtmlElement, HtmlNode, HtmlTag,
|
||||
attr, charsets, tag, HtmlDocument, HtmlElement, HtmlFrame, HtmlNode, HtmlTag,
|
||||
};
|
||||
use typst_library::layout::Frame;
|
||||
use typst_syntax::Span;
|
||||
|
||||
/// Encodes an HTML document into a string.
|
||||
@ -304,9 +303,15 @@ fn write_escape(w: &mut Writer, c: char) -> StrResult<()> {
|
||||
}
|
||||
|
||||
/// Encode a laid out frame into the writer.
|
||||
fn write_frame(w: &mut Writer, frame: &Frame) {
|
||||
fn write_frame(w: &mut Writer, frame: &HtmlFrame) {
|
||||
// FIXME: This string replacement is obviously a hack.
|
||||
let svg = typst_svg::svg_frame(frame)
|
||||
.replace("<svg class", "<svg style=\"overflow: visible;\" class");
|
||||
let svg = typst_svg::svg_frame(&frame.inner).replace(
|
||||
"<svg class",
|
||||
&format!(
|
||||
"<svg style=\"overflow: visible; width: {}em; height: {}em;\" class",
|
||||
frame.inner.width() / frame.text_size,
|
||||
frame.inner.height() / frame.text_size,
|
||||
),
|
||||
);
|
||||
w.buf.push_str(&svg);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use typst_library::diag::{bail, warning, At, SourceResult};
|
||||
use typst_library::engine::{Engine, Route, Sink, Traced};
|
||||
use typst_library::foundations::{Content, StyleChain, Target, TargetElem};
|
||||
use typst_library::html::{
|
||||
attr, tag, FrameElem, HtmlDocument, HtmlElem, HtmlElement, HtmlNode,
|
||||
attr, tag, FrameElem, HtmlDocument, HtmlElem, HtmlElement, HtmlFrame, HtmlNode,
|
||||
};
|
||||
use typst_library::introspection::{
|
||||
Introspector, Locator, LocatorLink, SplitLocator, TagElem,
|
||||
@ -246,7 +246,10 @@ fn handle(
|
||||
styles.chain(&style),
|
||||
Region::new(Size::splat(Abs::inf()), Axes::splat(false)),
|
||||
)?;
|
||||
output.push(HtmlNode::Frame(frame));
|
||||
output.push(HtmlNode::Frame(HtmlFrame {
|
||||
inner: frame,
|
||||
text_size: TextElem::size_in(styles),
|
||||
}));
|
||||
} else {
|
||||
engine.sink.warn(warning!(
|
||||
child.span(),
|
||||
|
@ -448,7 +448,7 @@ fn field_access_completions(
|
||||
match value {
|
||||
Value::Symbol(symbol) => {
|
||||
for modifier in symbol.modifiers() {
|
||||
if let Ok(modified) = symbol.clone().modified(modifier) {
|
||||
if let Ok(modified) = symbol.clone().modified((), modifier) {
|
||||
ctx.completions.push(Completion {
|
||||
kind: CompletionKind::Symbol(modified.get()),
|
||||
label: modifier.into(),
|
||||
|
@ -72,7 +72,8 @@ pub fn definition(
|
||||
|
||||
// Try to jump to the referenced content.
|
||||
DerefTarget::Ref(node) => {
|
||||
let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target()));
|
||||
let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target()))
|
||||
.expect("unexpected empty reference");
|
||||
let selector = Selector::Label(label);
|
||||
let elem = document?.introspector.query_first(&selector)?;
|
||||
return Some(Definition::Span(elem.span()));
|
||||
|
@ -1,7 +1,8 @@
|
||||
use ecow::{eco_format, EcoString};
|
||||
use typst_utils::{PicoStr, ResolvedPicoStr};
|
||||
|
||||
use crate::foundations::{func, scope, ty, Repr, Str};
|
||||
use crate::diag::StrResult;
|
||||
use crate::foundations::{bail, func, scope, ty, Repr, Str};
|
||||
|
||||
/// A label for an element.
|
||||
///
|
||||
@ -27,7 +28,8 @@ use crate::foundations::{func, scope, ty, Repr, Str};
|
||||
/// # Syntax
|
||||
/// This function also has dedicated syntax: You can create a label by enclosing
|
||||
/// its name in angle brackets. This works both in markup and code. A label's
|
||||
/// name can contain letters, numbers, `_`, `-`, `:`, and `.`.
|
||||
/// name can contain letters, numbers, `_`, `-`, `:`, and `.`. A label cannot
|
||||
/// be empty.
|
||||
///
|
||||
/// Note that there is a syntactical difference when using the dedicated syntax
|
||||
/// for this function. In the code below, the `[<a>]` terminates the heading and
|
||||
@ -50,8 +52,11 @@ pub struct Label(PicoStr);
|
||||
|
||||
impl Label {
|
||||
/// Creates a label from an interned string.
|
||||
pub fn new(name: PicoStr) -> Self {
|
||||
Self(name)
|
||||
///
|
||||
/// Returns `None` if the given string is empty.
|
||||
pub fn new(name: PicoStr) -> Option<Self> {
|
||||
const EMPTY: PicoStr = PicoStr::constant("");
|
||||
(name != EMPTY).then_some(Self(name))
|
||||
}
|
||||
|
||||
/// Resolves the label to a string.
|
||||
@ -70,10 +75,14 @@ impl Label {
|
||||
/// Creates a label from a string.
|
||||
#[func(constructor)]
|
||||
pub fn construct(
|
||||
/// The name of the label.
|
||||
/// The name of the label. Must not be empty.
|
||||
name: Str,
|
||||
) -> Label {
|
||||
Self(PicoStr::intern(name.as_str()))
|
||||
) -> StrResult<Label> {
|
||||
if name.is_empty() {
|
||||
bail!("label name must not be empty");
|
||||
}
|
||||
|
||||
Ok(Self(PicoStr::intern(name.as_str())))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,11 +19,8 @@ use crate::foundations::{repr, ty, Content, Scope, Value};
|
||||
///
|
||||
/// You can access definitions from the module using [field access
|
||||
/// notation]($scripting/#fields) and interact with it using the [import and
|
||||
/// include syntaxes]($scripting/#modules). Alternatively, it is possible to
|
||||
/// convert a module to a dictionary, and therefore access its contents
|
||||
/// dynamically, using the [dictionary constructor]($dictionary/#constructor).
|
||||
/// include syntaxes]($scripting/#modules).
|
||||
///
|
||||
/// # Example
|
||||
/// ```example
|
||||
/// <<< #import "utils.typ"
|
||||
/// <<< #utils.add(2, 5)
|
||||
@ -34,6 +31,20 @@ use crate::foundations::{repr, ty, Content, Scope, Value};
|
||||
/// >>>
|
||||
/// >>> #(-3)
|
||||
/// ```
|
||||
///
|
||||
/// You can check whether a definition is present in a module using the `{in}`
|
||||
/// operator, with a string on the left-hand side. This can be useful to
|
||||
/// [conditionally access]($category/foundations/std/#conditional-access)
|
||||
/// definitions in a module.
|
||||
///
|
||||
/// ```example
|
||||
/// #("table" in std) \
|
||||
/// #("nope" in std)
|
||||
/// ```
|
||||
///
|
||||
/// Alternatively, it is possible to convert a module to a dictionary, and
|
||||
/// therefore access its contents dynamically, using the [dictionary
|
||||
/// constructor]($dictionary/#constructor).
|
||||
#[ty(cast)]
|
||||
#[derive(Clone, Hash)]
|
||||
#[allow(clippy::derived_hash_with_manual_eq)]
|
||||
|
@ -558,6 +558,7 @@ pub fn contains(lhs: &Value, rhs: &Value) -> Option<bool> {
|
||||
(Str(a), Str(b)) => Some(b.as_str().contains(a.as_str())),
|
||||
(Dyn(a), Str(b)) => a.downcast::<Regex>().map(|regex| regex.is_match(b)),
|
||||
(Str(a), Dict(b)) => Some(b.contains(a)),
|
||||
(Str(a), Module(b)) => Some(b.scope().get(a).is_some()),
|
||||
(a, Array(b)) => Some(b.contains(a.clone())),
|
||||
|
||||
_ => Option::None,
|
||||
|
@ -8,7 +8,7 @@ use serde::{Serialize, Serializer};
|
||||
use typst_syntax::{is_ident, Span, Spanned};
|
||||
use typst_utils::hash128;
|
||||
|
||||
use crate::diag::{bail, SourceResult, StrResult};
|
||||
use crate::diag::{bail, DeprecationSink, SourceResult, StrResult};
|
||||
use crate::foundations::{
|
||||
cast, elem, func, scope, ty, Array, Content, Func, NativeElement, NativeFunc, Packed,
|
||||
PlainText, Repr as _,
|
||||
@ -54,18 +54,22 @@ enum Repr {
|
||||
/// A native symbol that has no named variant.
|
||||
Single(char),
|
||||
/// A native symbol with multiple named variants.
|
||||
Complex(&'static [(ModifierSet<&'static str>, char)]),
|
||||
Complex(&'static [Variant<&'static str>]),
|
||||
/// A symbol with multiple named variants, where some modifiers may have
|
||||
/// been applied. Also used for symbols defined at runtime by the user with
|
||||
/// no modifier applied.
|
||||
Modified(Arc<(List, ModifierSet<EcoString>)>),
|
||||
}
|
||||
|
||||
/// A symbol variant, consisting of a set of modifiers, a character, and an
|
||||
/// optional deprecation message.
|
||||
type Variant<S> = (ModifierSet<S>, char, Option<S>);
|
||||
|
||||
/// A collection of symbols.
|
||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||
enum List {
|
||||
Static(&'static [(ModifierSet<&'static str>, char)]),
|
||||
Runtime(Box<[(ModifierSet<EcoString>, char)]>),
|
||||
Static(&'static [Variant<&'static str>]),
|
||||
Runtime(Box<[Variant<EcoString>]>),
|
||||
}
|
||||
|
||||
impl Symbol {
|
||||
@ -76,14 +80,14 @@ impl Symbol {
|
||||
|
||||
/// Create a symbol with a static variant list.
|
||||
#[track_caller]
|
||||
pub const fn list(list: &'static [(ModifierSet<&'static str>, char)]) -> Self {
|
||||
pub const fn list(list: &'static [Variant<&'static str>]) -> Self {
|
||||
debug_assert!(!list.is_empty());
|
||||
Self(Repr::Complex(list))
|
||||
}
|
||||
|
||||
/// Create a symbol with a runtime variant list.
|
||||
#[track_caller]
|
||||
pub fn runtime(list: Box<[(ModifierSet<EcoString>, char)]>) -> Self {
|
||||
pub fn runtime(list: Box<[Variant<EcoString>]>) -> Self {
|
||||
debug_assert!(!list.is_empty());
|
||||
Self(Repr::Modified(Arc::new((List::Runtime(list), ModifierSet::default()))))
|
||||
}
|
||||
@ -93,9 +97,11 @@ impl Symbol {
|
||||
match &self.0 {
|
||||
Repr::Single(c) => *c,
|
||||
Repr::Complex(_) => ModifierSet::<&'static str>::default()
|
||||
.best_match_in(self.variants())
|
||||
.best_match_in(self.variants().map(|(m, c, _)| (m, c)))
|
||||
.unwrap(),
|
||||
Repr::Modified(arc) => arc.1.best_match_in(self.variants()).unwrap(),
|
||||
Repr::Modified(arc) => {
|
||||
arc.1.best_match_in(self.variants().map(|(m, c, _)| (m, c))).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +134,11 @@ impl Symbol {
|
||||
}
|
||||
|
||||
/// Apply a modifier to the symbol.
|
||||
pub fn modified(mut self, modifier: &str) -> StrResult<Self> {
|
||||
pub fn modified(
|
||||
mut self,
|
||||
sink: impl DeprecationSink,
|
||||
modifier: &str,
|
||||
) -> StrResult<Self> {
|
||||
if let Repr::Complex(list) = self.0 {
|
||||
self.0 =
|
||||
Repr::Modified(Arc::new((List::Static(list), ModifierSet::default())));
|
||||
@ -137,7 +147,12 @@ impl Symbol {
|
||||
if let Repr::Modified(arc) = &mut self.0 {
|
||||
let (list, modifiers) = Arc::make_mut(arc);
|
||||
modifiers.insert_raw(modifier);
|
||||
if modifiers.best_match_in(list.variants()).is_some() {
|
||||
if let Some(deprecation) =
|
||||
modifiers.best_match_in(list.variants().map(|(m, _, d)| (m, d)))
|
||||
{
|
||||
if let Some(message) = deprecation {
|
||||
sink.emit(message)
|
||||
}
|
||||
return Ok(self);
|
||||
}
|
||||
}
|
||||
@ -146,7 +161,7 @@ impl Symbol {
|
||||
}
|
||||
|
||||
/// The characters that are covered by this symbol.
|
||||
pub fn variants(&self) -> impl Iterator<Item = (ModifierSet<&str>, char)> {
|
||||
pub fn variants(&self) -> impl Iterator<Item = Variant<&str>> {
|
||||
match &self.0 {
|
||||
Repr::Single(c) => Variants::Single(Some(*c).into_iter()),
|
||||
Repr::Complex(list) => Variants::Static(list.iter()),
|
||||
@ -161,7 +176,7 @@ impl Symbol {
|
||||
_ => ModifierSet::default(),
|
||||
};
|
||||
self.variants()
|
||||
.flat_map(|(m, _)| m)
|
||||
.flat_map(|(m, _, _)| m)
|
||||
.filter(|modifier| !modifier.is_empty() && !modifiers.contains(modifier))
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
@ -256,7 +271,7 @@ impl Symbol {
|
||||
|
||||
let list = variants
|
||||
.into_iter()
|
||||
.map(|s| (ModifierSet::from_raw_dotted(s.v.0), s.v.1))
|
||||
.map(|s| (ModifierSet::from_raw_dotted(s.v.0), s.v.1, None))
|
||||
.collect();
|
||||
Ok(Symbol::runtime(list))
|
||||
}
|
||||
@ -316,17 +331,17 @@ impl crate::foundations::Repr for Symbol {
|
||||
}
|
||||
|
||||
fn repr_variants<'a>(
|
||||
variants: impl Iterator<Item = (ModifierSet<&'a str>, char)>,
|
||||
variants: impl Iterator<Item = Variant<&'a str>>,
|
||||
applied_modifiers: ModifierSet<&str>,
|
||||
) -> String {
|
||||
crate::foundations::repr::pretty_array_like(
|
||||
&variants
|
||||
.filter(|(modifiers, _)| {
|
||||
.filter(|(modifiers, _, _)| {
|
||||
// Only keep variants that can still be accessed, i.e., variants
|
||||
// that contain all applied modifiers.
|
||||
applied_modifiers.iter().all(|am| modifiers.contains(am))
|
||||
})
|
||||
.map(|(modifiers, c)| {
|
||||
.map(|(modifiers, c, _)| {
|
||||
let trimmed_modifiers =
|
||||
modifiers.into_iter().filter(|&m| !applied_modifiers.contains(m));
|
||||
if trimmed_modifiers.clone().all(|m| m.is_empty()) {
|
||||
@ -379,18 +394,20 @@ cast! {
|
||||
/// Iterator over variants.
|
||||
enum Variants<'a> {
|
||||
Single(std::option::IntoIter<char>),
|
||||
Static(std::slice::Iter<'static, (ModifierSet<&'static str>, char)>),
|
||||
Runtime(std::slice::Iter<'a, (ModifierSet<EcoString>, char)>),
|
||||
Static(std::slice::Iter<'static, Variant<&'static str>>),
|
||||
Runtime(std::slice::Iter<'a, Variant<EcoString>>),
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Variants<'a> {
|
||||
type Item = (ModifierSet<&'a str>, char);
|
||||
type Item = Variant<&'a str>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self {
|
||||
Self::Single(iter) => Some((ModifierSet::default(), iter.next()?)),
|
||||
Self::Single(iter) => Some((ModifierSet::default(), iter.next()?, None)),
|
||||
Self::Static(list) => list.next().copied(),
|
||||
Self::Runtime(list) => list.next().map(|(m, c)| (m.as_deref(), *c)),
|
||||
Self::Runtime(list) => {
|
||||
list.next().map(|(m, c, d)| (m.as_deref(), *c, d.as_deref()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +157,9 @@ impl Value {
|
||||
/// Try to access a field on the value.
|
||||
pub fn field(&self, field: &str, sink: impl DeprecationSink) -> StrResult<Value> {
|
||||
match self {
|
||||
Self::Symbol(symbol) => symbol.clone().modified(field).map(Self::Symbol),
|
||||
Self::Symbol(symbol) => {
|
||||
symbol.clone().modified(sink, field).map(Self::Symbol)
|
||||
}
|
||||
Self::Version(version) => version.component(field).map(Self::Int),
|
||||
Self::Dict(dict) => dict.get(field).cloned(),
|
||||
Self::Content(content) => content.field_by_name(field),
|
||||
|
@ -7,7 +7,7 @@ use typst_utils::{PicoStr, ResolvedPicoStr};
|
||||
use crate::diag::{bail, HintedStrResult, StrResult};
|
||||
use crate::foundations::{cast, Dict, Repr, Str};
|
||||
use crate::introspection::{Introspector, Tag};
|
||||
use crate::layout::Frame;
|
||||
use crate::layout::{Abs, Frame};
|
||||
use crate::model::DocumentInfo;
|
||||
|
||||
/// An HTML document.
|
||||
@ -30,8 +30,8 @@ pub enum HtmlNode {
|
||||
Text(EcoString, Span),
|
||||
/// Another element.
|
||||
Element(HtmlElement),
|
||||
/// A frame that will be displayed as an embedded SVG.
|
||||
Frame(Frame),
|
||||
/// Layouted content that will be embedded into HTML as an SVG.
|
||||
Frame(HtmlFrame),
|
||||
}
|
||||
|
||||
impl HtmlNode {
|
||||
@ -263,6 +263,17 @@ cast! {
|
||||
v: Str => Self::intern(&v)?,
|
||||
}
|
||||
|
||||
/// Layouted content that will be embedded into HTML as an SVG.
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
pub struct HtmlFrame {
|
||||
/// The frame that will be displayed as an SVG.
|
||||
pub inner: Frame,
|
||||
/// The text size where the frame was defined. This is used to size the
|
||||
/// frame with em units to make text in and outside of the frame sized
|
||||
/// consistently.
|
||||
pub text_size: Abs,
|
||||
}
|
||||
|
||||
/// Defines syntactical properties of HTML tags, attributes, and text.
|
||||
pub mod charsets {
|
||||
/// Check whether a character is in a tag name.
|
||||
|
@ -446,7 +446,7 @@ impl IntrospectorBuilder {
|
||||
HtmlNode::Element(elem) => self.discover_in_html(sink, &elem.children),
|
||||
HtmlNode::Frame(frame) => self.discover_in_frame(
|
||||
sink,
|
||||
frame,
|
||||
&frame.inner,
|
||||
NonZeroUsize::ONE,
|
||||
Transform::identity(),
|
||||
),
|
||||
|
@ -321,7 +321,11 @@ impl Bibliography {
|
||||
for d in data.iter() {
|
||||
let library = decode_library(d)?;
|
||||
for entry in library {
|
||||
match map.entry(Label::new(PicoStr::intern(entry.key()))) {
|
||||
let label = Label::new(PicoStr::intern(entry.key()))
|
||||
.ok_or("bibliography contains entry with empty key")
|
||||
.at(d.source.span)?;
|
||||
|
||||
match map.entry(label) {
|
||||
indexmap::map::Entry::Vacant(vacant) => {
|
||||
vacant.insert(entry);
|
||||
}
|
||||
|
@ -2,7 +2,10 @@ use smallvec::smallvec;
|
||||
|
||||
use crate::diag::SourceResult;
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{elem, Content, Packed, Show, Smart, StyleChain};
|
||||
use crate::foundations::{
|
||||
elem, Content, NativeElement, Packed, Show, Smart, StyleChain, TargetElem,
|
||||
};
|
||||
use crate::html::{attr, tag, HtmlElem};
|
||||
use crate::layout::{Abs, Corners, Length, Rel, Sides};
|
||||
use crate::text::{BottomEdge, BottomEdgeMetric, TextElem, TopEdge, TopEdgeMetric};
|
||||
use crate::visualize::{Color, FixedStroke, Paint, Stroke};
|
||||
@ -81,6 +84,16 @@ pub struct UnderlineElem {
|
||||
impl Show for Packed<UnderlineElem> {
|
||||
#[typst_macros::time(name = "underline", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
// Note: In modern HTML, `<u>` is not the underline element, but
|
||||
// rather an "Unarticulated Annotation" element (see HTML spec
|
||||
// 4.5.22). Using `text-decoration` instead is recommended by MDN.
|
||||
return Ok(HtmlElem::new(tag::span)
|
||||
.with_attr(attr::style, "text-decoration: underline")
|
||||
.with_body(Some(self.body.clone()))
|
||||
.pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
line: DecoLine::Underline {
|
||||
stroke: self.stroke(styles).unwrap_or_default(),
|
||||
@ -173,6 +186,13 @@ pub struct OverlineElem {
|
||||
impl Show for Packed<OverlineElem> {
|
||||
#[typst_macros::time(name = "overline", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::span)
|
||||
.with_attr(attr::style, "text-decoration: overline")
|
||||
.with_body(Some(self.body.clone()))
|
||||
.pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
line: DecoLine::Overline {
|
||||
stroke: self.stroke(styles).unwrap_or_default(),
|
||||
@ -250,6 +270,10 @@ pub struct StrikeElem {
|
||||
impl Show for Packed<StrikeElem> {
|
||||
#[typst_macros::time(name = "strike", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::s).with_body(Some(self.body.clone())).pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
// Note that we do not support evade option for strikethrough.
|
||||
line: DecoLine::Strikethrough {
|
||||
@ -345,6 +369,12 @@ pub struct HighlightElem {
|
||||
impl Show for Packed<HighlightElem> {
|
||||
#[typst_macros::time(name = "highlight", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::mark)
|
||||
.with_body(Some(self.body.clone()))
|
||||
.pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
line: DecoLine::Highlight {
|
||||
fill: self.fill(styles),
|
||||
|
@ -18,7 +18,7 @@ use typst_library::foundations::{
|
||||
SequenceElem, Show, ShowSet, Style, StyleChain, StyledElem, Styles, SymbolElem,
|
||||
Synthesize, Transformation,
|
||||
};
|
||||
use typst_library::html::{tag, HtmlElem};
|
||||
use typst_library::html::{tag, FrameElem, HtmlElem};
|
||||
use typst_library::introspection::{Locatable, SplitLocator, Tag, TagElem};
|
||||
use typst_library::layout::{
|
||||
AlignElem, BoxElem, HElem, InlineElem, PageElem, PagebreakElem, VElem,
|
||||
@ -237,9 +237,9 @@ fn visit<'a>(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Transformations for math content based on the realization kind. Needs
|
||||
// Transformations for content based on the realization kind. Needs
|
||||
// to happen before show rules.
|
||||
if visit_math_rules(s, content, styles)? {
|
||||
if visit_kind_rules(s, content, styles)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -280,9 +280,8 @@ fn visit<'a>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Handles special cases for math in normal content and nested equations in
|
||||
// math.
|
||||
fn visit_math_rules<'a>(
|
||||
// Handles transformations based on the realization kind.
|
||||
fn visit_kind_rules<'a>(
|
||||
s: &mut State<'a, '_, '_, '_>,
|
||||
content: &'a Content,
|
||||
styles: StyleChain<'a>,
|
||||
@ -335,6 +334,13 @@ fn visit_math_rules<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
if !s.kind.is_html() {
|
||||
if let Some(elem) = content.to_packed::<FrameElem>() {
|
||||
visit(s, &elem.body, styles)?;
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
|
@ -724,6 +724,8 @@ node! {
|
||||
|
||||
impl<'a> Ref<'a> {
|
||||
/// Get the target.
|
||||
///
|
||||
/// Will not be empty.
|
||||
pub fn target(self) -> &'a str {
|
||||
self.0
|
||||
.children()
|
||||
|
@ -185,7 +185,7 @@ impl Lexer<'_> {
|
||||
'h' if self.s.eat_if("ttp://") => self.link(),
|
||||
'h' if self.s.eat_if("ttps://") => self.link(),
|
||||
'<' if self.s.at(is_id_continue) => self.label(),
|
||||
'@' => self.ref_marker(),
|
||||
'@' if self.s.at(is_id_continue) => self.ref_marker(),
|
||||
|
||||
'.' if self.s.eat_if("..") => SyntaxKind::Shorthand,
|
||||
'-' if self.s.eat_if("--") => SyntaxKind::Shorthand,
|
||||
|
@ -256,8 +256,8 @@ In Typst, the same function can be used both to affect the appearance for the
|
||||
remainder of the document, a block (or scope), or just its arguments. For
|
||||
example, `[#text(weight: "bold")[bold text]]` will only embolden its argument,
|
||||
while `[#set text(weight: "bold")]` will embolden any text until the end of the
|
||||
current block, or, if there is none, document. The effects of a function are
|
||||
immediately obvious based on whether it is used in a call or a
|
||||
current block, or the end of the document, if there is none. The effects of a
|
||||
function are immediately obvious based on whether it is used in a call or a
|
||||
[set rule.]($styling/#set-rules)
|
||||
|
||||
```example
|
||||
|
@ -181,11 +181,7 @@
|
||||
[`sys.version`]($category/foundations/sys) can also be very useful.
|
||||
|
||||
```typ
|
||||
#let tiling = if "tiling" in dictionary(std) {
|
||||
tiling
|
||||
} else {
|
||||
pattern
|
||||
}
|
||||
#let tiling = if "tiling" in std { tiling } else { pattern }
|
||||
|
||||
...
|
||||
```
|
||||
|
@ -720,18 +720,12 @@ fn symbols_model(resolver: &dyn Resolver, group: &GroupData) -> SymbolsModel {
|
||||
}
|
||||
};
|
||||
|
||||
for (variant, c) in symbol.variants() {
|
||||
for (variant, c, deprecation) in symbol.variants() {
|
||||
let shorthand = |list: &[(&'static str, char)]| {
|
||||
list.iter().copied().find(|&(_, x)| x == c).map(|(s, _)| s)
|
||||
};
|
||||
|
||||
let name = complete(variant);
|
||||
let deprecation = match name.as_str() {
|
||||
"integral.sect" => {
|
||||
Some("`integral.sect` is deprecated, use `integral.inter` instead")
|
||||
}
|
||||
_ => binding.deprecation(),
|
||||
};
|
||||
|
||||
list.push(SymbolModel {
|
||||
name,
|
||||
@ -742,10 +736,10 @@ fn symbols_model(resolver: &dyn Resolver, group: &GroupData) -> SymbolsModel {
|
||||
accent: typst::math::Accent::combine(c).is_some(),
|
||||
alternates: symbol
|
||||
.variants()
|
||||
.filter(|(other, _)| other != &variant)
|
||||
.map(|(other, _)| complete(other))
|
||||
.filter(|(other, _, _)| other != &variant)
|
||||
.map(|(other, _, _)| complete(other))
|
||||
.collect(),
|
||||
deprecation,
|
||||
deprecation: deprecation.or_else(|| binding.deprecation()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
BIN
tests/ref/html-frame-in-layout.png
Normal file
BIN
tests/ref/html-frame-in-layout.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 146 B |
11
tests/ref/html/html-deco.html
Normal file
11
tests/ref/html/html-deco.html
Normal 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><s>Struck</s> <mark>Highlighted</mark> <span style="text-decoration: underline">Underlined</span> <span style="text-decoration: overline">Overlined</span></p>
|
||||
<p><span style="text-decoration: overline"><span style="text-decoration: underline"><mark><s>Mixed</s></mark></span></span></p>
|
||||
</body>
|
||||
</html>
|
BIN
tests/ref/ref-to-empty-label-not-possible.png
Normal file
BIN
tests/ref/ref-to-empty-label-not-possible.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 182 B |
@ -92,3 +92,7 @@ _Visible_
|
||||
--- label-non-existent-error ---
|
||||
// Error: 5-10 sequence does not have field "label"
|
||||
#[].label
|
||||
|
||||
--- label-empty ---
|
||||
// Error: 23-32 label name must not be empty
|
||||
= Something to label #label("")
|
||||
|
8
tests/suite/html/frame.typ
Normal file
8
tests/suite/html/frame.typ
Normal file
@ -0,0 +1,8 @@
|
||||
// No proper HTML tests here yet because we don't want to test SVG export just
|
||||
// yet. We'll definitely add tests at some point.
|
||||
|
||||
--- html-frame-in-layout ---
|
||||
// Ensure that HTML frames are transparent in layout. This is less important for
|
||||
// actual paged export than for _nested_ HTML frames, which take the same code
|
||||
// path.
|
||||
#html.frame[A]
|
@ -121,8 +121,8 @@ $a scripts(=)^"def" b quad a scripts(lt.eq)_"really" b quad a scripts(arrow.r.lo
|
||||
|
||||
--- math-attach-integral ---
|
||||
// Test default of scripts attachments on integrals at display size.
|
||||
$ integral.sect_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b $
|
||||
$integral.sect_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b$
|
||||
$ integral.inter_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b $
|
||||
$integral.inter_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b$
|
||||
|
||||
--- math-attach-large-operator ---
|
||||
// Test default of limit attachments on large operators at display size only.
|
||||
|
@ -75,6 +75,14 @@ Now we have multiple bibliographies containing @glacier-melt @keshav2007read
|
||||
// Error: 2-62 CSL style "Alphanumeric" is not suitable for bibliographies
|
||||
#bibliography("/assets/bib/works.bib", style: "alphanumeric")
|
||||
|
||||
--- bibliography-empty-key ---
|
||||
#let src = ```yaml
|
||||
"":
|
||||
type: Book
|
||||
```
|
||||
// Error: 15-30 bibliography contains entry with empty key
|
||||
#bibliography(bytes(src.text))
|
||||
|
||||
--- issue-4618-bibliography-set-heading-level ---
|
||||
// Test that the bibliography block's heading is set to 2 by the show rule,
|
||||
// and therefore should be rendered like a level-2 heading. Notably, this
|
||||
|
@ -86,3 +86,14 @@ Text seen on #ref(<text>, form: "page", supplement: "Page").
|
||||
// Test reference with non-whitespace before it.
|
||||
#figure[] <1>
|
||||
#test([(#ref(<1>))], [(@1)])
|
||||
|
||||
--- ref-to-empty-label-not-possible ---
|
||||
// @ without any following label should just produce the symbol in the output
|
||||
// and not produce a reference to a label with an empty name.
|
||||
@
|
||||
|
||||
--- ref-function-empty-label ---
|
||||
// using ref() should also not be possible
|
||||
// Error: 6-7 unexpected less-than operator
|
||||
// Error: 7-8 unexpected greater-than operator
|
||||
#ref(<>)
|
||||
|
@ -264,6 +264,8 @@
|
||||
#test("Hey" not in "abheyCd", true)
|
||||
#test("a" not
|
||||
/* fun comment? */ in "abc", false)
|
||||
#test("sys" in std, true)
|
||||
#test("system" in std, false)
|
||||
|
||||
--- ops-not-trailing ---
|
||||
// Error: 10 expected keyword `in`
|
||||
|
@ -83,3 +83,11 @@ We can also specify a customized value
|
||||
#highlight(stroke: 2pt + blue)[abc]
|
||||
#highlight(stroke: (top: blue, left: red, bottom: green, right: orange))[abc]
|
||||
#highlight(stroke: 1pt, radius: 3pt)[#lorem(5)]
|
||||
|
||||
--- html-deco html ---
|
||||
#strike[Struck]
|
||||
#highlight[Highlighted]
|
||||
#underline[Underlined]
|
||||
#overline[Overlined]
|
||||
|
||||
#(strike, highlight, underline, overline).fold([Mixed], (it, f) => f(it))
|
||||
|
Loading…
x
Reference in New Issue
Block a user