#
from librarian import dcparser, parser, functions
from lxml import etree
-import cStringIO
-import codecs
import os
-import re
functions.reg_substitute_entities()
%(text)s
"""
-def transform(input_filename, output_filename, is_file=True, parse_dublincore=True, **options):
- """Transforms file input_filename in XML to output_filename in TXT."""
+def transform(input_file, output_file, parse_dublincore=True, **options):
+ """Transforms input_file in XML to output_file in TXT."""
# Parse XSLT
style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt')
style = etree.parse(style_filename)
- if is_file:
- document = parser.WLDocument.from_file(input_filename, True, parse_dublincore=parse_dublincore)
- else:
- document = parser.WLDocument.from_string(input_filename, True, parse_dublincore=parse_dublincore)
-
+ document = parser.WLDocument.from_file(input_file, True, parse_dublincore=parse_dublincore)
result = document.transform(style, **options)
- output_file = codecs.open(output_filename, 'wb', encoding='utf-8')
-
if parse_dublincore:
- parsed_dc = dcparser.parse(input_filename)
+ parsed_dc = dcparser.BookInfo.from_element(document.edoc)
description = parsed_dc.description
url = parsed_dc.url
license_description = parsed_dc.license_description
license = ""
license_description = ""
source = ""
- output_file.write(TEMPLATE % {
+ output_file.write((TEMPLATE % {
'description': description,
'url': url,
'license_description': license_description,
'text': unicode(result),
'source': source,
- })
+ }).encode('utf-8'))
output_filename = os.path.splitext(input_filename)[0] + '.txt'
try:
- text.transform(input_filename, output_filename, parse_dublincore=options.parse_dublincore,
+ output_file = open(output_filename, 'w')
+ text.transform(open(input_filename), output_file, parse_dublincore=options.parse_dublincore,
wrapping=str(options.wrapping))
except ParseError, e:
print '%(file)s:%(name)s:%(message)s' % {