コード例 #1
0
def testCoreToOdt_SaveFlat(mockGUI, fncDir, outDir, refDir):
    """Test the document save functions.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)

    theDoc = ToOdt(theProject, isFlat=True)
    theDoc._isNovel = True
    assert theDoc.setLanguage(None) is False
    assert theDoc.setLanguage("nb_NO") is True
    theDoc.setColourHeaders(True)

    theDoc._theText = ("## Chapter One\n\n"
                       "Text\n\n"
                       "## Chapter Two\n\n"
                       "Text\n\n")
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()

    flatFile = os.path.join(fncDir, "document.fodt")
    testFile = os.path.join(outDir, "coreToOdt_SaveFlat_document.fodt")
    compFile = os.path.join(refDir, "coreToOdt_SaveFlat_document.fodt")

    theDoc.saveFlatXML(flatFile)
    assert os.path.isfile(flatFile)

    copyfile(flatFile, testFile)
    assert cmpFiles(testFile, compFile, [4, 5])
コード例 #2
0
def testCoreToOdt_Init(mockGUI):
    """Test initialisation of the ODT document.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)

    # Flat Doc
    # ========

    theDoc = ToOdt(theProject, isFlat=True)
    theDoc.initDocument()

    # Document XML
    assert theDoc._dFlat is not None
    assert theDoc._dCont is None
    assert theDoc._dMeta is None
    assert theDoc._dStyl is None

    # Content XML
    assert theDoc._xMeta is not None
    assert theDoc._xFont is not None
    assert theDoc._xFnt2 is None
    assert theDoc._xStyl is not None
    assert theDoc._xAuto is not None
    assert theDoc._xAut2 is None
    assert theDoc._xMast is not None
    assert theDoc._xBody is not None
    assert theDoc._xText is not None

    # ODT Doc
    # =======

    theDoc = ToOdt(theProject, isFlat=False)
    theDoc.initDocument()

    # Document XML
    assert theDoc._dFlat is None
    assert theDoc._dCont is not None
    assert theDoc._dMeta is not None
    assert theDoc._dStyl is not None

    # Content XML
    assert theDoc._xMeta is not None
    assert theDoc._xFont is not None
    assert theDoc._xFnt2 is not None
    assert theDoc._xStyl is not None
    assert theDoc._xAuto is not None
    assert theDoc._xAut2 is not None
    assert theDoc._xMast is not None
    assert theDoc._xBody is not None
    assert theDoc._xText is not None
コード例 #3
0
def testCoreToMarkdown_Format(mockGUI):
    """Test all the formatters for the ToMarkdown class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theMD = ToMarkdown(theProject)

    assert theMD._formatKeywords("", theMD.A_NONE) == ""
    assert theMD._formatKeywords("tag: Jane",
                                 theMD.A_NONE) == "**Tag:** Jane\n\n"
    assert theMD._formatKeywords("tag: Jane, John",
                                 theMD.A_NONE) == "**Tag:** Jane, John\n\n"
    assert theMD._formatKeywords("tag: Jane",
                                 theMD.A_Z_BTMMRG) == "**Tag:** Jane  \n"
コード例 #4
0
def testCoreToMarkdown_ConvertDirect(mockGUI):
    """Test the converter directly using the ToMarkdown class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theMD = ToMarkdown(theProject)

    theMD._isNovel = True
    theMD._isNote = False

    # Special Titles
    # ==============

    # Title
    theMD._theTokens = [
        (theMD.T_TITLE, 1, "A Title", None, theMD.A_PBB | theMD.A_CENTRE),
        (theMD.T_EMPTY, 1, "", None, theMD.A_NONE),
    ]
    theMD.doConvert()
    assert theMD.theResult == "# A Title\n\n"

    # Unnumbered
    theMD._theTokens = [
        (theMD.T_UNNUM, 1, "Prologue", None, theMD.A_PBB),
        (theMD.T_EMPTY, 1, "", None, theMD.A_NONE),
    ]
    theMD.doConvert()
    assert theMD.theResult == "## Prologue\n\n"

    # Separators
    # ==========

    # Separator
    theMD._theTokens = [
        (theMD.T_SEP, 1, "* * *", None, theMD.A_CENTRE),
        (theMD.T_EMPTY, 1, "", None, theMD.A_NONE),
    ]
    theMD.doConvert()
    assert theMD.theResult == "* * *\n\n"

    # Skip
    theMD._theTokens = [
        (theMD.T_SKIP, 1, "", None, theMD.A_NONE),
        (theMD.T_EMPTY, 1, "", None, theMD.A_NONE),
    ]
    theMD.doConvert()
    assert theMD.theResult == "\n\n\n"
