chore: Fix several style lints (#17488)

It's not comprehensive enough to start linting on `style` group, but
hey, it's a start.

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-09-06 11:58:39 +02:00 committed by GitHub
parent 93249fc82b
commit e6c1c51b37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
361 changed files with 3530 additions and 3587 deletions

View file

@ -9,7 +9,7 @@ use html5ever::Attribute;
/// [MDN: List of "inline" elements](https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Web/HTML/Inline_elements)
fn inline_elements() -> &'static HashSet<&'static str> {
static INLINE_ELEMENTS: OnceLock<HashSet<&str>> = OnceLock::new();
&INLINE_ELEMENTS.get_or_init(|| {
INLINE_ELEMENTS.get_or_init(|| {
HashSet::from_iter([
"a", "abbr", "acronym", "audio", "b", "bdi", "bdo", "big", "br", "button", "canvas",
"cite", "code", "data", "datalist", "del", "dfn", "em", "embed", "i", "iframe", "img",

View file

@ -18,7 +18,7 @@ pub use crate::html_element::*;
pub use crate::markdown_writer::*;
/// Converts the provided HTML to Markdown.
pub fn convert_html_to_markdown(html: impl Read, handlers: &mut Vec<TagHandler>) -> Result<String> {
pub fn convert_html_to_markdown(html: impl Read, handlers: &mut [TagHandler]) -> Result<String> {
let dom = parse_html(html).context("failed to parse HTML")?;
let markdown_writer = MarkdownWriter::new();

View file

@ -5,10 +5,7 @@ pub struct WebpageChromeRemover;
impl HandleTag for WebpageChromeRemover {
fn should_handle(&self, tag: &str) -> bool {
match tag {
"head" | "script" | "style" | "nav" => true,
_ => false,
}
matches!(tag, "head" | "script" | "style" | "nav")
}
fn handle_tag_start(
@ -39,19 +36,18 @@ impl HandleTag for ParagraphHandler {
) -> StartTagOutcome {
if tag.is_inline() && writer.is_inside("p") {
if let Some(parent) = writer.current_element_stack().iter().last() {
if !parent.is_inline() {
if !(writer.markdown.ends_with(' ') || writer.markdown.ends_with('\n')) {
writer.push_str(" ");
}
if !(parent.is_inline()
|| writer.markdown.ends_with(' ')
|| writer.markdown.ends_with('\n'))
{
writer.push_str(" ");
}
}
}
match tag.tag() {
"p" => writer.push_blank_line(),
_ => {}
if tag.tag() == "p" {
writer.push_blank_line()
}
StartTagOutcome::Continue
}
}
@ -60,10 +56,7 @@ pub struct HeadingHandler;
impl HandleTag for HeadingHandler {
fn should_handle(&self, tag: &str) -> bool {
match tag {
"h1" | "h2" | "h3" | "h4" | "h5" | "h6" => true,
_ => false,
}
matches!(tag, "h1" | "h2" | "h3" | "h4" | "h5" | "h6")
}
fn handle_tag_start(
@ -96,10 +89,7 @@ pub struct ListHandler;
impl HandleTag for ListHandler {
fn should_handle(&self, tag: &str) -> bool {
match tag {
"ul" | "ol" | "li" => true,
_ => false,
}
matches!(tag, "ul" | "ol" | "li")
}
fn handle_tag_start(
@ -142,12 +132,15 @@ impl TableHandler {
}
}
impl Default for TableHandler {
fn default() -> Self {
Self::new()
}
}
impl HandleTag for TableHandler {
fn should_handle(&self, tag: &str) -> bool {
match tag {
"table" | "thead" | "tbody" | "tr" | "th" | "td" => true,
_ => false,
}
matches!(tag, "table" | "thead" | "tbody" | "tr" | "th" | "td")
}
fn handle_tag_start(
@ -210,10 +203,7 @@ pub struct StyledTextHandler;
impl HandleTag for StyledTextHandler {
fn should_handle(&self, tag: &str) -> bool {
match tag {
"strong" | "em" => true,
_ => false,
}
matches!(tag, "strong" | "em")
}
fn handle_tag_start(
@ -243,10 +233,7 @@ pub struct CodeHandler;
impl HandleTag for CodeHandler {
fn should_handle(&self, tag: &str) -> bool {
match tag {
"pre" | "code" => true,
_ => false,
}
matches!(tag, "pre" | "code")
}
fn handle_tag_start(
@ -281,7 +268,7 @@ impl HandleTag for CodeHandler {
fn handle_text(&mut self, text: &str, writer: &mut MarkdownWriter) -> HandlerOutcome {
if writer.is_inside("pre") {
writer.push_str(&text);
writer.push_str(text);
return HandlerOutcome::Handled;
}

View file

@ -31,6 +31,12 @@ pub struct MarkdownWriter {
pub(crate) markdown: String,
}
impl Default for MarkdownWriter {
fn default() -> Self {
Self::new()
}
}
impl MarkdownWriter {
pub fn new() -> Self {
Self {
@ -64,8 +70,8 @@ impl MarkdownWriter {
self.push_str("\n\n");
}
pub fn run(mut self, root_node: &Handle, handlers: &mut Vec<TagHandler>) -> Result<String> {
self.visit_node(&root_node, handlers)?;
pub fn run(mut self, root_node: &Handle, handlers: &mut [TagHandler]) -> Result<String> {
self.visit_node(root_node, handlers)?;
Ok(Self::prettify_markdown(self.markdown))
}
@ -104,7 +110,7 @@ impl MarkdownWriter {
}
if let Some(current_element) = current_element.as_ref() {
match self.start_tag(&current_element, handlers) {
match self.start_tag(current_element, handlers) {
StartTagOutcome::Continue => {}
StartTagOutcome::Skip => return Ok(()),
}

View file

@ -54,13 +54,8 @@ impl HandleTag for WikipediaInfoboxHandler {
tag: &HtmlElement,
_writer: &mut MarkdownWriter,
) -> StartTagOutcome {
match tag.tag() {
"table" => {
if tag.has_class("infobox") {
return StartTagOutcome::Skip;
}
}
_ => {}
if tag.tag() == "table" && tag.has_class("infobox") {
return StartTagOutcome::Skip;
}
StartTagOutcome::Continue
@ -77,12 +72,15 @@ impl WikipediaCodeHandler {
}
}
impl Default for WikipediaCodeHandler {
fn default() -> Self {
Self::new()
}
}
impl HandleTag for WikipediaCodeHandler {
fn should_handle(&self, tag: &str) -> bool {
match tag {
"div" | "pre" | "code" => true,
_ => false,
}
matches!(tag, "div" | "pre" | "code")
}
fn handle_tag_start(
@ -134,7 +132,7 @@ impl HandleTag for WikipediaCodeHandler {
fn handle_text(&mut self, text: &str, writer: &mut MarkdownWriter) -> HandlerOutcome {
if writer.is_inside("pre") {
writer.push_str(&text);
writer.push_str(text);
return HandlerOutcome::Handled;
}