From ca145d4b5da3ea0a328ccd7a5505cef3da6646a0 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Mon, 20 Jun 2011 09:31:17 -0400 Subject: Made the searializer wrapping code a little more dry and more clearly defined the public methods. --- markdown/searializers.py | 30 ++++++++++-------------------- 1 file 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") -- cgit v1.2.3