Add support for indented paragraphs in exports

This commit is contained in:
Veronica Berglyd Olsen
2021-06-10 21:38:58 +02:00
parent 80c4826253
commit e5477f314e
3 changed files with 62 additions and 30 deletions
+8
View File
@@ -78,6 +78,9 @@ class ToHtml(Tokenizer):
html entities replacement.
"""
# Control characters must always be replaced
# This affects alignment and indenting code, so the Tokenizer
# must take this into account when parsing for markup using
# angle brackets.
self._trMap = str.maketrans({
"<" : "&lt;",
">" : "&gt;",
@@ -181,6 +184,11 @@ class ToHtml(Tokenizer):
if tStyle & self.A_Z_TOPMRG:
aStyle.append("margin-top: 0;")
if tStyle & self.A_IND_L:
aStyle.append("margin-left: %dpx;" % self.mainConf.tabWidth)
if tStyle & self.A_IND_R:
aStyle.append("margin-right: %dpx;" % self.mainConf.tabWidth)
if len(aStyle) > 0:
hStyle = " style='%s'" % (" ".join(aStyle))
else:
+18 -11
View File
@@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import nw
import logging
import re
@@ -82,6 +83,7 @@ class Tokenizer():
self.theProject = theProject
self.theParent = theProject.theParent
self.mainConf = nw.CONFIG
# Data Variables
self.theText = "" # The raw text to be tokenized
@@ -97,14 +99,15 @@ class Tokenizer():
self.textFont = "Serif" # Output text font
self.textSize = 11 # Output text size
self.textFixed = False # Fixed width text
self.lineHeight = 1.15 # Line height
self.lineHeight = 1.15 # Line height in units of em
self.blockIndent = 4.00 # Block indent in units of em
self.doJustify = False # Justify text
self.doBodyText = True # Include body text
self.doSynopsis = False # Also process synopsis comments
self.doComments = False # Also process comments
self.doKeywords = False # Also process keywords like tags and references
## Title Margins
## Margins
self.marginTitle = (1.000, 0.500)
self.marginHead1 = (1.000, 0.500)
self.marginHead2 = (0.834, 0.500)
@@ -188,7 +191,11 @@ class Tokenizer():
return
def setLineHeight(self, lineHeight):
self.lineHeight = float(lineHeight)
self.lineHeight = min(max(float(lineHeight), 0.5), 5.0)
return
def setBlockIndent(self, blockIndent):
self.blockIndent = min(max(float(blockIndent), 0.0), 10.0)
return
def setJustify(self, doJustify):
@@ -451,29 +458,29 @@ class Tokenizer():
indRight = False
if aLine.startswith(">>"):
tagRight = True
aLine = aLine[2:].lstrip()
aLine = aLine[2:].lstrip(" ")
elif aLine.startswith("&gt;&gt;"):
tagRight = True
aLine = aLine[8:].lstrip()
aLine = aLine[8:].lstrip(" ")
elif aLine.startswith(">"):
indLeft = True
aLine = aLine[1:].lstrip()
aLine = aLine[1:].lstrip(" ")
elif aLine.startswith("&gt;"):
indLeft = True
aLine = aLine[4:].lstrip()
aLine = aLine[4:].lstrip(" ")
if aLine.endswith("<<"):
tagLeft = True
aLine = aLine[:-2].rstrip()
aLine = aLine[:-2].rstrip(" ")
elif aLine.endswith("&lt;&lt;"):
tagLeft = True
aLine = aLine[:-8].rstrip()
aLine = aLine[:-8].rstrip(" ")
elif aLine.endswith("<"):
indRight = True
aLine = aLine[:-1].rstrip()
aLine = aLine[:-1].rstrip(" ")
elif aLine.endswith("&lt;"):
indRight = True
aLine = aLine[:-4].rstrip()
aLine = aLine[:-4].rstrip(" ")
textAlign = defAlign
if tagLeft and tagRight:
+36 -19
View File
@@ -99,19 +99,20 @@ class ToOdt(Tokenizer):
self.headerText = ""
# Internal
self._fontFamily = "&apos;Liberation Sans&apos;"
self._fontPitch = "variable"
self._fSizeTitle = "30pt"
self._fSizeHead1 = "24pt"
self._fSizeHead2 = "20pt"
self._fSizeHead3 = "16pt"
self._fSizeHead4 = "14pt"
self._fSizeHead = "14pt"
self._fSizeText = "12pt"
self._lineHeight = "115%"
self._textAlign = "left"
self._dLanguage = "en"
self._dCountry = "GB"
self._fontFamily = "&apos;Liberation Sans&apos;"
self._fontPitch = "variable"
self._fSizeTitle = "30pt"
self._fSizeHead1 = "24pt"
self._fSizeHead2 = "20pt"
self._fSizeHead3 = "16pt"
self._fSizeHead4 = "14pt"
self._fSizeHead = "14pt"
self._fSizeText = "12pt"
self._lineHeight = "115%"
self._blockIndent = "1.693cm"
self._textAlign = "left"
self._dLanguage = "en"
self._dCountry = "GB"
## Text Margings in Units of em
self._mTopTitle = "0.423cm"
@@ -222,8 +223,9 @@ class ToOdt(Tokenizer):
self._colMetaTx = "#813709"
self._opaMetaTx = "100%"
self._lineHeight = f"{round(100 * self.lineHeight):d}%"
self._textAlign = "justify" if self.doJustify else "left"
self._lineHeight = f"{round(100 * self.lineHeight):d}%"
self._blockIndent = self._emToCm(self.blockIndent)
self._textAlign = "justify" if self.doJustify else "left"
# Document Header
# ===============
@@ -356,6 +358,11 @@ class ToOdt(Tokenizer):
if tStyle & self.A_Z_TOPMRG:
oStyle.setMarginTop("0.000cm")
if tStyle & self.A_IND_L:
oStyle.setMarginLeft(self._blockIndent)
if tStyle & self.A_IND_R:
oStyle.setMarginRight(self._blockIndent)
# Process Text Types
if tType == self.T_EMPTY:
if len(thisPar) > 1 and parStyle is not None:
@@ -515,7 +522,7 @@ class ToOdt(Tokenizer):
def _formatKeywords(self, tText):
"""Apply formatting to keywords.
"""
isValid, theBits, thePos = self.theParent.theIndex.scanThis("@"+tText)
isValid, theBits, _ = self.theParent.theIndex.scanThis("@"+tText)
if not isValid or not theBits:
return ""
@@ -1008,6 +1015,8 @@ class ODTParagraphStyle():
self._pAttr = {
"margin-top": ["fo", None],
"margin-bottom": ["fo", None],
"margin-left": ["fo", None],
"margin-right": ["fo", None],
"line-height": ["fo", None],
"text-align": ["fo", None],
"break-before": ["fo", None],
@@ -1064,6 +1073,14 @@ class ODTParagraphStyle():
self._pAttr["margin-bottom"][1] = str(theValue)
return
def setMarginLeft(self, theValue):
self._pAttr["margin-left"][1] = str(theValue)
return
def setMarginRight(self, theValue):
self._pAttr["margin-right"][1] = str(theValue)
return
def setLineHeight(self, theValue):
self._pAttr["line-height"][1] = str(theValue)
return
@@ -1142,13 +1159,13 @@ class ODTParagraphStyle():
"""Check if there are new settings in refStyle that differ from
those in the current object.
"""
for aName, (aNm, aVal) in refStyle._mAttr.items():
for aName, (_, aVal) in refStyle._mAttr.items():
if aVal is not None and aVal != self._mAttr[aName][1]:
return True
for aName, (aNm, aVal) in refStyle._pAttr.items():
for aName, (_, aVal) in refStyle._pAttr.items():
if aVal is not None and aVal != self._pAttr[aName][1]:
return True
for aName, (aNm, aVal) in refStyle._tAttr.items():
for aName, (_, aVal) in refStyle._tAttr.items():
if aVal is not None and aVal != self._tAttr[aName][1]:
return True
return False