Move shared formats code to separate file, and rename some variables
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
"""
|
||||
novelWriter – Formats Shared
|
||||
============================
|
||||
|
||||
File History:
|
||||
Created: 2024-10-21 [2.6b1] TextFmt
|
||||
Created: 2024-10-21 [2.6b1] BlockTyp
|
||||
Created: 2024-10-21 [2.6b1] BlockFmt
|
||||
|
||||
This file is a part of novelWriter
|
||||
Copyright 2018–2024, Veronica Berglyd Olsen
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from enum import Flag, IntEnum
|
||||
|
||||
ESCAPES = {r"\*": "*", r"\~": "~", r"\_": "_", r"\[": "[", r"\]": "]", r"\ ": ""}
|
||||
RX_ESC = re.compile("|".join([re.escape(k) for k in ESCAPES.keys()]), flags=re.DOTALL)
|
||||
|
||||
|
||||
def stripEscape(text: str) -> str:
|
||||
"""Strip escaped Markdown characters from paragraph text."""
|
||||
if "\\" in text:
|
||||
return RX_ESC.sub(lambda x: ESCAPES[x.group(0)], text)
|
||||
return text
|
||||
|
||||
|
||||
# Enums
|
||||
# =====
|
||||
|
||||
class TextFmt(IntEnum):
|
||||
"""Text Format.
|
||||
|
||||
An enum indicating the beginning or end of a text format region.
|
||||
They must be paired with a position, and apply to locations in a
|
||||
text block.
|
||||
"""
|
||||
|
||||
B_B = 1 # Begin bold
|
||||
B_E = 2 # End bold
|
||||
I_B = 3 # Begin italics
|
||||
I_E = 4 # End italics
|
||||
D_B = 5 # Begin strikeout
|
||||
D_E = 6 # End strikeout
|
||||
U_B = 7 # Begin underline
|
||||
U_E = 8 # End underline
|
||||
M_B = 9 # Begin mark
|
||||
M_E = 10 # End mark
|
||||
SUP_B = 11 # Begin superscript
|
||||
SUP_E = 12 # End superscript
|
||||
SUB_B = 13 # Begin subscript
|
||||
SUB_E = 14 # End subscript
|
||||
DL_B = 15 # Begin dialogue
|
||||
DL_E = 16 # End dialogue
|
||||
ADL_B = 17 # Begin alt dialogue
|
||||
ADL_E = 18 # End alt dialogue
|
||||
FNOTE = 19 # Footnote marker
|
||||
STRIP = 20 # Strip the format code
|
||||
|
||||
|
||||
class BlockTyp(IntEnum):
|
||||
"""Text Block Type.
|
||||
|
||||
An enum indicating the type of a text block.
|
||||
"""
|
||||
|
||||
EMPTY = 1 # Empty line (new paragraph)
|
||||
SYNOPSIS = 2 # Synopsis comment
|
||||
SHORT = 3 # Short description comment
|
||||
COMMENT = 4 # Comment line
|
||||
KEYWORD = 5 # Command line
|
||||
TITLE = 6 # Title
|
||||
HEAD1 = 7 # Heading 1
|
||||
HEAD2 = 8 # Heading 2
|
||||
HEAD3 = 9 # Heading 3
|
||||
HEAD4 = 10 # Heading 4
|
||||
TEXT = 11 # Text line
|
||||
SEP = 12 # Scene separator
|
||||
SKIP = 13 # Paragraph break
|
||||
|
||||
|
||||
class BlockFmt(Flag):
|
||||
"""Text Block Format.
|
||||
|
||||
An enum of flags that can be combined to format a text block.
|
||||
"""
|
||||
|
||||
NONE = 0x0000 # No special style
|
||||
LEFT = 0x0001 # Left aligned
|
||||
RIGHT = 0x0002 # Right aligned
|
||||
CENTRE = 0x0004 # Centred
|
||||
JUSTIFY = 0x0008 # Justified
|
||||
PBB = 0x0010 # Page break before
|
||||
PBA = 0x0020 # Page break after
|
||||
Z_TOPMRG = 0x0040 # Zero top margin
|
||||
Z_BTMMRG = 0x0080 # Zero bottom margin
|
||||
IND_L = 0x0100 # Left indentation
|
||||
IND_R = 0x0200 # Right indentation
|
||||
IND_T = 0x0400 # Text indentation
|
||||
|
||||
|
||||
# Types
|
||||
# =====
|
||||
|
||||
# A list of formats for a single text string, consisting of:
|
||||
# text position, text format, and meta data
|
||||
T_Formats = list[tuple[int, TextFmt, str]]
|
||||
|
||||
# A note or comment with text and associated text formats
|
||||
T_Note = tuple[str, T_Formats]
|
||||
|
||||
# A tokenized text block, consisting of:
|
||||
# type, header number, text, text formats, and block format
|
||||
T_Block = tuple[BlockTyp, int, str, T_Formats, BlockFmt]
|
||||
Reference in New Issue
Block a user