summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/text/quote.rs
blob: d42dc2f2565d3a6e13e4f59e285aee78058bc023 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use super::{SmartquoteElem, SpaceElem, TextElem};
use crate::layout::{BlockElem, HElem, PadElem, Spacing, VElem};
use crate::meta::{CitationForm, CiteElem};
use crate::prelude::*;

/// Displays a quote alongside it's author.
///
/// # Example
/// ```example
/// Plato is often misquoted as the author of #quote[I know that I know
/// nothing], however, this is a derivation form his original quote:
/// #set quote(block: true)
/// #quote(attribution: [Plato])[
///   ... ἔοικα γοῦν τούτου γε σμικρῷ τινι αὐτῷ τούτῳ σοφώτερος εἶναι, ὅτι
///   ἃ μὴ οἶδα οὐδὲ οἴομαι εἰδέναι.
/// ]
/// #quote(attribution: [from the Henry Cary literal translation of 1897])[
///   ... I seem, then, in just this little thing to be wiser than this man at
///   any rate, that what I do not know I do not think I know either.
/// ]
/// ```
///
/// By default block quotes are padded left and right by `{1em}`, alignment and
/// padding can be controlled with show rules:
/// ```example
/// #set quote(block: true)
/// #show quote: set align(center)
/// #show quote: set pad(x: 5em)
///
/// #quote[
///   You cannot pass... I am a servant of the Secret Fire, wielder of the
///   flame of Anor. You cannot pass. The dark fire will not avail you,
///   flame of Udûn. Go back to the Shadow! You cannot pass.
/// ]
/// ```
#[elem(Finalize, Show)]
pub struct QuoteElem {
    /// Whether this is a block quote.
    ///
    /// ```example
    /// #quote(attribution: [René Descartes])[cogito, ergo sum]
    ///
    /// #set quote(block: true)
    /// #quote(attribution: [JFK])[Ich bin ein Berliner.]
    /// ```
    block: bool,

    /// Whether double quotes should be added around this quote.
    ///
    /// The double quotes used are inferred from the `quotes` property on
    /// [smartquote]($smartquote), which is affected by the `lang` property on
    /// [text]($text).
    ///
    /// - `{true}`: Wrap this quote in double quotes.
    /// - `{false}`: Do not wrap this quote in double quotes.
    /// - `{auto}`: Infer whether to wrap this quote in double quotes based on
    ///   the `block` property. If `block` is `{false}`, double quotes are
    ///   auomatically added.
    ///
    /// ```example
    /// #set text(lang: "de")
    /// #quote[Ich bin ein Berliner.]
    ///
    /// #set text(lang: "en")
    /// #set quote(quotes: true)
    /// #quote(block: true)[I am a Berliner.]
    /// ```
    quotes: Smart<bool>,

    /// The attribution of this quote, usually the author or source. Can be a
    /// label pointing to a bibliography entry or any content. By default only
    /// displayed for block quotes, but can be changed using a `{show}` rule.
    ///
    /// ```example
    /// #quote(attribution: [René Descartes])[cogito, ergo sum] \
    ///
    /// #show quote.where(block: false): it => [
    ///   "#it.body"
    ///   #if it.attribution != none [(#it.attribution)]
    /// ]
    /// #quote(attribution: link("https://typst.app/home")[typst.com])[
    ///   Compose papers faster
    /// ]
    ///
    /// #set quote(block: true)
    /// #quote(attribution: <tolkien54>)[
    ///   You cannot pass... I am a servant of the Secret Fire, wielder of the
    ///   flame of Anor. You cannot pass. The dark fire will not avail you,
    ///   flame of Udûn. Go back to the Shadow! You cannot pass.
    /// ]
    /// #bibliography("works.bib", style: "apa")
    /// ```
    ///
    /// Note that bilbiography styles which do not include the author in the
    /// citation (label, numeric and notes) currently produce attributions such
    /// as `[---#super[1]]` or `[--- [1]]`, this will be fixed soon with CSL
    /// support. In the mean time you can simply cite yourself:
    /// ```example
    /// #set quote(block: true)
    /// #quote(attribution: [J. R. R. Tolkien, @tolkien54])[In a hole there lived a hobbit.]
    ///
    /// #bibliography("works.bib")
    /// ```
    attribution: Option<Attribution>,

    /// The quote.
    #[required]
    body: Content,
}

#[derive(Debug, Clone)]
pub enum Attribution {
    Content(Content),
    Label(Label),
}

cast! {
    Attribution,
    self => match self {
        Self::Content(content) => content.into_value(),
        Self::Label(label) => label.into_value(),
    },
    content: Content => Self::Content(content),
    label: Label => Self::Label(label),
}

impl Show for QuoteElem {
    fn show(&self, _: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
        let mut realized = self.body();
        let block = self.block(styles);

        if self.quotes(styles) == Smart::Custom(true) || !block {
            // Add zero-width weak spacing to make the quotes "sticky".
            let hole = HElem::hole().pack();
            let quote = SmartquoteElem::new().with_double(true).pack();
            realized =
                Content::sequence([quote.clone(), hole.clone(), realized, hole, quote]);
        }

        if block {
            realized = BlockElem::new().with_body(Some(realized)).pack();

            if let Some(attribution) = self.attribution(styles) {
                let mut seq = vec![TextElem::packed('—'), SpaceElem::new().pack()];

                match attribution {
                    Attribution::Content(content) => {
                        seq.push(content);
                    }
                    Attribution::Label(label) => {
                        seq.push(
                            CiteElem::new(label)
                                .with_form(Some(CitationForm::Prose))
                                .pack(),
                        );
                    }
                }

                // Use v(0.9em, weak: true) bring the attribution closer to the
                // quote.
                let weak_v = VElem::weak(Spacing::Rel(Em::new(0.9).into())).pack();
                realized += weak_v + Content::sequence(seq).aligned(Align::END);
            }

            realized = PadElem::new(realized).pack();
        } else if let Some(Attribution::Label(label)) = self.attribution(styles) {
            realized += SpaceElem::new().pack() + CiteElem::new(label).pack();
        }

        Ok(realized)
    }
}

impl Finalize for QuoteElem {
    fn finalize(&self, realized: Content, _: StyleChain) -> Content {
        let x = Em::new(1.0).into();
        let above = Em::new(2.4).into();
        let below = Em::new(1.8).into();
        realized
            .styled(PadElem::set_left(x))
            .styled(PadElem::set_right(x))
            .styled(BlockElem::set_above(VElem::block_around(above)))
            .styled(BlockElem::set_below(VElem::block_around(below)))
    }
}