コード例 #5
0
def testCoreToOdt_Format(mockGUI):
    """Test the formatters for the ToOdt class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theDoc = ToOdt(theProject, isFlat=True)

    assert theDoc._formatSynopsis("synopsis text") == (
        "**Synopsis:** synopsis text", "_B         b_              ")
    assert theDoc._formatComments("comment text") == (
        "**Comment:** comment text", "_B        b_             ")

    assert theDoc._formatKeywords("") == ""
    assert theDoc._formatKeywords("tag: Jane") == ("**Tag:** Jane",
                                                   "_B    b_     ")
    assert theDoc._formatKeywords("char: Bod, Jane") == (
        "**Characters:** Bod, Jane", "_B           b_          ")
コード例 #6
0
def testCoreToOdt_ConvertDirect(mockGUI):
    """Test the converter directly using the ToOdt class to reach some
    otherwise hard to reach conditions.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theDoc = ToOdt(theProject, isFlat=True)

    theDoc._isNovel = True

    # Justified
    theDoc = ToOdt(theProject, isFlat=True)
    theDoc._theTokens = [
        (theDoc.T_TEXT, 1, "This is a paragraph", [], theDoc.A_JUSTIFY),
        (theDoc.T_EMPTY, 1, "", None, theDoc.A_NONE),
    ]
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert ('<style:style style:name="P1" style:family="paragraph" '
            'style:parent-style-name="Text_20_body">'
            '<style:paragraph-properties fo:text-align="justify"/>'
            '</style:style>') in xmlToText(theDoc._xAuto)
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="P1">This is a paragraph</text:p>'
        '</office:text>')

    # Page Break After
    theDoc = ToOdt(theProject, isFlat=True)
    theDoc._theTokens = [
        (theDoc.T_TEXT, 1, "This is a paragraph", [], theDoc.A_PBA),
        (theDoc.T_EMPTY, 1, "", None, theDoc.A_NONE),
    ]
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert ('<style:style style:name="P1" style:family="paragraph" '
            'style:parent-style-name="Text_20_body">'
            '<style:paragraph-properties fo:break-after="page"/>'
            '</style:style>') in xmlToText(theDoc._xAuto)
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="P1">This is a paragraph</text:p>'
        '</office:text>')
