fix: fix insert_appendix function when inserting HTML nodes

refactor: remove check for `<head>` in inline_css

The `<head>` element is automatically added when parsing an HTML document,
therefore, the program should panic if it still does not find the `<head>`
element
This commit is contained in:
Kenneth Gitere 2021-07-27 18:39:00 +03:00
parent 9c2232e37f
commit 0357eaebb6

View file

@ -344,10 +344,15 @@ fn insert_appendix(root_node: &NodeRef, article_links: Vec<(&MetaData, &str)>) {
format!("<a href=\"{}\">{}</a><br></br>", url, article_name) format!("<a href=\"{}\">{}</a><br></br>", url, article_name)
}) })
.collect(); .collect();
let footer_inner_html = format!("<h2>Appendix</h2><h2>Article sources</h3>{}", link_tags); let footer_inner_html = format!(
let footer_elem = "<footer><h2>Appendix</h2><h3>Article sources</h3>{}</footer>",
kuchiki::parse_fragment(create_qualname("footer"), Vec::new()).one(footer_inner_html); link_tags
root_node.append(footer_elem); );
let footer_container =
kuchiki::parse_fragment(create_qualname("div"), Vec::new()).one(footer_inner_html);
let footer_elem = footer_container.select_first("footer").unwrap();
root_node.append(footer_elem.as_node().clone());
} }
/// Inlines the CSS stylesheets into the HTML article node /// Inlines the CSS stylesheets into the HTML article node
@ -371,18 +376,9 @@ fn inline_css(root_node: &NodeRef, app_config: &AppConfig) {
let style_container = let style_container =
kuchiki::parse_fragment(create_qualname("div"), Vec::new()).one(css_html_str); kuchiki::parse_fragment(create_qualname("div"), Vec::new()).one(css_html_str);
let style_elem = style_container.select_first("style").unwrap(); let style_elem = style_container.select_first("style").unwrap();
match root_node.select_first("head") { let head_elem = root_node.select_first("head").expect(HEAD_ELEM_NOT_FOUND);
Ok(head_elem) => { head_elem.as_node().prepend(style_elem.as_node().to_owned());
head_elem.as_node().prepend(style_elem.as_node().to_owned()); }
}
Err(_) => {
debug!("{}", HEAD_ELEM_NOT_FOUND);
let html_elem = root_node.select_first("html").unwrap();
let head_elem = NodeRef::new_element(create_qualname("head"), BTreeMap::new());
head_elem.prepend(style_elem.as_node().to_owned());
html_elem.as_node().prepend(head_elem);
}
}
// Remove the <link> of the stylesheet since styles are now inlined // Remove the <link> of the stylesheet since styles are now inlined
if let Ok(style_link_elem) = root_node.select_first("link[href=\"stylesheet.css\"]") { if let Ok(style_link_elem) = root_node.select_first("link[href=\"stylesheet.css\"]") {