diff options
Diffstat (limited to 'markdown')
-rw-r--r-- | markdown/__init__.py | 10 | ||||
-rw-r--r-- | markdown/searializers.py (renamed from markdown/html4.py) | 47 |
2 files changed, 36 insertions, 21 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index 9b964b7..762da31 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -43,7 +43,7 @@ from treeprocessors import build_treeprocessors from inlinepatterns import build_inlinepatterns from postprocessors import build_postprocessors from extensions import Extension -import html4 +from searializers import to_html_string, to_xhtml_string # For backwards compatibility in the 2.0.x series # The things defined in these modules started off in __init__.py so third @@ -67,10 +67,10 @@ class Markdown: } output_formats = { - 'html' : html4.to_html_string, - 'html4' : html4.to_html_string, - 'xhtml' : util.etree.tostring, - 'xhtml1': util.etree.tostring, + 'html' : to_html_string, + 'html4' : to_html_string, + 'xhtml' : to_xhtml_string, + 'xhtml1': to_xhtml_string, } def __init__(self, extensions=[], **kwargs): diff --git a/markdown/html4.py b/markdown/searializers.py index 611e628..39dcb56 100644 --- a/markdown/html4.py +++ b/markdown/searializers.py @@ -119,6 +119,8 @@ def _escape_attrib_html(text, encoding): try: if "&" in text: text = text.replace("&", "&") + if "<" in text: + text = text.replace("<", "<") if ">" in text: text = text.replace(">", ">") if "\"" in text: @@ -128,7 +130,7 @@ def _escape_attrib_html(text, encoding): _raise_serialization_error(text) -def _serialize_html(write, elem, encoding, qnames, namespaces): +def _serialize_html(write, elem, encoding, qnames, namespaces, format): tag = elem.tag text = elem.text if tag is Comment: @@ -141,7 +143,7 @@ def _serialize_html(write, elem, encoding, qnames, namespaces): if text: write(_escape_cdata(text, encoding)) for e in elem: - _serialize_html(write, e, encoding, qnames, None) + _serialize_html(write, e, encoding, qnames, None, format) else: write("<" + tag) items = elem.items() @@ -166,24 +168,28 @@ def _serialize_html(write, elem, encoding, qnames, namespaces): k.encode(encoding), _escape_attrib(v, encoding) )) - write(">") - tag = tag.lower() - if text: - if tag == "script" or tag == "style": - write(_encode(text, encoding)) - else: - write(_escape_cdata(text, encoding)) - for e in elem: - _serialize_html(write, e, encoding, qnames, None) - if tag not in HTML_EMPTY: - write("</" + tag + ">") + if format == "xhtml" and tag in HTML_EMPTY: + write(" />") + else: + write(">") + tag = tag.lower() + if text: + if tag == "script" or tag == "style": + write(_encode(text, encoding)) + else: + write(_escape_cdata(text, encoding)) + for e in elem: + _serialize_html(write, e, encoding, qnames, None, format) + if tag not in HTML_EMPTY: + write("</" + tag + ">") if elem.tail: write(_escape_cdata(elem.tail, encoding)) def write_html(root, f, # keyword arguments encoding="us-ascii", - default_namespace=None): + default_namespace=None, + format="html"): assert root is not None if not hasattr(f, "write"): f = open(f, "wb") @@ -194,7 +200,7 @@ def write_html(root, f, root, encoding, default_namespace ) _serialize_html( - write, root, encoding, qnames, namespaces + write, root, encoding, qnames, namespaces, format ) # -------------------------------------------------------------------- @@ -273,5 +279,14 @@ def to_html_string(element, encoding=None): data = [] file = dummy() file.write = data.append - write_html(ElementTree(element).getroot(),file,encoding) + write_html(ElementTree(element).getroot(), file, encoding, format="html") + return "".join(data) + +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) |