From e089b6ea40015e012302dc55ac5d6cb42ca4876e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 8 Feb 2022 16:39:37 +0100 Subject: Set rules for everything --- src/eval/collapse.rs | 26 +++-- src/eval/mod.rs | 150 +++++------------------- src/eval/show.rs | 96 +++++++++++++++ src/eval/styles.rs | 30 ++--- src/eval/template.rs | 217 ++++++++++++++++++++-------------- src/eval/value.rs | 2 +- src/layout/mod.rs | 78 ++++++------- src/library/align.rs | 4 +- src/library/columns.rs | 7 +- src/library/container.rs | 2 +- src/library/deco.rs | 76 ++++++------ src/library/flow.rs | 34 +++--- src/library/grid.rs | 8 +- src/library/heading.rs | 78 +++++++------ src/library/hide.rs | 2 +- src/library/image.rs | 5 - src/library/link.rs | 34 +++++- src/library/list.rs | 57 +++++---- src/library/math.rs | 32 +++++ src/library/mod.rs | 20 ++-- src/library/pad.rs | 4 +- src/library/page.rs | 296 ++++++++++++++++++++--------------------------- src/library/par.rs | 2 +- src/library/place.rs | 4 +- src/library/raw.rs | 117 +++++++++++++++++++ src/library/shape.rs | 10 +- src/library/spacing.rs | 6 + src/library/stack.rs | 4 +- src/library/table.rs | 19 +-- src/library/text.rs | 125 +++++++++----------- src/library/transform.rs | 7 +- src/util/mod.rs | 2 + src/util/prehashed.rs | 72 ++++++++++++ 33 files changed, 934 insertions(+), 692 deletions(-) create mode 100644 src/eval/show.rs create mode 100644 src/library/math.rs create mode 100644 src/library/raw.rs create mode 100644 src/util/prehashed.rs (limited to 'src') diff --git a/src/eval/collapse.rs b/src/eval/collapse.rs index 0e91cae6..ef8a5255 100644 --- a/src/eval/collapse.rs +++ b/src/eval/collapse.rs @@ -3,7 +3,7 @@ use super::{StyleChain, StyleVec, StyleVecBuilder}; /// A wrapper around a [`StyleVecBuilder`] that allows to collapse items. pub struct CollapsingBuilder<'a, T> { builder: StyleVecBuilder<'a, T>, - staged: Vec<(T, StyleChain<'a>, bool)>, + staged: Vec<(T, StyleChain<'a>, Option)>, last: Last, } @@ -29,9 +29,21 @@ impl<'a, T: Merge> CollapsingBuilder<'a, T> { /// and to its right, with no destructive items or weak items in between to /// its left and no destructive items in between to its right. There may be /// ignorant items in between in both directions. - pub fn weak(&mut self, item: T, styles: StyleChain<'a>) { - if self.last == Last::Supportive { - self.staged.push((item, styles, true)); + pub fn weak(&mut self, item: T, strength: u8, styles: StyleChain<'a>) { + if self.last != Last::Destructive { + if self.last == Last::Weak { + if let Some(i) = self + .staged + .iter() + .position(|(.., prev)| prev.map_or(false, |p| p < strength)) + { + self.staged.remove(i); + } else { + return; + } + } + + self.staged.push((item, styles, Some(strength))); self.last = Last::Weak; } } @@ -52,7 +64,7 @@ impl<'a, T: Merge> CollapsingBuilder<'a, T> { /// Has no influence on other items. pub fn ignorant(&mut self, item: T, styles: StyleChain<'a>) { - self.staged.push((item, styles, false)); + self.staged.push((item, styles, None)); } /// Return the finish style vec and the common prefix chain. @@ -63,8 +75,8 @@ impl<'a, T: Merge> CollapsingBuilder<'a, T> { /// Push the staged items, filtering out weak items if `supportive` is false. fn flush(&mut self, supportive: bool) { - for (item, styles, weak) in self.staged.drain(..) { - if !weak || supportive { + for (item, styles, strength) in self.staged.drain(..) { + if supportive || strength.is_none() { push_merging(&mut self.builder, item, styles); } } diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 98b0152c..5a67555c 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -14,6 +14,7 @@ mod collapse; mod func; mod ops; mod scope; +mod show; mod template; pub use array::*; @@ -23,6 +24,7 @@ pub use collapse::*; pub use dict::*; pub use func::*; pub use scope::*; +pub use show::*; pub use styles::*; pub use template::*; pub use value::*; @@ -33,34 +35,23 @@ use std::io; use std::mem; use std::path::PathBuf; -use once_cell::sync::Lazy; -use syntect::easy::HighlightLines; -use syntect::highlighting::{FontStyle, Highlighter, Style as SynStyle, Theme, ThemeSet}; -use syntect::parsing::SyntaxSet; use unicode_segmentation::UnicodeSegmentation; use crate::diag::{At, Error, StrResult, Trace, Tracepoint, TypResult}; -use crate::geom::{Angle, Color, Fractional, Length, Paint, Relative}; +use crate::geom::{Angle, Fractional, Length, Relative}; use crate::image::ImageStore; use crate::layout::Layout; -use crate::library::{self, DecoLine, TextNode}; +use crate::library::{self}; use crate::loading::Loader; -use crate::parse; use crate::source::{SourceId, SourceStore}; -use crate::syntax; use crate::syntax::ast::*; -use crate::syntax::{RedNode, Span, Spanned}; +use crate::syntax::{Span, Spanned}; use crate::util::{EcoString, RefMutExt}; use crate::Context; -static THEME: Lazy = - Lazy::new(|| ThemeSet::load_defaults().themes.remove("InspiredGitHub").unwrap()); - -static SYNTAXES: Lazy = Lazy::new(|| SyntaxSet::load_defaults_newlines()); - /// An evaluated module, ready for importing or conversion to a root layout /// tree. -#[derive(Debug, Default, Clone)] +#[derive(Debug, Clone)] pub struct Module { /// The top-level definitions that were bound in this module. pub scope: Scope, @@ -194,17 +185,13 @@ fn eval_markup( MarkupNode::Expr(Expr::Wrap(wrap)) => { let tail = eval_markup(ctx, nodes)?; ctx.scopes.def_mut(wrap.binding().take(), tail); - wrap.body().eval(ctx)?.show() + wrap.body().eval(ctx)?.display() } _ => node.eval(ctx)?, }); } - if seq.len() == 1 { - Ok(seq.into_iter().next().unwrap()) - } else { - Ok(Template::Sequence(seq)) - } + Ok(Template::sequence(seq)) } impl Eval for MarkupNode { @@ -223,7 +210,7 @@ impl Eval for MarkupNode { Self::Heading(heading) => heading.eval(ctx)?, Self::List(list) => list.eval(ctx)?, Self::Enum(enum_) => enum_.eval(ctx)?, - Self::Expr(expr) => expr.eval(ctx)?.show(), + Self::Expr(expr) => expr.eval(ctx)?.display(), }) } } @@ -232,7 +219,7 @@ impl Eval for StrongNode { type Output = Template; fn eval(&self, ctx: &mut EvalContext) -> TypResult { - Ok(self.body().eval(ctx)?.styled(TextNode::STRONG, true)) + Ok(Template::show(library::StrongNode(self.body().eval(ctx)?))) } } @@ -240,7 +227,7 @@ impl Eval for EmphNode { type Output = Template; fn eval(&self, ctx: &mut EvalContext) -> TypResult { - Ok(self.body().eval(ctx)?.styled(TextNode::EMPH, true)) + Ok(Template::show(library::EmphNode(self.body().eval(ctx)?))) } } @@ -248,104 +235,25 @@ impl Eval for RawNode { type Output = Template; fn eval(&self, _: &mut EvalContext) -> TypResult { - let code = self.highlighted(); - Ok(if self.block { Template::Block(code.pack()) } else { code }) - } -} - -impl RawNode { - /// Styled template for a code block, with optional syntax highlighting. - pub fn highlighted(&self) -> Template { - let mut seq: Vec