Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kivikakk/comrak
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: furbooru/comrak
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 10 commits
  • 11 files changed
  • 2 contributors

Commits on Jul 8, 2020

  1. Copy the full SHA
    4af0505 View commit details

Commits on Jul 9, 2020

  1. support writing the extra syntax

    Xe committed Jul 9, 2020
    Copy the full SHA
    d5c8009 View commit details
  2. add initial subscript support

    Xe committed Jul 9, 2020
    Copy the full SHA
    493c2f7 View commit details
  3. add initial spoiler support

    Xe committed Jul 9, 2020
    Copy the full SHA
    36400d9 View commit details
  4. add broken image mention test

    Xe committed Jul 9, 2020
    Copy the full SHA
    fa17a33 View commit details
  5. Copy the full SHA
    ee99f4f View commit details

Commits on Sep 10, 2021

  1. Copy the full SHA
    ec3226e View commit details
  2. fix

    Meow committed Sep 10, 2021
    Copy the full SHA
    6f48adb View commit details
  3. cargo fmt

    Meow committed Sep 10, 2021
    Copy the full SHA
    4adb01d View commit details

Commits on Sep 12, 2021

  1. underline

    Meow committed Sep 12, 2021
    Copy the full SHA
    b69625e View commit details
Showing with 168 additions and 0 deletions.
  1. +1 −0 .envrc
  2. +10 −0 Cargo.lock
  3. +1 −0 Cargo.toml
  4. +15 −0 shell.nix
  5. +20 −0 src/cm.rs
  6. +29 −0 src/html.rs
  7. +8 −0 src/main.rs
  8. +12 −0 src/nodes.rs
  9. +26 −0 src/parser/inlines.rs
  10. +14 −0 src/parser/mod.rs
  11. +32 −0 src/tests.rs
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eval "$(lorri direnv)"
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ doc = false
typed-arena = "1.4.1"
regex = "1.0.1"
lazy_static = "1.0.1"
log = "0.4"
entities = "1.0.1"
unicode_categories = "0.1.1"
clap = { version = "2.32.0", optional = true }
15 changes: 15 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{ pkgs ? import <nixpkgs> { } }:

pkgs.mkShell {
buildInputs = with pkgs; [
cargo
cargo-fuzz
cargo-watch
rls
rustc
rustfmt

# keep this line if you use bash
pkgs.bashInteractive
];
}
20 changes: 20 additions & 0 deletions src/cm.rs
Original file line number Diff line number Diff line change
@@ -337,6 +337,10 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
NodeValue::TableCell => self.format_table_cell(node, entering),
NodeValue::FootnoteDefinition(_) => self.format_footnote_definition(entering),
NodeValue::FootnoteReference(ref r) => self.format_footnote_reference(r, entering),
NodeValue::Subscript => self.format_subscript(),
NodeValue::ImageMention(ref nl) => self.format_image_mention(nl),
NodeValue::SpoileredText => self.format_spoilered_text(),
NodeValue::Underline => self.format_underline(),
};
true
}
@@ -725,6 +729,22 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
self.write_all(b"]").unwrap();
}
}

fn format_subscript(&mut self) {
write!(self, "%").unwrap();
}

fn format_image_mention(&mut self, nl: &NodeLink) {
write!(self, ">>{}", String::from_utf8(nl.url.clone()).unwrap()).unwrap();
}

fn format_spoilered_text(&mut self) {
write!(self, "||").unwrap()
}

fn format_underline(&mut self) {
write!(self, "__").unwrap();
}
}

