Skip to content
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

feat: add rendering for inline math #124

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions inline/src/element/formatting/scoped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ macro_rules! scoped_parser {
pub(crate) fn $fn_name<'slice, 'input>(
mut parser: InlineParser<'slice, 'input>,
) -> (InlineParser<'slice, 'input>, Option<Inline>) {
let open_token_opt = parser.iter.peeking_next(|_| true);
if open_token_opt.is_none() {
let Some(open_token) = parser.iter.peeking_next(|_| true) else {
return (parser, None);
}

let open_token = open_token_opt.expect("Checked above to be not None.");
};

// No need to check for correct opening format, because parser is only assigned for valid opening tokens.
if parser.iter.peek_kind().map_or(true, |t| t.is_space()) {
Expand All @@ -33,6 +30,7 @@ macro_rules! scoped_parser {
!matcher.prev_is_space()
&& matcher.consumed_matches(&[InlineTokenKind::$kind.into()])
})));

scoped_parser.context.flags.allow_implicits = false;
scoped_parser.context.flags.keep_whitespaces = true;
scoped_parser.context.flags.logic_only = true;
Expand Down
2 changes: 2 additions & 0 deletions inline/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ impl<'slice, 'input> InlineParser<'slice, 'input> {

if let Some(parser_fn) = parser_fn_opt {
let checkpoint = parser.iter.checkpoint();

let (updated_parser, inline_opt) = parser_fn(parser);
parser = updated_parser;

match inline_opt {
Some(inline) => {
inlines.push(inline);
Expand Down
3 changes: 2 additions & 1 deletion render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ unimarkup-inline = { path = "../inline/", version = "0" }
unimarkup-parser = { path = "../parser/", version = "0" }
syntect = "5.0"
spreadsheet-ods = "0.17.0"
mathemascii = "0.4.0"
serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
serde_yaml.workspace = true
30 changes: 21 additions & 9 deletions render/src/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use unimarkup_inline::element::{
Underline, Verbatim,
},
textbox::{hyperlink::Hyperlink, TextBox},
InlineElement,
Inline, InlineElement,
};
use unimarkup_parser::elements::indents::{BulletList, BulletListEntry};

Expand Down Expand Up @@ -294,16 +294,28 @@ impl Renderer<Html> for HtmlRenderer {
fn render_inline_math(
&mut self,
math: &Math,
context: &Context,
_context: &Context,
) -> Result<Html, crate::log_id::RenderError> {
// TODO: use proper math rendering once supported
let inner = self.render_nested_inline(math.inner(), context)?;
// TODO: resolve logic inlines before parsing math.
let content_str: String = math
.inner()
.iter()
.filter_map(|i| match i {
Inline::Plain(p) => Some(p.content().clone()),
_ => None,
})
.collect();

Ok(Html::nested(
HtmlTag::Span,
HtmlAttributes::default(),
inner,
))
let math = mathemascii::render_mathml(mathemascii::parse(&content_str));

Ok(Html::with_body(HtmlBody {
elements: vec![HtmlElement {
tag: HtmlTag::PlainContent,
attributes: HtmlAttributes::default(),
content: Some(math),
}]
.into(),
}))
}

fn render_plain(
Expand Down
Loading