Split markup and math shorthands for docs

This commit is contained in:
Laurenz 2023-08-03 16:33:17 +02:00
parent 53a896f049
commit 028d2f5308
2 changed files with 32 additions and 19 deletions

View File

@ -214,7 +214,8 @@ pub struct CategoryModel {
pub details: Html, pub details: Html,
pub kind: &'static str, pub kind: &'static str,
pub items: Vec<CategoryItem>, pub items: Vec<CategoryItem>,
pub shorthands: Option<Vec<SymbolModel>>, pub markup_shorthands: Option<Vec<SymbolModel>>,
pub math_shorthands: Option<Vec<SymbolModel>>,
} }
/// Details about a category item. /// Details about a category item.
@ -325,13 +326,20 @@ fn category_page(resolver: &dyn Resolver, category: &str) -> PageModel {
items.sort_by_cached_key(|item| item.name.clone()); items.sort_by_cached_key(|item| item.name.clone());
// Add symbol pages. These are ordered manually. // Add symbol pages. These are ordered manually.
let mut shorthands = vec![]; let mut markup_shorthands = vec![];
let mut math_shorthands = vec![];
if category == "symbols" { if category == "symbols" {
for module in ["sym", "emoji"] { for module in ["sym", "emoji"] {
let subpage = symbol_page(resolver, &route, module); let subpage = symbol_page(resolver, &route, module);
let BodyModel::Symbols(model) = &subpage.body else { continue }; let BodyModel::Symbols(model) = &subpage.body else { continue };
shorthands.extend( let list = &model.list;
model.list.iter().filter(|symbol| symbol.shorthand.is_some()).cloned(), markup_shorthands.extend(
list.iter()
.filter(|symbol| symbol.markup_shorthand.is_some())
.cloned(),
);
math_shorthands.extend(
list.iter().filter(|symbol| symbol.math_shorthand.is_some()).cloned(),
); );
items.push(CategoryItem { items.push(CategoryItem {
@ -361,7 +369,8 @@ fn category_page(resolver: &dyn Resolver, category: &str) -> PageModel {
details: Html::markdown(resolver, details(category)), details: Html::markdown(resolver, details(category)),
kind, kind,
items, items,
shorthands: Some(shorthands), markup_shorthands: Some(markup_shorthands),
math_shorthands: Some(math_shorthands),
}), }),
children, children,
} }
@ -659,7 +668,8 @@ fn types_page(resolver: &dyn Resolver, parent: &str) -> PageModel {
details: Html::markdown(resolver, details("types")), details: Html::markdown(resolver, details("types")),
kind: "Types", kind: "Types",
items, items,
shorthands: None, markup_shorthands: None,
math_shorthands: None,
}), }),
children, children,
} }
@ -836,7 +846,8 @@ pub struct SymbolsModel {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SymbolModel { pub struct SymbolModel {
pub name: String, pub name: String,
pub shorthand: Option<&'static str>, pub markup_shorthand: Option<&'static str>,
pub math_shorthand: Option<&'static str>,
pub codepoint: u32, pub codepoint: u32,
pub accent: bool, pub accent: bool,
pub unicode_name: Option<String>, pub unicode_name: Option<String>,
@ -859,13 +870,14 @@ fn symbol_page(resolver: &dyn Resolver, parent: &str, name: &str) -> PageModel {
}; };
for (variant, c) in symbol.variants() { for (variant, c) in symbol.variants() {
let shorthand = |list: &[(&'static str, char)]| {
list.iter().copied().find(|&(_, x)| x == c).map(|(s, _)| s)
};
list.push(SymbolModel { list.push(SymbolModel {
name: complete(variant), name: complete(variant),
shorthand: typst::syntax::ast::Shorthand::LIST markup_shorthand: shorthand(typst::syntax::ast::Shorthand::MARKUP_LIST),
.iter() math_shorthand: shorthand(typst::syntax::ast::Shorthand::MATH_LIST),
.copied()
.find(|&(_, x)| x == c)
.map(|(s, _)| s),
codepoint: c as u32, codepoint: c as u32,
accent: typst::eval::Symbol::combining_accent(c).is_some(), accent: typst::eval::Symbol::combining_accent(c).is_some(),
unicode_name: unicode_names2::name(c) unicode_name: unicode_names2::name(c)

View File

@ -435,16 +435,18 @@ node! {
} }
impl Shorthand { impl Shorthand {
/// A list of all shorthands. /// A list of all shorthands in markup mode.
pub const LIST: &[(&'static str, char)] = &[ pub const MARKUP_LIST: &[(&'static str, char)] = &[
// Both.
("...", '…'), ("...", '…'),
// Text only.
("~", '\u{00A0}'), ("~", '\u{00A0}'),
("--", '\u{2013}'), ("--", '\u{2013}'),
("---", '\u{2014}'), ("---", '\u{2014}'),
("-?", '\u{00AD}'), ("-?", '\u{00AD}'),
// Math only. ];
/// A list of all shorthands in math mode.
pub const MATH_LIST: &[(&'static str, char)] = &[
("...", '…'),
("-", '\u{2212}'), ("-", '\u{2212}'),
("'", ''), ("'", ''),
("*", ''), ("*", ''),
@ -487,8 +489,7 @@ impl Shorthand {
/// Get the shorthanded character. /// Get the shorthanded character.
pub fn get(&self) -> char { pub fn get(&self) -> char {
let text = self.0.text(); let text = self.0.text();
Self::LIST (Self::MARKUP_LIST.iter().chain(Self::MATH_LIST))
.iter()
.find(|&&(s, _)| s == text) .find(|&&(s, _)| s == text)
.map_or_else(char::default, |&(_, c)| c) .map_or_else(char::default, |&(_, c)| c)
} }