Skip to content

Commit 4b5f3c8

Browse files
authored
Clarify text alignment and break behaviour (#2427)
2 parents 0bbe29b + c1dd2eb commit 4b5f3c8

File tree

6 files changed

+206
-24
lines changed

6 files changed

+206
-24
lines changed

docs/source/usage/advanced_formatting.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ activated by clicking the left-most icon button in the editor header.
5656
.. versionadded:: 2.2
5757

5858

59+
.. _docs_usage_formatting_shortcodes_break:
60+
61+
Forced Line Break
62+
-----------------
63+
64+
Inserting ``[br]`` in the text will ensure a line break is always inserted in that place, even if
65+
you turn off **Preserve Hard Line Breaks** in your manuscript build settings.
66+
67+
You can add a manual line break after it too, for a better visual representation in the editor, but
68+
keep in mind that this line break is removed before the text is processed, so the text on either
69+
side of the ``[br]`` shortcode will be considered as belonging to the same line. This can affect
70+
how alignment is treated. See :ref:`docs_usage_align_indent_forced` for more details.
71+
72+
5973
.. _docs_usage_formatting_breaks:
6074

6175
Vertical Space and Page Breaks

docs/source/usage/alignment_and_indent.rst

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,33 @@ the entire paragraph. For the following text, all lines will be centred:
5353

5454
.. code-block:: md
5555
56-
>> I am the very model of a modern Major-General
56+
>> I am the very model of a modern Major-General <<
5757
I've information vegetable, animal, and mineral
5858
I know the kings of England, and I quote the fights historical
59-
From Marathon to Waterloo, in order categorical <<
59+
From Marathon to Waterloo, in order categorical
60+
61+
If you have multiple conflicting alignments on a paragraph, only one is applied. The order of
62+
precedence is:
63+
64+
#. Left alignment
65+
#. Right alignment
66+
#. Centred text
67+
#. Justified text
68+
69+
.. note::
70+
71+
It is strongly recommended that you keep the **Preserve Hard Line Breaks** setting enabled in
72+
your manuscript build settings. This setting assumes all single line breaks in your text are
73+
intended. Turning this off makes adding line breaks more complicated, but it is still possible.
74+
See :ref:`docs_usage_align_indent_forced`.
6075

6176

6277
Alignment with First Line Indent
6378
================================
6479

6580
If you have first line indent enabled in your manuscript build settings, you probably want to
66-
disable it for text in verses. Adding any alignment tags will cause the first line indent to be
67-
switched off for that paragraph.
81+
disable it for text in verses. Adding any alignment tags on a paragraph will cause the first
82+
line indent to be switched off for that paragraph.
6883

6984
:bdg-info:`Example`
7085

@@ -76,3 +91,35 @@ The following text will always be aligned against the left margin:
7691
I've information vegetable, animal, and mineral
7792
I know the kings of England, and I quote the fights historical
7893
From Marathon to Waterloo, in order categorical
94+
95+
96+
.. _docs_usage_align_indent_forced:
97+
98+
Alignment with Forced Line Breaks
99+
=================================
100+
101+
If you turn off **Preserve Hard Line Breaks** in your manuscript build settings, you can still
102+
force line breaks in paragraphs using the ``[br]`` shortcode. For clarity in the text, you can add
103+
a line break after it as well. It doesn't result in two line breaks.
104+
105+
Keep in mind that when the text is processed, the lines on either side of a ``[br]`` shortcode are
106+
combined, and any trailing hard line break is *ignored*. This means that when such a paragraph is
107+
processed, these line breaks count as the same line. This affects how alignment tags are handled.
108+
For instance, this text becomes centred instead of left aligned.
109+
110+
.. code-block:: md
111+
112+
>> I am the very model of a modern Major-General[br]
113+
I've information vegetable, animal, and mineral[br]
114+
I know the kings of England, and I quote the fights historical[br]
115+
From Marathon to Waterloo, in order categorical <<
116+
117+
Since this is understood as one line, this is the only way you can actually centre this paragraph.
118+
119+
.. caution::
120+
121+
Due to this difference in how text with ``[br]`` tags are processed, it is generally better to
122+
stick with the **Preserve Hard Line Breaks** setting enabled. It ensures a better correspondence
123+
between what you see in the editor and what output you get.
124+
125+
See also :ref:`docs_usage_formatting_shortcodes_break`.

novelwriter/formats/tokenizer.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -880,38 +880,40 @@ def tokenizeText(self) -> None:
880880
if nBlock[0] != BlockTyp.TEXT:
881881
# Next block is not text, so we add the buffer to blocks
882882
nLines = len(pLines)
883-
cStyle = pLines[0][4]
884-
if firstIndent and not (self._noIndent or cStyle & BlockFmt.ALIGNED):
885-
# If paragraph indentation is enabled, not temporarily
886-
# turned off, and the block is not aligned, we add the
887-
# text indentation flag
888-
cStyle |= BlockFmt.IND_T
883+
tFmt: T_Formats = []
884+
pTxt = ""
885+
cStyle = BlockFmt.NONE
889886

890887
if nLines == 1:
891-
# The paragraph contains a single line, so we just save
892-
# that directly to the blocks list. If justify is
893-
# enabled, and there is no alignment, we apply it.
894-
if doJustify and not cStyle & BlockFmt.ALIGNED:
895-
cStyle |= BlockFmt.JUSTIFY
896-
888+
# The paragraph contains a single line
889+
tFmt = pLines[0][3]
897890
pTxt = pLines[0][2].translate(transMapB)
898-
sBlocks.append((
899-
BlockTyp.TEXT, pLines[0][1], pTxt, pLines[0][3], cStyle
900-
))
891+
cStyle = pLines[0][4]
901892

902893
elif nLines > 1:
903894
# The paragraph contains multiple lines, so we need to
904895
# join them according to the line break policy, and
905896
# recompute all the formatting markers
906897
tTxt = ""
907-
tFmt: T_Formats = []
908898
for aBlock in pLines:
909899
tLen = len(tTxt)
910900
tTxt += f"{aBlock[2]}{lineSep}"
911901
tFmt.extend((p+tLen, fmt, key) for p, fmt, key in aBlock[3])
912902
cStyle |= aBlock[4]
913903

914904
pTxt = tTxt[:-1].translate(transMapB)
905+
906+
if nLines:
907+
isAligned = cStyle & BlockFmt.ALIGNED
908+
if firstIndent and not (self._noIndent or isAligned):
909+
# If paragraph indentation is enabled, not temporarily
910+
# turned off, and the block is not aligned, we add the
911+
# text indentation flag
912+
cStyle |= BlockFmt.IND_T
913+
914+
if doJustify and not isAligned:
915+
cStyle |= BlockFmt.JUSTIFY
916+
915917
sBlocks.append((
916918
BlockTyp.TEXT, pLines[0][1], pTxt, tFmt, cStyle
917919
))

tests/test_formats/test_fmt_tohtml.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ def testFmtToHtml_ConvertParagraphs(mockGUI):
147147
html._isNovel = True
148148
html._isFirst = True
149149

150-
# Paragraphs
151-
# ==========
152-
153150
# Text
154151
html._text = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n"
155152
html.tokenizeText()
@@ -280,6 +277,54 @@ def testFmtToHtml_ConvertParagraphs(mockGUI):
280277
)
281278

