From 7254f766d5e630eb3a6e674ae05fe2a05436b074 Mon Sep 17 00:00:00 2001
From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com>
Date: Thu, 28 Jan 2021 20:42:39 +0100
Subject: [PATCH] Remove div tags from HTML output and bring Tokenizer and
ToHtml tests back to 100%
---
nw/core/tohtml.py | 7 +-
tests/test_core/test_core_tohtml.py | 109 +++++++++++++++++++++++--
tests/test_core/test_core_tokenizer.py | 50 +++++++++++-
3 files changed, 152 insertions(+), 14 deletions(-)
diff --git a/nw/core/tohtml.py b/nw/core/tohtml.py
index 980ac6e6..1a5b345c 100644
--- a/nw/core/tohtml.py
+++ b/nw/core/tohtml.py
@@ -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 = "
%s
\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", " ")
+ bodyText = bodyText.replace("\t", " ").rstrip()
theHtml = (
"\n"
@@ -399,7 +400,7 @@ class ToHtml(Tokenizer):
))
retText += ", ".join(refTags)
- return "%s
\n" % retText
+ return retText
def _buildRegEx(self):
"""Build the regular expressions
diff --git a/tests/test_core/test_core_tohtml.py b/tests/test_core/test_core_tohtml.py
index 247e9bf5..e55b2980 100644
--- a/tests/test_core/test_core_tohtml.py
+++ b/tests/test_core/test_core_tohtml.py
@@ -20,8 +20,11 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see .
"""
+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") == (
- "\n"
+ "Tag: Jane"
)
assert theHtml._formatKeywords("char: Bod, Jane") == (
- ""
"
Characters: "
"
Bod, "
"
Jane"
- "
\n"
)
# Preview Mode
@@ -68,14 +69,12 @@ def testCoreToHtml_Format(dummyGUI):
assert theHtml._formatKeywords("") == ""
assert theHtml._formatKeywords("tag: Jane") == (
- "\n"
+ "Tag: Jane"
)
assert theHtml._formatKeywords("char: Bod, Jane") == (
- ""
"
Characters: "
"
Bod, "
"
Jane"
- "
\n"
)
# END Test testCoreToHtml_Format
@@ -200,8 +199,27 @@ def testCoreToHtml_Convert(dummyGUI):
theHtml.tokenizeText()
theHtml.doConvert()
assert theHtml.theResult == (
- "\n"
+ "Characters: "
+ "Bod, Jane
\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 == (
+ ""
+ "Chapter
\n"
+ ""
+ "Point of View: Bod"
+ "
\n"
+ ""
+ "Plot: Main"
+ "
\n"
+ ""
+ "Locations: Europe"
+ "
\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 = [
+ "My Novel
\nBy Jane Doh
\n",
+ "Chapter 1
\nThe text of chapter one.
\n",
+ "Scene 1
\nThe text of scene one.
\n",
+ "A Section
\nMore text in scene one.
\n",
+ "Chapter 2
\nThe text of chapter two.
\n",
+ "Scene 2
\nThe text of scene two.
\n",
+ "A Section
\n\tMore text in scene two.
\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=" ")
+ resText[6] = "A Section
\n More text in scene two.
\n"
+
+ # Check File
+ # ==========
+
+ theStyle = theHtml.getStyleSheet()
+ theStyle.append("article {width: 800px; margin: 40px auto;}")
+ htmlDoc = (
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "{bodyText:s}\n"
+ "\n"
+ "\n"
+ "\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 <brackets> & short–dash, long—dash …\n\n"
)
+ # Result Size
+ assert theHtml.getFullResultSize() == 83
+
# CSS
# ===
diff --git a/tests/test_core/test_core_tokenizer.py b/tests/test_core/test_core_tokenizer.py
index db6b6220..6cee910f 100644
--- a/tests/test_core/test_core_tokenizer.py
+++ b/tests/test_core/test_core_tokenizer.py
@@ -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()