From: Radek Czajka Date: Wed, 9 Jun 2010 10:10:54 +0000 (+0200) Subject: fix handling empty xml files X-Git-Tag: 1.7~287 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/f517a747988a667aafec9475ef106007481edcf7?ds=inline;hp=993a8cd64b2913a6d1c42f094c37c6c2d66d2332 fix handling empty xml files --- diff --git a/librarian/html.py b/librarian/html.py index c2e0dea..f163d5b 100644 --- a/librarian/html.py +++ b/librarian/html.py @@ -45,7 +45,12 @@ ns['substitute_entities'] = substitute_entities def transform(input, output_filename=None, is_file=True, \ parse_dublincore=True, stylesheet='legacy', options={}): - """Transforms file input_filename in XML to output_filename in XHTML.""" + """Transforms file input_filename in XML to output_filename in XHTML. + + If output_filename is None, returns an XML, + otherwise returns True if file has been written,False if it hasn't. + File won't be written if it has no content. + """ # Parse XSLT try: style_filename = get_stylesheet(stylesheet) @@ -61,7 +66,7 @@ def transform(input, output_filename=None, is_file=True, \ result = document.transform(style, **options) del document # no longer needed large object :) - if etree.ETXPath('//p|//{%s}p' % str(XHTMLNS))(result) is not None: + if etree.ETXPath('//p|//{%s}p' % str(XHTMLNS))(result): add_anchors(result.getroot()) add_table_of_contents(result.getroot()) @@ -71,7 +76,10 @@ def transform(input, output_filename=None, is_file=True, \ return result return True else: - return "" + if output_filename is not None: + return False + else: + return "" except KeyError: raise ValueError("'%s' is not a valid stylesheet.") except (XMLSyntaxError, XSLTApplyError), e: diff --git a/tests/test_html.py b/tests/test_html.py index 8d3aede..b5cf4da 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -41,3 +41,6 @@ def test_passing_parse_dublincore_to_transform(): get_fixture('text', 'asnyk_miedzy_nami.html'), parse_dublincore=False, ) + +def test_empty(): + assert html.transform('', is_file=False, parse_dublincore=False).find('empty')