コード例 #7
0
def testCoreToHtml_Format(mockGUI):
    """Test all the formatters for the ToHtml class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theHtml = ToHtml(theProject)

    # Export Mode
    # ===========

    assert theHtml._formatSynopsis("synopsis text") == (
        "<p class='synopsis'><strong>Synopsis:</strong> synopsis text</p>\n")
    assert theHtml._formatComments("comment text") == (
        "<p class='comment'><strong>Comment:</strong> comment text</p>\n")

    assert theHtml._formatKeywords("") == ""
    assert theHtml._formatKeywords("tag: Jane") == (
        "<span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a>")
    assert theHtml._formatKeywords("char: Bod, Jane") == (
        "<span class='tags'>Characters:</span> "
        "<a href='#tag_Bod'>Bod</a>, "
        "<a href='#tag_Jane'>Jane</a>")

    # Preview Mode
    # ============

    theHtml.setPreview(True, True)

    assert theHtml._formatSynopsis("synopsis text") == (
        "<p class='comment'><span class='synopsis'>Synopsis:</span> synopsis text</p>\n"
    )
    assert theHtml._formatComments("comment text") == (
        "<p class='comment'>comment text</p>\n")

    assert theHtml._formatKeywords("") == ""
    assert theHtml._formatKeywords("tag: Jane") == (
        "<span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a>")
    assert theHtml._formatKeywords("char: Bod, Jane") == (
        "<span class='tags'>Characters:</span> "
        "<a href='#char=Bod'>Bod</a>, "
        "<a href='#char=Jane'>Jane</a>")
コード例 #8
0
def testCoreToHtml_ConvertFormat(mockGUI):
    """Test the tokenizer and converter chain using the ToHtml class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theHtml = ToHtml(theProject)

    # Novel Files Headers
    # ===================

    theHtml._isNovel = True
    theHtml._isNote = False
    theHtml._isFirst = True

    # Header 1
    theHtml._theText = "# Partition\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' style='text-align: center;'>Partition</h1>\n")

    # Header 2
    theHtml._theText = "## Chapter Title\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 style='page-break-before: always;'>Chapter Title</h1>\n")

    # Header 3
    theHtml._theText = "### Scene Title\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h2>Scene Title</h2>\n"

    # Header 4
    theHtml._theText = "#### Section Title\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h3>Section Title</h3>\n"

    # Title
    theHtml._theText = "#! Title\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' style='text-align: center;'>Title</h1>\n")

    # Unnumbered
    theHtml._theText = "##! Prologue\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h1 style='page-break-before: always;'>Prologue</h1>\n"

    # Note Files Headers
    # ==================

    theHtml._isNovel = False
    theHtml._isNote = True
    theHtml._isFirst = True
    theHtml.setLinkHeaders(True)

    # Header 1
    theHtml._theText = "# Heading One\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h1><a name='T000001'></a>Heading One</h1>\n"

    # Header 2
    theHtml._theText = "## Heading Two\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h2><a name='T000001'></a>Heading Two</h2>\n"

    # Header 3
    theHtml._theText = "### Heading Three\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h3><a name='T000001'></a>Heading Three</h3>\n"

    # Header 4
    theHtml._theText = "#### Heading Four\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h4><a name='T000001'></a>Heading Four</h4>\n"

    # Title
    theHtml._theText = "#! Heading One\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 style='text-align: center;'><a name='T000001'></a>Heading One</h1>\n"
    )

    # Unnumbered
    theHtml._theText = "##! Heading Two\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == "<h2><a name='T000001'></a>Heading Two</h2>\n"

    # Paragraphs
    # ==========

    # Text
    theHtml._theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<p>Some <strong>nested bold and <em>italic</em> and "
        "<del>strikethrough</del> text</strong> here</p>\n")

    # Text w/Hard Break
    theHtml._theText = "Line one  \nLine two  \nLine three\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<p class='break'>Line one<br/>Line two<br/>Line three</p>\n")

    # Synopsis
    theHtml._theText = "%synopsis: The synopsis ...\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == ""

    theHtml.setSynopsis(True)
    theHtml._theText = "%synopsis: The synopsis ...\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<p class='synopsis'><strong>Synopsis:</strong> The synopsis ...</p>\n"
    )

    # Comment
    theHtml._theText = "% A comment ...\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == ""

    theHtml.setComments(True)
    theHtml._theText = "% A comment ...\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<p class='comment'><strong>Comment:</strong> A comment ...</p>\n")

    # Keywords
    theHtml._theText = "@char: Bod, Jane\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == ""

    theHtml.setKeywords(True)
    theHtml._theText = "@char: Bod, Jane\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<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")

    # Preview Mode
    # ============

    theHtml.setPreview(True, True)

    # Text (HTML4)
    theHtml._theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n"
    theHtml.tokenizeText()
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<p>Some <b>nested bold and <i>italic</i> and "
        "<span style='text-decoration: line-through;'>strikethrough</span> "
        "text</b> here</p>\n")
