Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add "Convert text markup" feature #507

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions src/guiguts/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
unicode_normalize,
proofer_comment_check,
asterisk_check,
TextMarkupConvertorDialog,
)
from guiguts.page_details import PageDetailsDialog
from guiguts.preferences import preferences, PrefKey
Expand Down Expand Up @@ -357,6 +358,11 @@ def initialize_preferences(self) -> None:
preferences.set_default(PrefKey.WRAP_INDEX_MAIN_MARGIN, 2)
preferences.set_default(PrefKey.WRAP_INDEX_WRAP_MARGIN, 8)
preferences.set_default(PrefKey.WRAP_INDEX_RIGHT_MARGIN, 72)
preferences.set_default(PrefKey.TEXT_MARKUP_ITALIC, "_")
preferences.set_default(PrefKey.TEXT_MARKUP_BOLD, "=")
preferences.set_default(PrefKey.TEXT_MARKUP_SMALLCAPS, "+")
preferences.set_default(PrefKey.TEXT_MARKUP_GESPERRT, "~")
preferences.set_default(PrefKey.TEXT_MARKUP_FONT, "=")
preferences.set_default(PrefKey.PAGESEP_AUTO_TYPE, PageSepAutoType.AUTO_FIX)
preferences.set_default(PrefKey.THEME_NAME, "Default")
preferences.set_callback(
Expand Down Expand Up @@ -407,6 +413,7 @@ def init_menus(self) -> None:
self.init_edit_menu()
self.init_search_menu()
self.init_tools_menu()
self.init_text_menu()
self.init_view_menu()
self.init_help_menu()
self.init_os_menu()
Expand Down Expand Up @@ -692,6 +699,13 @@ def init_tools_menu(self) -> None:
"Cmd/Ctrl+I",
)

def init_text_menu(self) -> None:
"""Create the Text menu."""
menu_tools = Menu(menubar(), "Te~xt")
menu_tools.add_button(
"Convert ~Markup...", TextMarkupConvertorDialog.show_dialog
)

def init_view_menu(self) -> None:
"""Create the View menu."""
menu_view = Menu(menubar(), "~View")
Expand Down
2 changes: 1 addition & 1 deletion src/guiguts/misc_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def add_label_spinbox(row: int, label: str, key: PrefKey, tooltip: str) -> None:
tooltip: Text for tooltip.
"""
ttk.Label(wrapping_frame, text=label).grid(
column=0, row=row, sticky="NE", pady=2
column=0, row=row, sticky="NSE", pady=2
)
spinbox = ttk.Spinbox(
wrapping_frame,
Expand Down
118 changes: 116 additions & 2 deletions src/guiguts/misc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
preferences,
PersistentBoolean,
)
from guiguts.utilities import IndexRowCol, IndexRange, cmd_ctrl_string, is_mac
from guiguts.utilities import (
IndexRowCol,
IndexRange,
cmd_ctrl_string,
is_mac,
sound_bell,
)
from guiguts.widgets import ToolTip, ToplevelDialog

logger = logging.getLogger(__package__)
Expand Down Expand Up @@ -217,7 +223,7 @@ class PageSeparatorDialog(ToplevelDialog):
BTN_WIDTH = 16

def __init__(self) -> None:
"""Initialize messagelog dialog."""
"""Initialize page separator dialog."""
super().__init__("Page Separator Fixup", resize_x=False, resize_y=False)
for col in range(0, 3):
self.top_frame.columnconfigure(col, pad=2, weight=1)
Expand Down Expand Up @@ -1224,3 +1230,111 @@ def asterisk_check() -> None:
line, IndexRange(match.rowcol, end_rowcol), match.rowcol.col, end_rowcol.col
)
checker_dialog.display_entries()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - the code prettifier forces 2 blank lines before the start of a new class (TextMarkupConvertorDialog), and 1 line between functions.


class TextMarkupConvertorDialog(ToplevelDialog):
"""Dialog for converting DP markup to text markup."""

def __init__(self) -> None:
"""Initialize text markup convertor dialog."""
super().__init__("Text Markup Convertor", resize_x=False, resize_y=False)

def add_row(row: int, markup: str, prefkey: PrefKey) -> None:
ttk.Label(
self.top_frame, text=f"<{markup}>...</{markup}>", anchor=tk.CENTER
).grid(column=0, row=row, sticky="NSEW")
ttk.Button(
self.top_frame,
text="Convert ⟹",
command=lambda: self.convert(
rf"</?{markup}>", preferences.get(prefkey)
),
).grid(
column=1,
row=row,
pady=2,
sticky="NSEW",
)
ttk.Entry(
self.top_frame,
width=2,
textvariable=PersistentString(prefkey),
).grid(column=2, row=row, padx=5, pady=2, sticky="NSEW")

add_row(0, "i", PrefKey.TEXT_MARKUP_ITALIC)
add_row(1, "b", PrefKey.TEXT_MARKUP_BOLD)
ttk.Label(self.top_frame, text="<sc>...</sc>", anchor=tk.CENTER).grid(
column=0, row=2, sticky="NSEW"
)
ttk.Button(
self.top_frame, text="Convert ⟹", command=self.convert_smallcaps_to_allcaps
).grid(column=1, row=2, pady=2, sticky="NSEW")
ttk.Label(self.top_frame, text="ALLCAPS").grid(
column=2, row=2, padx=5, sticky="NSEW"
)
add_row(3, "sc", PrefKey.TEXT_MARKUP_SMALLCAPS)
add_row(4, "g", PrefKey.TEXT_MARKUP_GESPERRT)
add_row(5, "f", PrefKey.TEXT_MARKUP_FONT)
ttk.Label(self.top_frame, text="<tb>", anchor=tk.CENTER).grid(
column=0, row=6, sticky="NSEW"
)
ttk.Button(
self.top_frame,
text="Convert ⟹",
command=lambda: self.convert(r"<tb>", " *" * 5),
).grid(column=1, row=6, pady=2, sticky="NSEW")
ttk.Label(self.top_frame, text="Asterisks").grid(
column=2, row=6, padx=5, sticky="NSEW"
)

def convert(self, regex: str, replacement: str) -> None:
"""Convert one type of DP markup to text markup.

