Skip to content

Commit

Permalink
feat: add textbox & hyperlink rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mhatzl committed Nov 19, 2023
1 parent e7ffc75 commit b5f16ae
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 8 deletions.
1 change: 1 addition & 0 deletions inline/src/element/textbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub(crate) fn parse<'slice, 'input>(
}
None => {
scoped_parser.iter.rollback(checkpoint);
scoped_parser.iter.next(); // Consume open bracket
}
}

Expand Down
2 changes: 1 addition & 1 deletion inline/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ mod test {

#[test]
fn dummy_for_debugging() {
let tokens = unimarkup_commons::lexer::token::lex_str("**bold *+italic*** plain");
let tokens = unimarkup_commons::lexer::token::lex_str("[Simple textbox]");
let mut inline_parser = InlineParser {
iter: InlineTokenIterator::from(TokenIterator::from(&*tokens)),
context: InlineContext::default(),
Expand Down
16 changes: 16 additions & 0 deletions inline/tests/spec/markup/textbox.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Unimarkup specification version
spec: "0.0.1"

name: textbox
description: Contains tests for the textbox element.

tests:
- name: simple-textbox
description: |
Parse a simple textbox.
input: |
[Simple textbox]
html: |
<span>Simple textbox</span>
17 changes: 17 additions & 0 deletions inline/tests/spec/snapshots/parser/textbox/simple-textbox.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: inline/tests/parser/mod.rs
info: "Test 'simple-textbox' from 'markup\\textbox.yml'"
---
TextBox @ (1:1)->(1:17) (
Plain @ (1:2)->(1:16) (
Simple textbox
^^^^^^^^^^^^^^
)
)

---
With input:

[Simple textbox]


55 changes: 53 additions & 2 deletions render/src/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use unimarkup_commons::lexer::{span::Span, symbol::SymbolKind, token::TokenKind}
use unimarkup_inline::element::{
base::{EscapedNewline, EscapedPlain, EscapedWhitespace, Newline, Plain},
formatting::{
Bold, Highlight, Italic, Overline, Quote, Strikethrough, Subscript, Superscript, Underline,
Verbatim,
Bold, Highlight, Italic, Math, Overline, Quote, Strikethrough, Subscript, Superscript,
Underline, Verbatim,
},
textbox::{hyperlink::Hyperlink, TextBox},
InlineElement,
};
use unimarkup_parser::elements::indents::{BulletList, BulletListEntry};
Expand Down Expand Up @@ -128,6 +129,41 @@ impl Renderer<Html> for HtmlRenderer {
Ok(html)
}

fn render_textbox(
&mut self,
textbox: &TextBox,
context: &Context,
) -> Result<Html, crate::log_id::RenderError> {
let inner = self.render_nested_inline(textbox.inner(), context)?;

Ok(Html::nested(
HtmlTag::Span,
HtmlAttributes::default(),
inner,
))
}

fn render_hyperlink(
&mut self,
hyperlink: &Hyperlink,
context: &Context,
) -> Result<Html, crate::log_id::RenderError> {
let inner = self.render_nested_inline(hyperlink.inner(), context)?;
let mut attributes = vec![HtmlAttribute {
name: "href".to_string(),
value: Some(hyperlink.link().to_string()),
}];

if let Some(link_text) = hyperlink.link_text() {
attributes.push(HtmlAttribute {
name: "title".to_string(),
value: Some(link_text.to_string()),
})
}

Ok(Html::nested(HtmlTag::A, HtmlAttributes(attributes), inner))
}

fn render_bold(
&mut self,
bold: &Bold,
Expand Down Expand Up @@ -255,6 +291,21 @@ impl Renderer<Html> for HtmlRenderer {
Ok(html)
}

fn render_inline_math(
&mut self,
math: &Math,
context: &Context,
) -> Result<Html, crate::log_id::RenderError> {
// TODO: use proper math rendering once supported
let inner = self.render_nested_inline(math.inner(), context)?;

Ok(Html::nested(
HtmlTag::Span,
HtmlAttributes::default(),
inner,
))
}

fn render_plain(
&mut self,
plain: &Plain,
Expand Down
2 changes: 2 additions & 0 deletions render/src/html/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum HtmlTag {
Br,
Ul,
Li,
A,
}

impl HtmlTag {
Expand Down Expand Up @@ -57,6 +58,7 @@ impl HtmlTag {
HtmlTag::Br => "br",
HtmlTag::Ul => "ul",
HtmlTag::Li => "li",
HtmlTag::A => "a",
}
}
}
Expand Down
31 changes: 26 additions & 5 deletions render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use unimarkup_commons::{config::icu_locid::Locale, lexer::span::Span};
use unimarkup_inline::element::{
base::{EscapedNewline, EscapedPlain, EscapedWhitespace, Newline, Plain},
formatting::{
Bold, Highlight, Italic, Overline, Quote, Strikethrough, Subscript, Superscript, Underline,
Verbatim,
Bold, Highlight, Italic, Math, Overline, Quote, Strikethrough, Subscript, Superscript,
Underline, Verbatim,
},
textbox::{hyperlink::Hyperlink, TextBox},
Inline,
};
use unimarkup_parser::{
Expand Down Expand Up @@ -107,6 +108,20 @@ pub trait Renderer<T: OutputFormat> {

//--------------------------------- INLINES ---------------------------------

/// Render a [`TextBox`] to the output format `T`.
fn render_textbox(&mut self, _textbox: &TextBox, _context: &Context) -> Result<T, RenderError> {
Err(RenderError::Unimplemented)
}

/// Render a [`Hyperlink`] to the output format `T`.
fn render_hyperlink(
&mut self,
_hyperlink: &Hyperlink,
_context: &Context,
) -> Result<T, RenderError> {
Err(RenderError::Unimplemented)
}

/// Render a [`Bold` formatting](unimarkup_inline::inlines::Inline) to the output format `T`.
fn render_bold(&mut self, _bold: &Bold, _context: &Context) -> Result<T, RenderError> {
Err(RenderError::Unimplemented)
Expand Down Expand Up @@ -185,6 +200,11 @@ pub trait Renderer<T: OutputFormat> {
Err(RenderError::Unimplemented)
}

/// Render a [`Math`] to the output format `T`.
fn render_inline_math(&mut self, _math: &Math, _context: &Context) -> Result<T, RenderError> {
Err(RenderError::Unimplemented)
}

/// Render [`Plain` content](unimarkup_inline::inlines::Inline) to the output format `T`.
fn render_plain(&mut self, _plain: &Plain, _context: &Context) -> Result<T, RenderError> {
Err(RenderError::Unimplemented)
Expand Down Expand Up @@ -330,9 +350,10 @@ pub trait Renderer<T: OutputFormat> {
Inline::ImplicitNewline(implicit_newline) => {
self.render_implicit_newline(implicit_newline, context)
}
Inline::Math(_) => todo!(),
Inline::TextBox(_) => todo!(),
Inline::Hyperlink(_) => todo!(),
Inline::Math(math) => self.render_inline_math(math, context),
Inline::TextBox(textbox) => self.render_textbox(textbox, context),
Inline::Hyperlink(hyperlink) => self.render_hyperlink(hyperlink, context),

Inline::NamedSubstitution(_) => todo!(),
Inline::ImplicitSubstitution(_) => todo!(),
Inline::DirectUri(_) => todo!(),
Expand Down

0 comments on commit b5f16ae

Please sign in to comment.