summaryrefslogtreecommitdiff
path: root/src/library/lang.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-03-25 21:32:33 +0100
committerLaurenz <laurmaedje@gmail.com>2021-03-25 21:32:33 +0100
commit76fc4cca62f5b955200b2c62cc85b69eea491ece (patch)
tree5b8492268c996cf23b13e26c7a4356fbd156286d /src/library/lang.rs
parente8057a53856dc09594c9e5861f1cd328531616e0 (diff)
Refactor alignments & directions 📐
- Adds lang function - Refactors execution context - Adds StackChild and ParChild enums
Diffstat (limited to 'src/library/lang.rs')
-rw-r--r--src/library/lang.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/library/lang.rs b/src/library/lang.rs
new file mode 100644
index 00000000..79015c7d
--- /dev/null
+++ b/src/library/lang.rs
@@ -0,0 +1,45 @@
+use super::*;
+
+/// `lang`: Configure the language.
+///
+/// # Positional parameters
+/// - Language: of type `string`. Has to be a valid ISO 639-1 code.
+///
+/// # Named parameters
+/// - Text direction: `dir`, of type `direction`, must be horizontal.
+///
+/// # Return value
+/// A template that configures language properties.
+///
+/// # Relevant types and constants
+/// - Type `direction`
+/// - `ltr`
+/// - `rtl`
+pub fn lang(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
+ let iso = args.find::<String>(ctx).map(|s| s.to_ascii_lowercase());
+ let dir = args.get::<Spanned<Dir>>(ctx, "dir");
+
+ Value::template("lang", move |ctx| {
+ if let Some(iso) = &iso {
+ ctx.state.lang.dir = lang_dir(iso);
+ }
+
+ if let Some(dir) = dir {
+ if dir.v.axis() == SpecAxis::Horizontal {
+ ctx.state.lang.dir = dir.v;
+ } else {
+ ctx.diag(error!(dir.span, "must be horizontal"));
+ }
+ }
+
+ ctx.push_parbreak();
+ })
+}
+
+/// The default direction for the language identified by `iso`.
+fn lang_dir(iso: &str) -> Dir {
+ match iso {
+ "ar" | "he" | "fa" | "ur" | "ps" | "yi" => Dir::RTL,
+ "en" | "fr" | "de" | _ => Dir::LTR,
+ }
+}