Process heading line breaks in the Tokenizer

This commit is contained in:
Veronica Berglyd Olsen
2024-10-23 21:18:15 +02:00
parent bfa82f6bb3
commit 560540618c
6 changed files with 25 additions and 34 deletions
+5 -10
View File
@@ -255,24 +255,19 @@ class ToDocX(Tokenizer):
self._processFragments(par, S_NORM, tText, tFormat) self._processFragments(par, S_NORM, tText, tFormat)
elif tType == BlockTyp.TITLE: elif tType == BlockTyp.TITLE:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._processFragments(par, S_TITLE, tText, tFormat)
self._processFragments(par, S_TITLE, tHead, tFormat)
elif tType == BlockTyp.HEAD1: elif tType == BlockTyp.HEAD1:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._processFragments(par, S_HEAD1, tText, tFormat)
self._processFragments(par, S_HEAD1, tHead, tFormat)
elif tType == BlockTyp.HEAD2: elif tType == BlockTyp.HEAD2:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._processFragments(par, S_HEAD2, tText, tFormat)
self._processFragments(par, S_HEAD2, tHead, tFormat)
elif tType == BlockTyp.HEAD3: elif tType == BlockTyp.HEAD3:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._processFragments(par, S_HEAD3, tText, tFormat)
self._processFragments(par, S_HEAD3, tHead, tFormat)
elif tType == BlockTyp.HEAD4: elif tType == BlockTyp.HEAD4:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._processFragments(par, S_HEAD4, tText, tFormat)
self._processFragments(par, S_HEAD4, tHead, tFormat)
elif tType == BlockTyp.SEP: elif tType == BlockTyp.SEP:
self._processFragments(par, S_SEP, tText) self._processFragments(par, S_SEP, tText)
+6 -6
View File
@@ -30,7 +30,7 @@ from pathlib import Path
from time import time from time import time
from novelwriter.common import formatTimeStamp from novelwriter.common import formatTimeStamp
from novelwriter.constants import nwHeadFmt, nwHtmlUnicode from novelwriter.constants import nwHtmlUnicode
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape
from novelwriter.formats.tokenizer import Tokenizer from novelwriter.formats.tokenizer import Tokenizer
@@ -211,23 +211,23 @@ class ToHtml(Tokenizer):
lines.append(f"<p{hStyle}>{self._formatText(tText, tFmt)}</p>\n") lines.append(f"<p{hStyle}>{self._formatText(tText, tFmt)}</p>\n")
elif tType == BlockTyp.TITLE: elif tType == BlockTyp.TITLE:
tHead = tText.replace(nwHeadFmt.BR, "<br>") tHead = tText.replace("\n", "<br>")
lines.append(f"<h1 class='title'{hStyle}>{aNm}{tHead}</h1>\n") lines.append(f"<h1 class='title'{hStyle}>{aNm}{tHead}</h1>\n")
elif tType == BlockTyp.HEAD1: elif tType == BlockTyp.HEAD1:
tHead = tText.replace(nwHeadFmt.BR, "<br>") tHead = tText.replace("\n", "<br>")
lines.append(f"<{h1}{h1Cl}{hStyle}>{aNm}{tHead}</{h1}>\n") lines.append(f"<{h1}{h1Cl}{hStyle}>{aNm}{tHead}</{h1}>\n")
elif tType == BlockTyp.HEAD2: elif tType == BlockTyp.HEAD2:
tHead = tText.replace(nwHeadFmt.BR, "<br>") tHead = tText.replace("\n", "<br>")
lines.append(f"<{h2}{hStyle}>{aNm}{tHead}</{h2}>\n") lines.append(f"<{h2}{hStyle}>{aNm}{tHead}</{h2}>\n")
elif tType == BlockTyp.HEAD3: elif tType == BlockTyp.HEAD3:
tHead = tText.replace(nwHeadFmt.BR, "<br>") tHead = tText.replace("\n", "<br>")
lines.append(f"<{h3}{hStyle}>{aNm}{tHead}</{h3}>\n") lines.append(f"<{h3}{hStyle}>{aNm}{tHead}</{h3}>\n")
elif tType == BlockTyp.HEAD4: elif tType == BlockTyp.HEAD4:
tHead = tText.replace(nwHeadFmt.BR, "<br>") tHead = tText.replace("\n", "<br>")
lines.append(f"<{h4}{hStyle}>{aNm}{tHead}</{h4}>\n") lines.append(f"<{h4}{hStyle}>{aNm}{tHead}</{h4}>\n")
elif tType == BlockTyp.SEP: elif tType == BlockTyp.SEP:
+1
View File
@@ -1187,6 +1187,7 @@ class HeadingFormatter:
def apply(self, hFormat: str, text: str, nHead: int) -> str: def apply(self, hFormat: str, text: str, nHead: int) -> str:
"""Apply formatting to a specific heading.""" """Apply formatting to a specific heading."""
hFormat = hFormat.replace(nwHeadFmt.TITLE, text) hFormat = hFormat.replace(nwHeadFmt.TITLE, text)
hFormat = hFormat.replace(nwHeadFmt.BR, "\n")
hFormat = hFormat.replace(nwHeadFmt.CH_NUM, str(self._chCount)) hFormat = hFormat.replace(nwHeadFmt.CH_NUM, str(self._chCount))
hFormat = hFormat.replace(nwHeadFmt.SC_NUM, str(self._scChCount)) hFormat = hFormat.replace(nwHeadFmt.SC_NUM, str(self._scChCount))
hFormat = hFormat.replace(nwHeadFmt.SC_ABS, str(self._scAbsCount)) hFormat = hFormat.replace(nwHeadFmt.SC_ABS, str(self._scAbsCount))
+6 -6
View File
@@ -27,7 +27,7 @@ import logging
from pathlib import Path from pathlib import Path
from novelwriter.constants import nwHeadFmt, nwUnicode from novelwriter.constants import nwUnicode
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
from novelwriter.formats.tokenizer import Tokenizer from novelwriter.formats.tokenizer import Tokenizer
@@ -113,23 +113,23 @@ class ToMarkdown(Tokenizer):
lines.append(f"{tTemp}\n\n") lines.append(f"{tTemp}\n\n")
elif tType == BlockTyp.TITLE: elif tType == BlockTyp.TITLE:
tHead = tText.replace(nwHeadFmt.BR, " - ") tHead = tText.replace("\n", " - ")
lines.append(f"# {tHead}\n\n") lines.append(f"# {tHead}\n\n")
elif tType == BlockTyp.HEAD1: elif tType == BlockTyp.HEAD1:
tHead = tText.replace(nwHeadFmt.BR, " - ") tHead = tText.replace("\n", " - ")
lines.append(f"# {tHead}\n\n") lines.append(f"# {tHead}\n\n")
elif tType == BlockTyp.HEAD2: elif tType == BlockTyp.HEAD2:
tHead = tText.replace(nwHeadFmt.BR, " - ") tHead = tText.replace("\n", " - ")
lines.append(f"## {tHead}\n\n") lines.append(f"## {tHead}\n\n")
elif tType == BlockTyp.HEAD3: elif tType == BlockTyp.HEAD3:
tHead = tText.replace(nwHeadFmt.BR, " - ") tHead = tText.replace("\n", " - ")
lines.append(f"### {tHead}\n\n") lines.append(f"### {tHead}\n\n")
elif tType == BlockTyp.HEAD4: elif tType == BlockTyp.HEAD4:
tHead = tText.replace(nwHeadFmt.BR, " - ") tHead = tText.replace("\n", " - ")
lines.append(f"#### {tHead}\n\n") lines.append(f"#### {tHead}\n\n")
elif tType == BlockTyp.SEP: elif tType == BlockTyp.SEP:
+5 -10
View File
@@ -444,24 +444,19 @@ class ToOdt(Tokenizer):
elif tType == BlockTyp.TITLE: elif tType == BlockTyp.TITLE:
# Title must be text:p # Title must be text:p
tHead = tText.replace(nwHeadFmt.BR, "\n") self._addTextPar(xText, S_TITLE, oStyle, tText, isHead=False)
self._addTextPar(xText, S_TITLE, oStyle, tHead, isHead=False)
elif tType == BlockTyp.HEAD1: elif tType == BlockTyp.HEAD1:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._addTextPar(xText, S_HEAD1, oStyle, tText, isHead=True, oLevel="1")
self._addTextPar(xText, S_HEAD1, oStyle, tHead, isHead=True, oLevel="1")
elif tType == BlockTyp.HEAD2: elif tType == BlockTyp.HEAD2:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._addTextPar(xText, S_HEAD2, oStyle, tText, isHead=True, oLevel="2")
self._addTextPar(xText, S_HEAD2, oStyle, tHead, isHead=True, oLevel="2")
elif tType == BlockTyp.HEAD3: elif tType == BlockTyp.HEAD3:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._addTextPar(xText, S_HEAD3, oStyle, tText, isHead=True, oLevel="3")
self._addTextPar(xText, S_HEAD3, oStyle, tHead, isHead=True, oLevel="3")
elif tType == BlockTyp.HEAD4: elif tType == BlockTyp.HEAD4:
tHead = tText.replace(nwHeadFmt.BR, "\n") self._addTextPar(xText, S_HEAD4, oStyle, tText, isHead=True, oLevel="4")
self._addTextPar(xText, S_HEAD4, oStyle, tHead, isHead=True, oLevel="4")
elif tType == BlockTyp.SEP: elif tType == BlockTyp.SEP:
self._addTextPar(xText, S_SEP, oStyle, tText) self._addTextPar(xText, S_SEP, oStyle, tText)
+2 -2
View File
@@ -34,7 +34,7 @@ from PyQt5.QtGui import (
) )
from PyQt5.QtPrintSupport import QPrinter from PyQt5.QtPrintSupport import QPrinter
from novelwriter.constants import nwHeadFmt, nwStyles, nwUnicode from novelwriter.constants import nwStyles, nwUnicode
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
from novelwriter.formats.tokenizer import HEADINGS, Tokenizer from novelwriter.formats.tokenizer import HEADINGS, Tokenizer
@@ -217,7 +217,7 @@ class ToQTextDocument(Tokenizer):
elif tType in HEADINGS: elif tType in HEADINGS:
bFmt, cFmt = self._genHeadStyle(tType, tMeta, bFmt) bFmt, cFmt = self._genHeadStyle(tType, tMeta, bFmt)
newBlock(cursor, bFmt) newBlock(cursor, bFmt)
cursor.insertText(tText.replace(nwHeadFmt.BR, "\n"), cFmt) cursor.insertText(tText, cFmt)
elif tType == BlockTyp.SEP: elif tType == BlockTyp.SEP:
newBlock(cursor, bFmt) newBlock(cursor, bFmt)