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]]
name = "snekdown"
version = "0.22.10"
version = "0.23.0"
dependencies = [
"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)",

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

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

@ -397,31 +397,56 @@ impl ToHtml for Image {
impl ToHtml for BoldText {
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 {
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 {
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 {
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 {
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};
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_image(&mut self) -> ParseResult<Image>;
fn parse_url(&mut self, short_syntax: bool) -> ParseResult<Url>;
@ -36,12 +36,18 @@ pub(crate) trait ParseInline {
impl ParseInline for Parser {
/// 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();
self.ctm.assert_char(surrounding, Some(start_index))?;
self.ctm.seek_one()?;
let inline = self.parse_inline()?;
self.ctm.assert_char(surrounding, Some(start_index))?;
let mut inline = vec![self.parse_inline()?];
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() {
self.ctm.seek_one()?;
}
@ -181,24 +187,28 @@ impl ParseInline for Parser {
let start_index = self.ctm.get_index();
self.ctm.assert_sequence(&BOLD, Some(start_index))?;
self.ctm.seek_one()?;
let inline = self.parse_inline()?;
self.ctm.assert_sequence(&BOLD, Some(start_index))?;
let mut inline = vec![self.parse_inline()?];
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()?;
Ok(BoldText {
value: Box::new(inline),
})
Ok(BoldText { value: inline })
}
fn parse_italic(&mut self) -> ParseResult<ItalicText> {
Ok(ItalicText {
value: Box::new(self.parse_surrounded(&ITALIC)?),
value: self.parse_surrounded(&ITALIC)?,
})
}
fn parse_striked(&mut self) -> ParseResult<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> {
Ok(UnderlinedText {
value: Box::new(self.parse_surrounded(&UNDERLINED)?),
value: self.parse_surrounded(&UNDERLINED)?,
})
}
fn parse_superscript(&mut self) -> ParseResult<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 {
Inline::TemplateVar(temp) => vec![Arc::clone(temp)],
Inline::Colored(col) => col.value.get_template_variables(),
Inline::Superscript(sup) => sup.value.get_template_variables(),
Inline::Striked(striked) => striked.value.get_template_variables(),
Inline::Underlined(under) => under.value.get_template_variables(),
Inline::Italic(it) => it.value.get_template_variables(),
Inline::Bold(bo) => bo.value.get_template_variables(),
Inline::Superscript(sup) => sup
.value
.iter()
.map(|e| e.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(),
}
}
@ -155,30 +180,71 @@ impl FreezeVariables for Inline {
col.value = Box::new(Inline::TemplateVar(temp))
}
}
Inline::Superscript(sup) => {
if let Some(temp) = sup.value.freeze_variables() {
sup.value = Box::new(Inline::TemplateVar(temp))
}
sup.value = sup
.value
.iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
}
Inline::Striked(striked) => {
if let Some(temp) = striked.value.freeze_variables() {
striked.value = Box::new(Inline::TemplateVar(temp))
}
striked.value = striked
.value
.iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
}
Inline::Underlined(under) => {
if let Some(temp) = under.value.freeze_variables() {
under.value = Box::new(Inline::TemplateVar(temp))
}
under.value = under
.value
.iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
}
Inline::Italic(it) => {
if let Some(temp) = it.value.freeze_variables() {
it.value = Box::new(Inline::TemplateVar(temp))
}
it.value = it
.value
.iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
}
Inline::Bold(bo) => {
if let Some(temp) = bo.value.freeze_variables() {
bo.value = Box::new(Inline::TemplateVar(temp))
}
bo.value = bo
.value
.iter_mut()
.map(|e| {
if let Some(temp) = e.freeze_variables() {
Inline::TemplateVar(temp)
} else {
e.clone()
}
})
.collect();
}
_ => {}
}

Loading…
Cancel
Save