Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/core/src/view/dictionary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod bottom_bar;

use lazy_static::lazy_static;
use regex::Regex;
use crate::device::CURRENT_DEVICE;
use crate::framebuffer::{Framebuffer, UpdateMode, Pixmap};
Expand Down Expand Up @@ -29,6 +30,13 @@ use self::bottom_bar::BottomBar;
const VIEWER_STYLESHEET: &str = "css/dictionary.css";
const USER_STYLESHEET: &str = "css/dictionary-user.css";

lazy_static! {
// Many dict-format entries mix plain text with inline markup (e.g. `<br>` between senses,
// `<a href="...">` cross-references) instead of being pure HTML from the very first
// character, so `starts_with('<')` alone misses them.
static ref HTML_TAG: Regex = Regex::new(r"</?[a-zA-Z][a-zA-Z0-9]*(?:\s[^<>]*)?/?>").unwrap();
}

pub struct Dictionary {
id: Id,
rect: Rectangle,
Expand Down Expand Up @@ -67,7 +75,7 @@ fn query_to_content(query: &str, language: &String, fuzzy: bool, target: Option<
if !body.trim_start().starts_with("<h2") {
content.push_str(&format!("<h2 class=\"headword\">{}</h2>\n", head.replace('<', "&lt;").replace('>', "&gt;")));
}
if body.trim_start().starts_with('<') {
if HTML_TAG.is_match(&body) {
content.push_str(&body);
} else {
content.push_str(&format!("<pre>{}</pre>", body.replace('<', "&lt;").replace('>', "&gt;")));
Expand Down