コード例 #9
0
def testCoreToHtml_ConvertDirect(mockGUI):
    """Test the converter directly using the ToHtml class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theHtml = ToHtml(theProject)

    theHtml._isNovel = True
    theHtml._isNote = False
    theHtml.setLinkHeaders(True)

    # Special Titles
    # ==============

    # Title
    theHtml._theTokens = [
        (theHtml.T_TITLE, 1, "A Title", None,
         theHtml.A_PBB | theHtml.A_CENTRE),
        (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' style='text-align: center; page-break-before: always;'>"
        "<a name='T000001'></a>A Title</h1>\n")

    # Unnumbered
    theHtml._theTokens = [
        (theHtml.T_UNNUM, 1, "Prologue", None, theHtml.A_PBB),
        (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == ("<h1 style='page-break-before: always;'>"
                                 "<a name='T000001'></a>Prologue</h1>\n")

    # Separators
    # ==========

    # Separator
    theHtml._theTokens = [
        (theHtml.T_SEP, 1, "* * *", None, theHtml.A_CENTRE),
        (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == "<p class='sep' style='text-align: center;'>* * *</p>\n"

    # Skip
    theHtml._theTokens = [
        (theHtml.T_SKIP, 1, "", None, theHtml.A_NONE),
        (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == "<p class='skip'>&nbsp;</p>\n"

    # Alignment
    # =========

    theHtml.setLinkHeaders(False)

    # Align Left
    theHtml.setStyles(False)
    theHtml._theTokens = [
        (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_LEFT),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == ("<h1 class='title'>A Title</h1>\n")

    theHtml.setStyles(True)

    # Align Left
    theHtml._theTokens = [
        (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_LEFT),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' style='text-align: left;'>A Title</h1>\n")

    # Align Right
    theHtml._theTokens = [
        (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_RIGHT),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' style='text-align: right;'>A Title</h1>\n")

    # Align Centre
    theHtml._theTokens = [
        (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_CENTRE),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' style='text-align: center;'>A Title</h1>\n")

    # Align Justify
    theHtml._theTokens = [
        (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_JUSTIFY),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' style='text-align: justify;'>A Title</h1>\n")

    # Page Break
    # ==========

    # Page Break Always
    theHtml._theTokens = [
        (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_PBB | theHtml.A_PBA),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<h1 class='title' "
        "style='page-break-before: always; page-break-after: always;'>A Title</h1>\n"
    )

    # Indent
    # ======

    # Indent Left
    theHtml._theTokens = [
        (theHtml.T_TEXT, 1, "Some text ...", [], theHtml.A_IND_L),
        (theHtml.T_EMPTY, 2, "", None, theHtml.A_NONE),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<p style='margin-left: 40px;'>Some text ...</p>\n")

    # Indent Right
    theHtml._theTokens = [
        (theHtml.T_TEXT, 1, "Some text ...", [], theHtml.A_IND_R),
        (theHtml.T_EMPTY, 2, "", None, theHtml.A_NONE),
    ]
    theHtml.doConvert()
    assert theHtml.theResult == (
        "<p style='margin-right: 40px;'>Some text ...</p>\n")
コード例 #10
0
def testCoreToOdt_SaveFull(mockGUI, fncDir, outDir, refDir):
    """Test the document save functions.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)

    theDoc = ToOdt(theProject, isFlat=False)
    theDoc._isNovel = True

    theDoc._theText = ("## Chapter One\n\n"
                       "Text\n\n"
                       "## Chapter Two\n\n"
                       "Text\n\n")
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()

    fullFile = os.path.join(fncDir, "document.odt")

    theDoc.saveOpenDocText(fullFile)
    assert os.path.isfile(fullFile)
    assert zipfile.is_zipfile(fullFile)

    maniFile = os.path.join(outDir, "coreToOdt_SaveFull_manifest.xml")
    settFile = os.path.join(outDir, "coreToOdt_SaveFull_settings.xml")
    contFile = os.path.join(outDir, "coreToOdt_SaveFull_content.xml")
    metaFile = os.path.join(outDir, "coreToOdt_SaveFull_meta.xml")
    stylFile = os.path.join(outDir, "coreToOdt_SaveFull_styles.xml")

    maniComp = os.path.join(refDir, "coreToOdt_SaveFull_manifest.xml")
    settComp = os.path.join(refDir, "coreToOdt_SaveFull_settings.xml")
    contComp = os.path.join(refDir, "coreToOdt_SaveFull_content.xml")
    metaComp = os.path.join(refDir, "coreToOdt_SaveFull_meta.xml")
    stylComp = os.path.join(refDir, "coreToOdt_SaveFull_styles.xml")

    extaxtTo = os.path.join(outDir, "coreToOdt_SaveFull")

    with zipfile.ZipFile(fullFile, mode="r") as theZip:
        theZip.extract("META-INF/manifest.xml", extaxtTo)
        theZip.extract("settings.xml", extaxtTo)
        theZip.extract("content.xml", extaxtTo)
        theZip.extract("meta.xml", extaxtTo)
        theZip.extract("styles.xml", extaxtTo)

    maniOut = os.path.join(outDir, "coreToOdt_SaveFull", "META-INF",
                           "manifest.xml")
    settOut = os.path.join(outDir, "coreToOdt_SaveFull", "settings.xml")
    contOut = os.path.join(outDir, "coreToOdt_SaveFull", "content.xml")
    metaOut = os.path.join(outDir, "coreToOdt_SaveFull", "meta.xml")
    stylOut = os.path.join(outDir, "coreToOdt_SaveFull", "styles.xml")

    def prettifyXml(inFile, outFile):
        with open(outFile, mode="wb") as fileStream:
            fileStream.write(
                etree.tostring(etree.parse(inFile),
                               pretty_print=True,
                               encoding="utf-8",
                               xml_declaration=True))

    prettifyXml(maniOut, maniFile)
    prettifyXml(settOut, settFile)
    prettifyXml(contOut, contFile)
    prettifyXml(metaOut, metaFile)
    prettifyXml(stylOut, stylFile)

    assert cmpFiles(maniFile, maniComp)
    assert cmpFiles(settFile, settComp)
    assert cmpFiles(contFile, contComp)
    assert cmpFiles(metaFile, metaComp, [4, 5])
    assert cmpFiles(stylFile, stylComp)
