Remove div tags from HTML output and bring Tokenizer and ToHtml tests back to 100%

This commit is contained in:
Veronica K. B. Olsen
2021-01-28 20:42:39 +01:00
parent 83e07ec76c
commit 7254f766d5
3 changed files with 152 additions and 14 deletions
+4 -3
View File
@@ -267,7 +267,8 @@ class ToHtml(Tokenizer):
tmpResult.append(self._formatComments(tText))
elif tType == self.T_KEYWORD and self.doKeywords:
tmpResult.append(self._formatKeywords(tText))
tTemp = "<p%s>%s</p>\n" % (hStyle, self._formatKeywords(tText))
tmpResult.append(tTemp)
self.theResult = "".join(tmpResult)
tmpResult = []
@@ -284,7 +285,7 @@ class ToHtml(Tokenizer):
theStyle = self.getStyleSheet()
theStyle.append("article {width: 800px; margin: 40px auto;}")
bodyText = "".join(self.fullHTML)
bodyText = bodyText.replace("\t", "&#09;")
bodyText = bodyText.replace("\t", "&#09;").rstrip()
theHtml = (
"<!DOCTYPE html>\n"
@@ -399,7 +400,7 @@ class ToHtml(Tokenizer):
))
retText += ", ".join(refTags)
return "<div>%s</div>\n" % retText
return retText
def _buildRegEx(self):
"""Build the regular expressions
+101 -8
View File
@@ -20,8 +20,11 @@ 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 os
import pytest
from tools import readFile
from nw.core import NWProject, NWIndex, ToHtml
@pytest.mark.core
@@ -44,14 +47,12 @@ def testCoreToHtml_Format(dummyGUI):
assert theHtml._formatKeywords("") == ""
assert theHtml._formatKeywords("tag: Jane") == (
"<div><span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a></div>\n"
"<span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a>"
)
assert theHtml._formatKeywords("char: Bod, Jane") == (
"<div>"
"<span class='tags'>Characters:</span> "
"<a href='#tag_Bod'>Bod</a>, "
"<a href='#tag_Jane'>Jane</a>"
"</div>\n"
)
# Preview Mode
@@ -68,14 +69,12 @@ def testCoreToHtml_Format(dummyGUI):
assert theHtml._formatKeywords("") == ""
assert theHtml._formatKeywords("tag: Jane") == (
"<div><span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a></div>\n"
"<span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a>"
)
assert theHtml._formatKeywords("char: Bod, Jane") == (
"<div>"
"<span class='tags'>Characters:</span> "
"<a href='#char=Bod'>Bod</a>, "
"<a href='#char=Jane'>Jane</a>"
"</div>\n"
)
# END Test testCoreToHtml_Format
@@ -200,8 +199,27 @@ def testCoreToHtml_Convert(dummyGUI):
theHtml.tokenizeText()
theHtml.doConvert()
assert theHtml.theResult == (
"<div><span class='tags'>Characters:</span> "
"<a href='#tag_Bod'>Bod</a>, <a href='#tag_Jane'>Jane</a></div>\n"
"<p><span class='tags'>Characters:</span> "
"<a href='#tag_Bod'>Bod</a>, <a href='#tag_Jane'>Jane</a></p>\n"
)
# Multiple Keywords
theHtml.setKeywords(True)
theHtml.theText = "## Chapter\n\n@pov: Bod\n@plot: Main\n@location: Europe\n\n"
theHtml.tokenizeText()
theHtml.doConvert()
assert theHtml.theResult == (
"<h2>"
"<a name='T000001'></a>Chapter</h2>\n"
"<p style='margin-bottom: 0;'>"
"<span class='tags'>Point of View:</span> <a href='#tag_Bod'>Bod</a>"
"</p>\n"
"<p style='margin-bottom: 0; margin-top: 0;'>"
"<span class='tags'>Plot:</span> <a href='#tag_Main'>Main</a>"
"</p>\n"
"<p style='margin-top: 0;'>"
"<span class='tags'>Locations:</span> <a href='#tag_Europe'>Europe</a>"
"</p>\n"
)
# Direct Tests
@@ -326,6 +344,78 @@ def testCoreToHtml_Convert(dummyGUI):
# END Test testCoreToHtml_Convert
def testCoreToHtml_Complex(dummyGUI, fncDir):
"""Test the ave method of the ToHtml class.
"""
theProject = NWProject(dummyGUI)
theHtml = ToHtml(theProject, dummyGUI)
# Build Project
# =============
docText = [
"# My Novel\n**By Jane Doh**\n",
"## Chapter 1\n\nThe text of chapter one.\n",
"### Scene 1\n\nThe text of scene one.\n",
"#### A Section\n\nMore text in scene one.\n",
"## Chapter 2\n\nThe text of chapter two.\n",
"### Scene 2\n\nThe text of scene two.\n",
"#### A Section\n\n\tMore text in scene two.\n",
]
resText = [
"<h1>My Novel</h1>\n<p><strong>By Jane Doh</strong></p>\n",
"<h2>Chapter 1</h2>\n<p>The text of chapter one.</p>\n",
"<h3>Scene 1</h3>\n<p>The text of scene one.</p>\n",
"<h4>A Section</h4>\n<p>More text in scene one.</p>\n",
"<h2>Chapter 2</h2>\n<p>The text of chapter two.</p>\n",
"<h3>Scene 2</h3>\n<p>The text of scene two.</p>\n",
"<h4>A Section</h4>\n<p>\tMore text in scene two.</p>\n",
]
for i in range(len(docText)):
theHtml.theText = docText[i]
theHtml.doAutoReplace()
theHtml.tokenizeText()
theHtml.doConvert()
assert theHtml.theResult == resText[i]
assert theHtml.fullHTML == resText
theHtml.replaceTabs(nSpaces=2, spaceChar="&nbsp;")
resText[6] = "<h4>A Section</h4>\n<p>&nbsp;&nbsp;More text in scene two.</p>\n"
# Check File
# ==========
theStyle = theHtml.getStyleSheet()
theStyle.append("article {width: 800px; margin: 40px auto;}")
htmlDoc = (
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<meta charset='utf-8'>\n"
"<title></title>\n"
"</head>\n"
"<style>\n"
"{htmlStyle:s}\n"
"</style>\n"
"<body>\n"
"<article>\n"
"{bodyText:s}\n"
"</article>\n"
"</body>\n"
"</html>\n"
).format(
htmlStyle = "\n".join(theStyle),
bodyText = "".join(resText).rstrip()
)
saveFile = os.path.join(fncDir, "outFile.htm")
theHtml.saveHTML5(saveFile)
assert readFile(saveFile) == htmlDoc
# END Test testCoreToHtml_Save
@pytest.mark.core
def testCoreToHtml_Methods(dummyGUI):
"""Test all the other methods of the ToHtml class.
@@ -365,6 +455,9 @@ def testCoreToHtml_Methods(dummyGUI):
"Text with &lt;brackets&gt; &amp; short&ndash;dash, long&mdash;dash &hellip;\n\n"
)
# Result Size
assert theHtml.getFullResultSize() == 83
# CSS
# ===
+47 -3
View File
@@ -38,6 +38,18 @@ def testCoreToken_Setters(dummyGUI):
assert theToken.fmtUnNum == "%title%"
assert theToken.fmtScene == "%title%"
assert theToken.fmtSection == "%title%"
assert theToken.textFont == "Serif"
assert theToken.textSize == 11
assert theToken.textFixed is False
assert theToken.lineHeight == 1.15
assert theToken.doJustify is False
assert theToken.marginTitle == (1.000, 0.500)
assert theToken.marginHead1 == (1.000, 0.500)
assert theToken.marginHead2 == (0.834, 0.500)
assert theToken.marginHead3 == (0.584, 0.500)
assert theToken.marginHead4 == (0.584, 0.500)
assert theToken.marginText == (0.000, 0.584)
assert theToken.marginMeta == (0.000, 0.584)
assert theToken.hideScene is False
assert theToken.hideSection is False
assert theToken.linkHeaders is False
@@ -45,7 +57,6 @@ def testCoreToken_Setters(dummyGUI):
assert theToken.doSynopsis is False
assert theToken.doComments is False
assert theToken.doKeywords is False
assert theToken.doJustify is False
# Set new values
theToken.setTitleFormat("T: %title%")
@@ -53,12 +64,21 @@ def testCoreToken_Setters(dummyGUI):
theToken.setUnNumberedFormat("U: %title%")
theToken.setSceneFormat("S: %title%", True)
theToken.setSectionFormat("X: %title%", True)
theToken.setFont("Monospace", 10, True)
theToken.setLineHeight(2)
theToken.setJustify(True)
theToken.setTitleMargins(2.0, 2.0)
theToken.setHead1Margins(2.0, 2.0)
theToken.setHead2Margins(2.0, 2.0)
theToken.setHead3Margins(2.0, 2.0)
theToken.setHead4Margins(2.0, 2.0)
theToken.setTextMargins(2.0, 2.0)
theToken.setMetaMargins(2.0, 2.0)
theToken.setLinkHeaders(True)
theToken.setBodyText(False)
theToken.setSynopsis(True)
theToken.setComments(True)
theToken.setKeywords(True)
theToken.setJustify(True)
# Check new values
assert theToken.fmtTitle == "T: %title%"
@@ -66,6 +86,18 @@ def testCoreToken_Setters(dummyGUI):
assert theToken.fmtUnNum == "U: %title%"
assert theToken.fmtScene == "S: %title%"
assert theToken.fmtSection == "X: %title%"
assert theToken.textFont == "Monospace"
assert theToken.textSize == 10
assert theToken.textFixed is True
assert theToken.lineHeight == 2.0
assert theToken.doJustify is True
assert theToken.marginTitle == (2.0, 2.0)
assert theToken.marginHead1 == (2.0, 2.0)
assert theToken.marginHead2 == (2.0, 2.0)
assert theToken.marginHead3 == (2.0, 2.0)
assert theToken.marginHead4 == (2.0, 2.0)
assert theToken.marginText == (2.0, 2.0)
assert theToken.marginMeta == (2.0, 2.0)
assert theToken.hideScene is True
assert theToken.hideSection is True
assert theToken.linkHeaders is True
@@ -73,7 +105,6 @@ def testCoreToken_Setters(dummyGUI):
assert theToken.doSynopsis is True
assert theToken.doComments is True
assert theToken.doKeywords is True
assert theToken.doJustify is True
# END Test testCoreToken_Setters
@@ -242,6 +273,19 @@ def testCoreToken_Tokenize(dummyGUI):
theToken.tokenizeText()
assert theToken.theMarkdown[-1] == "@char: Bod\n\n"
theToken.theText = "@pov: Bod\n@plot: Main\n@location: Europe\n"
theToken.tokenizeText()
styTop = Tokenizer.A_NONE | Tokenizer.A_Z_BTMMRG
styMid = Tokenizer.A_NONE | Tokenizer.A_Z_BTMMRG | Tokenizer.A_Z_TOPMRG
styBtm = Tokenizer.A_NONE | Tokenizer.A_Z_TOPMRG
assert theToken.theTokens == [
(Tokenizer.T_KEYWORD, 1, "pov: Bod", None, styTop),
(Tokenizer.T_KEYWORD, 2, "plot: Main", None, styMid),
(Tokenizer.T_KEYWORD, 3, "location: Europe", None, styBtm),
(Tokenizer.T_EMPTY, 3, "", None, Tokenizer.A_NONE),
]
assert theToken.theMarkdown[-1] == "@pov: Bod\n@plot: Main\n@location: Europe\n\n"
# Text
theToken.theText = "Some plain text\non two lines\n\n\n"
theToken.tokenizeText()