Skip to content

Commit

Permalink
Split markup and math shorthands for docs
Browse files Browse the repository at this point in the history
  • Loading branch information
laurmaedje committed Aug 3, 2023
1 parent 53a896f commit 028d2f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
36 changes: 24 additions & 12 deletions crates/typst-docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ pub struct CategoryModel {
pub details: Html,
pub kind: &'static str,
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.
Expand Down Expand Up @@ -325,13 +326,20 @@ fn category_page(resolver: &dyn Resolver, category: &str) -> PageModel {
items.sort_by_cached_key(|item| item.name.clone());

// Add symbol pages. These are ordered manually.
let mut shorthands = vec![];
let mut markup_shorthands = vec![];
let mut math_shorthands = vec![];
if category == "symbols" {
for module in ["sym", "emoji"] {
let subpage = symbol_page(resolver, &route, module);
let BodyModel::Symbols(model) = &subpage.body else { continue };
shorthands.extend(
model.list.iter().filter(|symbol| symbol.shorthand.is_some()).cloned(),
let list = &model.list;
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 {
Expand Down Expand Up @@ -361,7 +369,8 @@ fn category_page(resolver: &dyn Resolver, category: &str) -> PageModel {
details: Html::markdown(resolver, details(category)),
kind,
items,
shorthands: Some(shorthands),
markup_shorthands: Some(markup_shorthands),
math_shorthands: Some(math_shorthands),
}),
children,
}
Expand Down Expand Up @@ -659,7 +668,8 @@ fn types_page(resolver: &dyn Resolver, parent: &str) -> PageModel {
details: Html::markdown(resolver, details("types")),
kind: "Types",
items,
shorthands: None,
markup_shorthands: None,
math_shorthands: None,
}),
children,
}
Expand Down Expand Up @@ -836,7 +846,8 @@ pub struct SymbolsModel {
#[serde(rename_all = "camelCase")]
pub struct SymbolModel {
pub name: String,
pub shorthand: Option<&'static str>,
pub markup_shorthand: Option<&'static str>,
pub math_shorthand: Option<&'static str>,
pub codepoint: u32,
pub accent: bool,
pub unicode_name: Option<String>,
Expand All @@ -859,13 +870,14 @@ fn symbol_page(resolver: &dyn Resolver, parent: &str, name: &str) -> PageModel {
};

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 {
name: complete(variant),
shorthand: typst::syntax::ast::Shorthand::LIST
.iter()
.copied()
.find(|&(_, x)| x == c)
.map(|(s, _)| s),
markup_shorthand: shorthand(typst::syntax::ast::Shorthand::MARKUP_LIST),
math_shorthand: shorthand(typst::syntax::ast::Shorthand::MATH_LIST),
codepoint: c as u32,
accent: typst::eval::Symbol::combining_accent(c).is_some(),
unicode_name: unicode_names2::name(c)
Expand Down
15 changes: 8 additions & 7 deletions crates/typst-syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,18 @@ node! {
}

impl Shorthand {
/// A list of all shorthands.
pub const LIST: &[(&'static str, char)] = &[
// Both.
/// A list of all shorthands in markup mode.
pub const MARKUP_LIST: &[(&'static str, char)] = &[
("...", '…'),
// Text only.
("~", '\u{00A0}'),
("--", '\u{2013}'),
("---", '\u{2014}'),
("-?", '\u{00AD}'),
// Math only.
];

/// A list of all shorthands in math mode.
pub const MATH_LIST: &[(&'static str, char)] = &[
("...", '…'),
("-", '\u{2212}'),
("'", '′'),
("*", '∗'),
Expand Down Expand Up @@ -487,8 +489,7 @@ impl Shorthand {
/// Get the shorthanded character.
pub fn get(&self) -> char {
let text = self.0.text();
Self::LIST
.iter()
(Self::MARKUP_LIST.iter().chain(Self::MATH_LIST))
.find(|&&(s, _)| s == text)
.map_or_else(char::default, |&(_, c)| c)
}
Expand Down

0 comments on commit 028d2f5

Please sign in to comment.