diff options
| author | Yip Coekjan <69834864+Coekjan@users.noreply.github.com> | 2024-05-25 23:31:04 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-25 15:31:04 +0000 |
| commit | 485aa2e1ff0fdf80266fef0d5a4fa020e0e75768 (patch) | |
| tree | c6e51fb244845bdf2792b6c5f3ee062eab837e29 /crates | |
| parent | 794a215514f0bc80ca4f9c86227221c47fdb8a4b (diff) | |
Hint for language-region pair on `text.lang` (#4183)
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/typst/src/text/mod.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/crates/typst/src/text/mod.rs b/crates/typst/src/text/mod.rs index c641c8a5..be0f3b17 100644 --- a/crates/typst/src/text/mod.rs +++ b/crates/typst/src/text/mod.rs @@ -29,13 +29,14 @@ pub use self::smartquote::*; pub use self::space::*; use std::fmt::{self, Debug, Formatter}; +use std::str::FromStr; use ecow::{eco_format, EcoString}; use rustybuzz::{Feature, Tag}; use smallvec::SmallVec; use ttf_parser::Rect; -use crate::diag::{bail, warning, SourceResult, StrResult}; +use crate::diag::{bail, warning, At, Hint, SourceResult, StrResult}; use crate::engine::Engine; use crate::foundations::{ cast, category, dict, elem, Args, Array, Cast, Category, Construct, Content, Dict, @@ -393,6 +394,7 @@ pub struct TextElem { /// = Einleitung /// In diesem Dokument, ... /// ``` + #[parse(parse_lang(args)?)] #[default(Lang::ENGLISH)] #[ghost] pub lang: Lang, @@ -1300,3 +1302,27 @@ cast! { ret }, } + +/// Function to parse the language argument. +/// Provides a hint if a region is used in the language parameter. +fn parse_lang(args: &mut Args) -> SourceResult<Option<Lang>> { + let Some(Spanned { v: iso, span }) = args.named::<Spanned<EcoString>>("lang")? else { + return Ok(None); + }; + + let result = Lang::from_str(&iso); + if result.is_err() { + if let Some((lang, region)) = iso.split_once('-') { + if Lang::from_str(lang).is_ok() && Region::from_str(region).is_ok() { + return result + .hint(eco_format!( + "you should leave only \"{}\" in the `lang` parameter and specify \"{}\" in the `region` parameter", + lang, region, + )) + .at(span) + .map(Some); + } + } + } + result.at(span).map(Some) +} |