コード例 #11
0
def testCoreToOdt_Convert(mockGUI):
    """Test the converter of the ToOdt class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theDoc = ToOdt(theProject, isFlat=True)

    theDoc._isNovel = True

    def getStyle(styleName):
        for aSet in theDoc._autoPara.values():
            if aSet[0] == styleName:
                return aSet[1]
        return None

    # Headers
    # =======

    # Header 1
    theDoc._theText = "# Title\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="P1" text:outline-level="1">Title</text:h>'
        '</office:text>')

    # Header 2
    theDoc._theText = "## Chapter\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="P2" text:outline-level="2">Chapter</text:h>'
        '</office:text>')

    # Header 3
    theDoc._theText = "### Scene\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
        '</office:text>')

    # Header 4
    theDoc._theText = "#### Section\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="Heading_20_4" text:outline-level="4">Section</text:h>'
        '</office:text>')

    # Title
    theDoc._theText = "#! Title\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(
        theDoc._xText) == ('<office:text>'
                           '<text:h text:style-name="Title">Title</text:h>'
                           '</office:text>')

    # Unnumbered chapter
    theDoc._theText = "##! Prologue\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="P2" text:outline-level="2">Prologue</text:h>'
        '</office:text>')

    # Paragraphs
    # ==========

    # Nested Text
    theDoc._theText = "Some ~~nested **bold** and _italics_ text~~ text."
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="Text_20_body">Some '
        '<text:span text:style-name="T1">nested </text:span>'
        '<text:span text:style-name="T2">bold</text:span>'
        '<text:span text:style-name="T1"> and </text:span>'
        '<text:span text:style-name="T3">italics</text:span>'
        '<text:span text:style-name="T1"> text</text:span> text.</text:p>'
        '</office:text>')

    # Hard Break
    theDoc._theText = "Some text.\nNext line\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="Text_20_body">Some text.<text:line-break/>Next line</text:p>'
        '</office:text>')

    # Tab
    theDoc._theText = "\tItem 1\tItem 2\n"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="Text_20_body"><text:tab/>Item 1<text:tab/>Item 2</text:p>'
        '</office:text>')

    # Tab in Format
    theDoc._theText = "Some **bold\ttext**"
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="Text_20_body">Some <text:span text:style-name="T4">'
        'bold<text:tab/>text</text:span></text:p>'
        '</office:text>')

    # Multiple Spaces
    theDoc._theText = ("### Scene\n\n"
                       "Hello World\n\n"
                       "Hello  World\n\n"
                       "Hello   World\n\n")
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
        '<text:p text:style-name="Text_20_body">Hello World</text:p>'
        '<text:p text:style-name="Text_20_body">Hello <text:s/>World</text:p>'
        '<text:p text:style-name="Text_20_body">Hello <text:s text:c="2"/>World</text:p>'
        '</office:text>')

    # Synopsis, Comment, Keywords
    theDoc._theText = ("### Scene\n\n"
                       "@pov: Jane\n\n"
                       "% synopsis: So it begins\n\n"
                       "% a plain comment\n\n")
    theDoc.setSynopsis(True)
    theDoc.setComments(True)
    theDoc.setKeywords(True)
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
        '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">'
        'Point of View:</text:span> Jane</text:p>'
        '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">'
        'Synopsis:</text:span> So it begins</text:p>'
        '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">'
        'Comment:</text:span> a plain comment</text:p>'
        '</office:text>')

    # Scene Separator
    theDoc._theText = "### Scene One\n\nText\n\n### Scene Two\n\nText"
    theDoc.setSceneFormat("* * *", False)
    theDoc.tokenizeText()
    theDoc.doHeaders()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="P3">* * *</text:p>'
        '<text:p text:style-name="Text_20_body">Text</text:p>'
        '<text:p text:style-name="P3">* * *</text:p>'
        '<text:p text:style-name="Text_20_body">Text</text:p>'
        '</office:text>')

    # Scene Break
    theDoc._theText = "### Scene One\n\nText\n\n### Scene Two\n\nText"
    theDoc.setSceneFormat("", False)
    theDoc.tokenizeText()
    theDoc.doHeaders()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:p text:style-name="Text_20_body"></text:p>'
        '<text:p text:style-name="Text_20_body">Text</text:p>'
        '<text:p text:style-name="Text_20_body"></text:p>'
        '<text:p text:style-name="Text_20_body">Text</text:p>'
        '</office:text>')

    # Paragraph Styles
    theDoc._theText = ("### Scene\n\n"
                       "@pov: Jane\n"
                       "@char: John\n"
                       "@plot: Main\n\n"
                       ">> Right align\n\n"
                       "Left Align <<\n\n"
                       ">> Centered <<\n\n"
                       "> Left indent\n\n"
                       "Right indent <\n\n")
    theDoc.setKeywords(True)
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
        '<text:p text:style-name="P4"><text:span text:style-name="T4">'
        'Point of View:</text:span> Jane</text:p>'
        '<text:p text:style-name="P5"><text:span text:style-name="T4">'
        'Characters:</text:span> John</text:p>'
        '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">'
        'Plot:</text:span> Main</text:p>'
        '<text:p text:style-name="P6">Right align</text:p>'
        '<text:p text:style-name="Text_20_body">Left Align</text:p>'
        '<text:p text:style-name="P3">Centered</text:p>'
        '<text:p text:style-name="P7">Left indent</text:p>'
        '<text:p text:style-name="P8">Right indent</text:p>'
        '</office:text>')
    assert getStyle("P4")._pAttr["margin-bottom"] == ["fo", "0.000cm"]
    assert getStyle("P5")._pAttr["margin-bottom"] == ["fo", "0.000cm"]
    assert getStyle("P5")._pAttr["margin-top"] == ["fo", "0.000cm"]
    assert getStyle("P6")._pAttr["text-align"] == ["fo", "right"]
    assert getStyle("P3")._pAttr["text-align"] == ["fo", "center"]
    assert getStyle("P7")._pAttr["margin-left"] == ["fo", "1.693cm"]
    assert getStyle("P8")._pAttr["margin-right"] == ["fo", "1.693cm"]

    # Justified
    theDoc._theText = ("### Scene\n\n"
                       "Regular paragraph\n\n"
                       "with\nbreak\n\n"
                       "Left Align <<\n\n")
    theDoc.setJustify(True)
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
        '<text:p text:style-name="Text_20_body">Regular paragraph</text:p>'
        '<text:p text:style-name="P9">with<text:line-break/>break</text:p>'
        '<text:p text:style-name="P9">Left Align</text:p>'
        '</office:text>')
    assert getStyle("P9")._pAttr["text-align"] == ["fo", "left"]

    # Page Breaks
    theDoc._theText = ("## Chapter One\n\n"
                       "Text\n\n"
                       "## Chapter Two\n\n"
                       "Text\n\n")
    theDoc.tokenizeText()
    theDoc.initDocument()
    theDoc.doConvert()
    theDoc.closeDocument()
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        '<office:text>'
        '<text:h text:style-name="P2" text:outline-level="2">Chapter One</text:h>'
        '<text:p text:style-name="Text_20_body">Text</text:p>'
        '<text:h text:style-name="P2" text:outline-level="2">Chapter Two</text:h>'
        '<text:p text:style-name="Text_20_body">Text</text:p>'
        '</office:text>')
コード例 #12
0
def testCoreToOdt_TextFormatting(mockGUI):
    """Test formatting of paragraphs.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theDoc = ToOdt(theProject, isFlat=True)

    theDoc.initDocument()
    assert xmlToText(theDoc._xText) == "<office:text/>"

    # Paragraph Style
    # ===============
    oStyle = ODTParagraphStyle()

    assert theDoc._paraStyle("stuff", oStyle) == "Standard"
    assert theDoc._paraStyle("Text_20_body", oStyle) == "Text_20_body"

    # Create new para style
    oStyle.setTextAlign("center")
    assert theDoc._paraStyle("Text_20_body", oStyle) == "P1"

    # Return the same style on second call
    assert theDoc._paraStyle("Text_20_body", oStyle) == "P1"

    assert list(theDoc._mainPara.keys()) == [
        "Text_20_body", "Text_20_Meta", "Title", "Heading_20_1",
        "Heading_20_2", "Heading_20_3", "Heading_20_4", "Header"
    ]

    theKey = "071d6b2e4764749f8c78d3c1ab9099fa04c07d2d53fd3de61eb1bdf1cb4845c3"
    assert theDoc._autoPara[theKey][0] == "P1"
    assert isinstance(theDoc._autoPara[theKey][1], ODTParagraphStyle)

    # Paragraph Formatting
    # ====================
    oStyle = ODTParagraphStyle()

    # No Text
    theDoc.initDocument()
    theDoc._addTextPar("Standard", oStyle, "")
    assert xmlToText(
        theDoc._xText) == ("<office:text>"
                           "<text:p text:style-name=\"Standard\"></text:p>"
                           "</office:text>")

    # No Format
    theDoc.initDocument()
    theDoc._addTextPar("Standard", oStyle, "Hello World")
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        "<office:text>"
        "<text:p text:style-name=\"Standard\">Hello World</text:p>"
        "</office:text>")

    # Heading Level None
    theDoc.initDocument()
    theDoc._addTextPar("Standard", oStyle, "Hello World", isHead=True)
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        "<office:text>"
        "<text:h text:style-name=\"Standard\">Hello World</text:h>"
        "</office:text>")

    # Heading Level 1
    theDoc.initDocument()
    theDoc._addTextPar("Standard",
                       oStyle,
                       "Hello World",
                       isHead=True,
                       oLevel="1")
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        "<office:text>"
        "<text:h text:style-name=\"Standard\" text:outline-level=\"1\">Hello World</text:h>"
        "</office:text>")

    # Formatted Text
    theDoc.initDocument()
    theTxt = "A **few** _words_ from ~~our~~ sponsor"
    theFmt = "  _B   b_ I     i      _S   s_        "
    theDoc._addTextPar("Standard", oStyle, theTxt, theFmt=theFmt)
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        "<office:text>"
        "<text:p text:style-name=\"Standard\">A <text:span text:style-name=\"T1\">few</text:span> "
        "<text:span text:style-name=\"T2\">words</text:span> from <text:span text:style-name=\"T3"
        "\">our</text:span> sponsor</text:p>"
        "</office:text>")

    # Incorrectly Formatted Text
    theDoc.initDocument()
    theTxt = "A **few** _wordsXXX"
    theFmt = "  _b   b_ I     XXX"
    theDoc._addTextPar("Standard", oStyle, theTxt, theFmt=theFmt)
    assert theDoc.getErrors() == ["Unknown format tag encountered"]
    assert xmlToText(theDoc._xText) == (
        "<office:text>"
        "<text:p text:style-name=\"Standard\">"
        "A few <text:span text:style-name=\"T2\">words</text:span>"
        "</text:p>"
        "</office:text>")

    # Formatted Text
    theDoc.initDocument()
    theTxt = "Hello\n\tWorld"
    theFmt = "            "
    theDoc._addTextPar("Standard", oStyle, theTxt, theFmt=theFmt)
    assert theDoc.getErrors() == []
    assert xmlToText(theDoc._xText) == (
        "<office:text>"
        "<text:p text:style-name=\"Standard\">Hello<text:line-break/><text:tab/>World</text:p>"
        "</office:text>")
