2016-03-18 05:41:00 +08:00
|
|
|
|
pub mod fs;
|
2018-01-06 05:03:30 +08:00
|
|
|
|
mod string;
|
2015-08-07 05:04:19 +08:00
|
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
|
use pulldown_cmark::{html, Event, Options, Parser, Tag, OPTION_ENABLE_FOOTNOTES,
|
|
|
|
|
OPTION_ENABLE_TABLES};
|
2017-06-01 13:28:08 +08:00
|
|
|
|
use std::borrow::Cow;
|
2015-09-17 05:35:16 +08:00
|
|
|
|
|
2018-01-06 05:03:30 +08:00
|
|
|
|
pub use self::string::{RangeArgument, take_lines};
|
2015-09-17 05:35:16 +08:00
|
|
|
|
|
2015-12-30 07:46:55 +08:00
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// Wrapper around the pulldown-cmark parser and renderer to render markdown
|
|
|
|
|
|
2017-06-01 13:28:08 +08:00
|
|
|
|
pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
|
2015-12-30 07:46:55 +08:00
|
|
|
|
let mut s = String::with_capacity(text.len() * 3 / 2);
|
2016-01-03 19:02:39 +08:00
|
|
|
|
|
|
|
|
|
let mut opts = Options::empty();
|
|
|
|
|
opts.insert(OPTION_ENABLE_TABLES);
|
|
|
|
|
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
|
|
|
|
|
2017-02-16 11:01:26 +08:00
|
|
|
|
let p = Parser::new_ext(text, opts);
|
2017-06-01 13:28:08 +08:00
|
|
|
|
let mut converter = EventQuoteConverter::new(curly_quotes);
|
2017-10-03 19:40:23 +08:00
|
|
|
|
let events = p.map(clean_codeblock_headers)
|
|
|
|
|
.map(|event| converter.convert(event));
|
2017-08-11 17:29:14 +08:00
|
|
|
|
|
2017-06-01 13:28:08 +08:00
|
|
|
|
html::push_html(&mut s, events);
|
2015-12-30 07:46:55 +08:00
|
|
|
|
s
|
|
|
|
|
}
|
2017-06-01 13:28:08 +08:00
|
|
|
|
|
|
|
|
|
struct EventQuoteConverter {
|
|
|
|
|
enabled: bool,
|
|
|
|
|
convert_text: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EventQuoteConverter {
|
|
|
|
|
fn new(enabled: bool) -> Self {
|
2017-10-03 19:40:23 +08:00
|
|
|
|
EventQuoteConverter {
|
|
|
|
|
enabled: enabled,
|
|
|
|
|
convert_text: true,
|
|
|
|
|
}
|
2017-06-01 13:28:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn convert<'a>(&mut self, event: Event<'a>) -> Event<'a> {
|
|
|
|
|
if !self.enabled {
|
|
|
|
|
return event;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match event {
|
2017-10-03 19:40:23 +08:00
|
|
|
|
Event::Start(Tag::CodeBlock(_)) | Event::Start(Tag::Code) => {
|
2017-06-01 13:28:08 +08:00
|
|
|
|
self.convert_text = false;
|
|
|
|
|
event
|
2017-10-03 19:40:23 +08:00
|
|
|
|
}
|
|
|
|
|
Event::End(Tag::CodeBlock(_)) | Event::End(Tag::Code) => {
|
2017-06-01 13:28:08 +08:00
|
|
|
|
self.convert_text = true;
|
|
|
|
|
event
|
2017-10-03 19:40:23 +08:00
|
|
|
|
}
|
|
|
|
|
Event::Text(ref text) if self.convert_text => {
|
|
|
|
|
Event::Text(Cow::from(convert_quotes_to_curly(text)))
|
|
|
|
|
}
|
2017-06-01 13:28:08 +08:00
|
|
|
|
_ => event,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-11 17:29:14 +08:00
|
|
|
|
fn clean_codeblock_headers(event: Event) -> Event {
|
|
|
|
|
match event {
|
|
|
|
|
Event::Start(Tag::CodeBlock(ref info)) => {
|
2017-10-03 19:40:23 +08:00
|
|
|
|
let info: String = info.chars().filter(|ch| !ch.is_whitespace()).collect();
|
2017-08-11 17:29:14 +08:00
|
|
|
|
|
|
|
|
|
Event::Start(Tag::CodeBlock(Cow::from(info)))
|
2017-10-03 19:40:23 +08:00
|
|
|
|
}
|
2017-08-11 17:29:14 +08:00
|
|
|
|
_ => event,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-06-01 13:28:08 +08:00
|
|
|
|
fn convert_quotes_to_curly(original_text: &str) -> String {
|
|
|
|
|
// We'll consider the start to be "whitespace".
|
|
|
|
|
let mut preceded_by_whitespace = true;
|
|
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
|
original_text.chars()
|
|
|
|
|
.map(|original_char| {
|
|
|
|
|
let converted_char = match original_char {
|
|
|
|
|
'\'' => {
|
|
|
|
|
if preceded_by_whitespace {
|
|
|
|
|
'‘'
|
|
|
|
|
} else {
|
|
|
|
|
'’'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
'"' => {
|
|
|
|
|
if preceded_by_whitespace {
|
|
|
|
|
'“'
|
|
|
|
|
} else {
|
|
|
|
|
'”'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => original_char,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
preceded_by_whitespace = original_char.is_whitespace();
|
|
|
|
|
|
|
|
|
|
converted_char
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2017-06-01 13:28:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
mod render_markdown {
|
|
|
|
|
use super::super::render_markdown;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_can_keep_quotes_straight() {
|
|
|
|
|
assert_eq!(render_markdown("'one'", false), "<p>'one'</p>\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_can_make_quotes_curly_except_when_they_are_in_code() {
|
|
|
|
|
let input = r#"
|
|
|
|
|
'one'
|
|
|
|
|
```
|
|
|
|
|
'two'
|
|
|
|
|
```
|
|
|
|
|
`'three'` 'four'"#;
|
|
|
|
|
let expected = r#"<p>‘one’</p>
|
|
|
|
|
<pre><code>'two'
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p><code>'three'</code> ‘four’</p>
|
|
|
|
|
"#;
|
|
|
|
|
assert_eq!(render_markdown(input, true), expected);
|
|
|
|
|
}
|
2017-08-11 17:29:14 +08:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn whitespace_outside_of_codeblock_header_is_preserved() {
|
|
|
|
|
let input = r#"
|
|
|
|
|
some text with spaces
|
|
|
|
|
```rust
|
|
|
|
|
fn main() {
|
|
|
|
|
// code inside is unchanged
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
more text with spaces
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
let expected = r#"<p>some text with spaces</p>
|
|
|
|
|
<pre><code class="language-rust">fn main() {
|
|
|
|
|
// code inside is unchanged
|
|
|
|
|
}
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>more text with spaces</p>
|
|
|
|
|
"#;
|
|
|
|
|
assert_eq!(render_markdown(input, false), expected);
|
|
|
|
|
assert_eq!(render_markdown(input, true), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn rust_code_block_properties_are_passed_as_space_delimited_class() {
|
|
|
|
|
let input = r#"
|
|
|
|
|
```rust,no_run,should_panic,property_3
|
|
|
|
|
```
|
|
|
|
|
"#;
|
|
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
|
let expected =
|
|
|
|
|
r#"<pre><code class="language-rust,no_run,should_panic,property_3"></code></pre>
|
2017-08-11 17:29:14 +08:00
|
|
|
|
"#;
|
|
|
|
|
assert_eq!(render_markdown(input, false), expected);
|
|
|
|
|
assert_eq!(render_markdown(input, true), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn rust_code_block_properties_with_whitespace_are_passed_as_space_delimited_class() {
|
|
|
|
|
let input = r#"
|
|
|
|
|
```rust, no_run,,,should_panic , ,property_3
|
|
|
|
|
```
|
|
|
|
|
"#;
|
|
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
|
let expected =
|
|
|
|
|
r#"<pre><code class="language-rust,no_run,,,should_panic,,property_3"></code></pre>
|
2017-08-11 17:29:14 +08:00
|
|
|
|
"#;
|
|
|
|
|
assert_eq!(render_markdown(input, false), expected);
|
|
|
|
|
assert_eq!(render_markdown(input, true), expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn rust_code_block_without_properties_has_proper_html_class() {
|
|
|
|
|
let input = r#"
|
2017-10-03 19:40:23 +08:00
|
|
|
|
```rust
|
2017-08-11 17:29:14 +08:00
|
|
|
|
```
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
let expected = r#"<pre><code class="language-rust"></code></pre>
|
|
|
|
|
"#;
|
|
|
|
|
assert_eq!(render_markdown(input, false), expected);
|
|
|
|
|
assert_eq!(render_markdown(input, true), expected);
|
|
|
|
|
|
|
|
|
|
let input = r#"
|
|
|
|
|
```rust
|
|
|
|
|
```
|
|
|
|
|
"#;
|
|
|
|
|
assert_eq!(render_markdown(input, false), expected);
|
|
|
|
|
assert_eq!(render_markdown(input, true), expected);
|
|
|
|
|
}
|
2017-06-01 13:28:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod convert_quotes_to_curly {
|
|
|
|
|
use super::super::convert_quotes_to_curly;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_converts_single_quotes() {
|
2017-10-03 19:40:23 +08:00
|
|
|
|
assert_eq!(convert_quotes_to_curly("'one', 'two'"),
|
|
|
|
|
"‘one’, ‘two’");
|
2017-06-01 13:28:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_converts_double_quotes() {
|
2017-10-03 19:40:23 +08:00
|
|
|
|
assert_eq!(convert_quotes_to_curly(r#""one", "two""#),
|
|
|
|
|
"“one”, “two”");
|
2017-06-01 13:28:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_treats_tab_as_whitespace() {
|
|
|
|
|
assert_eq!(convert_quotes_to_curly("\t'one'"), "\t‘one’");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|