282279

280+
@pytest.mark.core
281+
def testFmtToHtml_Alignment(mockGUI):
282+
"""Test paragraph alignment in the ToHtml class."""
283+
project = NWProject()
284+
html = ToHtml(project)
285+
html.initDocument()
286+
287+
# Left
288+
html._text = "This is text <<\nspanning multiple\nlines"
289+
html.tokenizeText()
290+
html.doConvert()
291+
assert html._pages[-1] == (
292+
"<p style='text-align: left;'>This is text<br>spanning multiple<br>lines</p>\n"
293+
)
294+
295+
# Right
296+
html._text = ">> This is text\nspanning multiple\nlines"
297+
html.tokenizeText()
298+
html.doConvert()
299+
assert html._pages[-1] == (
300+
"<p style='text-align: right;'>This is text<br>spanning multiple<br>lines</p>\n"
301+
)
302+
303+
# Centre
304+
html._text = ">> This is text <<\nspanning multiple\nlines"
305+
html.tokenizeText()
306+
html.doConvert()
307+
assert html._pages[-1] == (
308+
"<p style='text-align: center;'>This is text<br>spanning multiple<br>lines</p>\n"
309+
)
310+
311+
# Left before Right
312+
html._text = ">> This is text\nspanning multiple <<\nlines"
313+
html.tokenizeText()
314+
html.doConvert()
315+
assert html._pages[-1] == (
316+
"<p style='text-align: left;'>This is text<br>spanning multiple<br>lines</p>\n"
317+
)
318+
319+
# Right before Centre
320+
html._text = ">> This is text <<\n>> spanning multiple\nlines"
321+
html.tokenizeText()
322+
html.doConvert()
323+
assert html._pages[-1] == (
324+
"<p style='text-align: right;'>This is text<br>spanning multiple<br>lines</p>\n"
325+
)
326+
327+
283328
@pytest.mark.core
284329
def testFmtToHtml_Dialog(mockGUI):
285330
"""Test paragraph formats in the ToHtml class."""

