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,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<PartOfSpeech> = 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,
})
}
}

@ -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