Skip to content

Commit

Permalink
Fix for format range error and table problems(kivikakk#24)
Browse files Browse the repository at this point in the history
* Fix for format range error

* Eliminate breaks and unused syntax

* Remove breaks and fix blockquote behavior

* Fix table error and format to work for nesting
  • Loading branch information
Shaquille Johnson committed Jan 26, 2018
1 parent db108b6 commit 4f96638
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 63 deletions.
25 changes: 18 additions & 7 deletions specs/rtjson.spec
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ This post has a paragraph in the middle.
// TODO: This utilizes one of the breaks.

```````````````````````````````` example
>Line proceeding; this line has a [link](https://reddit.com) and a r/redditlink
>Line proceeding; this line has a [link](https://reddit.com) and a r/redditlink.
>
>Line preceding; no line proceeding
>
>No line preceding; no line proceeding
>
>No line preceding; line proceeding
>No line preceding; line proceeding
>
>Line preceding
.
{"document":[{"e":"blockquote","c":[{"e":"par","c":[{"e":"text","t":"Line proceeding; this line has a "},{"e":"link","u":"https://reddit.com","t":"link"},{"e":"text","t":" and a "},{"e":"link","u":"/r/redditlink","t":"r/redditlink"},{"e":"br"},{"e":"text","t":"Line preceding; no line proceeding"}]},{"e":"par","c":[{"e":"text","t":"No line preceding; no line proceeding"}]},{"e":"par","c":[{"e":"br"},{"e":"text","t":"No line preceding; line proceedingLine preceding"}]}]}]}````````````````````````````````
{"document":[{"e":"blockquote","c":[{"e":"par","c":[{"e":"text","t":"Line proceeding; this line has a "},{"e":"link","u":"https://reddit.com","t":"link"},{"e":"text","t":" and a "},{"e":"link","u":"/r/redditlink","t":"r/redditlink"},{"e":"text","t":"."}]},{"e":"par","c":[{"e":"text","t":"Line preceding; no line proceeding"}]},{"e":"par","c":[{"e":"text","t":"No line preceding; no line proceeding"}]},{"e":"par","c":[{"e":"text","t":"No line preceding; line proceeding"}]},{"e":"par","c":[{"e":"text","t":"Line preceding"}]}]}]}````````````````````````````````

```````````````````````````````` example
>This post ends with a blockquote\n\nwith embedded newlines.
Expand All @@ -53,7 +55,7 @@ This post has a paragraph in the middle.
```````````````````````````````` example
Hello, **this is bold**, *this is italic*, ***this is both***. And this is a u/username and a r/subreddit.
.
{"document":[{"e":"par","c":[{"e":"text","t":"Hello, this is bold, this is italic, this is both. And this is a ","f":[[1, 7, 12], [2, 21, 14], [3, 37, 12], [2, 49, 16]]},{"e":"link","u":"/u/username","t":"u/username"},{"e":"text","t":" and a "},{"e":"link","u":"/r/subreddit","t":"r/subreddit"},{"e":"text","t":"."}]}]}````````````````````````````````
{"document":[{"e":"par","c":[{"e":"text","t":"Hello, this is bold, this is italic, this is both. And this is a ","f":[[1, 7, 12], [2, 21, 14], [3, 37, 12]]},{"e":"link","u":"/u/username","t":"u/username"},{"e":"text","t":" and a "},{"e":"link","u":"/r/subreddit","t":"r/subreddit"},{"e":"text","t":"."}]}]}````````````````````````````````

```````````````````````````````` example
Below this is a list:
Expand All @@ -79,9 +81,9 @@ Above this is a list.
```````````````````````````````` example
|Col 1|Col 2|Col 3|
|:-|:-:|-:|
|a |**bold*****bold+italic****italic*|a |
|a |**bold**​***bold+italic***​*italic*|a |
.
{"document":[{"e":"table","h":[[{"a":"L","e":"text","t":"Col 1"},{"a":"C","e":"text","t":"Col 2"},{"a":"R","e":"text","t":"Col 3"}]],"c":[,[{"c":[{"e":"text","t":"a"}]},{"c":[{"e":"text","t":"boldbold+italic**italic","f":[[1, 0, 4], [3, 4, 11], [2, 15, 8]]}]},{"c":[{"e":"text","t":"a"}]}]]}]}````````````````````````````````
{"document":[{"e":"table","h":[{"a":"L","c":[{"e":"text","t":"Col 1"}]},{"a":"C","c":[{"e":"text","t":"Col 2"}]},{"a":"R","c":[{"e":"text","t":"Col 3"}]}],"c":[[{"c":[{"e":"text","t":"a"}]},{"c":[{"e":"text","t":"bold​bold+italicitalic","f":[[1, 0, 4], [3, 7, 11], [2, 21, 6]]}]},{"c":[{"e":"text","t":"a"}]}]]}]}````````````````````````````````

```````````````````````````````` example
These are two tables:
Expand All @@ -94,7 +96,7 @@ These are two tables:
|:-|:-|
|c1:r2|c2:r2|
.
{"document":[{"e":"par","c":[{"e":"text","t":"These are two tables:"}]},{"e":"table","h":[[{"a":"L","e":"text","t":"Table"},{"a":"L","e":"text","t":"1"}]],"c":[,[{"c":[{"e":"text","t":"c1:r1"}]},{"c":[{"e":"text","t":"c2:r1"}]}]]},{"e":"table","h":[[{"a":"L","e":"text","t":"Table"},{"a":"L","e":"text","t":"2"}]],"c":[,[{"c":[{"e":"text","t":"c1:r2"}]},{"c":[{"e":"text","t":"c2:r2"}]}]]}]}````````````````````````````````
{"document":[{"e":"par","c":[{"e":"text","t":"These are two tables:"}]},{"e":"table","h":[{"a":"L","c":[{"e":"text","t":"Table"}]},{"a":"L","c":[{"e":"text","t":"1"}]}],"c":[[{"c":[{"e":"text","t":"c1:r1"}]},{"c":[{"e":"text","t":"c2:r1"}]}]]},{"e":"table","h":[{"a":"L","c":[{"e":"text","t":"Table"}]},{"a":"L","c":[{"e":"text","t":"2"}]}],"c":[[{"c":[{"e":"text","t":"c1:r2"}]},{"c":[{"e":"text","t":"c2:r2"}]}]]}]}````````````````````````````````

```````````````````````````````` example
Hello reddit, \*\***this should be bold,**\*\* the stars around it should not be.
Expand Down Expand Up @@ -154,3 +156,12 @@ www.thisisalink.com
}
.
{"document":[{"e":"code","c":[{"e":"raw","t":"function test() {"},{"e":"raw","t":" console.log("notice the blank line before this function?");"},{"e":"raw","t":"}"},{"e":"raw","t":""}]}]}````````````````````````````````

Say I have many formats nested in one format range. We would want to keep that
overall format through the whole thing, while also getting rid of the old format
each time we went on.

```````````````````````````````` example
*__bold__ ~underline~ ~~strikethrough~~*
.
{"document":[{"e":"par","c":[{"e":"text","t":"bold underline strikethrough","f":[[3, 0, 4], [2, 4, 1], [6, 5, 9], [2, 14, 1], [10, 15, 13]]}]}]}````````````````````````````````
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ extern crate libc;
#[macro_use] extern crate cpython;

use cpython::{PyResult, Python};
use libc::c_char;
use std::ffi::{CStr, CString};
use nodes::AstNode;

/// Render Markdown to HTML.
///
Expand Down
19 changes: 9 additions & 10 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,16 +1333,6 @@ impl<'a, 'o> Parser<'a, 'o> {
format_ranges.push(new_range);
}
unformatted_text.push_str(text);
if node.same_node(node.parent().unwrap().last_child().unwrap()){
match node.parent().unwrap().data.borrow().value {
NodeValue::Strong => *current_format -= 1,
NodeValue::Emph => *current_format -= 2,
NodeValue::Underline => *current_format -= 4,
NodeValue::Strikethrough => *current_format -= 8,
NodeValue::Superscript => *current_format -= 32,
_ => ()
}
}
},
NodeValue::Link(_) => {
if !unformatted_text.is_empty() {
Expand Down Expand Up @@ -1389,6 +1379,15 @@ impl<'a, 'o> Parser<'a, 'o> {
format_ranges
);
}

match node.data.borrow().value {
NodeValue::Strong => *current_format -= 1,
NodeValue::Emph => *current_format -= 2,
NodeValue::Underline => *current_format -= 4,
NodeValue::Strikethrough => *current_format -= 8,
NodeValue::Superscript => *current_format -= 32,
_ => ()
}

if node.data.borrow_mut().value.contains_inlines() {
match node.data.borrow_mut().value {
Expand Down
65 changes: 22 additions & 43 deletions src/rtjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ impl<'o> RTJsonFormatter<'o> {
}
NodeValue::HtmlBlock(_) => unreachable!(),
NodeValue::ThematicBreak | NodeValue::LineBreak |
NodeValue:: SoftBreak => {
if entering {
self.s += r#"{"e":"br"}"#
}
}
NodeValue:: SoftBreak => (),
NodeValue::Code(_) => {
if entering {
self.s += r#"{"e":"error code"}"#;
Expand All @@ -231,24 +227,15 @@ impl<'o> RTJsonFormatter<'o> {
NodeValue::Image(_) => self.s += self.escape(literal).as_str(),
NodeValue::Text(ref literal) |
NodeValue::Code(ref literal) => self.s += self.escape(literal).as_str(),
NodeValue::LineBreak | NodeValue::SoftBreak | NodeValue::ThematicBreak => self.s += r#"{"e":"br"},"#,
NodeValue::LineBreak | NodeValue::SoftBreak | NodeValue::ThematicBreak => (),
NodeValue::Heading(_) | NodeValue::CodeBlock(_) => {
self.s += format!(r#"{{"e":"raw","t":"{}"}}"#, self.escape(literal)).as_str();
}
NodeValue::BlockQuote | NodeValue::Paragraph => {
self.s += format!(r#"{{"e":"text","t":"{}"}}"#, self.escape(literal)).as_str();
}
NodeValue::TableCell => {
let row = &node.parent().unwrap().parent().unwrap().data.borrow().value;
let in_header = match *row {
NodeValue::TableRow(header) => header,
_ => panic!(),
};
if in_header {
self.s += format!( r#""e":"text","t":"{}"}}"#, self.escape(literal)).as_str();
} else {
self.s += format!( r#"{{"e":"text","t":"{}"}}"#, self.escape(literal)).as_str();
}
self.s += format!( r#"{{"e":"text","t":"{}"}}"#, self.escape(literal)).as_str();
}
NodeValue::Document | NodeValue::Strong | NodeValue::Emph |
NodeValue::Underline | NodeValue::Superscript |
Expand All @@ -264,22 +251,13 @@ impl<'o> RTJsonFormatter<'o> {
if entering {
match node.parent().unwrap().data.borrow().value {
NodeValue::TableCell => {
let row = &node.parent().unwrap().parent().unwrap().data.borrow().value;
let in_header = match *row {
NodeValue::TableRow(header) => header,
_ => panic!(),
};
if in_header {
self.s += format!( r#""e":"text","t":"{}","f":{:?}}}"#, self.escape(literal), format_ranges).as_str();
} else {
self.s += format!( r#"{{"e":"text","t":"{}","f":{:?}}}"#, self.escape(literal), format_ranges).as_str();
}
self.s += format!( r#"{{"e":"text","t":"{}","f":{:?}}}"#, self.escape(literal), format_ranges).as_str();
},
NodeValue::Link(_) => self.s += self.escape(literal).as_str(),
NodeValue::Image(_) => self.s += self.escape(literal).as_str(),
NodeValue::Text(ref literal) |
NodeValue::Code(ref literal) => self.s += self.escape(literal).as_str(),
NodeValue::LineBreak | NodeValue::SoftBreak | NodeValue::ThematicBreak => self.s += r#"{"e":"br"},"#,
NodeValue::LineBreak | NodeValue::SoftBreak | NodeValue::ThematicBreak => (),
NodeValue::Heading(_) | NodeValue::CodeBlock(_) => {
self.s += format!(r#"{{"e":"raw","t":"{}"}}"#, self.escape(literal)).as_str();
},
Expand Down Expand Up @@ -311,18 +289,11 @@ impl<'o> RTJsonFormatter<'o> {
self.s += format!(r#"{{"e":"link","u":"{}","t":"{}"}}"#, self.escape_href(&nl.url), self.escape(&nl.title)).as_str();
}
}
NodeValue::Link(ref nl) => {
if entering {
self.s += format!(r#"{{"e":"link","u":"{}","t":"{}"}}"#, self.escape_href(&nl.url), self.escape(&nl.title)).as_str();
self.append_comma(node);
}
}
NodeValue::Image(ref nl) => {
if entering {
self.s += format!(r#"{{"e":"link","u":"{}","t":""#, self.escape_href(&nl.url)).as_str();
} else {
self.s += r#""}"#;
self.append_comma(node);
}
}
NodeValue::Table(..) => {
Expand All @@ -341,15 +312,16 @@ impl<'o> RTJsonFormatter<'o> {
if entering {
if header {
self.s += r#""h":["#;
} else {
self.s += "[";
}
self.s += "[";
} else {
self.s += "]";
if node.next_sibling().is_some() && !header {
self.s += ",";
}
if header {
self.s += "],";
self.s += ",";
self.s += r#""c":["#;
}
}
Expand Down Expand Up @@ -382,26 +354,33 @@ impl<'o> RTJsonFormatter<'o> {
start = start.next_sibling().unwrap();
}

self.s += "{";
if in_header {
self.s += "{";

match alignments[i] {
TableAlignment::Left => self.s += r#""a":"L","#,
TableAlignment::Right => self.s += r#""a":"R","#,
TableAlignment::Center => self.s += r#""a":"C","#,
TableAlignment::None => (),
}
} else {
self.s += r#"{"c":["#;
}

} else if !in_header {

self.s += r#""c":["#;
} else {
self.s += "]}";
}
}
}
if !entering {
self.append_comma(node);

match node.data.borrow().value {
NodeValue::TableRow(..) |
NodeValue::LineBreak | NodeValue::SoftBreak |
NodeValue::ThematicBreak => (),
_ => {
if !entering {
self.append_comma(node);
}
}
}
false
}
Expand Down

0 comments on commit 4f96638

Please sign in to comment.