diff options
-rw-r--r-- | parse_wiki_text/src/display.rs | 4 | ||||
-rw-r--r-- | src/display.rs | 30 |
2 files changed, 17 insertions, 17 deletions
diff --git a/parse_wiki_text/src/display.rs b/parse_wiki_text/src/display.rs index 4c1f03a..011dd40 100644 --- a/parse_wiki_text/src/display.rs +++ b/parse_wiki_text/src/display.rs @@ -32,7 +32,7 @@ impl std::fmt::Display for Node<'_> { match name.get(0) { Some(name) => if let Node::Text { value, .. } = name { - write!(f, "{}", handle_template(*value, parameters)?) + write!(f, "{}", expand_template(*value, parameters)?) } else { Err(Error) }, @@ -91,7 +91,7 @@ impl std::fmt::Display for Node<'_> { // https://en.wiktionary.org/wiki/Wiktionary:Templates // wow, a function entirely composed of edge cases -fn handle_template(name: &str, parameters: &Vec<Parameter>) -> Result<String, Error> { +fn expand_template(name: &str, parameters: &Vec<Parameter>) -> Result<String, Error> { let mut buffer = String::from(""); match name { "m" => { diff --git a/src/display.rs b/src/display.rs index 3c372d2..45bbca9 100644 --- a/src/display.rs +++ b/src/display.rs @@ -15,21 +15,25 @@ const skippable_headers: &[&str; 15] = pub fn display(definition: String, state: &State) { let definition = Configuration::default().parse(&definition); - // impl display on that shit then - // definition.display(&state.lang); - - // this is really quite terrible code - if !display_language(&definition, &state.lang) { - display_language(&definition, ""); - } + display_language(&definition, &state.lang); } // no overloading?? O_O // matching on an enum of structs SUCKS // functions as parameters is too hard -pub fn display_language(definition: &Output, lang: &str) -> bool { +fn display_language(definition: &Output, lang: &str) { + let mut has_lang = false; + for (i, node) in definition.nodes.iter().enumerate() { + if let Node::Heading { nodes, level, .. } = node + && let Some(Node::Text { value, .. }) = nodes.get(0) { + if *level == 2 && *value == lang { + has_lang = true; + break; + } + } + } + let mut inside_heading = false; - let mut correct_language = false; let mut skipping_heading = false; for (i, node) in definition.nodes.iter().enumerate() { @@ -46,9 +50,8 @@ pub fn display_language(definition: &Output, lang: &str) -> bool { } print!("\n{}\n", node); } - } else if *level == 2 && *value == lang { + } else if *level == 2 && (*value == lang || has_lang == false) { inside_heading = true; - correct_language = true; print!("{}", node); } } else if inside_heading && !skipping_heading { @@ -59,9 +62,6 @@ pub fn display_language(definition: &Output, lang: &str) -> bool { } } } - if correct_language { - println!(); - } - return correct_language; + println!(); } |