Skip to content

Commit c15326f

Browse files
committed
remove need to clone attribute values too.
1 parent f65cd37 commit c15326f

File tree

6 files changed

+44
-41
lines changed

6 files changed

+44
-41
lines changed

examples/syntax_highlighter.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use comrak::adapters::SyntaxHighlighterAdapter;
44
use comrak::{markdown_to_html_with_plugins, Options, Plugins};
5+
use std::borrow::Cow;
56
use std::collections::HashMap;
67
use std::fmt::{self, Write};
78

@@ -35,7 +36,7 @@ impl SyntaxHighlighterAdapter for PotatoSyntaxAdapter {
3536
fn write_pre_tag(
3637
&self,
3738
output: &mut dyn Write,
38-
attributes: HashMap<&'static str, String>,
39+
attributes: HashMap<&'static str, Cow<str>>,
3940
) -> fmt::Result {
4041
if attributes.contains_key("lang") {
4142
write!(output, "<pre lang=\"{}\">", attributes["lang"])
@@ -47,7 +48,7 @@ impl SyntaxHighlighterAdapter for PotatoSyntaxAdapter {
4748
fn write_code_tag(
4849
&self,
4950
output: &mut dyn Write,
50-
attributes: HashMap<&'static str, String>,
51+
attributes: HashMap<&'static str, Cow<str>>,
5152
) -> fmt::Result {
5253
if attributes.contains_key("class") {
5354
write!(output, "<code class=\"{}\">", attributes["class"])

src/adapters.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! Each plugin has to implement one of the traits available in this module.
44
5+
use std::borrow::Cow;
56
use std::collections::HashMap;
67
use std::fmt;
78

@@ -24,20 +25,20 @@ pub trait SyntaxHighlighterAdapter: Send + Sync {
2425
/// `<pre>` tag possibly with some HTML attribute pre-filled.
2526
///
2627
/// `attributes`: A map of HTML attributes provided by comrak.
27-
fn write_pre_tag(
28+
fn write_pre_tag<'s>(
2829
&self,
2930
output: &mut dyn fmt::Write,
30-
attributes: HashMap<&'static str, String>,
31+
attributes: HashMap<&'static str, Cow<'s, str>>,
3132
) -> fmt::Result;
3233

3334
/// Generates the opening `<code>` tag. Some syntax highlighter libraries might include their own
3435
/// `<code>` tag possibly with some HTML attribute pre-filled.
3536
///
3637
/// `attributes`: A map of HTML attributes provided by comrak.
37-
fn write_code_tag(
38+
fn write_code_tag<'s>(
3839
&self,
3940
output: &mut dyn fmt::Write,
40-
attributes: HashMap<&'static str, String>,
41+
attributes: HashMap<&'static str, Cow<'s, str>>,
4142
) -> fmt::Result;
4243
}
4344

src/html.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ fn render_code_block<'a, T>(
530530
context.cr()?;
531531

532532
let mut first_tag = 0;
533-
let mut pre_attributes: HashMap<&str, String> = HashMap::new();
534-
let mut code_attributes: HashMap<&str, String> = HashMap::new();
533+
let mut pre_attributes: HashMap<&str, Cow<str>> = HashMap::new();
534+
let mut code_attributes: HashMap<&str, Cow<str>> = HashMap::new();
535535
let code_attr: String;
536536

537537
let literal = &ncb.literal;
@@ -544,27 +544,27 @@ fn render_code_block<'a, T>(
544544
}
545545

546546
let lang_str = &info[..first_tag];
547-
let info_str = &info[first_tag..].trim();
547+
let info_str = info[first_tag..].trim();
548548

549549
if context.options.render.github_pre_lang {
550-
pre_attributes.insert("lang", lang_str.to_string());
550+
pre_attributes.insert("lang", lang_str.into());
551551

552552
if context.options.render.full_info_string && !info_str.is_empty() {
553-
pre_attributes.insert("data-meta", info_str.trim().to_string());
553+
pre_attributes.insert("data-meta", info_str.trim().into());
554554
}
555555
} else {
556556
code_attr = format!("language-{}", lang_str);
557-
code_attributes.insert("class", code_attr);
557+
code_attributes.insert("class", code_attr.into());
558558

559559
if context.options.render.full_info_string && !info_str.is_empty() {
560-
code_attributes.insert("data-meta", info_str.to_string());
560+
code_attributes.insert("data-meta", info_str.into());
561561
}
562562
}
563563
}
564564

565565
if context.options.render.sourcepos {
566566
let ast = node.data.borrow();
567-
pre_attributes.insert("data-sourcepos", ast.sourcepos.to_string());
567+
pre_attributes.insert("data-sourcepos", ast.sourcepos.to_string().into());
568568
}
569569

570570
match context.plugins.render.codefence_syntax_highlighter {
@@ -1314,7 +1314,7 @@ pub fn render_math<'a, T>(
13141314
nm: &NodeMath,
13151315
) -> Result<ChildRendering, fmt::Error> {
13161316
if entering {
1317-
let mut tag_attributes: Vec<(Cow<str>, Cow<str>)> = Vec::new();
1317+
let mut tag_attributes: Vec<(&str, Cow<str>)> = Vec::new();
13181318
let style_attr = if nm.display_math { "display" } else { "inline" };
13191319
let tag: &str = if nm.dollar_math { "span" } else { "code" };
13201320

@@ -1343,8 +1343,8 @@ pub fn render_math_code_block<'a, T>(
13431343

13441344
// use vectors to ensure attributes always written in the same order,
13451345
// for testing stability
1346-
let mut pre_attributes: Vec<(Cow<str>, Cow<str>)> = Vec::new();
1347-
let mut code_attributes: Vec<(Cow<str>, Cow<str>)> = Vec::new();
1346+
let mut pre_attributes: Vec<(&str, Cow<str>)> = Vec::new();
1347+
let mut code_attributes: Vec<(&str, Cow<str>)> = Vec::new();
13481348
let lang_str = "math";
13491349

13501350
if context.options.render.github_pre_lang {

src/parser/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ pub struct RenderPlugins<'p> {
11401140
/// ```
11411141
/// # use comrak::{markdown_to_html, Options, Plugins, markdown_to_html_with_plugins};
11421142
/// # use comrak::adapters::SyntaxHighlighterAdapter;
1143+
/// use std::borrow::Cow;
11431144
/// use std::collections::HashMap;
11441145
/// use std::fmt::{self, Write};
11451146
/// let options = Options::default();
@@ -1155,11 +1156,11 @@ pub struct RenderPlugins<'p> {
11551156
/// write!(output, "<span class=\"lang-{}\">{}</span>", lang.unwrap(), code)
11561157
/// }
11571158
///
1158-
/// fn write_pre_tag(&self, output: &mut dyn fmt::Write, _attributes: HashMap<String, String>) -> fmt::Result {
1159+
/// fn write_pre_tag<'s>(&self, output: &mut dyn fmt::Write, _attributes: HashMap<&'static str, Cow<'s, str>>) -> fmt::Result {
11591160
/// output.write_str("<pre lang=\"rust\">")
11601161
/// }
11611162
///
1162-
/// fn write_code_tag(&self, output: &mut dyn fmt::Write, _attributes: HashMap<String, String>) -> fmt::Result {
1163+
/// fn write_code_tag<'s>(&self, output: &mut dyn fmt::Write, _attributes: HashMap<&'static str, Cow<'s, str>>) -> fmt::Result {
11631164
/// output.write_str("<code class=\"language-rust\">")
11641165
/// }
11651166
/// }

src/plugins/syntect.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Adapter for the Syntect syntax highlighter plugin.
22
3-
use crate::adapters::SyntaxHighlighterAdapter;
4-
use crate::html;
3+
use std::borrow::Cow;
54
use std::collections::{hash_map, HashMap};
65
use std::fmt::{self, Write};
76
use syntect::easy::HighlightLines;
@@ -13,6 +12,9 @@ use syntect::parsing::{SyntaxReference, SyntaxSet};
1312
use syntect::util::LinesWithEndings;
1413
use syntect::Error;
1514

15+
use crate::adapters::SyntaxHighlighterAdapter;
16+
use crate::html;
17+
1618
#[derive(Debug)]
1719
/// Syntect syntax highlighter plugin.
1820
pub struct SyntectAdapter {
@@ -103,10 +105,10 @@ impl SyntaxHighlighterAdapter for SyntectAdapter {
103105
}
104106
}
105107

106-
fn write_pre_tag(
108+
fn write_pre_tag<'s>(
107109
&self,
108110
output: &mut dyn Write,
109-
attributes: HashMap<&'static str, String>,
111+
attributes: HashMap<&'static str, Cow<'s, str>>,
110112
) -> fmt::Result {
111113
match &self.theme {
112114
Some(theme) => {
@@ -121,37 +123,33 @@ impl SyntaxHighlighterAdapter for SyntectAdapter {
121123
let mut pre_attributes = SyntectPreAttributes::new(attributes, &style);
122124
html::write_opening_tag(output, "pre", pre_attributes.iter_mut())
123125
}
124-
None => html::write_opening_tag(
125-
output,
126-
"pre",
127-
vec![("class", "syntax-highlighting")],
128-
),
126+
None => html::write_opening_tag(output, "pre", vec![("class", "syntax-highlighting")]),
129127
}
130128
}
131129

132-
fn write_code_tag(
130+
fn write_code_tag<'s>(
133131
&self,
134132
output: &mut dyn Write,
135-
attributes: HashMap<&'static str, String>,
133+
attributes: HashMap<&'static str, Cow<'s, str>>,
136134
) -> fmt::Result {
137135
html::write_opening_tag(output, "code", attributes)
138136
}
139137
}
140138

141-
struct SyntectPreAttributes {
139+
struct SyntectPreAttributes<'s> {
142140
syntect_style: String,
143-
attributes: HashMap<&'static str, String>,
141+
attributes: HashMap<&'static str, Cow<'s, str>>,
144142
}
145143

146-
impl SyntectPreAttributes {
147-
fn new(attributes: HashMap<&'static str, String>, syntect_style: &str) -> Self {
144+
impl<'s> SyntectPreAttributes<'s> {
145+
fn new(attributes: HashMap<&'static str, Cow<'s, str>>, syntect_style: &str) -> Self {
148146
Self {
149147
syntect_style: syntect_style.into(),
150148
attributes,
151149
}
152150
}
153151

154-
fn iter_mut(&mut self) -> SyntectPreAttributesIter<'_> {
152+
fn iter_mut(&mut self) -> SyntectPreAttributesIter<'_, 's> {
155153
SyntectPreAttributesIter {
156154
iter_mut: self.attributes.iter_mut(),
157155
syntect_style: &self.syntect_style,
@@ -160,20 +158,20 @@ impl SyntectPreAttributes {
160158
}
161159
}
162160

163-
struct SyntectPreAttributesIter<'a> {
164-
iter_mut: hash_map::IterMut<'a, &'static str, String>,
161+
struct SyntectPreAttributesIter<'a, 's> {
162+
iter_mut: hash_map::IterMut<'a, &'static str, Cow<'s, str>>,
165163
syntect_style: &'a str,
166164
style_written: bool,
167165
}
168166

169-
impl<'a> Iterator for SyntectPreAttributesIter<'a> {
167+
impl<'a, 's> Iterator for SyntectPreAttributesIter<'a, 's> {
170168
type Item = (&'a str, &'a str);
171169

172170
fn next(&mut self) -> Option<Self::Item> {
173171
match self.iter_mut.next() {
174172
Some((k, v)) if *k == "style" && !self.style_written => {
175173
self.style_written = true;
176-
v.insert_str(0, self.syntect_style);
174+
v.to_mut().insert_str(0, self.syntect_style);
177175
Some((k, v))
178176
}
179177
Some((k, v)) => Some((k, v)),

src/tests/plugins.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use crate::{
24
adapters::{HeadingAdapter, HeadingMeta, SyntaxHighlighterAdapter},
35
nodes::Sourcepos,
@@ -22,15 +24,15 @@ fn syntax_highlighter_plugin() {
2224
fn write_pre_tag(
2325
&self,
2426
output: &mut dyn std::fmt::Write,
25-
attributes: HashMap<&'static str, String>,
27+
attributes: HashMap<&'static str, Cow<str>>,
2628
) -> std::fmt::Result {
2729
html::write_opening_tag(output, "pre", attributes)
2830
}
2931

3032
fn write_code_tag(
3133
&self,
3234
output: &mut dyn std::fmt::Write,
33-
attributes: HashMap<&'static str, String>,
35+
attributes: HashMap<&'static str, Cow<str>>,
3436
) -> std::fmt::Result {
3537
html::write_opening_tag(output, "code", attributes)
3638
}

0 commit comments

Comments
 (0)