Skip to content

Commit

Permalink
Add option to remove default link styles in EPUB books
Browse files Browse the repository at this point in the history
  • Loading branch information
xxyzz committed Jan 21, 2024
1 parent c14241b commit f4dbb71
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
8 changes: 8 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
prefs.defaults["last_opened_kindle_lemmas_language"] = "ca"
prefs.defaults["last_opened_wiktionary_lemmas_language"] = "ca"
prefs.defaults["use_wiktionary_for_kindle"] = False
prefs.defaults["remove_link_styles"] = False
prefs.defaults["python_path"] = ""
for code in load_plugin_json(get_plugin_path(), "data/languages.json").keys():
prefs.defaults[f"{code}_wiktionary_difficulty_limit"] = 5
Expand Down Expand Up @@ -210,6 +211,12 @@ def __init__(self):
self.locator_map_box.setChecked(prefs["add_locator_map"])
vl.addWidget(self.locator_map_box)

self.remove_link_styles = QCheckBox(
_("Remove Word Wise and X-Ray links underline and color in EPUB books")
)
self.remove_link_styles.setChecked(prefs["remove_link_styles"])
vl.addWidget(self.remove_link_styles)

donate_button = QPushButton(QIcon.ic("donate.png"), "Tree-fiddy?")
donate_button.clicked.connect(donate)
vl.addWidget(donate_button)
Expand Down Expand Up @@ -237,6 +244,7 @@ def save_settings(self) -> None:
prefs["fandom"] = self.fandom_url.text().removesuffix("/")
prefs["add_locator_map"] = self.locator_map_box.isChecked()
prefs["minimal_x_ray_count"] = self.minimal_x_ray_count.value()
prefs["remove_link_styles"] = self.remove_link_styles.isChecked()
if not ismacos:
prefs["use_gpu"] = self.use_gpu_box.isChecked()
prefs["cuda"] = self.cuda_version_box.currentData()
Expand Down
2 changes: 1 addition & 1 deletion custom_lemmas.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def init_filters(self, form_layout: QFormLayout) -> None:
self.filter_difficulty_box.currentIndexChanged.connect(self.filter_data)
form_layout.addRow(_("Filter difficulty"), self.filter_difficulty_box)

def add_enable_disable_buttons(self, form_layout:QFormLayout) -> None:
def add_enable_disable_buttons(self, form_layout: QFormLayout) -> None:
hl = QHBoxLayout()
enable_all_button = QPushButton(_("Enable all"))
disable_all_button = QPushButton(_("Disable all"))
Expand Down
33 changes: 22 additions & 11 deletions epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ def modify_epub(
lemmas_conn.close()

def insert_anchor_elements(self, lang: str) -> None:
css_rules = ""
if len(self.lemmas) > 0:
css_rules += """
body {line-height: 2.5;}
ruby.wordwise {text-decoration: overline;}
ruby.wordwise a {text-decoration: none;}
"""
if self.prefs["remove_link_styles"]:
css_rules += """
a.x-ray, a.wordwise, ruby.wordwise a {
text-decoration: none;
color: inherit;
}
"""

for xhtml_path, entity_list in self.entity_occurrences.items():
if self.entities and self.lemmas:
entity_list = sorted(entity_list, key=operator.itemgetter(0))
Expand All @@ -269,28 +284,24 @@ def insert_anchor_elements(self, lang: str) -> None:
new_xhtml_str += xhtml_str[last_end:start]
if isinstance(entity_id, int):
new_xhtml_str += (
f'<a epub:type="noteref" href="x_ray.xhtml#'
f'<a class="x-ray" epub:type="noteref" href="x_ray.xhtml#'
f'{entity_id}">{entity}</a>'
)
else:
new_xhtml_str += self.build_word_wise_tag(entity_id, entity, lang)
last_end = end
new_xhtml_str += xhtml_str[last_end:]

# add epub namespace and Word Wise CSS
# add epub namespace and CSS
with xhtml_path.open("w", encoding="utf-8") as f:
if NAMESPACES["ops"] not in new_xhtml_str:
new_xhtml_str = new_xhtml_str.replace(
f'xmlns="{NAMESPACES["xml"]}"',
f'xmlns="{NAMESPACES["xml"]}" '
f'xmlns:epub="{NAMESPACES["ops"]}"',
f'xmlns="{NAMESPACES["xml"]}" xmlns:epub="{NAMESPACES["ops"]}"',
)
if self.lemmas:
if len(css_rules) > 0:
new_xhtml_str = new_xhtml_str.replace(
"</head>",
"<style>body {line-height: 2.5;} ruby "
"{text-decoration:overline;} ruby a {text-decoration:none;}"
"</style></head>",
"</head>", f"<style>{css_rules}</style></head>"
)
f.write(new_xhtml_str)

Expand All @@ -306,12 +317,12 @@ def build_word_wise_tag(self, word: str, origin_word: str, lang: str) -> str:
word_id = self.lemmas[word]
if len(short_def) / len(origin_word) > len_ratio:
return (
'<a epub:type="noteref" href="word_wise.xhtml#'
'<a class="wordwise" epub:type="noteref" href="word_wise.xhtml#'
f'{word_id}">{origin_word}</a>'
)
else:
return (
'<ruby><a epub:type="noteref" href="word_wise.xhtml#'
'<ruby class="wordwise"><a epub:type="noteref" href="word_wise.xhtml#'
f'{word_id}">{origin_word}</a><rp>(</rp><rt>{short_def}'
"</rt><rp>)</rp></ruby>"
)
Expand Down

0 comments on commit f4dbb71

Please sign in to comment.