aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJJ2023-01-07 20:06:35 +0000
committerJJ2023-01-08 19:31:55 +0000
commit1c5ae132e1aea028529a3ad2e27ff04a9de602ea (patch)
tree8ea7cc9390db691a0c83bfe0dac0f93697493d7f
parent14a24ee8b22633d8e9e3e205f7f318127b64e539 (diff)
parse_wiki_text: impl Display for Node, except for template nodes
templates are going to be like several dozen specific cases
-rw-r--r--parse_wiki_text/src/display.rs102
-rw-r--r--parse_wiki_text/src/lib.rs1
2 files changed, 103 insertions, 0 deletions
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 <https://j-james.me>
+// 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<Parameter>) -> 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;