-
Notifications
You must be signed in to change notification settings - Fork 0
Render Mustache tags as typed spans #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -193,6 +193,14 @@ fn parse_inner(src: &str, ctx: &InlineContext<'_>, depth: usize) -> Vec<Inline> | |
| let mut failed = FailedScans::default(); | ||
| let mut i = 0; | ||
| while i < src.len() { | ||
| if ctx.options.mustache && starts(src, i, "{{") { | ||
| if let Some((item, next)) = mustache(src, i) { | ||
| scanner.flush_text(); | ||
| scanner.push_inline(item); | ||
| i = next; | ||
| continue; | ||
| } | ||
| } | ||
| if starts(src, i, "\\[") | ||
| && matches!(ctx.options.math, MathMode::Brackets | MathMode::Dollars) | ||
| { | ||
|
|
@@ -420,6 +428,9 @@ fn plain_text_fast_path(src: &str, ctx: &InlineContext<'_>) -> bool { | |
| if src.contains("==") { | ||
| return false; | ||
| } | ||
| if ctx.options.mustache && src.contains("{{") { | ||
| return false; | ||
| } | ||
| if ctx.options.math == MathMode::Dollars && src.contains('$') { | ||
| return false; | ||
| } | ||
|
|
@@ -437,6 +448,24 @@ fn plain_text_fast_path(src: &str, ctx: &InlineContext<'_>) -> bool { | |
| !can_link_or_span | ||
| } | ||
|
|
||
| fn mustache(src: &str, i: usize) -> Option<(Inline, usize)> { | ||
| let end = src[i + 2..].find("}}")? + i + 4; | ||
| let body = src[i + 2..end - 2].trim_start(); | ||
| let class = match body.chars().next() { | ||
| Some('#' | '^' | '/') => "mustache.section", | ||
| Some('!') => "mustache.comment", | ||
| Some('>') => "mustache.partial", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. {{> partial}} is something we could use in fast tract to include other docx or pdf's at rendering time. |
||
| _ => "mustache.placeholder", | ||
| }; | ||
| Some(( | ||
| Inline::Span { | ||
| attrs: Attr::with_class(class), | ||
| children: vec![Inline::Html(src[i..end].to_string())], | ||
| }, | ||
| end, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We reuse existing span for simplicity instead of exposing a mustache markup in ast. |
||
| )) | ||
| } | ||
|
|
||
| struct InlineScanner<'a, 'b> { | ||
| src: &'a str, | ||
| ctx: &'b InlineContext<'b>, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mustache.comment is very unlikely class due to the
.which minimize hitting existing styles.