Skip to content

Commit

Permalink
arch: rename to_plain_string to as_unimarkup
Browse files Browse the repository at this point in the history
  • Loading branch information
mhatzl committed Nov 19, 2023
1 parent ec5e8b3 commit 8a30879
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 73 deletions.
4 changes: 2 additions & 2 deletions commons/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::lexer::{position::Position, span::Span};

/// Every Unimarkup element must implement this trait.
pub trait Element {
/// Convert the element into its original plain markup form.
fn to_plain_string(&self) -> String;
/// Shows the element in its original plain markup form.
fn as_unimarkup(&self) -> String;
/// Return the start of the element in the original content.
fn start(&self) -> Position;
/// Return the end of the element in the original content.
Expand Down
2 changes: 1 addition & 1 deletion core/tests/snapshot/bullet_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl AsSnapshot for Snapshot<&BulletListEntry> {
.heading
.iter()
.fold(String::default(), |mut s, inline| {
s.push_str(&inline.to_plain_string());
s.push_str(&inline.as_unimarkup());
s
});
let entry_heading = if entry_heading.lines().count() > 1 {
Expand Down
2 changes: 1 addition & 1 deletion core/tests/snapshot/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl AsSnapshot for Snapshot<&Heading> {
.content
.iter()
.fold(String::default(), |mut s, inline| {
s.push_str(&inline.to_plain_string());
s.push_str(&inline.as_unimarkup());
s
});

Expand Down
2 changes: 1 addition & 1 deletion core/tests/snapshot/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl AsSnapshot for Snapshot<&Paragraph> {
.content
.iter()
.fold(String::default(), |mut s, inline| {
s.push_str(&inline.to_plain_string());
s.push_str(&inline.as_unimarkup());
s
});

Expand Down
4 changes: 2 additions & 2 deletions inline/src/element/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ macro_rules! base_inlines {

$(
impl InlineElement for $element {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
self.$content.clone()
}

Expand Down Expand Up @@ -217,7 +217,7 @@ macro_rules! element_without_content {
}

impl InlineElement for $element {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
InlineTokenKind::$element.as_str().to_string()
}

Expand Down
4 changes: 2 additions & 2 deletions inline/src/element/formatting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ macro_rules! inline_formats {
}

impl InlineElement for $format {
fn to_plain_string(&self) -> String {
format!("{}{}{}", InlineTokenKind::$format.as_str(), self.inner.to_plain_string(), if self.implicit_end {""} else {InlineTokenKind::$format.as_str()})
fn as_unimarkup(&self) -> String {
format!("{}{}{}", InlineTokenKind::$format.as_str(), self.inner.as_unimarkup(), if self.implicit_end {""} else {InlineTokenKind::$format.as_str()})
}

fn start(&self) -> Position {
Expand Down
58 changes: 31 additions & 27 deletions inline/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ pub mod textbox;

// Needed to implement the [`Element`] trait for Vec<Inline> in this crate
pub trait InlineElement {
fn to_plain_string(&self) -> String;
/// Shows the element in its original plain markup form.
fn as_unimarkup(&self) -> String;
/// Return the start of the element in the original content.
fn start(&self) -> Position;
/// Return the end of the element in the original content.
fn end(&self) -> Position;
/// The span of an element in the original content.
fn span(&self) -> Span {
Span {
start: self.start(),
Expand All @@ -36,8 +40,8 @@ pub trait InlineElement {
}

impl Element for dyn InlineElement {
fn to_plain_string(&self) -> String {
self.to_plain_string()
fn as_unimarkup(&self) -> String {
self.as_unimarkup()
}

fn start(&self) -> Position {
Expand Down Expand Up @@ -154,29 +158,29 @@ impl Inline {
}

impl InlineElement for Inline {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
match self {
Inline::Bold(inline) => inline.to_plain_string(),
Inline::Italic(inline) => inline.to_plain_string(),
Inline::Underline(inline) => inline.to_plain_string(),
Inline::Subscript(inline) => inline.to_plain_string(),
Inline::Superscript(inline) => inline.to_plain_string(),
Inline::Overline(inline) => inline.to_plain_string(),
Inline::Strikethrough(inline) => inline.to_plain_string(),
Inline::Highlight(inline) => inline.to_plain_string(),
Inline::Quote(inline) => inline.to_plain_string(),
Inline::Math(inline) => inline.to_plain_string(),
Inline::TextBox(inline) => inline.to_plain_string(),
Inline::Hyperlink(inline) => inline.to_plain_string(),
Inline::Verbatim(inline) => inline.to_plain_string(),
Inline::Newline(inline) => inline.to_plain_string(),
Inline::ImplicitNewline(inline) => inline.to_plain_string(),
Inline::EscapedNewline(inline) => inline.to_plain_string(),
Inline::EscapedWhitespace(inline) => inline.to_plain_string(),
Inline::Plain(inline) => inline.to_plain_string(),
Inline::EscapedPlain(inline) => inline.to_plain_string(),
Inline::DirectUri(inline) => inline.to_plain_string(),
Inline::ImplicitSubstitution(inline) => inline.to_plain_string(),
Inline::Bold(inline) => inline.as_unimarkup(),
Inline::Italic(inline) => inline.as_unimarkup(),
Inline::Underline(inline) => inline.as_unimarkup(),
Inline::Subscript(inline) => inline.as_unimarkup(),
Inline::Superscript(inline) => inline.as_unimarkup(),
Inline::Overline(inline) => inline.as_unimarkup(),
Inline::Strikethrough(inline) => inline.as_unimarkup(),
Inline::Highlight(inline) => inline.as_unimarkup(),
Inline::Quote(inline) => inline.as_unimarkup(),
Inline::Math(inline) => inline.as_unimarkup(),
Inline::TextBox(inline) => inline.as_unimarkup(),
Inline::Hyperlink(inline) => inline.as_unimarkup(),
Inline::Verbatim(inline) => inline.as_unimarkup(),
Inline::Newline(inline) => inline.as_unimarkup(),
Inline::ImplicitNewline(inline) => inline.as_unimarkup(),
Inline::EscapedNewline(inline) => inline.as_unimarkup(),
Inline::EscapedWhitespace(inline) => inline.as_unimarkup(),
Inline::Plain(inline) => inline.as_unimarkup(),
Inline::EscapedPlain(inline) => inline.as_unimarkup(),
Inline::DirectUri(inline) => inline.as_unimarkup(),
Inline::ImplicitSubstitution(inline) => inline.as_unimarkup(),

Inline::NamedSubstitution(_) => todo!(),
}
Expand Down Expand Up @@ -240,9 +244,9 @@ impl InlineElement for Inline {
}

impl InlineElement for Vec<Inline> {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
self.iter().fold(String::default(), |mut combined, inline| {
combined.push_str(&inline.to_plain_string());
combined.push_str(&inline.as_unimarkup());
combined
})
}
Expand Down
4 changes: 2 additions & 2 deletions inline/src/element/substitution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ImplicitSubstitution {
}

impl InlineElement for ImplicitSubstitution {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
self.kind.orig().to_string()
}

Expand Down Expand Up @@ -57,7 +57,7 @@ impl DirectUri {
}

impl InlineElement for DirectUri {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
self.uri.clone()
}

Expand Down
4 changes: 2 additions & 2 deletions inline/src/element/textbox/hyperlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl From<Hyperlink> for Inline {
}

impl InlineElement for Hyperlink {
fn to_plain_string(&self) -> String {
format!("[{}]({})", self.inner.to_plain_string(), self.link)
fn as_unimarkup(&self) -> String {
format!("[{}]({})", self.inner.as_unimarkup(), self.link)
}

fn start(&self) -> Position {
Expand Down
4 changes: 2 additions & 2 deletions inline/src/element/textbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ impl From<TextBox> for Inline {
}

impl InlineElement for TextBox {
fn to_plain_string(&self) -> String {
format!("[{}]", self.inner.to_plain_string())
fn as_unimarkup(&self) -> String {
format!("[{}]", self.inner.as_unimarkup())
}

fn start(&self) -> Position {
Expand Down
4 changes: 2 additions & 2 deletions inline/tests/parser/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl AsSnapshot for Snapshot<&str> {
impl AsSnapshot for Snapshot<&[Inline]> {
fn as_snapshot(&self) -> String {
self.iter()
.filter(|inline| !inline.to_plain_string().trim().is_empty())
.filter(|inline| !inline.as_unimarkup().trim().is_empty())
.map(Snapshot::snap)
.collect()
}
Expand All @@ -27,7 +27,7 @@ impl AsSnapshot for Snapshot<&[Inline]> {
impl AsSnapshot for Snapshot<&Vec<Inline>> {
fn as_snapshot(&self) -> String {
self.iter()
.filter(|inline| !inline.to_plain_string().trim().is_empty())
.filter(|inline| !inline.as_unimarkup().trim().is_empty())
.map(Snapshot::snap)
.collect()
}
Expand Down
6 changes: 3 additions & 3 deletions parser/src/elements/atomic/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ pub struct Heading {
}

impl BlockElement for Heading {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
let prefix = self.level.as_str();
let content = self
.content
.to_plain_string()
.as_unimarkup()
.lines()
.join(&" ".repeat(prefix.len()));
format!("{prefix}{content}")
Expand Down Expand Up @@ -226,7 +226,7 @@ impl Heading {
/// Whitespaces are replaced with `-`, quotes and backslash are removed,
/// and all other content is lowercased.
fn as_id(content: &Vec<Inline>) -> String {
let mut s = content.to_plain_string().to_lowercase();
let mut s = content.as_unimarkup().to_lowercase();
s = s.replace(char::is_whitespace, "-");
s = s.replace('\\', ""); // backslash removed to prevent html escapes
s.replace(['\'', '"'], "") // quotes removed to prevent early attribute closing
Expand Down
4 changes: 2 additions & 2 deletions parser/src/elements/atomic/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub struct Paragraph {
}

impl BlockElement for Paragraph {
fn to_plain_string(&self) -> String {
self.content.to_plain_string()
fn as_unimarkup(&self) -> String {
self.content.as_unimarkup()
}

fn start(&self) -> unimarkup_commons::lexer::position::Position {
Expand Down
16 changes: 8 additions & 8 deletions parser/src/elements/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ impl Block {
}

impl BlockElement for Block {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
match self {
Block::Blankline(_) => String::default(), // Newline is pushed after every block, so blankline is empty on its own
Block::Heading(block) => block.to_plain_string(),
Block::Paragraph(block) => block.to_plain_string(),
Block::VerbatimBlock(block) => block.to_plain_string(),
Block::BulletList(block) => block.to_plain_string(),
Block::BulletListEntry(block) => block.to_plain_string(),
Block::Heading(block) => block.as_unimarkup(),
Block::Paragraph(block) => block.as_unimarkup(),
Block::VerbatimBlock(block) => block.as_unimarkup(),
Block::BulletList(block) => block.as_unimarkup(),
Block::BulletListEntry(block) => block.as_unimarkup(),
}
}

Expand Down Expand Up @@ -78,9 +78,9 @@ impl BlockElement for Block {
}

impl BlockElement for Vec<Block> {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
let mut s = self.iter().fold(String::default(), |mut combined, block| {
combined.push_str(&block.to_plain_string());
combined.push_str(&block.as_unimarkup());
combined.push_str(SymbolKind::Newline.as_str());
combined
});
Expand Down
4 changes: 2 additions & 2 deletions parser/src/elements/enclosed/verbatim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct VerbatimBlock {
}

impl BlockElement for VerbatimBlock {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
let ticks = SymbolKind::Tick.as_str().repeat(self.tick_len);
let lang = self.data_lang.clone().unwrap_or_default();
format!(
Expand Down Expand Up @@ -131,7 +131,7 @@ impl VerbatimBlock {
(
parser,
Some(Block::VerbatimBlock(VerbatimBlock {
content: content.to_plain_string(),
content: content.as_unimarkup(),
data_lang,
attributes: None,
implicit_closed,
Expand Down
10 changes: 5 additions & 5 deletions parser/src/elements/indents/bullet_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ pub struct BulletList {
}

impl BlockElement for BulletList {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
let mut s = String::default();

for entry in &self.entries {
s.push_str(&entry.to_plain_string())
s.push_str(&entry.as_unimarkup())
}

s
Expand Down Expand Up @@ -121,7 +121,7 @@ pub struct BulletListEntry {
}

impl BlockElement for BulletListEntry {
fn to_plain_string(&self) -> String {
fn as_unimarkup(&self) -> String {
let head_body_separator = match self.body.first() {
Some(Block::BulletList(_)) | None => SymbolKind::Newline.as_str().to_string(),
Some(_) => SymbolKind::Newline.as_str().repeat(2), // to get a blankline between head and body
Expand All @@ -130,13 +130,13 @@ impl BlockElement for BulletListEntry {
let plain_body = if self.body.is_empty() {
String::default()
} else {
self.body.to_plain_string().lines().join("\n ")
self.body.as_unimarkup().lines().join("\n ")
}; // Two space indentation after newline

format!(
"{} {}{}{}",
self.keyword.as_str(),
self.heading.to_plain_string(),
self.heading.as_unimarkup(),
head_body_separator,
plain_body
)
Expand Down
15 changes: 7 additions & 8 deletions parser/src/elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ pub type Blocks = Vec<Block>;

/// Needed trait to implement [`Element`] trait for Vec<Block> in this crate
pub trait BlockElement {
/// Converts a block into the original string representation.
/// e.g. "# Heading" for level-1 heading
fn to_plain_string(&self) -> String;
/// The start of a block in the original content.
/// Shows the element in its original plain markup form.
fn as_unimarkup(&self) -> String;
/// Return the start of the element in the original content.
fn start(&self) -> Position;
/// The end of a block in the original content.
/// Return the end of the element in the original content.
fn end(&self) -> Position;
/// The span of the block in the original content.
/// The span of an element in the original content.
fn span(&self) -> Span {
Span {
start: self.start(),
Expand All @@ -36,8 +35,8 @@ pub trait BlockElement {
}

impl Element for dyn BlockElement {
fn to_plain_string(&self) -> String {
self.to_plain_string()
fn as_unimarkup(&self) -> String {
self.as_unimarkup()
}

fn start(&self) -> Position {
Expand Down
2 changes: 1 addition & 1 deletion render/src/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl Renderer<Html> for HtmlRenderer {
let html = Html::with_body(HtmlBody::from(HtmlElement {
tag: HtmlTag::Code,
attributes: HtmlAttributes::default(),
content: Some(verbatim.inner().to_plain_string()),
content: Some(verbatim.inner().as_unimarkup()),
}));

Ok(html)
Expand Down

0 comments on commit 8a30879

Please sign in to comment.