fn longest_char_sequence(literal: &[u8], ch: u8) -> usize {
29 changes: 29 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
@@ -820,6 +820,35 @@ impl<'o> HtmlFormatter<'o> {
}
}
}
NodeValue::Subscript => {
if entering {
self.output.write_all(b"<sub>")?;
} else {
self.output.write_all(b"</sub>")?;
}
}
NodeValue::SpoileredText => {
if entering {
self.output.write_all(b"<span class=\"spoiler\">")?;
} else {
self.output.write_all(b"</span>")?;
}
}
NodeValue::ImageMention(ref data) => {
log::debug!("TODO(Xe): this");
write!(
self.output,
"<a href=\"{0}\">>>{0}</a>",
String::from_utf8(data.url.clone()).unwrap()
)?;
}
NodeValue::Underline => {
if entering {
self.output.write_all(b"<ins>")?;
} else {
self.output.write_all(b"</ins>")?;
}
}
}
Ok(false)
}
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -107,6 +107,10 @@ if the file does not exist.\
"autolink",
"tasklist",
"superscript",
"subscript",
"spoiler",
"furbooru",
"underline",
"footnotes",
"description-lists",
])
@@ -197,6 +201,10 @@ if the file does not exist.\
autolink: exts.remove("autolink") || matches.is_present("gfm"),
tasklist: exts.remove("tasklist") || matches.is_present("gfm"),
superscript: exts.remove("superscript"),
subscript: exts.remove("subscript"),
spoiler: exts.remove("spoiler"),
furbooru: exts.remove("furbooru"),
underline: exts.remove("underline"),
header_ids: matches.value_of("header-ids").map(|s| s.to_string()),
footnotes: exts.remove("footnotes"),
description_lists: exts.remove("description-lists"),
12 changes: 12 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
@@ -133,6 +133,12 @@ pub enum NodeValue {
/// **Inline**. Superscript. Enabled with `ext_superscript` option.
Superscript,

/// **Inline**. Subscript. Enabled with `ext_subscript` option.
Subscript,

/// **Inline**. Underline.
Underline,

/// **Inline**. A [link](https://github.github.com/gfm/#links) to some URL, with possible
/// title.
Link(NodeLink),
@@ -142,6 +148,12 @@ pub enum NodeValue {

/// **Inline**. A footnote reference; the `Vec<u8>` is the referent footnote's name.
FootnoteReference(Vec<u8>),

/// **Inline**. Spoilered text. Enabled with `ext_furbooru` option.
SpoileredText,

/// **Inline**. Image mention link, Enabled with `ext_furbooru` option.
ImageMention(NodeLink),
}

/// Alignment of a single table cell.
26 changes: 26 additions & 0 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
@@ -91,6 +91,12 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
if options.extension.superscript {
s.special_chars[b'^' as usize] = true;
}
if options.extension.subscript {
s.special_chars[b'%' as usize] = true;
}
if options.extension.spoiler {
s.special_chars[b'|' as usize] = true;
}
for &c in &[b'"', b'\'', b'.', b'-'] {
s.smart_chars[c as usize] = true;
}
@@ -141,6 +147,12 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
new_inl = Some(self.handle_delim(b'~'));
} else if self.options.extension.superscript && c == '^' {
new_inl = Some(self.handle_delim(b'^'));
} else if self.options.extension.subscript && c == '%' {
new_inl = Some(self.handle_delim(b'%'));
} else if self.options.extension.spoiler && c == '|' {
new_inl = Some(self.handle_delim(b'|'));
} else if self.options.extension.furbooru && c == '>' {
new_inl = Some(self.handle_delim(b'>'));
} else {
let endpos = self.find_special_char();
let mut contents = self.input[self.pos..endpos].to_vec();
@@ -240,6 +252,12 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
if self.options.extension.superscript {
i['^' as usize] = stack_bottom;
}
if self.options.extension.subscript {
i['%' as usize] = stack_bottom;
}
if self.options.extension.spoiler {
i['|' as usize] = stack_bottom;
}
}

// This is traversing the stack from the top to the bottom, setting `closer` to
@@ -310,6 +328,8 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
|| closer.unwrap().delim_char == b'_'
|| (self.options.extension.strikethrough && closer.unwrap().delim_char == b'~')
|| (self.options.extension.superscript && closer.unwrap().delim_char == b'^')
|| (self.options.extension.subscript && closer.unwrap().delim_char == b'%')
|| (self.options.extension.spoiler && closer.unwrap().delim_char == b'|')
{
if opener_found {
// Finally, here's the happy case where the delimiters
@@ -774,6 +794,12 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
NodeValue::Strikethrough
} else if self.options.extension.superscript && opener_char == b'^' {
NodeValue::Superscript
} else if self.options.extension.subscript && opener_char == b'%' {
NodeValue::Subscript
} else if self.options.extension.spoiler && opener_char == b'|' && use_delims == 2 {
NodeValue::SpoileredText
} else if self.options.extension.underline && opener_char == b'_' && use_delims == 2 {
NodeValue::Underline
} else if use_delims == 1 {
NodeValue::Emph
} else {
14 changes: 14 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -218,6 +218,11 @@ pub struct ComrakExtensionOptions {
/// ```
pub superscript: bool,

/// Enables the subscript Comrak extension.
///
/// TODO(Xe): Add example here once things work.
pub subscript: bool,

/// Enables the header IDs Comrak extension.
///
/// ```
@@ -308,6 +313,15 @@ pub struct ComrakExtensionOptions {
/// assert_eq!(&String::from_utf8(buf).unwrap(), input);
/// ```
pub front_matter_delimiter: Option<String>,

/// Enables spoilertext
pub spoiler: bool,

/// Enables Furbooru extensions to Markdown parsing.
pub furbooru: bool,

/// Enables underline
pub underline: bool,
}

#[derive(Default, Debug, Clone)]
32 changes: 32 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -740,6 +740,38 @@ fn superscript() {
);
}

#[test]
fn subscript() {
html_opts!(
[extension.subscript],
concat!("e = mc%2%.\n"),
concat!("<p>e = mc<sub>2</sub>.</p>\n"),
);
}

#[test]
fn spoiler() {
html_opts!(
[extension.spoiler],
concat!("The ||dog dies at the end of Marley and Me||.\n"),
concat!(
"<p>The <span class=\"spoiler\">dog dies at the end of Marley and Me</span>.</p>\n"
),
);
}

#[test]
#[should_panic] // XXX(Xe): Still need to figure this out
fn image_mention() {
html_opts!(
[extension.furbooru],
concat!("test >>1 test\n"),
concat!(
"<p>The <span class=\"spoiler\">dog dies at the end of Marley and Me</span>.</p>\n"
),
);
}

#[test]
fn header_ids() {
html_opts(