html4.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # markdown/html4.py
  2. #
  3. # Add html4 serialization to older versions of Elementree
  4. # Taken from ElementTree 1.3 preview with slight modifications
  5. #
  6. # Copyright (c) 1999-2007 by Fredrik Lundh. All rights reserved.
  7. #
  8. # fredrik@pythonware.com
  9. # http://www.pythonware.com
  10. #
  11. # --------------------------------------------------------------------
  12. # The ElementTree toolkit is
  13. #
  14. # Copyright (c) 1999-2007 by Fredrik Lundh
  15. #
  16. # By obtaining, using, and/or copying this software and/or its
  17. # associated documentation, you agree that you have read, understood,
  18. # and will comply with the following terms and conditions:
  19. #
  20. # Permission to use, copy, modify, and distribute this software and
  21. # its associated documentation for any purpose and without fee is
  22. # hereby granted, provided that the above copyright notice appears in
  23. # all copies, and that both that copyright notice and this permission
  24. # notice appear in supporting documentation, and that the name of
  25. # Secret Labs AB or the author not be used in advertising or publicity
  26. # pertaining to distribution of the software without specific, written
  27. # prior permission.
  28. #
  29. # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  30. # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
  31. # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
  32. # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  33. # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  34. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  35. # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  36. # OF THIS SOFTWARE.
  37. # --------------------------------------------------------------------
  38. import markdown
  39. ElementTree = markdown.etree.ElementTree
  40. QName = markdown.etree.QName
  41. Comment = markdown.etree.Comment
  42. PI = markdown.etree.PI
  43. ProcessingInstruction = markdown.etree.ProcessingInstruction
  44. HTML_EMPTY = ("area", "base", "basefont", "br", "col", "frame", "hr",
  45. "img", "input", "isindex", "link", "meta" "param")
  46. try:
  47. HTML_EMPTY = set(HTML_EMPTY)
  48. except NameError:
  49. pass
  50. _namespace_map = {
  51. # "well-known" namespace prefixes
  52. "http://www.w3.org/XML/1998/namespace": "xml",
  53. "http://www.w3.org/1999/xhtml": "html",
  54. "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
  55. "http://schemas.xmlsoap.org/wsdl/": "wsdl",
  56. # xml schema
  57. "http://www.w3.org/2001/XMLSchema": "xs",
  58. "http://www.w3.org/2001/XMLSchema-instance": "xsi",
  59. # dublic core
  60. "http://purl.org/dc/elements/1.1/": "dc",
  61. }
  62. def _raise_serialization_error(text):
  63. raise TypeError(
  64. "cannot serialize %r (type %s)" % (text, type(text).__name__)
  65. )
  66. def _encode(text, encoding):
  67. try:
  68. return text.encode(encoding, "xmlcharrefreplace")
  69. except (TypeError, AttributeError):
  70. _raise_serialization_error(text)
  71. def _escape_cdata(text, encoding):
  72. # escape character data
  73. try:
  74. # it's worth avoiding do-nothing calls for strings that are
  75. # shorter than 500 character, or so. assume that's, by far,
  76. # the most common case in most applications.
  77. if "&" in text:
  78. text = text.replace("&", "&")
  79. if "<" in text:
  80. text = text.replace("<", "&lt;")
  81. if ">" in text:
  82. text = text.replace(">", "&gt;")
  83. return text.encode(encoding, "xmlcharrefreplace")
  84. except (TypeError, AttributeError):
  85. _raise_serialization_error(text)
  86. def _escape_attrib(text, encoding):
  87. # escape attribute value
  88. try:
  89. if "&" in text:
  90. text = text.replace("&", "&amp;")
  91. if "<" in text:
  92. text = text.replace("<", "&lt;")
  93. if ">" in text:
  94. text = text.replace(">", "&gt;")
  95. if "\"" in text:
  96. text = text.replace("\"", "&quot;")
  97. if "\n" in text:
  98. text = text.replace("\n", "&#10;")
  99. return text.encode(encoding, "xmlcharrefreplace")
  100. except (TypeError, AttributeError):
  101. _raise_serialization_error(text)
  102. def _escape_attrib_html(text, encoding):
  103. # escape attribute value
  104. try:
  105. if "&" in text:
  106. text = text.replace("&", "&amp;")
  107. if ">" in text:
  108. text = text.replace(">", "&gt;")
  109. if "\"" in text:
  110. text = text.replace("\"", "&quot;")
  111. return text.encode(encoding, "xmlcharrefreplace")
  112. except (TypeError, AttributeError):
  113. _raise_serialization_error(text)
  114. def _serialize_html(write, elem, encoding, qnames, namespaces):
  115. tag = elem.tag
  116. text = elem.text
  117. if tag is Comment:
  118. write("<!--%s-->" % _escape_cdata(text, encoding))
  119. elif tag is ProcessingInstruction:
  120. write("<?%s?>" % _escape_cdata(text, encoding))
  121. else:
  122. tag = qnames[tag]
  123. if tag is None:
  124. if text:
  125. write(_escape_cdata(text, encoding))
  126. for e in elem:
  127. _serialize_html(write, e, encoding, qnames, None)
  128. else:
  129. write("<" + tag)
  130. items = elem.items()
  131. if items or namespaces:
  132. items.sort() # lexical order
  133. for k, v in items:
  134. if isinstance(k, QName):
  135. k = k.text
  136. if isinstance(v, QName):
  137. v = qnames[v.text]
  138. else:
  139. v = _escape_attrib_html(v, encoding)
  140. # FIXME: handle boolean attributes
  141. write(" %s=\"%s\"" % (qnames[k], v))
  142. if namespaces:
  143. items = namespaces.items()
  144. items.sort(key=lambda x: x[1]) # sort on prefix
  145. for v, k in items:
  146. if k:
  147. k = ":" + k
  148. write(" xmlns%s=\"%s\"" % (
  149. k.encode(encoding),
  150. _escape_attrib(v, encoding)
  151. ))
  152. write(">")
  153. tag = tag.lower()
  154. if text:
  155. if tag == "script" or tag == "style":
  156. write(_encode(text, encoding))
  157. else:
  158. write(_escape_cdata(text, encoding))
  159. for e in elem:
  160. _serialize_html(write, e, encoding, qnames, None)
  161. if tag not in HTML_EMPTY:
  162. write("</" + tag + ">")
  163. if elem.tail:
  164. write(_escape_cdata(elem.tail, encoding))
  165. def write_html(root, f,
  166. # keyword arguments
  167. encoding="us-ascii",
  168. default_namespace=None):
  169. assert root is not None
  170. if not hasattr(f, "write"):
  171. f = open(f, "wb")
  172. write = f.write
  173. if not encoding:
  174. encoding = "us-ascii"
  175. qnames, namespaces = _namespaces(
  176. root, encoding, default_namespace
  177. )
  178. _serialize_html(
  179. write, root, encoding, qnames, namespaces
  180. )
  181. # --------------------------------------------------------------------
  182. # serialization support
  183. def _namespaces(elem, encoding, default_namespace=None):
  184. # identify namespaces used in this tree
  185. # maps qnames to *encoded* prefix:local names
  186. qnames = {None: None}
  187. # maps uri:s to prefixes
  188. namespaces = {}
  189. if default_namespace:
  190. namespaces[default_namespace] = ""
  191. def encode(text):
  192. return text.encode(encoding)
  193. def add_qname(qname):
  194. # calculate serialized qname representation
  195. try:
  196. if qname[:1] == "{":
  197. uri, tag = qname[1:].split("}", 1)
  198. prefix = namespaces.get(uri)
  199. if prefix is None:
  200. prefix = _namespace_map.get(uri)
  201. if prefix is None:
  202. prefix = "ns%d" % len(namespaces)
  203. if prefix != "xml":
  204. namespaces[uri] = prefix
  205. if prefix:
  206. qnames[qname] = encode("%s:%s" % (prefix, tag))
  207. else:
  208. qnames[qname] = encode(tag) # default element
  209. else:
  210. if default_namespace:
  211. # FIXME: can this be handled in XML 1.0?
  212. raise ValueError(
  213. "cannot use non-qualified names with "
  214. "default_namespace option"
  215. )
  216. qnames[qname] = encode(qname)
  217. except TypeError:
  218. _raise_serialization_error(qname)
  219. # populate qname and namespaces table
  220. try:
  221. iterate = elem.iter
  222. except AttributeError:
  223. iterate = elem.getiterator # cET compatibility
  224. for elem in iterate():
  225. tag = elem.tag
  226. if isinstance(tag, QName) and tag.text not in qnames:
  227. add_qname(tag.text)
  228. elif isinstance(tag, basestring):
  229. if tag not in qnames:
  230. add_qname(tag)
  231. elif tag is not None and tag is not Comment and tag is not PI:
  232. _raise_serialization_error(tag)
  233. for key, value in elem.items():
  234. if isinstance(key, QName):
  235. key = key.text
  236. if key not in qnames:
  237. add_qname(key)
  238. if isinstance(value, QName) and value.text not in qnames:
  239. add_qname(value.text)
  240. text = elem.text
  241. if isinstance(text, QName) and text.text not in qnames:
  242. add_qname(text.text)
  243. return qnames, namespaces
  244. def to_html_string(element, encoding=None):
  245. class dummy:
  246. pass
  247. data = []
  248. file = dummy()
  249. file.write = data.append
  250. write_html(ElementTree(element).getroot(),file,encoding)
  251. return "".join(data)