diff options
author | Waylan Limberg <waylan@gmail.com> | 2011-06-20 09:31:17 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2011-06-20 09:31:17 -0400 |
commit | ca145d4b5da3ea0a328ccd7a5505cef3da6646a0 (patch) | |
tree | 27b51b8ed2e71dcc968b77886ffd18a38f377191 | |
parent | e020c623ac6fb1a230475026e7b884c897fce662 (diff) | |
download | markdown-ca145d4b5da3ea0a328ccd7a5505cef3da6646a0.tar.gz markdown-ca145d4b5da3ea0a328ccd7a5505cef3da6646a0.tar.bz2 markdown-ca145d4b5da3ea0a328ccd7a5505cef3da6646a0.zip |
Made the searializer wrapping code a little more dry and more clearly defined the public methods.
-rw-r--r-- | markdown/searializers.py | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/markdown/searializers.py b/markdown/searializers.py index 16f178c..2de7b0f 100644 --- a/markdown/searializers.py +++ b/markdown/searializers.py @@ -47,6 +47,8 @@ else: PI = util.etree.PI ProcessingInstruction = util.etree.ProcessingInstruction +__all__ = ['to_html_string', 'to_xhtml_string'] + HTML_EMPTY = ("area", "base", "basefont", "br", "col", "frame", "hr", "img", "input", "isindex", "link", "meta" "param") @@ -188,23 +190,23 @@ def _serialize_html(write, elem, encoding, qnames, namespaces, format): if elem.tail: write(_escape_cdata(elem.tail, encoding)) -def write_html(root, f, +def _write_html(root, # keyword arguments - encoding="us-ascii", + encoding="utf-8", default_namespace=None, format="html"): assert root is not None - if not hasattr(f, "write"): - f = open(f, "wb") - write = f.write + data = [] + write = data.append if not encoding: - encoding = "us-ascii" + encoding = "utf-8" qnames, namespaces = _namespaces( root, encoding, default_namespace ) _serialize_html( write, root, encoding, qnames, namespaces, format ) + return "".join(data) # -------------------------------------------------------------------- # serialization support @@ -277,19 +279,7 @@ def _namespaces(elem, encoding, default_namespace=None): return qnames, namespaces def to_html_string(element, encoding=None): - class dummy: - pass - data = [] - file = dummy() - file.write = data.append - write_html(ElementTree(element).getroot(), file, encoding, format="html") - return "".join(data) + return _write_html(ElementTree(element).getroot(), encoding, format="html") def to_xhtml_string(element, encoding=None): - class dummy: - pass - data = [] - file = dummy() - file.write = data.append - write_html(ElementTree(element).getroot(), file, encoding, format="xhtml") - return "".join(data) + return _write_html(ElementTree(element).getroot(), encoding, format="xhtml") |