mirror of
https://github.com/typst/typst
synced 2025-06-19 20:02:52 +08:00
54 lines
1.6 KiB
Rust
54 lines
1.6 KiB
Rust
use crate::diag::SourceResult;
|
|
use crate::engine::Engine;
|
|
use crate::foundations::{
|
|
elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem,
|
|
};
|
|
use crate::html::{tag, HtmlElem};
|
|
use crate::introspection::Locatable;
|
|
use crate::text::{ItalicToggle, TextElem};
|
|
|
|
/// Emphasizes content by toggling italics.
|
|
///
|
|
/// - If the current [text style]($text.style) is `{"normal"}`, this turns it
|
|
/// into `{"italic"}`.
|
|
/// - If it is already `{"italic"}` or `{"oblique"}`, it turns it back to
|
|
/// `{"normal"}`.
|
|
///
|
|
/// # Example
|
|
/// ```example
|
|
/// This is _emphasized._ \
|
|
/// This is #emph[too.]
|
|
///
|
|
/// #show emph: it => {
|
|
/// text(blue, it.body)
|
|
/// }
|
|
///
|
|
/// This is _emphasized_ differently.
|
|
/// ```
|
|
///
|
|
/// # Syntax
|
|
/// This function also has dedicated syntax: To emphasize content, simply
|
|
/// enclose it in underscores (`_`). Note that this only works at word
|
|
/// boundaries. To emphasize part of a word, you have to use the function.
|
|
#[elem(title = "Emphasis", keywords = ["italic"], Locatable, Show)]
|
|
pub struct EmphElem {
|
|
/// The content to emphasize.
|
|
#[required]
|
|
pub body: Content,
|
|
}
|
|
|
|
impl Show for Packed<EmphElem> {
|
|
#[typst_macros::time(name = "emph", span = self.span())]
|
|
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
|
let body = self.body.clone();
|
|
Ok(if TargetElem::target_in(styles).is_html() {
|
|
HtmlElem::new(tag::em)
|
|
.with_body(Some(body))
|
|
.pack()
|
|
.spanned(self.span())
|
|
} else {
|
|
body.styled(TextElem::set_emph(ItalicToggle(true)))
|
|
})
|
|
}
|
|
}
|