#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2007 Søren Roug, European Environment Agency # # This is free software. You may redistribute it under the terms # of the Apache license and the GNU General Public License Version # 2 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # Contributor(s): # import unittest import sys, os, os.path, re import io from odf.odf2xhtml import ODF2XHTML from odf.opendocument import OpenDocumentText from odf import style from odf.text import H, P, Span def has_rules(html, selector, rules): """ Returns false if the selector or rule is not found in html """ selstart = html.find(selector) if selstart == -1: return False selend = html[selstart:].find('}') if selend == -1: return False rulelist = rules.split(";") for rule in rulelist: if html[selstart:selstart+selend].find(rule.strip()) == -1: return False return True def divWithClass_has_styles(s,classname, styleString): """ Checks whether a div with some class attribute bears a set of other attributes. @param s string to be searched @param classname the value of the class attribute @param styleString a sequence of style stances separated by semicolons @return True when the div opening tag bears all the requested styles, independently of their order """ pattern=re.compile(r'
]*class="'+classname+r'"[^>]*>', re.MULTILINE) found=pattern.findall(s) if not found: return False found=found[0] foundStyle=re.match(r'''.*style ?= ?["']([^"']*)["'].*''', found) if not foundStyle: return False foundStyle=foundStyle.group(1) foundStyle = map(lambda x: x.replace(' ',''), foundStyle.split(';')) styleString = map(lambda x: x.replace(' ',''), styleString.split(';')) return set(foundStyle)==set(styleString) htmlout = """

Simple test document

The earth's climate has not changed many times in the course of its long history. This part is bold. This is after bold.

""" class TestXHTML(unittest.TestCase): def setUp(self): d = OpenDocumentText() # Styles h1style = style.Style(name=u"Heading 1",family=u"paragraph") h1style.addElement(style.TextProperties(attributes={'fontsize':u"24pt", 'fontweight':u"bold"})) d.styles.addElement(h1style) boldstyle = style.Style(name=u"Bold",family=u"text") boldstyle.addElement(style.TextProperties(attributes={'fontweight':u"bold"})) d.automaticstyles.addElement(boldstyle) # Text h = H(outlinelevel=1, stylename=h1style, text=u"Simple test document") d.text.addElement(h) p = P(text=u"The earth's climate has not changed many times in the course of its long history. ") d.text.addElement(p) boldpart = Span(stylename=boldstyle, text=u"This part is bold. ") p.addElement(boldpart) p.addText(u"This is after bold.") d.save(u"TEST.odt") def tearDown(self): os.unlink(u"TEST.odt") def testParsing(self): """ Parse the test file """ odhandler = ODF2XHTML() outf = io.BytesIO() result = odhandler.odf2xhtml(u"TEST.odt") outf.write(result.encode('utf-8')) strresult = outf.getvalue().decode('utf-8') #self.assertEqual(strresult, htmlout) self.assertNotEqual(-1, strresult.find(u"""

The earth's climate has \ not changed many times in the course of its long history. \ This part is bold. This is after bold.

""")) self.assertNotEqual(-1, strresult.find(u"""

Simple test document

""")) self.assertNotEqual(-1, strresult.find(u""".S-Bold {""")) self.assertEqual(-1, strresult.find(u"
    Italic

    \n""")) self.assertNotEqual(-1, result.find(u"""\n\ttext-decoration: underline;\n""")) self.assertNotEqual(-1, result.find(u"""

    Underline italic

    \n""")) def test_simplelist(self): """ Check CSS has list styles """ simplelist_odt = os.path.join( os.path.dirname(__file__), u"examples", u"simplelist.odt") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(simplelist_odt) assert has_rules(result,u".L1_1",u"list-style-type: disc;") assert has_rules(result,u".L1_2",u"list-style-type: circle;") assert has_rules(result,u".L1_3",u"list-style-type: square;") self.assertNotEqual(-1, result.find(u"""

    Line 1

    \n