From 1c5ae132e1aea028529a3ad2e27ff04a9de602ea Mon Sep 17 00:00:00 2001 From: JJ Date: Sat, 7 Jan 2023 12:06:35 -0800 Subject: parse_wiki_text: impl Display for Node, except for template nodes templates are going to be like several dozen specific cases --- parse_wiki_text/src/display.rs | 102 +++++++++++++++++++++++++++++++++++++++++ parse_wiki_text/src/lib.rs | 1 + 2 files changed, 103 insertions(+) create mode 100644 parse_wiki_text/src/display.rs diff --git a/parse_wiki_text/src/display.rs b/parse_wiki_text/src/display.rs new file mode 100644 index 0000000..fa73e5e --- /dev/null +++ b/parse_wiki_text/src/display.rs @@ -0,0 +1,102 @@ +// Copyright 2022 JJ +// This is free software distributed under the terms specified in +// the file LICENSE at the top-level directory of this distribution. + +#![allow(unused_variables)] + +use crate::{Node, Parameter}; +use std::fmt::Error; + +impl std::fmt::Display for Node<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + return match self { + Node::CharacterEntity { character, .. } => write!(f, "{}", character), + Node::Text { value, .. } => write!(f, "{}", value), + Node::ParagraphBreak { .. } => write!(f, " "), + Node::HorizontalDivider { .. } => write!(f, "\n------\n"), + + Node::Heading { nodes, level, .. } => { + match nodes.get(0) { + Some(node) => write!(f, "\x1b[1m{}\x1b[0m", node), + None => Err(Error), + } + }, + Node::Link { text, target, .. } => { + match text.get(0) { + Some(node) => write!(f, "{}", node), + None => Err(Error), + } + }, + Node::Template { name, parameters, .. } => { + match name.get(0) { + Some(name) => + if let Node::Text { value, .. } = name { + write!(f, "") + } else { + Err(Error) + }, + None => Err(Error), + } + }, + + Node::OrderedList { items, .. } => { + for (i, item) in items.iter().enumerate() { + write!(f, "\n{}. ", i+1)?; + for node in &item.nodes { + write!(f, "{}", node)? + } + } + return Ok(()); + }, + Node::UnorderedList { items, .. } => { + for item in items { + write!(f, "\n• ")?; + for node in &item.nodes { + write!(f, "{}", node)? + } + } + return Ok(()); + }, + Node::DefinitionList { items, .. } => { + for item in items { + write!(f, "\n")?; + for node in &item.nodes { + write!(f, "{}", node)? + } + write!(f, " ")?; + } + return Ok(()); + }, + + Node::ExternalLink { nodes, .. } => { + for node in nodes { + write!(f, "{}", node)?; + } + return Ok(()); + } , + Node::Image { target, text, .. } => { + write!(f, "{}", target)?; + for node in text { + write!(f, "{}", node)?; + } + return Ok(()); + }, + + Node::Table { .. } => todo!(), + Node::Parameter { default, name, .. } => todo!(), + Node::Redirect { target, .. } => Ok(()), + Node::Preformatted { nodes, .. } => Ok(()), + Node::Category { target, ordinal, .. } => Ok(()), + Node::Comment { .. } | Node::MagicWord { .. } => Ok(()), + Node::StartTag { .. } | Node::Tag { .. } | Node::EndTag { .. } => Ok(()), + Node::Bold { .. } | Node::Italic { .. } | Node::BoldItalic { .. } => Ok(()), + } + } +} + +// https://en.wiktionary.org/wiki/Wiktionary:Templates +fn handle_template(name: &str, parameters: &Vec) -> String { + match name { + _ => "--template--".to_string(), + } +} diff --git a/parse_wiki_text/src/lib.rs b/parse_wiki_text/src/lib.rs index 0eac2ea..9763954 100644 --- a/parse_wiki_text/src/lib.rs +++ b/parse_wiki_text/src/lib.rs @@ -112,6 +112,7 @@ mod character_entity; mod comment; mod configuration; mod default; +mod display; mod external_link; mod heading; mod html_entities; -- cgit v1.2.3-70-g09d2