-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify bolding logic, remove font weight bolding
It's not that useful since the result in Anki is the same and significantly complicates the logic
- Loading branch information
Showing
8 changed files
with
46 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,19 @@ | ||
from PyQt5.QtGui import QFont | ||
from PyQt5.QtCore import pyqtSlot | ||
from .searchable_text_edit import SearchableTextEdit | ||
from ..text_manipulation import (markdown_boldings_to_bold_tag_boldings, | ||
bold_char_boldings_to_bold_tag_boldings, | ||
apply_bold_tags, | ||
remove_bold_char_boldings | ||
) | ||
from ..global_names import logger | ||
import re | ||
from ..global_names import settings | ||
|
||
|
||
class SearchableBoldableTextEdit(SearchableTextEdit): | ||
def __init__(self): | ||
super().__init__() | ||
self.currentCharFormatChanged.connect(self.handleCurrentCharFormatChanged) | ||
|
||
@property | ||
def textBoldedByTags(self): | ||
""" | ||
Current text content with bolded words defined by <b/>, irrespective of | ||
`settings.value("bold_style")`. | ||
""" | ||
if settings.value("bold_style", type=int) == 1: | ||
return markdown_boldings_to_bold_tag_boldings( | ||
# toMarkdown() includes erroneous newlines | ||
self.toMarkdown()[:-2]) | ||
elif settings.value("bold_style", type=int) == 2: | ||
return bold_char_boldings_to_bold_tag_boldings(self.toPlainText())[0] | ||
else: | ||
return self.toPlainText() | ||
|
||
def keyPressEvent(self, e): | ||
super().keyPressEvent(e) | ||
|
||
# Every time the user writes "_", check if we need to perform the | ||
# substitution "__{word}__" -> "<b>{word}</b>" | ||
if settings.value("bold_style", type=int) == 1 \ | ||
and e.text() == settings.value("bold_char"): | ||
self.performBoldSubstitutions() | ||
|
||
def performBoldSubstitutions(self): | ||
bold_tags_substituted, subs_performed = \ | ||
bold_char_boldings_to_bold_tag_boldings(self.toPlainText()) | ||
|
||
if subs_performed: | ||
def rebold_previously_bolded_words(string: str): | ||
for should_bold in set(re.findall(r"\*\*(.+?)\*\*", self.toMarkdown())): | ||
string = re.sub( | ||
re.escape(should_bold), | ||
lambda match: apply_bold_tags(match.group(0)), | ||
string) | ||
return string | ||
|
||
bold_tags_substituted = rebold_previously_bolded_words( | ||
bold_tags_substituted) | ||
|
||
# Save cursor position | ||
old_cursor_pos = self.textCursor().position() | ||
# Set text | ||
self.setText(bold_tags_substituted) | ||
|
||
# Move back by the 4 characters removed when substituting | ||
# "__{word}__" => "<b>{word}</b>" (note that `cursor` does not | ||
# consider "<b/>" when calling `cursor.setPosition()` | ||
cursor = self.textCursor() | ||
cursor.setPosition(old_cursor_pos - 4 * subs_performed) | ||
self.setTextCursor(cursor) | ||
|
||
@pyqtSlot() | ||
def handleCurrentCharFormatChanged(self): | ||
""" | ||
If `settings.value("bold_style") == BoldStyle.FONTWEIGHT.value`, bolded characters are | ||
added to the text editor. By default, the user cannot type in non-bold | ||
font adjacent to these characters, so we reset the font weight to | ||
non-bold every time it changes. | ||
""" | ||
|
||
def ensureNormalFontWeight(): | ||
cursor = self.textCursor() | ||
fmt = cursor.charFormat() | ||
if fmt.fontWeight() != QFont.Weight.Normal: | ||
fmt.setFontWeight(QFont.Weight.Normal) | ||
cursor.setCharFormat(fmt) | ||
self.setTextCursor(cursor) | ||
ensureNormalFontWeight() | ||
|
||
@property | ||
def unboldedText(self): | ||
if settings.value("bold_style", type=int) == 1: | ||
# `.toPlainText()` strips <b/> for us | ||
return self.toPlainText() | ||
elif settings.value("bold_style", type=int) == 2: | ||
# Remove bolds using bold_char | ||
return remove_bold_char_boldings(self.toPlainText()) | ||
elif settings.value("bold_style", type=int) == 0: | ||
# Text was never bolded in the first place | ||
return self.toPlainText() | ||
else: | ||
raise ValueError("Invalid bold style") | ||
def unbold(self): | ||
self.setPlainText(self.toPlainText().replace('__', '')) | ||
|
||
def bold(self, word): | ||
logger.debug(f'bolding {word}') | ||
self.setPlainText(re.sub(r'\b' + re.escape(word) + r'\b', '__' + word + '__', self.toPlainText())) | ||
|
||
def toAnki(self): | ||
# substitute __word__ with <b>word</b> | ||
result = re.sub(r'__(.*?)__', r'<b>\1</b>', self.toPlainText()) | ||
# substitute newlines with <br> | ||
result = result.replace('\n', '<br>') | ||
return result |