Skip to content

Commit

Permalink
Add shortcodes to the main menu (#1583)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo authored Nov 7, 2023
2 parents 08e67b0 + a07177c commit f3b6be3
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 228 deletions.
18 changes: 18 additions & 0 deletions novelwriter/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ class nwRegEx:
# END Class nwRegEx


class nwShortcode:

BOLD_O = "[b]"
BOLD_C = "[/b]"
ITALIC_O = "[i]"
ITALIC_C = "[/i]"
STRIKE_O = "[s]"
STRIKE_C = "[/s]"
ULINE_O = "[u]"
ULINE_C = "[/u]"
SUP_O = "[sup]"
SUP_C = "[/sup]"
SUB_O = "[sub]"
SUB_C = "[/sub]"

# END Class nwShortcode


class nwHeaders:

H_VALID = ("H0", "H1", "H2", "H3", "H4")
Expand Down
14 changes: 7 additions & 7 deletions novelwriter/core/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from novelwriter.enum import nwItemLayout
from novelwriter.common import formatTimeStamp, numberToRoman, checkInt
from novelwriter.constants import nwHeadFmt, nwRegEx, nwUnicode
from novelwriter.constants import nwHeadFmt, nwRegEx, nwShortcode, nwUnicode
from novelwriter.core.project import NWProject

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -180,12 +180,12 @@ def __init__(self, project: NWProject) -> None:
self._rxShortCodeVals = QRegularExpression(nwRegEx.FMT_SV)

self._shortCodeFmt = {
"[i]": self.FMT_I_B, "[/i]": self.FMT_I_E,
"[b]": self.FMT_B_B, "[/b]": self.FMT_B_E,
"[s]": self.FMT_D_B, "[/s]": self.FMT_D_E,
"[u]": self.FMT_U_B, "[/u]": self.FMT_U_E,
"[sup]": self.FMT_SUP_B, "[/sup]": self.FMT_SUP_E,
"[sub]": self.FMT_SUB_B, "[/sub]": self.FMT_SUB_E,
nwShortcode.ITALIC_O: self.FMT_I_B, nwShortcode.ITALIC_C: self.FMT_I_E,
nwShortcode.BOLD_O: self.FMT_B_B, nwShortcode.BOLD_C: self.FMT_B_E,
nwShortcode.STRIKE_O: self.FMT_D_B, nwShortcode.STRIKE_C: self.FMT_D_E,
nwShortcode.ULINE_O: self.FMT_U_B, nwShortcode.ULINE_C: self.FMT_U_E,
nwShortcode.SUP_O: self.FMT_SUP_B, nwShortcode.SUP_C: self.FMT_SUP_E,
nwShortcode.SUB_O: self.FMT_SUB_B, nwShortcode.SUB_C: self.FMT_SUB_E,
}

return
Expand Down
6 changes: 6 additions & 0 deletions novelwriter/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class nwDocAction(Enum):
ALIGN_R = 26
INDENT_L = 27
INDENT_R = 28
SC_ITALIC = 29
SC_BOLD = 30
SC_STRIKE = 31
SC_ULINE = 32
SC_SUP = 33
SC_SUB = 34

# END Enum nwDocAction

Expand Down
14 changes: 13 additions & 1 deletion novelwriter/gui/doceditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from novelwriter import CONFIG, SHARED
from novelwriter.enum import nwDocAction, nwDocInsert, nwDocMode, nwItemClass, nwTrinary
from novelwriter.common import minmax, transferCase
from novelwriter.constants import nwKeyWords, nwLabels, nwUnicode, trConst
from novelwriter.constants import nwKeyWords, nwLabels, nwShortcode, nwUnicode, trConst
from novelwriter.core.item import NWItem
from novelwriter.core.index import countWords
from novelwriter.core.document import NWDocument
Expand Down Expand Up @@ -713,6 +713,18 @@ def docAction(self, action: nwDocAction) -> bool:
self._formatBlock(nwDocAction.INDENT_L)
elif action == nwDocAction.INDENT_R:
self._formatBlock(nwDocAction.INDENT_R)
elif action == nwDocAction.SC_ITALIC:
self._wrapSelection(nwShortcode.ITALIC_O, nwShortcode.ITALIC_C)
elif action == nwDocAction.SC_BOLD:
self._wrapSelection(nwShortcode.BOLD_O, nwShortcode.BOLD_C)
elif action == nwDocAction.SC_STRIKE:
self._wrapSelection(nwShortcode.STRIKE_O, nwShortcode.STRIKE_C)
elif action == nwDocAction.SC_ULINE:
self._wrapSelection(nwShortcode.ULINE_O, nwShortcode.ULINE_C)
elif action == nwDocAction.SC_SUP:
self._wrapSelection(nwShortcode.SUP_O, nwShortcode.SUP_C)
elif action == nwDocAction.SC_SUB:
self._wrapSelection(nwShortcode.SUB_O, nwShortcode.SUB_C)
else:
logger.debug("Unknown or unsupported document action '%s'", str(action))
self._allowAutoReplace(True)
Expand Down
Loading

0 comments on commit f3b6be3

Please sign in to comment.