コード例 #13
0
def testCoreToMarkdown_ConvertFormat(mockGUI):
    """Test the tokenizer and converter chain using the ToMarkdown class.
    """
    theProject = NWProject(mockGUI)
    mockGUI.theIndex = NWIndex(theProject)
    theMD = ToMarkdown(theProject)

    # Headers
    # =======

    theMD._isNovel = True
    theMD._isNote = False
    theMD._isFirst = True

    # Header 1
    theMD._theText = "# Partition\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "# Partition\n\n"

    # Header 2
    theMD._theText = "## Chapter Title\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "## Chapter Title\n\n"

    # Header 3
    theMD._theText = "### Scene Title\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "### Scene Title\n\n"

    # Header 4
    theMD._theText = "#### Section Title\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "#### Section Title\n\n"

    # Title
    theMD._theText = "#! Title\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "# Title\n\n"

    # Unnumbered
    theMD._theText = "##! Prologue\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "## Prologue\n\n"

    # Paragraphs
    # ==========

    # Text for GitHub Markdown
    theMD.setGitHubMarkdown()
    theMD._theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == (
        "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n\n"
    )

    # Text for Standard Markdown
    theMD.setStandardMarkdown()
    theMD._theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == (
        "Some **nested bold and _italic_ and strikethrough text** here\n\n")

    # Text w/Hard Break
    theMD._theText = "Line one  \nLine two  \nLine three\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "Line one  \nLine two  \nLine three\n\n"

    # Synopsis
    theMD._theText = "%synopsis: The synopsis ...\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == ""

    theMD.setSynopsis(True)
    theMD._theText = "%synopsis: The synopsis ...\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "**Synopsis:** The synopsis ...\n\n"

    # Comment
    theMD._theText = "% A comment ...\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == ""

    theMD.setComments(True)
    theMD._theText = "% A comment ...\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "**Comment:** A comment ...\n\n"

    # Keywords
    theMD._theText = "@char: Bod, Jane\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == ""

    theMD.setKeywords(True)
    theMD._theText = "@char: Bod, Jane\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == "**Characters:** Bod, Jane\n\n"

    # Multiple Keywords
    theMD.setKeywords(True)
    theMD._theText = "## Chapter\n\n@pov: Bod\n@plot: Main\n@location: Europe\n\n"
    theMD.tokenizeText()
    theMD.doConvert()
    assert theMD.theResult == ("## Chapter\n\n"
                               "**Point of View:** Bod  \n"
                               "**Plot:** Main  \n"
                               "**Locations:** Europe\n\n")