From 25906df96724513d7c135ea0f5a5540a3e89fba8 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Fri, 2 Jun 2023 11:32:46 -0700 Subject: [PATCH] Working dictionary links --- src/challenge.rs | 64 ++++++++++++++++++++++----------------- templates/index.html.tera | 6 ++++ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/challenge.rs b/src/challenge.rs index b98c865..7577658 100644 --- a/src/challenge.rs +++ b/src/challenge.rs @@ -1,3 +1,4 @@ +use core::panic; use std::str::FromStr; use serde::{Deserialize, Deserializer, Serialize}; @@ -42,40 +43,49 @@ impl<'de> Deserialize<'de> for ChallengeWord { &"a string or map", )); }; - let dictionary = map.get("dictionary").and_then(|value| match value { - Value::Null => None, - _ => Some(value.as_str().unwrap().to_owned()), + let text = map.get("text").map(|value| match value { + Value::String(string) => vec![Furigana { + kanji: string.clone(), + furigana: None, + }], + Value::Sequence(sequence) => sequence + .iter() + .map(|value| match value { + Value::String(kanji) => Furigana { + kanji: kanji.to_owned(), + furigana: None, + }, + Value::Mapping(mapping) => { + let (kanji, furigana) = mapping.iter().next().unwrap(); + Furigana { + kanji: kanji.as_str().unwrap().to_owned(), + furigana: Some(furigana.as_str().unwrap().to_owned()), + } + } + _ => panic!(), + }) + .collect(), + _ => panic!(), }); + let dictionary = match map.get("dictionary") { + Some(value) => match value { + Value::Null => None, + Value::String(dictionary) => Some(if !dictionary.starts_with("http") { + format!("https://jisho.org/word/{dictionary}") + } else { + dictionary.to_owned() + }), + _ => panic!("dictionary must be string!"), + }, + None => text.as_ref().map(|furigana| furigana.iter().map(|segment| segment.kanji.clone()).collect()), + }; let pos: Option = map .get("pos") .map(|value| value.as_str().unwrap().parse().unwrap()); Ok(ChallengeWord { dictionary, pos, - text: map.get("text").map(|value| match value { - Value::String(string) => vec![Furigana { - kanji: string.clone(), - furigana: None, - }], - Value::Sequence(sequence) => sequence - .iter() - .map(|value| match value { - Value::String(kanji) => Furigana { - kanji: kanji.to_owned(), - furigana: None, - }, - Value::Mapping(mapping) => { - let (kanji, furigana) = mapping.iter().next().unwrap(); - Furigana { - kanji: kanji.as_str().unwrap().to_owned(), - furigana: Some(furigana.as_str().unwrap().to_owned()), - } - } - _ => panic!(), - }) - .collect(), - _ => panic!(), - }), + text, }) } } diff --git a/templates/index.html.tera b/templates/index.html.tera index 21b46d5..733e628 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -12,9 +12,15 @@ {% for subline in line %}

{% for word in subline %} + {% if word.dictionary %} + + {% endif %} {% for segment in word.text %} {{ segment.kanji }}({{ segment.furigana | safe }}) {% endfor %} + {% if word.dictionary %} + + {% endif %} {% endfor %}

{% endfor %}