# -*- coding: utf-8 -*-
"""novelWriter HTML Text Converter
novelWriter – HTML Text Converter
===================================
Extends the Tokenizer class to write HTML
File History:
Created: 2019-05-07 [0.0.1]
This file is a part of novelWriter
Copyright 2018–2020, 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 ",
self.FMT_D_E : "",
}
if self.isNovel and self.genMode != self.M_PREVIEW:
# For novel files for export, we bump the titles one level
# up as this is more useful for printing and word processor
# imports.
h1 = "h1 class='title'"
h2 = "h1"
h3 = "h2"
h4 = "h3"
else:
h1 = "h1"
h2 = "h2"
h3 = "h3"
h4 = "h4"
self.theResult = ""
thisPar = []
parStyle = None
tmpResult = []
hasHardBreak = False
for tType, tLine, tText, tFormat, tStyle in self.theTokens:
# Styles
aStyle = []
if tStyle is not None and self.cssStyles:
if tStyle & self.A_LEFT:
aStyle.append("text-align: left;")
if tStyle & self.A_RIGHT:
aStyle.append("text-align: right;")
if tStyle & self.A_CENTRE:
aStyle.append("text-align: center;")
if tStyle & self.A_JUSTIFY:
aStyle.append("text-align: justify;")
if tStyle & self.A_PBB:
aStyle.append("page-break-before: always;")
if tStyle & self.A_PBB_AV:
aStyle.append("page-break-before: avoid;")
if tStyle & self.A_PBB_NO:
aStyle.append("page-break-before: never;")
if tStyle & self.A_PBA:
aStyle.append("page-break-after: always;")
if tStyle & self.A_PBA_AV:
aStyle.append("page-break-after: avoid;")
if tStyle & self.A_PBA_NO:
aStyle.append("page-break-after: never;")
if len(aStyle) > 0:
hStyle = " style='%s'" % (" ".join(aStyle))
else:
hStyle = ""
if self.linkHeaders:
aNm = "" % tLine
else:
aNm = ""
# Process TextType
if tType == self.T_EMPTY:
if parStyle is None:
parStyle = ""
if hasHardBreak and self.cssStyles:
parClass = " class='break'"
else:
parClass = ""
if len(thisPar) > 0:
tTemp = "".join(thisPar)
tmpResult.append("
%s
\n" % (parStyle, parClass, tTemp.rstrip())) thisPar = [] parStyle = None hasHardBreak = False elif tType == self.T_TITLE: tHead = tText.replace(r"\\", "%s
\n" % tText) elif tType == self.T_SKIP: tmpResult.append("\n") elif tType == self.T_TEXT: tTemp = tText if parStyle is None: parStyle = hStyle for xPos, xLen, xFmt in reversed(tFormat): tTemp = tTemp[:xPos]+htmlTags[xFmt]+tTemp[xPos+xLen:] if tText.endswith(" "): thisPar.append(tTemp.rstrip()+"
Synopsis: %s
\n" % tText else: return "Synopsis: %s
\n" % tText def _formatComments(self, tText): """Apply HTML formatting to comments. """ if self.genMode == self.M_PREVIEW: return "%s
\n" % tText else: return "Comment: %s
\n" % tText def _formatKeywords(self, tText): """Apply HTML formatting to keywords. """ isValid, theBits, thePos = self.theParent.theIndex.scanThis("@"+tText) if not isValid or not theBits: return "" retText = "" refTags = [] if theBits[0] in nwLabels.KEY_NAME: retText += " " % nwLabels.KEY_NAME[theBits[0]] if len(theBits) > 1: if theBits[0] == nwKeyWords.TAG_KEY: retText += "%s" % ( theBits[1], theBits[1] ) else: if self.genMode == self.M_PREVIEW: for tTag in theBits[1:]: refTags.append("%s" % ( theBits[0][1:], tTag, tTag )) retText += ", ".join(refTags) else: for tTag in theBits[1:]: refTags.append("%s" % ( tTag, tTag )) retText += ", ".join(refTags) return "