tests/test_formats/test_fmt_tokenizer.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,80 @@ def testFmtToken_Paragraphs(mockGUI):
10741074
]
10751075

10761076

1077+
@pytest.mark.core
1078+
def testFmtToken_BreakAlignIndent(mockGUI):
1079+
"""Test the splitting of paragraphs with alignment."""
1080+
project = NWProject()
1081+
tokens = BareTokenizer(project)
1082+
tokens._handle = TMH
1083+
1084+
for text in [
1085+
"This is text <<\nspanning multiple\nlines",
1086+
"This is text\nspanning multiple <<\nlines",
1087+
"This is text\nspanning multiple\nlines <<",
1088+
]:
1089+
# Preserve Breaks
1090+
tokens.setKeepLineBreaks(True)
1091+
tokens._text = text
1092+
tokens.tokenizeText()
1093+
assert tokens._blocks == [
1094+
(BlockTyp.TEXT, "", "This is text\nspanning multiple\nlines", [], BlockFmt.LEFT),
1095+
]
1096+
1097+
# Don't Preserve Breaks
1098+
tokens.setKeepLineBreaks(False)
1099+
tokens._text = text
1100+
tokens.tokenizeText()
1101+
assert tokens._blocks == [
1102+
(BlockTyp.TEXT, "", "This is text spanning multiple lines", [], BlockFmt.LEFT),
1103+
]
1104+
1105+
# With Justify
1106+
# This should disable justify
1107+
tokens.setKeepLineBreaks(True)
1108+
tokens.setJustify(True)
1109+
tokens._text = text
1110+
tokens.tokenizeText()
1111+
assert tokens._blocks == [
1112+
(BlockTyp.TEXT, "", "This is text\nspanning multiple\nlines", [], BlockFmt.LEFT),
1113+
]
1114+
1115+
# With Indent
1116+
# This should disable indent
1117+
tokens.setKeepLineBreaks(True)
1118+
tokens.setFirstLineIndent(True, 1.0, False)
1119+
tokens._text = text
1120+
tokens.tokenizeText()
1121+
assert tokens._blocks == [
1122+
(BlockTyp.TEXT, "", "This is text\nspanning multiple\nlines", [], BlockFmt.LEFT),
1123+
]
1124+
1125+
1126+
@pytest.mark.core
1127+
def testFmtToken_BreakJustify(mockGUI):
1128+
"""Test the of processing of justify with breaks."""
1129+
project = NWProject()
1130+
tokens = BareTokenizer(project)
1131+
tokens._handle = TMH
1132+
tokens.setJustify(True)
1133+
1134+
# Applied to all lines when breaks are preserved
1135+
tokens._text = "This is text\nspanning multiple\nlines"
1136+
tokens.setKeepLineBreaks(True)
1137+
tokens.tokenizeText()
1138+
assert tokens._blocks == [
1139+
(BlockTyp.TEXT, "", "This is text\nspanning multiple\nlines", [], BlockFmt.JUSTIFY),
1140+
]
1141+
1142+
# Turning off breaks should make no difference (see issue #2426)
1143+
tokens._text = "This is text\nspanning multiple\nlines"
1144+
tokens.setKeepLineBreaks(False)
1145+
tokens.tokenizeText()
1146+
assert tokens._blocks == [
1147+
(BlockTyp.TEXT, "", "This is text spanning multiple lines", [], BlockFmt.JUSTIFY),
1148+
]
1149+
1150+
10771151
@pytest.mark.core
10781152
def testFmtToken_TextFormat(mockGUI):
10791153
"""Test the tokenization of text formats in the Tokenizer class."""

tests/test_formats/test_fmt_toodt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def getStyle(styleName):
733733
'<office:text>'
734734
'<text:h text:style-name="Heading_20_2" text:outline-level="2">Scene</text:h>'
735735
'<text:p text:style-name="P7">Regular paragraph</text:p>'
736-
'<text:p text:style-name="Text_20_body">with<text:line-break />break</text:p>'
736+
'<text:p text:style-name="P7">with<text:line-break />break</text:p>'
737737
'<text:p text:style-name="Text_20_body">Left Align</text:p>'
738738
'</office:text>'
739739
)

0 commit comments

Comments
 (0)