Skip to content

Commit

Permalink
fix(HTML Encoding): Use attributes that are consistent with themes
Browse files Browse the repository at this point in the history
This adjusts both the HTML encoding and the address resolution to use `data-itemprop` and `data-itemscope=root` for consistency with themes
  • Loading branch information
nokome committed Oct 9, 2021
1 parent fd2ea89 commit a0cbc9f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 60 deletions.
46 changes: 23 additions & 23 deletions rust/src/methods/encode/html/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
attr, attr_id, attr_itemprop, attr_itemtype, attr_slot, concat, elem, elem_empty, json,
attr, attr_id, attr_itemprop, attr_itemtype, attr_prop, concat, elem, elem_empty, json,
Context, ToHtml,
};
use html_escape::encode_safe;
Expand All @@ -20,7 +20,7 @@ impl ToHtml for Vec<BlockContent> {
if slot.is_empty() {
blocks
} else {
elem("div", &[attr_slot(slot)], &blocks)
elem("div", &[attr_prop(slot)], &blocks)
}
}
}
Expand Down Expand Up @@ -50,7 +50,7 @@ impl ToHtml for ClaimSimple {
elem(
"pre",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
attr("class", "todo"),
Expand Down Expand Up @@ -90,7 +90,7 @@ impl ToHtml for CodeBlock {

elem(
"pre",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[meta, text].concat(),
)
}
Expand All @@ -100,7 +100,7 @@ impl ToHtml for CodeChunk {
fn to_html(&self, slot: &str, context: &Context) -> String {
let label = match &self.label {
None => String::new(),
Some(label) => elem("label", &[attr_slot("label")], label),
Some(label) => elem("label", &[attr_prop("label")], label),
};

let caption = match &self.caption {
Expand Down Expand Up @@ -130,7 +130,7 @@ impl ToHtml for CodeChunk {
elem(
"stencila-code-chunk",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
attr("programming-language", &self.programming_language),
Expand All @@ -148,7 +148,7 @@ impl ToHtml for CollectionSimple {
fn to_html(&self, slot: &str, context: &Context) -> String {
elem(
"ol",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&concat(&self.parts, |part| {
elem("li", &[], &part.to_html("", context))
}),
Expand All @@ -160,7 +160,7 @@ impl ToHtml for FigureSimple {
fn to_html(&self, slot: &str, context: &Context) -> String {
let label = match &self.label {
None => String::new(),
Some(label) => elem("label", &[attr_slot("label")], label),
Some(label) => elem("label", &[attr_prop("label")], label),
};

let content = match &self.content {
Expand All @@ -172,7 +172,7 @@ impl ToHtml for FigureSimple {
None => String::new(),
Some(caption) => elem(
"figcaption",
&[attr_slot("caption")],
&[attr_prop("caption")],
&match caption {
FigureCaption::String(string) => encode_safe(&string.clone()).to_string(),
FigureCaption::VecBlockContent(content) => content.to_html("", context),
Expand All @@ -182,7 +182,7 @@ impl ToHtml for FigureSimple {

elem(
"figure",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[label, content, caption].concat(),
)
}
Expand All @@ -209,7 +209,7 @@ impl ToHtml for Heading {

elem(
&["h", &depth.to_string()].concat(),
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&self.content.to_html("", context),
)
}
Expand All @@ -224,7 +224,7 @@ impl ToHtml for Include {

elem(
"div",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&content,
)
}
Expand All @@ -239,7 +239,7 @@ impl ToHtml for List {

let items = elem(
tag,
&[attr("slot", "items")],
&[attr_prop("items")],
&self
.items
.iter()
Expand All @@ -251,7 +251,7 @@ impl ToHtml for List {

elem(
"div",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&items,
)
}
Expand Down Expand Up @@ -295,7 +295,7 @@ impl ToHtml for ListItem {

elem(
"li",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&content,
)
}
Expand All @@ -306,7 +306,7 @@ impl ToHtml for MathBlock {
elem(
"pre",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
attr("class", "todo"),
Expand All @@ -320,7 +320,7 @@ impl ToHtml for Paragraph {
fn to_html(&self, slot: &str, context: &Context) -> String {
elem(
"p",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&self.content.to_html("", context),
)
}
Expand All @@ -330,7 +330,7 @@ impl ToHtml for QuoteBlock {
fn to_html(&self, slot: &str, context: &Context) -> String {
elem(
"blockquote",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&self.content.to_html("", context),
)
}
Expand All @@ -340,14 +340,14 @@ impl ToHtml for TableSimple {
fn to_html(&self, slot: &str, context: &Context) -> String {
let label = match &self.label {
None => String::new(),
Some(label) => elem("label", &[attr_slot("label")], label),
Some(label) => elem("label", &[attr_prop("label")], label),
};

let caption = match self.caption.as_deref() {
None => String::new(),
Some(caption) => elem(
"div",
&[attr_slot("caption")],
&[attr_prop("caption")],
&match caption {
TableCaption::String(string) => encode_safe(&string.clone()).to_string(),
TableCaption::VecBlockContent(content) => content.to_html("", context),
Expand Down Expand Up @@ -396,7 +396,7 @@ impl ToHtml for TableSimple {

elem(
"table",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[caption, head, body, foot].concat(),
)
}
Expand Down Expand Up @@ -430,7 +430,7 @@ fn table_row_to_html(
elem(
"tr",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<TableRow>(),
attr_id(&row.id),
],
Expand All @@ -442,7 +442,7 @@ impl ToHtml for ThematicBreak {
fn to_html(&self, slot: &str, _context: &Context) -> String {
elem_empty(
"hr",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
)
}
}
30 changes: 15 additions & 15 deletions rust/src/methods/encode/html/inlines.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
attr, attr_id, attr_itemprop, attr_itemtype, attr_itemtype_str, attr_slot, concat, elem,
attr, attr_id, attr_itemprop, attr_itemtype, attr_itemtype_str, attr_prop, concat, elem,
elem_empty, json, Context, ToHtml,
};
use crate::methods::encode::txt::ToTxt;
Expand All @@ -21,7 +21,7 @@ impl ToHtml for Vec<InlineContent> {
if slot.is_empty() {
inlines
} else {
elem("span", &[attr_slot(slot)], &inlines)
elem("span", &[attr_prop(slot)], &inlines)
}
}
}
Expand Down Expand Up @@ -62,7 +62,7 @@ macro_rules! mark_to_html {
fn to_html(&self, slot: &str, context: &Context) -> String {
elem(
$tag,
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&self.content.to_html("", context),
)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ impl ToHtml for AudioObjectSimple {
elem(
"audio",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype_str("AudioObject"),
attr_id(&self.id),
"controls".to_string(),
Expand All @@ -135,7 +135,7 @@ impl ToHtml for ImageObjectSimple {
elem_empty(
"img",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype_str("ImageObject"),
attr_id(&self.id),
content_url_to_src_attr(&self.content_url, context),
Expand All @@ -154,7 +154,7 @@ impl ToHtml for VideoObjectSimple {
elem(
"video",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype_str("VideoObject"),
attr_id(&self.id),
"controls".to_string(),
Expand Down Expand Up @@ -278,7 +278,7 @@ impl ToHtml for Cite {
};
elem(
"cite",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&elem("a", &[attr("href", &self.target)], &content),
)
}
Expand All @@ -288,7 +288,7 @@ impl ToHtml for CiteGroup {
fn to_html(&self, slot: &str, context: &Context) -> String {
elem(
"span",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&concat(&self.items, |cite| cite.to_html("", context)),
)
}
Expand All @@ -303,7 +303,7 @@ impl ToHtml for CodeExpression {
elem(
"stencila-code-expression",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
attr("programming-language", &self.programming_language),
Expand Down Expand Up @@ -342,7 +342,7 @@ impl ToHtml for CodeFragment {
elem(
"code",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
class,
Expand All @@ -357,7 +357,7 @@ impl ToHtml for Link {
elem(
"a",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
attr("href", &self.target),
Expand All @@ -371,7 +371,7 @@ impl ToHtml for MathFragment {
fn to_html(&self, slot: &str, _context: &Context) -> String {
elem(
"code",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&encode_safe(&self.text),
)
}
Expand All @@ -382,7 +382,7 @@ impl ToHtml for Note {
elem(
"code",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
attr("class", "todo"),
Expand All @@ -405,7 +405,7 @@ impl ToHtml for Parameter {
elem_empty(
"input",
&[
attr_slot(slot),
attr_prop(slot),
attr_itemtype::<Self>(),
attr_id(&self.id),
attr("type", input_type),
Expand All @@ -420,7 +420,7 @@ impl ToHtml for Quote {
fn to_html(&self, slot: &str, context: &Context) -> String {
elem(
"q",
&[attr_slot(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&[attr_prop(slot), attr_itemtype::<Self>(), attr_id(&self.id)],
&self.content.to_html("", context),
)
}
Expand Down
12 changes: 7 additions & 5 deletions rust/src/methods/encode/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,16 @@ fn attr(name: &str, value: &str) -> String {
.concat()
}

/// Encode a "slot" attribute of an HTML element
/// Encode one of the attributes used to identify a property of a Stencila node
///
/// Will ensure that the name is camelCased.
fn attr_slot(name: &str) -> String {
fn attr_prop(name: &str) -> String {
if name.is_empty() {
"".to_string()
} else if name == "root" {
attr("data-itemscope", "root")
} else {
attr("slot", &to_camel_case(name))
attr("data-itemprop", &to_camel_case(name))
}
}

Expand All @@ -218,9 +220,9 @@ fn attr_itemtype_str(name: &str) -> String {
let itemtype = match name {
// TODO: complete list of schema.org types
"Article" | "AudioObject" | "ImageObject" | "VideoObject" => {
["https://schema.org/", name].concat()
["http://schema.org/", name].concat()
}
_ => ["https://stenci.la/", name].concat(),
_ => ["http://schema.stenci.la/", name].concat(),
};
[&attr("itemtype", &itemtype), " itemscope"].concat()
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/methods/encode/html/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::{attr, attr_slot, elem, json, Context, ToHtml};
use super::{attr, attr_prop, elem, json, Context, ToHtml};
use stencila_schema::Node;

impl ToHtml for Vec<Node> {
fn to_html(&self, slot: &str, context: &Context) -> String {
elem(
"div",
&[attr_slot(slot)],
&[attr_prop(slot)],
&self
.iter()
.map(|item| item.to_html("", context))
Expand Down

0 comments on commit a0cbc9f

Please sign in to comment.