Working dictionary links

rust
Elnu 2 years ago
parent bd58b7aecd
commit 25906df967

@ -1,3 +1,4 @@
use core::panic;
use std::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize};
@ -42,17 +43,7 @@ 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 pos: Option<PartOfSpeech> = map
.get("pos")
.map(|value| value.as_str().unwrap().parse().unwrap());
Ok(ChallengeWord {
dictionary,
pos,
text: map.get("text").map(|value| match value {
let text = map.get("text").map(|value| match value {
Value::String(string) => vec![Furigana {
kanji: string.clone(),
furigana: None,
@ -75,7 +66,26 @@ impl<'de> Deserialize<'de> for ChallengeWord {
})
.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<PartOfSpeech> = map
.get("pos")
.map(|value| value.as_str().unwrap().parse().unwrap());
Ok(ChallengeWord {
dictionary,
pos,
text,
})
}
}

@ -12,9 +12,15 @@
{% for subline in line %}
<p>
{% for word in subline %}
{% if word.dictionary %}
<a href="{{ word.dictionary }}" target="_blank"{% if word.pos %} class="{{ word.pos }}"{% endif %}>
{% endif %}
{% for segment in word.text %}
<ruby>{{ segment.kanji }}<rp>(</rp><rt>{{ segment.furigana | safe }}</rt><rp>)</rp></ruby>
{% endfor %}
{% if word.dictionary %}
</a>
{% endif %}
{% endfor %}
</p>
{% endfor %}

Loading…
Cancel
Save