Change inline formatting to accept multiple inline types

Signed-off-by: trivernis <trivernis@protonmail.com>
feature/epub-rendering
trivernis 4 years ago
parent ea481feb6c
commit 53123705ac
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -1117,7 +1117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "snekdown" name = "snekdown"
version = "0.22.10" version = "0.23.0"
dependencies = [ dependencies = [
"asciimath-rs 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", "asciimath-rs 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)",

@ -1,6 +1,6 @@
[package] [package]
name = "snekdown" name = "snekdown"
version = "0.22.10" version = "0.23.0"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"

@ -184,22 +184,22 @@ pub struct PlainText {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct BoldText { pub struct BoldText {
pub(crate) value: Box<Inline>, pub(crate) value: Vec<Inline>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ItalicText { pub struct ItalicText {
pub(crate) value: Box<Inline>, pub(crate) value: Vec<Inline>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct UnderlinedText { pub struct UnderlinedText {
pub(crate) value: Box<Inline>, pub(crate) value: Vec<Inline>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct StrikedText { pub struct StrikedText {
pub(crate) value: Box<Inline>, pub(crate) value: Vec<Inline>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -209,7 +209,7 @@ pub struct MonospaceText {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SuperscriptText { pub struct SuperscriptText {
pub(crate) value: Box<Inline>, pub(crate) value: Vec<Inline>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

@ -397,31 +397,56 @@ impl ToHtml for Image {
impl ToHtml for BoldText { impl ToHtml for BoldText {
fn to_html(&self) -> String { fn to_html(&self) -> String {
format!("<b>{}</b>", self.value.to_html()) format!(
"<b>{}</b>",
self.value
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()))
)
} }
} }
impl ToHtml for UnderlinedText { impl ToHtml for UnderlinedText {
fn to_html(&self) -> String { fn to_html(&self) -> String {
format!("<u>{}</u>", self.value.to_html()) format!(
"<u>{}</u>",
self.value
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()))
)
} }
} }
impl ToHtml for ItalicText { impl ToHtml for ItalicText {
fn to_html(&self) -> String { fn to_html(&self) -> String {
format!("<i>{}</i>", self.value.to_html()) format!(
"<i>{}</i>",
self.value
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()))
)
} }
} }
impl ToHtml for StrikedText { impl ToHtml for StrikedText {
fn to_html(&self) -> String { fn to_html(&self) -> String {
format!("<del>{}</del>", self.value.to_html()) format!(
"<del>{}</del>",
self.value
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()))
)
} }
} }
impl ToHtml for SuperscriptText { impl ToHtml for SuperscriptText {
fn to_html(&self) -> String { fn to_html(&self) -> String {
format!("<sup>{}</sup>", self.value.to_html()) format!(
"<sup>{}</sup>",
self.value
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()))
)
} }
} }

@ -10,7 +10,7 @@ use std::collections::HashMap;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
pub(crate) trait ParseInline { pub(crate) trait ParseInline {
fn parse_surrounded(&mut self, surrounding: &char) -> ParseResult<Inline>; fn parse_surrounded(&mut self, surrounding: &char) -> ParseResult<Vec<Inline>>;
fn parse_inline(&mut self) -> ParseResult<Inline>; fn parse_inline(&mut self) -> ParseResult<Inline>;
fn parse_image(&mut self) -> ParseResult<Image>; fn parse_image(&mut self) -> ParseResult<Image>;
fn parse_url(&mut self, short_syntax: bool) -> ParseResult<Url>; fn parse_url(&mut self, short_syntax: bool) -> ParseResult<Url>;
@ -36,12 +36,18 @@ pub(crate) trait ParseInline {
impl ParseInline for Parser { impl ParseInline for Parser {
/// parses Inline surrounded by characters /// parses Inline surrounded by characters
fn parse_surrounded(&mut self, surrounding: &char) -> ParseResult<Inline> { fn parse_surrounded(&mut self, surrounding: &char) -> ParseResult<Vec<Inline>> {
let start_index = self.ctm.get_index(); let start_index = self.ctm.get_index();
self.ctm.assert_char(surrounding, Some(start_index))?; self.ctm.assert_char(surrounding, Some(start_index))?;
self.ctm.seek_one()?; self.ctm.seek_one()?;
let inline = self.parse_inline()?; let mut inline = vec![self.parse_inline()?];
self.ctm.assert_char(surrounding, Some(start_index))?; while !self.ctm.check_char(surrounding) {
if let Ok(result) = self.parse_inline() {
inline.push(result)
} else {
return Err(self.ctm.rewind_with_error(start_index));
}
}
if !self.ctm.check_eof() { if !self.ctm.check_eof() {
self.ctm.seek_one()?; self.ctm.seek_one()?;
} }
@ -181,24 +187,28 @@ impl ParseInline for Parser {
let start_index = self.ctm.get_index(); let start_index = self.ctm.get_index();
self.ctm.assert_sequence(&BOLD, Some(start_index))?; self.ctm.assert_sequence(&BOLD, Some(start_index))?;
self.ctm.seek_one()?; self.ctm.seek_one()?;
let inline = self.parse_inline()?; let mut inline = vec![self.parse_inline()?];
self.ctm.assert_sequence(&BOLD, Some(start_index))?; while !self.ctm.check_sequence(&BOLD) {
if let Ok(result) = self.parse_inline() {
inline.push(result);
} else {
return Err(self.ctm.rewind_with_error(start_index));
}
}
self.ctm.seek_one()?; self.ctm.seek_one()?;
Ok(BoldText { Ok(BoldText { value: inline })
value: Box::new(inline),
})
} }
fn parse_italic(&mut self) -> ParseResult<ItalicText> { fn parse_italic(&mut self) -> ParseResult<ItalicText> {
Ok(ItalicText { Ok(ItalicText {
value: Box::new(self.parse_surrounded(&ITALIC)?), value: self.parse_surrounded(&ITALIC)?,
}) })
} }
fn parse_striked(&mut self) -> ParseResult<StrikedText> { fn parse_striked(&mut self) -> ParseResult<StrikedText> {
Ok(StrikedText { Ok(StrikedText {
value: Box::new(self.parse_surrounded(&STRIKED)?), value: self.parse_surrounded(&STRIKED)?,
}) })
} }
@ -232,13 +242,13 @@ impl ParseInline for Parser {
fn parse_underlined(&mut self) -> ParseResult<UnderlinedText> { fn parse_underlined(&mut self) -> ParseResult<UnderlinedText> {
Ok(UnderlinedText { Ok(UnderlinedText {
value: Box::new(self.parse_surrounded(&UNDERLINED)?), value: self.parse_surrounded(&UNDERLINED)?,
}) })
} }
fn parse_superscript(&mut self) -> ParseResult<SuperscriptText> { fn parse_superscript(&mut self) -> ParseResult<SuperscriptText> {
Ok(SuperscriptText { Ok(SuperscriptText {
value: Box::new(self.parse_surrounded(&SUPER)?), value: self.parse_surrounded(&SUPER)?,
}) })
} }

@ -65,11 +65,36 @@ impl GetTemplateVariables for Inline {
match self { match self {
Inline::TemplateVar(temp) => vec![Arc::clone(temp)], Inline::TemplateVar(temp) => vec![Arc::clone(temp)],
Inline::Colored(col) => col.value.get_template_variables(), Inline::Colored(col) => col.value.get_template_variables(),
Inline::Superscript(sup) => sup.value.get_template_variables(), Inline::Superscript(sup) => sup
Inline::Striked(striked) => striked.value.get_template_variables(), .value
Inline::Underlined(under) => under.value.get_template_variables(), .iter()
Inline::Italic(it) => it.value.get_template_variables(), .map(|e| e.get_template_variables())
Inline::Bold(bo) => bo.value.get_template_variables(), .flatten()
.collect(),
Inline::Striked(striked) => striked
.value
.iter()
.map(|e| e.get_template_variables())
.flatten()
.collect(),
Inline::Underlined(under) => under
.value
.iter()
.map(|e| e.get_template_variables())
.flatten()
.collect(),
Inline::Italic(it) => it
.value
.iter()
.map(|e| e.get_template_variables())
.flatten()
.collect(),
Inline::Bold(bo) => bo
.value
.iter()
.map(|e| e.get_template_variables())
.flatten()
.collect(),
_ => Vec::new(), _ => Vec::new(),
} }
} }
@ -155,30 +180,71 @@ impl FreezeVariables for Inline {
col.value = Box::new(Inline::TemplateVar(temp)) col.value = Box::new(Inline::TemplateVar(temp))
} }
} }
Inline::Superscript(sup) => { Inline::Superscript(sup) => {
if let Some(temp) = sup.value.freeze_variables() { sup.value = sup
sup.value = Box::new(Inline::TemplateVar(temp)) .value
} .iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
} }
Inline::Striked(striked) => { Inline::Striked(striked) => {
if let Some(temp) = striked.value.freeze_variables() { striked.value = striked
striked.value = Box::new(Inline::TemplateVar(temp)) .value
} .iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
} }
Inline::Underlined(under) => { Inline::Underlined(under) => {
if let Some(temp) = under.value.freeze_variables() { under.value = under
under.value = Box::new(Inline::TemplateVar(temp)) .value
} .iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
} }
Inline::Italic(it) => { Inline::Italic(it) => {
if let Some(temp) = it.value.freeze_variables() { it.value = it
it.value = Box::new(Inline::TemplateVar(temp)) .value
} .iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
} }
Inline::Bold(bo) => { Inline::Bold(bo) => {
if let Some(temp) = bo.value.freeze_variables() { bo.value = bo
bo.value = Box::new(Inline::TemplateVar(temp)) .value
} .iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
} }
_ => {} _ => {}
} }

Loading…
Cancel
Save