From f517a747988a667aafec9475ef106007481edcf7 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 9 Jun 2010 12:10:54 +0200 Subject: [PATCH] fix handling empty xml files --- librarian/html.py | 14 +++++++++++--- tests/test_html.py | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) 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') -- 2.20.1