Args:
regex: Regex that matches open/close markup.
replacement: String to replace markup with.
"""
found = False
maintext().undo_block_begin()
search_range = IndexRange(maintext().start(), maintext().end())
# Find each piece of markup that matches the regex
while match := maintext().find_match(
regex, search_range, regexp=True, nocase=True
):
match_index = match.rowcol.index()
maintext().replace(
match_index, f"{match_index}+{match.count}c", replacement
)
search_range = IndexRange(
maintext().index(f"{match_index}+1c"), maintext().end()
)
found = True
if not found:
sound_bell()

def convert_smallcaps_to_allcaps(self) -> None:
"""Convert text marked up with <sc> to ALLCAPS."""
found = False
maintext().undo_block_begin()
search_range = IndexRange(maintext().start(), maintext().end())
# Find start of each smallcap markup
while match := maintext().find_match(
"<sc>", search_range, regexp=False, nocase=True
):
match_index = match.rowcol.index()
search_range = IndexRange(
maintext().index(f"{match_index}+4c"), maintext().end()
)
end_match = maintext().find_match(
"</sc>", search_range, regexp=False, nocase=True
)
if end_match is None:
logger.error(f"Unclosed <sc> markup on line {match.rowcol.row}")
maintext().set_insert_index(match.rowcol, focus_widget=maintext())
return
end_match_index = end_match.rowcol.index()
replacement = maintext().get(f"{match_index}+4c", end_match_index).upper()
maintext().replace(match_index, f"{end_match_index}+5c", replacement)
found = True
if not found:
sound_bell()
5 changes: 5 additions & 0 deletions src/guiguts/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class PrefKey(StrEnum):
WRAP_INDEX_MAIN_MARGIN = auto()
WRAP_INDEX_WRAP_MARGIN = auto()
WRAP_INDEX_RIGHT_MARGIN = auto()
TEXT_MARKUP_ITALIC = auto()
TEXT_MARKUP_BOLD = auto()
TEXT_MARKUP_SMALLCAPS = auto()
TEXT_MARKUP_GESPERRT = auto()
TEXT_MARKUP_FONT = auto()
PAGESEP_AUTO_TYPE = auto()
THEME_NAME = auto()
TEAROFF_MENUS = auto()
Expand Down
Loading