flm_core/lang/rewrites/blocks/config.rs
1//! The options of text resolution: [`BlocksConfig`], which says which families
2//! of typographic substitution the [block-structure pass](super) applies.
3//!
4//! The block-structure pass resolves the text of a tree once (see the
5//! [module documentation](super)), and in the same scan applies the typographic
6//! substitutions that the source spells out: `--` means an en dash in the same
7//! way that `\emph{…}` means emphasis, so the substitution is a property of the
8//! text and not of one output format. [`BlocksConfig`] holds one flag per family
9//! of substitutions. Collapsing whitespace is not one of the families: it
10//! always happens and has no flag.
11//!
12//! The `blocks` feature ([`crate::stdlib::features::blocks`]) takes a
13//! [`BlocksConfig`] as its configuration, a value fixed when the registry is
14//! built (see [settings, options and
15//! configuration](crate::guide::concepts_overview#settings-options-and-configuration)).
16//! The feature feeds the flags into the registry's [structural
17//! digest](crate::model::digest)
18//! ([`FlmRegistry::digest`](crate::core::registry::FlmRegistry::digest)), which
19//! every cache key of the library includes, so that two registries that differ
20//! in these options never share cached trees.
21
22/// The options of text resolution in the [block-structure pass](super): one
23/// flag per family of typographic substitutions. Collapsing whitespace always
24/// happens and has no flag.
25///
26/// Every family is on by default, so that FLM source written with the usual TeX
27/// spellings (`` `quoted' ``, `--`, `...`) is shown as its author meant it in
28/// every output format. Switch a family off to have those spellings reach the
29/// output unchanged.
30///
31/// A value is passed to [`structure`](super::structure), or to the `blocks`
32/// feature's constructor
33/// ([`Blocks::new`](crate::stdlib::features::blocks::Blocks::new)). Because
34/// every field is public, a value can be built from the default with one change,
35/// or from [`no_typography`](BlocksConfig::no_typography) with one family
36/// switched on. In settings written as data ([`crate::core::settings`]), the
37/// fields are the settings keys of the `blocks` feature, and a key left out
38/// takes its default.
39///
40/// # Examples
41///
42/// ```
43/// use flm_core::lang::rewrites::blocks::BlocksConfig;
44///
45/// let default = BlocksConfig::new();
46/// assert!(default.any_typography());
47///
48/// // Everything but the dash ligatures.
49/// let config = BlocksConfig { ligature_unicode_dashes: false, ..default };
50/// assert!(config.any_typography());
51/// assert!(!BlocksConfig::no_typography().any_typography());
52/// ```
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
54#[derive(schemars::JsonSchema)]
55#[serde(default, deny_unknown_fields)]
56pub struct BlocksConfig {
57 /// Whether the straight double quote `"` and the straight single quote `'`
58 /// become curly quotes, opening (`“`, `‘`) or closing (`”`, `’`) according
59 /// to their position. When
60 /// [`ligature_unicode_quotes`](BlocksConfig::ligature_unicode_quotes) is
61 /// also on, the two-character ligature `''` is that family's, and this test
62 /// applies to a single `'`.
63 ///
64 /// A quote opens at the start of a text run that no content precedes (a
65 /// comment does not count, nor the spaces `~` and `\ `), and after
66 /// whitespace, an opening bracket
67 /// (`(`, `[`, `{`) or a dash (`-`, `–`, `—`). A single quote also opens
68 /// before a digit (`x'9` becomes `x‘9`), except in a decade abbreviation:
69 /// after a letter, a digit or an underscore, and before two digits and an
70 /// `s` (`x'90s` becomes
71 /// `x’90s`). A quote closes everywhere else: `don't` becomes `don’t`. The
72 /// test looks at the node before the text run, so in `\(A_j\)'s` the quote
73 /// after the formula closes.
74 pub auto_unicode_quotes: bool,
75 /// Whether the TeX quote ligatures apply: ``` `` ``` becomes `“`, `''`
76 /// becomes `”`, and `` ` `` becomes `‘`. A single `'` becomes `’`, as in
77 /// TeX, only when [`auto_unicode_quotes`](BlocksConfig::auto_unicode_quotes) is
78 /// off; when both families are on, the position test of
79 /// `auto_unicode_quotes` decides a single `'`.
80 pub ligature_unicode_quotes: bool,
81 /// Whether the TeX dash ligatures apply: `---` becomes an em dash `—` and
82 /// `--` an en dash `–`.
83 pub ligature_unicode_dashes: bool,
84 /// Whether the ellipsis ligature applies: `...` becomes `…`.
85 pub ligature_unicode_ellipses: bool,
86}
87
88impl BlocksConfig {
89 /// The default options: every family of substitutions on. Returns the same
90 /// value as [`BlocksConfig::default`].
91 pub fn new() -> BlocksConfig {
92 BlocksConfig::default()
93 }
94
95 /// The options with every family of substitutions off. Whitespace is still
96 /// collapsed, which no option can switch off.
97 pub fn no_typography() -> BlocksConfig {
98 BlocksConfig {
99 auto_unicode_quotes: false,
100 ligature_unicode_quotes: false,
101 ligature_unicode_dashes: false,
102 ligature_unicode_ellipses: false,
103 }
104 }
105
106 /// Whether at least one family of substitutions is on.
107 pub fn any_typography(&self) -> bool {
108 self.auto_unicode_quotes
109 || self.ligature_unicode_quotes
110 || self.ligature_unicode_dashes
111 || self.ligature_unicode_ellipses
112 }
113}
114
115impl Default for BlocksConfig {
116 fn default() -> BlocksConfig {
117 BlocksConfig {
118 auto_unicode_quotes: true,
119 ligature_unicode_quotes: true,
120 ligature_unicode_dashes: true,
121 ligature_unicode_ellipses: true,
122 }
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 #[test]
131 fn every_family_is_on_by_default() {
132 assert!(BlocksConfig::default().any_typography());
133 assert_eq!(BlocksConfig::new(), BlocksConfig::default());
134 }
135
136 #[test]
137 fn no_typography_switches_every_family_off() {
138 assert!(!BlocksConfig::no_typography().any_typography());
139 }
140
141 #[test]
142 fn a_partial_configuration_fills_in_the_defaults() {
143 let config: BlocksConfig =
144 serde_json::from_str(r#"{"ligature_unicode_dashes": false}"#).unwrap();
145 assert!(!config.ligature_unicode_dashes);
146 assert!(config.ligature_unicode_quotes);
147 }
148}