From d6b5b9a721af32f541741a16c373983b5494acb7 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Sun, 27 Dec 2020 20:38:23 +0100 Subject: [PATCH] upgrade to pulldown v0.8 --- Cargo.lock | 8 +++-- plume-common/Cargo.toml | 2 +- plume-common/src/utils.rs | 69 +++++++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6bf81759..90c4b1e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2600,7 +2600,7 @@ dependencies = [ "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.35 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.30 (registry+https://github.com/rust-lang/crates.io-index)", - "pulldown-cmark 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pulldown-cmark 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.6.18 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2770,10 +2770,12 @@ dependencies = [ [[package]] name = "pulldown-cmark" -version = "0.2.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -4883,7 +4885,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa" "checksum publicsuffix 1.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3bbaa49075179162b49acac1c6aa45fb4dafb5f13cf6794276d77bc7fd95757b" -"checksum pulldown-cmark 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eef52fac62d0ea7b9b4dc7da092aa64ea7ec3d90af6679422d3d7e0e14b6ee15" +"checksum pulldown-cmark 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" "checksum quick-error 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" "checksum quick-xml 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1d8065cbb01701c11cc195cde85cbf39d1c6a80705b67a157ebb3042e0e5777f" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" diff --git a/plume-common/Cargo.toml b/plume-common/Cargo.toml index ce9735aa..05480410 100644 --- a/plume-common/Cargo.toml +++ b/plume-common/Cargo.toml @@ -30,4 +30,4 @@ version = "0.4" [dependencies.pulldown-cmark] default-features = false -version = "0.2.0" +version = "0.8.0" diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index 89d5b3a2..a51b7c15 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -1,12 +1,11 @@ use heck::CamelCase; use openssl::rand::rand_bytes; -use pulldown_cmark::{html, Event, Options, Parser, Tag}; +use pulldown_cmark::{html, LinkType, Event, Options, Parser, Tag, CodeBlockKind, CowStr}; use regex_syntax::is_word_character; use rocket::{ http::uri::Uri, response::{Flash, Redirect}, }; -use std::borrow::Cow; use std::collections::HashSet; use syntect::html::ClassedHTMLGenerator; use syntect::parsing::SyntaxSet; @@ -51,10 +50,10 @@ enum State { fn to_inline(tag: Tag<'_>) -> Tag<'_> { match tag { - Tag::Header(_) | Tag::Table(_) | Tag::TableHead | Tag::TableRow | Tag::TableCell => { + Tag::Heading(_) | Tag::Table(_) | Tag::TableHead | Tag::TableRow | Tag::TableCell => { Tag::Paragraph } - Tag::Image(url, title) => Tag::Link(url, title), + Tag::Image(typ, url, title) => Tag::Link(typ, url, title), t => t, } } @@ -66,21 +65,31 @@ fn highlight_code<'a>( evt: Event<'a>, ) -> Option>> { match evt { - Event::Start(Tag::CodeBlock(lang)) => { - if lang.is_empty() { - Some(vec![Event::Start(Tag::CodeBlock(lang))]) - } else { - *context = Some(HighlighterContext { content: vec![] }); - Some(vec![Event::Start(Tag::CodeBlock(lang))]) + Event::Start(Tag::CodeBlock(kind)) => { + match &kind { + CodeBlockKind::Fenced(lang) if !lang.is_empty() => { + *context = Some(HighlighterContext { content: vec![] }); + }, + _ => {} } + Some(vec![Event::Start(Tag::CodeBlock(kind))]) } - Event::End(Tag::CodeBlock(x)) => { + Event::End(Tag::CodeBlock(kind)) => { let mut result = vec![]; if let Some(ctx) = context.take() { + let lang = if let CodeBlockKind::Fenced(lang) = &kind { + if lang.is_empty() { + unreachable!(); + } else { + lang + } + } else { + unreachable!(); + }; let syntax_set = SyntaxSet::load_defaults_newlines(); - let syntax = syntax_set.find_syntax_by_token(&x).unwrap_or_else(|| { + let syntax = syntax_set.find_syntax_by_token(&lang).unwrap_or_else(|| { syntax_set - .find_syntax_by_name(&x) + .find_syntax_by_name(&lang) .unwrap_or_else(|| syntax_set.find_syntax_plain_text()) }); let mut html = ClassedHTMLGenerator::new(&syntax, &syntax_set); @@ -90,7 +99,7 @@ fn highlight_code<'a>( let q = html.finalize(); result.push(Event::Html(q.into())); } - result.push(Event::End(Tag::CodeBlock(x))); + result.push(Event::End(Tag::CodeBlock(kind))); *context = None; Some(result) } @@ -113,10 +122,10 @@ fn flatten_text<'a>(state: &mut Option, evt: Event<'a>) -> Option (Some(txt.into_owned()), vec![]), + None => (Some(txt.into_string()), vec![]), }, e => match state.take() { - Some(prev) => (None, vec![Event::Text(Cow::Owned(prev)), e]), + Some(prev) => (None, vec![Event::Text(CowStr::Boxed(prev.into())), e]), None => (None, vec![e]), }, }; @@ -156,11 +165,11 @@ fn process_image<'a, 'b>( ) -> Event<'a> { if let Some(ref processor) = *processor { match evt { - Event::Start(Tag::Image(id, title)) => { + Event::Start(Tag::Image(typ, id, title)) => { if let Some((url, cw)) = id.parse::().ok().and_then(processor.as_ref()) { if let (Some(cw), false) = (cw, inline) { // there is a cw, and where are not inline - Event::Html(Cow::Owned(format!( + Event::Html(CowStr::Boxed(format!( r#""#, )) } } else { - Event::End(Tag::Image(id, title)) + Event::End(Tag::Image(typ, id, title)) } } e => e, @@ -231,19 +240,19 @@ pub fn md_to_html<'a>( // Ignore headings, images, and tables if inline = true .scan((vec![], inline), inline_tags) .scan(&mut DocumentContext::default(), |ctx, evt| match evt { - Event::Start(Tag::CodeBlock(_)) | Event::Start(Tag::Code) => { + Event::Start(Tag::CodeBlock(_)) => { ctx.in_code = true; Some((vec![evt], vec![], vec![])) } - Event::End(Tag::CodeBlock(_)) | Event::End(Tag::Code) => { + Event::End(Tag::CodeBlock(_)) => { ctx.in_code = false; Some((vec![evt], vec![], vec![])) } - Event::Start(Tag::Link(_, _)) => { + Event::Start(Tag::Link(_, _, _)) => { ctx.in_link = true; Some((vec![evt], vec![], vec![])) } - Event::End(Tag::Link(_, _)) => { + Event::End(Tag::Link(_, _, _)) => { ctx.in_link = false; Some((vec![evt], vec![], vec![])) } @@ -264,6 +273,7 @@ pub fn md_to_html<'a>( let mention = text_acc; let short_mention = mention.splitn(1, '@').next().unwrap_or(""); let link = Tag::Link( + LinkType::Inline, format!("{}@/{}/", base_url, &mention).into(), short_mention.to_owned().into(), ); @@ -294,6 +304,7 @@ pub fn md_to_html<'a>( } let hashtag = text_acc; let link = Tag::Link( + LinkType::Inline, format!("{}tag/{}", base_url, &hashtag).into(), hashtag.to_owned().into(), );