Exemplo n.º 1
0
def testCoreToOdt_Convert(mockGUI):
    """Test the converter of the ToOdt class.
    """
    theProject = NWProject(mockGUI)
    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>')
Exemplo n.º 2
0
def testCoreToOdt_TextFormatting(mockGUI):
    """Test formatting of paragraphs.
    """
    theProject = NWProject(mockGUI)
    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>")