write tests
parent
7a365baaaa
commit
875329100c
|
@ -24,11 +24,11 @@ let gemtext = md2gemtext::convert("some markdown")?;
|
||||||
- code blocks get turned into code blocks (info strings are discarded)
|
- code blocks get turned into code blocks (info strings are discarded)
|
||||||
- something happens to HTML, i forget what
|
- something happens to HTML, i forget what
|
||||||
- paragraphs get empty lines between them, because i think that looks better
|
- paragraphs get empty lines between them, because i think that looks better
|
||||||
- block quotes get turned into quotes
|
- block quotes get turned into quotes (multi-paragraph contiguous block quotes break though)
|
||||||
- lists get turned into lists ("loose lists" probably misbehave, nested lists *definitely* misbehave, numbering is not preserved)
|
- lists get turned into lists ("loose lists" probably misbehave, nested lists *definitely* misbehave, numbering is not preserved)
|
||||||
- `` `markdown code spans` `` turn into `` `code spans` ``
|
- `` `markdown code spans` `` retain their backticks
|
||||||
- `*markdown italics*` turn into `_italics_`
|
- `_italics_` are surrounded by single underscores
|
||||||
- `**markdown bold**` turns into `**bold**`
|
- `**bold**` is surrounded by double asterisks
|
||||||
- `a [link](a://url) with context` turns into `a link with context` followed by `=> a://url link`
|
- `a [link](a://url) with context` turns into `a link with context` followed by `=> a://url link`
|
||||||
- `an ![inline](a://url) image` turns into `an [image: inline] image` followed by `=> a://url [image: inline]`
|
- `an ![inline](a://url) image` turns into `an [image: inline] image` followed by `=> a://url [image: inline]`
|
||||||
- if a link or image is its own paragraph, it becomes just the gemtext link, to not be redundant
|
- if a link or image is its own paragraph, it becomes just the gemtext link, to not be redundant
|
||||||
|
|
73
src/lib.rs
73
src/lib.rs
|
@ -11,7 +11,6 @@ pub fn convert(markdown_text: &str) -> String {
|
||||||
let mut state = State::new();
|
let mut state = State::new();
|
||||||
|
|
||||||
for event in parser {
|
for event in parser {
|
||||||
println!("{:?}", event);
|
|
||||||
match event {
|
match event {
|
||||||
md::Event::Start(md::Tag::Paragraph) => (),
|
md::Event::Start(md::Tag::Paragraph) => (),
|
||||||
md::Event::Start(md::Tag::Heading(level)) => state.start_heading(level),
|
md::Event::Start(md::Tag::Heading(level)) => state.start_heading(level),
|
||||||
|
@ -32,7 +31,7 @@ pub fn convert(markdown_text: &str) -> String {
|
||||||
|
|
||||||
md::Event::End(md::Tag::Paragraph) => state.finish_node(),
|
md::Event::End(md::Tag::Paragraph) => state.finish_node(),
|
||||||
md::Event::End(md::Tag::Heading(_)) => state.finish_node(),
|
md::Event::End(md::Tag::Heading(_)) => state.finish_node(),
|
||||||
md::Event::End(md::Tag::BlockQuote) => state.finish_node(),
|
md::Event::End(md::Tag::BlockQuote) => (),
|
||||||
md::Event::End(md::Tag::CodeBlock(_)) => state.finish_node(),
|
md::Event::End(md::Tag::CodeBlock(_)) => state.finish_node(),
|
||||||
md::Event::End(md::Tag::List(_)) => state.finish_list(),
|
md::Event::End(md::Tag::List(_)) => state.finish_list(),
|
||||||
md::Event::End(md::Tag::Item) => state.finish_node(),
|
md::Event::End(md::Tag::Item) => state.finish_node(),
|
||||||
|
@ -60,6 +59,7 @@ pub fn convert(markdown_text: &str) -> String {
|
||||||
|
|
||||||
let nodes = state.nodes
|
let nodes = state.nodes
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.filter(|cluster| !cluster.is_empty())
|
||||||
.map(condense)
|
.map(condense)
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(&gemtext::Node::blank());
|
.join(&gemtext::Node::blank());
|
||||||
|
@ -205,5 +205,74 @@ impl State {
|
||||||
|
|
||||||
fn add_rule(&mut self) {
|
fn add_rule(&mut self) {
|
||||||
self.add_text("-----");
|
self.add_text("-----");
|
||||||
|
self.finish_node();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[test]
|
||||||
|
fn run_tests() {
|
||||||
|
let markdown_demo = r#"
|
||||||
|
# h1
|
||||||
|
## h2
|
||||||
|
### h3
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
```
|
||||||
|
sample
|
||||||
|
text
|
||||||
|
```
|
||||||
|
|
||||||
|
> implying
|
||||||
|
|
||||||
|
1. don't pick up the phone
|
||||||
|
2. don't let him in
|
||||||
|
3. don't be his friend
|
||||||
|
|
||||||
|
some `code` and some `` fancy`code `` and *italics*
|
||||||
|
and __bold__ and ***semi-overlapping* bold *and* italics**
|
||||||
|
|
||||||
|
this [paragraph](http://example.com) has [several links](http://example.org)
|
||||||
|
and an ![inline image](a://url) in it
|
||||||
|
|
||||||
|
![this one's just an image](https://placekitten.com/200/300)
|
||||||
|
"#;
|
||||||
|
let gemtext_demo = r#"# h1
|
||||||
|
|
||||||
|
## h2
|
||||||
|
|
||||||
|
### h3
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
```
|
||||||
|
sample
|
||||||
|
text
|
||||||
|
```
|
||||||
|
|
||||||
|
> implying
|
||||||
|
|
||||||
|
* don't pick up the phone
|
||||||
|
* don't let him in
|
||||||
|
* don't be his friend
|
||||||
|
|
||||||
|
some `code` and some `fancy`code` and _italics_ and **bold** and **_semi-overlapping_ bold _and_ italics**
|
||||||
|
|
||||||
|
this paragraph has several links and an [image: inline image] in it
|
||||||
|
=> http://example.com paragraph
|
||||||
|
=> http://example.org several links
|
||||||
|
=> a://url [image: inline image]
|
||||||
|
|
||||||
|
=> https://placekitten.com/200/300 [image: this one's just an image]
|
||||||
|
"#;
|
||||||
|
assert_eq!(convert(markdown_demo), gemtext_demo);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[test]
|
||||||
|
fn test_list_start() {
|
||||||
|
let markdown = "> hi\n\n1. uh\n2. ah\n";
|
||||||
|
let gemtext = "> hi\n\n* uh\n* ah\n";
|
||||||
|
assert_eq!(convert(markdown), gemtext);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue