mirror of
https://codeberg.org/postscriptum/snac2nex.git
synced 2026-03-31 21:25:28 +00:00
implement snac markdown filter with tests
This commit is contained in:
parent
bb2979d7e2
commit
7cc68b512e
1 changed files with 92 additions and 11 deletions
|
|
@ -27,17 +27,7 @@ impl Template {
|
||||||
) -> String {
|
) -> String {
|
||||||
let mut c = String::with_capacity(content.len() + 128);
|
let mut c = String::with_capacity(content.len() + 128);
|
||||||
for l in content.lines() {
|
for l in content.lines() {
|
||||||
if (l.starts_with("http://")
|
format_line(&mut c, l, &self.format);
|
||||||
|| l.starts_with("https://")
|
|
||||||
|| l.starts_with("nex://")
|
|
||||||
|| l.starts_with("gopher://")
|
|
||||||
|| l.starts_with("gemini://"))
|
|
||||||
&& !l.contains(' ')
|
|
||||||
&& !matches!(self.format, Format::Plain)
|
|
||||||
{
|
|
||||||
c.push_str("=> ")
|
|
||||||
}
|
|
||||||
c.push_str(l);
|
|
||||||
c.push('\n')
|
c.push('\n')
|
||||||
}
|
}
|
||||||
self.pattern
|
self.pattern
|
||||||
|
|
@ -110,3 +100,94 @@ impl Template {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Filter snac markdown
|
||||||
|
/// https://comam.es/snac-doc/snac.5.html
|
||||||
|
fn format_line(b: &mut String, l: &str, f: &Format) {
|
||||||
|
if l.starts_with('*') && l.ends_with('*') {
|
||||||
|
b.push_str(l.trim_matches('*').trim())
|
||||||
|
} else if l.starts_with('~') && l.ends_with('~') {
|
||||||
|
b.push_str(l.trim_matches('~').trim())
|
||||||
|
} else if l.starts_with('_') && l.ends_with('_') {
|
||||||
|
b.push_str(l.trim_matches('_').trim())
|
||||||
|
} else if l.starts_with('#') && !matches!(f, Format::Gemtext) {
|
||||||
|
b.push_str(l.trim_matches('#').trim())
|
||||||
|
} else if l.starts_with('>') && !matches!(f, Format::Gemtext) {
|
||||||
|
b.push_str(l.trim_matches('>').trim())
|
||||||
|
} else if (l.starts_with("http://")
|
||||||
|
|| l.starts_with("https://")
|
||||||
|
|| l.starts_with("nex://")
|
||||||
|
|| l.starts_with("gopher://")
|
||||||
|
|| l.starts_with("gemini://"))
|
||||||
|
&& !l.contains(' ')
|
||||||
|
&& matches!(f, Format::Gemtext | Format::Dir)
|
||||||
|
{
|
||||||
|
b.push_str("=> ");
|
||||||
|
b.push_str(l)
|
||||||
|
} else {
|
||||||
|
b.push_str(l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
fn test(a: &str, b: &str, f: &Format) {
|
||||||
|
let mut s = String::new();
|
||||||
|
format_line(&mut s, a, f);
|
||||||
|
assert_eq!(s, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
let f = Format::Dir;
|
||||||
|
|
||||||
|
test("*t*", "t", &f);
|
||||||
|
test("**t**", "t", &f);
|
||||||
|
test("~~t~~", "t", &f);
|
||||||
|
test("__t__", "t", &f);
|
||||||
|
|
||||||
|
test("> t", "t", &f);
|
||||||
|
test("# t", "t", &f);
|
||||||
|
test("## t", "t", &f);
|
||||||
|
test("### t", "t", &f);
|
||||||
|
|
||||||
|
test("gemini://t", "=> gemini://t", &f);
|
||||||
|
test("gopher://t", "=> gopher://t", &f);
|
||||||
|
test("http://t", "=> http://t", &f);
|
||||||
|
test("https://t", "=> https://t", &f);
|
||||||
|
test("nex://t", "=> nex://t", &f);
|
||||||
|
|
||||||
|
let f = Format::Gemtext;
|
||||||
|
|
||||||
|
test("*t*", "t", &f);
|
||||||
|
test("**t**", "t", &f);
|
||||||
|
test("~~t~~", "t", &f);
|
||||||
|
test("__t__", "t", &f);
|
||||||
|
|
||||||
|
test("> t", "> t", &f);
|
||||||
|
test("# t", "# t", &f);
|
||||||
|
test("## t", "## t", &f);
|
||||||
|
test("### t", "### t", &f);
|
||||||
|
|
||||||
|
test("gemini://t", "=> gemini://t", &f);
|
||||||
|
test("gopher://t", "=> gopher://t", &f);
|
||||||
|
test("http://t", "=> http://t", &f);
|
||||||
|
test("https://t", "=> https://t", &f);
|
||||||
|
test("nex://t", "=> nex://t", &f);
|
||||||
|
|
||||||
|
let f = Format::Plain;
|
||||||
|
|
||||||
|
test("*t*", "t", &f);
|
||||||
|
test("**t**", "t", &f);
|
||||||
|
test("~~t~~", "t", &f);
|
||||||
|
test("__t__", "t", &f);
|
||||||
|
|
||||||
|
test("> t", "t", &f);
|
||||||
|
test("# t", "t", &f);
|
||||||
|
test("## t", "t", &f);
|
||||||
|
test("### t", "t", &f);
|
||||||
|
|
||||||
|
test("gemini://t", "gemini://t", &f);
|
||||||
|
test("gopher://t", "gopher://t", &f);
|
||||||
|
test("http://t", "http://t", &f);
|
||||||
|
test("https://t", "https://t", &f);
|
||||||
|
test("nex://t", "nex://t", &f);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue