Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change attribute key type from &str to String #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/ferris/block_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ impl NodeValue for BlockFerris {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
// build attributes for `div`
let mut attrs_div = node.attrs.clone();
attrs_div.push(("class", "ferris-block".into()));
attrs_div.push(("class".into(), "ferris-block".into()));

// build attributes for `img`
let attrs_img = vec![("src", CRAB_URL.into())];
let attrs_img = vec![("src".into(), CRAB_URL.into())];

fmt.cr(); // linebreak, multiples get merged
fmt.open("div", &attrs_div); // opening tag, `<div>`
Expand Down
2 changes: 1 addition & 1 deletion examples/ferris/core_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl NodeValue for FerrisCounter {
let mut attrs = node.attrs.clone();

// add a custom class attribute
attrs.push(("class", "ferris-counter".into()));
attrs.push(("class".into(), "ferris-counter".into()));

fmt.cr(); // linebreak, multiples get merged
fmt.open("footer", &attrs);
Expand Down
2 changes: 1 addition & 1 deletion examples/ferris/inline_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl NodeValue for InlineFerris {
let mut attrs = node.attrs.clone();

// add a custom class attribute
attrs.push(("class", "ferris-inline".into()));
attrs.push(("class".into(), "ferris-inline".into()));

fmt.open("span", &attrs);
fmt.text("🦀");
Expand Down
2 changes: 1 addition & 1 deletion src/parser/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Node {
pub ext: NodeExtSet,

/// Additional attributes to be added to resulting html.
pub attrs: Vec<(&'static str, String)>,
pub attrs: Vec<(String, String)>,

/// Type name, used for debugging.
#[readonly]
Expand Down
14 changes: 7 additions & 7 deletions src/parser/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::Node;
/// into internal buffer.
pub trait Renderer {
/// Write opening html tag with attributes, e.g. `<a href="url">`.
fn open(&mut self, tag: &str, attrs: &[(&str, String)]);
fn open(&mut self, tag: &str, attrs: &[(String, String)]);
/// Write closing html tag, e.g. `</a>`.
fn close(&mut self, tag: &str);
/// Write self-closing html tag with attributes, e.g. `<img src="url"/>`.
fn self_close(&mut self, tag: &str, attrs: &[(&str, String)]);
fn self_close(&mut self, tag: &str, attrs: &[(String, String)]);
/// Loop through child nodes and render each one.
fn contents(&mut self, nodes: &[Node]);
/// Write line break (`\n`). Default renderer ignores it if last char in the buffer is `\n` already.
Expand Down Expand Up @@ -56,14 +56,14 @@ impl<const XHTML: bool> HTMLRenderer<XHTML> {
self.result.push('"');
}

fn make_attrs(&mut self, attrs: &[(&str, String)]) {
fn make_attrs(&mut self, attrs: &[(String, String)]) {
let mut attr_hash = HashMap::new();
let mut attr_order = Vec::with_capacity(attrs.len());

for (name, value) in attrs {
let entry = attr_hash.entry(*name).or_insert(Vec::new());
let entry = attr_hash.entry(name).or_insert(Vec::new());
entry.push(value.as_str());
attr_order.push(*name);
attr_order.push(name);
}

for name in attr_order {
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<const XHTML: bool> From<HTMLRenderer<XHTML>> for String {
}

impl<const XHTML: bool> Renderer for HTMLRenderer<XHTML> {
fn open(&mut self, tag: &str, attrs: &[(&str, String)]) {
fn open(&mut self, tag: &str, attrs: &[(String, String)]) {
self.result.push('<');
self.result.push_str(tag);
self.make_attrs(attrs);
Expand All @@ -115,7 +115,7 @@ impl<const XHTML: bool> Renderer for HTMLRenderer<XHTML> {
self.result.push('>');
}

fn self_close(&mut self, tag: &str, attrs: &[(&str, String)]) {
fn self_close(&mut self, tag: &str, attrs: &[(String, String)]) {
self.result.push('<');
self.result.push_str(tag);
self.make_attrs(attrs);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/cmark/block/fence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl NodeValue for CodeFence {

if !lang_name.is_empty() {
class = format!("{}{}", self.lang_prefix, lang_name);
attrs.push(("class", class));
attrs.push(("class".into(), class));
}

fmt.cr();
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/cmark/block/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl NodeValue for OrderedList {
let start;
if self.start != 1 {
start = self.start.to_string();
attrs.push(("start", start));
attrs.push(("start".into(), start));
}
fmt.cr();
fmt.open("ol", &attrs);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/cmark/inline/autolink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Autolink {
impl NodeValue for Autolink {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
let mut attrs = node.attrs.clone();
attrs.push(("href", self.url.clone()));
attrs.push(("href".into(), self.url.clone()));

fmt.open("a", &attrs);
fmt.contents(&node.children);
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/cmark/inline/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub struct Image {
impl NodeValue for Image {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
let mut attrs = node.attrs.clone();
attrs.push(("src", self.url.clone()));
attrs.push(("alt", node.collect_text()));
attrs.push(("src".into(), self.url.clone()));
attrs.push(("alt".into(), node.collect_text()));

if let Some(title) = &self.title {
attrs.push(("title", title.clone()));
attrs.push(("title".into(), title.clone()));
}

fmt.self_close("img", &attrs);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/cmark/inline/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub struct Link {
impl NodeValue for Link {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
let mut attrs = node.attrs.clone();
attrs.push(("href", self.url.clone()));
attrs.push(("href".into(), self.url.clone()));

if let Some(title) = &self.title {
attrs.push(("title", title.clone()));
attrs.push(("title".into(), title.clone()));
}

fmt.open("a", &attrs);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/extra/heading_anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl CoreRule for AddHeadingAnchors {

root.walk_mut(|node, _| {
if node.is::<ATXHeading>() || node.is::<SetextHeader>() {
node.attrs.push(("id", slugify(&node.collect_text())));
node.attrs.push(("id".into(), slugify(&node.collect_text())));
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/extra/linkify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Linkified {
impl NodeValue for Linkified {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
let mut attrs = node.attrs.clone();
attrs.push(("href", self.url.clone()));
attrs.push(("href".into(), self.url.clone()));

fmt.open("a", &attrs);
fmt.contents(&node.children);
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/extra/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ impl NodeValue for TableCell {

match ctx.alignments.get(ctx.index).copied().unwrap_or_default() {
ColumnAlignment::None => (),
ColumnAlignment::Left => attrs.push(("style", "text-align:left".to_owned())),
ColumnAlignment::Right => attrs.push(("style", "text-align:right".to_owned())),
ColumnAlignment::Center => attrs.push(("style", "text-align:center".to_owned())),
ColumnAlignment::Left => attrs.push(("style".into(), "text-align:left".to_owned())),
ColumnAlignment::Right => attrs.push(("style".into(), "text-align:right".to_owned())),
ColumnAlignment::Center => attrs.push(("style".into(), "text-align:center".to_owned())),
}

ctx.index += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/sourcepos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl CoreRule for SyntaxPosRule {
root.walk_mut(|node, _| {
if let Some(map) = node.srcmap {
let ((startline, startcol), (endline, endcol)) = map.get_positions(&mapping);
node.attrs.push(("data-sourcepos", format!("{}:{}-{}:{}", startline, startcol, endline, endcol)));
node.attrs.push(("data-sourcepos".into(), format!("{}:{}-{}:{}", startline, startcol, endline, endcol)));
}
});
}
Expand Down
Loading