PageRenderTime 34ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/utils/markdown.py

https://github.com/gonbo/weiyi
Python | 1878 lines | 1658 code | 65 blank | 155 comment | 83 complexity | 7da02529c432192413af18d9e5b592ca MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright (c) 2007-2008 ActiveState Corp.
  3. # License: MIT (http://www.opensource.org/licenses/mit-license.php)
  4. r"""A fast and complete Python implementation of Markdown.
  5. [from http://daringfireball.net/projects/markdown/]
  6. > Markdown is a text-to-HTML filter; it translates an easy-to-read /
  7. > easy-to-write structured text format into HTML. Markdown's text
  8. > format is most similar to that of plain text email, and supports
  9. > features such as headers, *emphasis*, code blocks, blockquotes, and
  10. > links.
  11. >
  12. > Markdown's syntax is designed not as a generic markup language, but
  13. > specifically to serve as a front-end to (X)HTML. You can use span-level
  14. > HTML tags anywhere in a Markdown document, and you can use block level
  15. > HTML tags (like <div> and <table> as well).
  16. Module usage:
  17. >>> import markdown2
  18. >>> markdown2.markdown("*boo!*") # or use `html = markdown_path(PATH)`
  19. u'<p><em>boo!</em></p>\n'
  20. >>> markdowner = Markdown()
  21. >>> markdowner.convert("*boo!*")
  22. u'<p><em>boo!</em></p>\n'
  23. >>> markdowner.convert("**boom!**")
  24. u'<p><strong>boom!</strong></p>\n'
  25. This implementation of Markdown implements the full "core" syntax plus a
  26. number of extras (e.g., code syntax coloring, footnotes) as described on
  27. <http://code.google.com/p/python-markdown2/wiki/Extras>.
  28. """
  29. cmdln_desc = """A fast and complete Python implementation of Markdown, a
  30. text-to-HTML conversion tool for web writers.
  31. """
  32. # Dev Notes:
  33. # - There is already a Python markdown processor
  34. # (http://www.freewisdom.org/projects/python-markdown/).
  35. # - Python's regex syntax doesn't have '\z', so I'm using '\Z'. I'm
  36. # not yet sure if there implications with this. Compare 'pydoc sre'
  37. # and 'perldoc perlre'.
  38. __version_info__ = (1, 0, 1, 14) # first three nums match Markdown.pl
  39. __version__ = '1.0.1.14'
  40. __author__ = "Trent Mick"
  41. import os
  42. import sys
  43. from pprint import pprint
  44. import re
  45. import logging
  46. try:
  47. from hashlib import md5
  48. except ImportError:
  49. from md5 import md5
  50. import optparse
  51. from random import random
  52. import codecs
  53. #---- Python version compat
  54. if sys.version_info[:2] < (2,4):
  55. from sets import Set as set
  56. def reversed(sequence):
  57. for i in sequence[::-1]:
  58. yield i
  59. def _unicode_decode(s, encoding, errors='xmlcharrefreplace'):
  60. return unicode(s, encoding, errors)
  61. else:
  62. def _unicode_decode(s, encoding, errors='strict'):
  63. return s.decode(encoding, errors)
  64. #---- globals
  65. DEBUG = False
  66. log = logging.getLogger("markdown")
  67. DEFAULT_TAB_WIDTH = 4
  68. # Table of hash values for escaped characters:
  69. def _escape_hash(s):
  70. # Lame attempt to avoid possible collision with someone actually
  71. # using the MD5 hexdigest of one of these chars in there text.
  72. # Other ideas: random.random(), uuid.uuid()
  73. #return md5(s).hexdigest() # Markdown.pl effectively does this.
  74. return 'md5-'+md5(s).hexdigest()
  75. g_escape_table = dict([(ch, _escape_hash(ch))
  76. for ch in '\\`*_{}[]()>#+-.!'])
  77. #---- exceptions
  78. class MarkdownError(Exception):
  79. pass
  80. #---- public api
  81. def markdown_path(path, encoding="utf-8",
  82. html4tags=False, tab_width=DEFAULT_TAB_WIDTH,
  83. safe_mode=None, extras=None, link_patterns=None,
  84. use_file_vars=False):
  85. text = codecs.open(path, 'r', encoding).read()
  86. return Markdown(html4tags=html4tags, tab_width=tab_width,
  87. safe_mode=safe_mode, extras=extras,
  88. link_patterns=link_patterns,
  89. use_file_vars=use_file_vars).convert(text)
  90. def markdown(text, html4tags=False, tab_width=DEFAULT_TAB_WIDTH,
  91. safe_mode=None, extras=None, link_patterns=None,
  92. use_file_vars=False):
  93. return Markdown(html4tags=html4tags, tab_width=tab_width,
  94. safe_mode=safe_mode, extras=extras,
  95. link_patterns=link_patterns,
  96. use_file_vars=use_file_vars).convert(text)
  97. class Markdown(object):
  98. # The dict of "extras" to enable in processing -- a mapping of
  99. # extra name to argument for the extra. Most extras do not have an
  100. # argument, in which case the value is None.
  101. #
  102. # This can be set via (a) subclassing and (b) the constructor
  103. # "extras" argument.
  104. extras = None
  105. urls = None
  106. titles = None
  107. html_blocks = None
  108. html_spans = None
  109. html_removed_text = "[HTML_REMOVED]" # for compat with markdown.py
  110. # Used to track when we're inside an ordered or unordered list
  111. # (see _ProcessListItems() for details):
  112. list_level = 0
  113. _ws_only_line_re = re.compile(r"^[ \t]+$", re.M)
  114. def __init__(self, html4tags=False, tab_width=4, safe_mode=None,
  115. extras=None, link_patterns=None, use_file_vars=False):
  116. if html4tags:
  117. self.empty_element_suffix = ">"
  118. else:
  119. self.empty_element_suffix = " />"
  120. self.tab_width = tab_width
  121. # For compatibility with earlier markdown2.py and with
  122. # markdown.py's safe_mode being a boolean,
  123. # safe_mode == True -> "replace"
  124. if safe_mode is True:
  125. self.safe_mode = "replace"
  126. else:
  127. self.safe_mode = safe_mode
  128. if self.extras is None:
  129. self.extras = {}
  130. elif not isinstance(self.extras, dict):
  131. self.extras = dict([(e, None) for e in self.extras])
  132. if extras:
  133. if not isinstance(extras, dict):
  134. extras = dict([(e, None) for e in extras])
  135. self.extras.update(extras)
  136. assert isinstance(self.extras, dict)
  137. self._instance_extras = self.extras.copy()
  138. self.link_patterns = link_patterns
  139. self.use_file_vars = use_file_vars
  140. self._outdent_re = re.compile(r'^(\t|[ ]{1,%d})' % tab_width, re.M)
  141. def reset(self):
  142. self.urls = {}
  143. self.titles = {}
  144. self.html_blocks = {}
  145. self.html_spans = {}
  146. self.list_level = 0
  147. self.extras = self._instance_extras.copy()
  148. if "footnotes" in self.extras:
  149. self.footnotes = {}
  150. self.footnote_ids = []
  151. def convert(self, text):
  152. """Convert the given text."""
  153. # Main function. The order in which other subs are called here is
  154. # essential. Link and image substitutions need to happen before
  155. # _EscapeSpecialChars(), so that any *'s or _'s in the <a>
  156. # and <img> tags get encoded.
  157. # Clear the global hashes. If we don't clear these, you get conflicts
  158. # from other articles when generating a page which contains more than
  159. # one article (e.g. an index page that shows the N most recent
  160. # articles):
  161. self.reset()
  162. if not isinstance(text, unicode):
  163. #TODO: perhaps shouldn't presume UTF-8 for string input?
  164. text = unicode(text, 'utf-8')
  165. if self.use_file_vars:
  166. # Look for emacs-style file variable hints.
  167. emacs_vars = self._get_emacs_vars(text)
  168. if "markdown-extras" in emacs_vars:
  169. splitter = re.compile("[ ,]+")
  170. for e in splitter.split(emacs_vars["markdown-extras"]):
  171. if '=' in e:
  172. ename, earg = e.split('=', 1)
  173. try:
  174. earg = int(earg)
  175. except ValueError:
  176. pass
  177. else:
  178. ename, earg = e, None
  179. self.extras[ename] = earg
  180. # Standardize line endings:
  181. text = re.sub("\r\n|\r", "\n", text)
  182. # Make sure $text ends with a couple of newlines:
  183. text += "\n\n"
  184. # Convert all tabs to spaces.
  185. text = self._detab(text)
  186. # Strip any lines consisting only of spaces and tabs.
  187. # This makes subsequent regexen easier to write, because we can
  188. # match consecutive blank lines with /\n+/ instead of something
  189. # contorted like /[ \t]*\n+/ .
  190. text = self._ws_only_line_re.sub("", text)
  191. if self.safe_mode:
  192. text = self._hash_html_spans(text)
  193. # Turn block-level HTML blocks into hash entries
  194. text = self._hash_html_blocks(text, raw=True)
  195. # Strip link definitions, store in hashes.
  196. if "footnotes" in self.extras:
  197. # Must do footnotes first because an unlucky footnote defn
  198. # looks like a link defn:
  199. # [^4]: this "looks like a link defn"
  200. text = self._strip_footnote_definitions(text)
  201. text = self._strip_link_definitions(text)
  202. text = self._run_block_gamut(text)
  203. if "footnotes" in self.extras:
  204. text = self._add_footnotes(text)
  205. text = self._unescape_special_chars(text)
  206. if self.safe_mode:
  207. text = self._unhash_html_spans(text)
  208. text += "\n"
  209. return text
  210. _emacs_oneliner_vars_pat = re.compile(r"-\*-\s*([^\r\n]*?)\s*-\*-", re.UNICODE)
  211. # This regular expression is intended to match blocks like this:
  212. # PREFIX Local Variables: SUFFIX
  213. # PREFIX mode: Tcl SUFFIX
  214. # PREFIX End: SUFFIX
  215. # Some notes:
  216. # - "[ \t]" is used instead of "\s" to specifically exclude newlines
  217. # - "(\r\n|\n|\r)" is used instead of "$" because the sre engine does
  218. # not like anything other than Unix-style line terminators.
  219. _emacs_local_vars_pat = re.compile(r"""^
  220. (?P<prefix>(?:[^\r\n|\n|\r])*?)
  221. [\ \t]*Local\ Variables:[\ \t]*
  222. (?P<suffix>.*?)(?:\r\n|\n|\r)
  223. (?P<content>.*?\1End:)
  224. """, re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE)
  225. def _get_emacs_vars(self, text):
  226. """Return a dictionary of emacs-style local variables.
  227. Parsing is done loosely according to this spec (and according to
  228. some in-practice deviations from this):
  229. http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html#Specifying-File-Variables
  230. """
  231. emacs_vars = {}
  232. SIZE = pow(2, 13) # 8kB
  233. # Search near the start for a '-*-'-style one-liner of variables.
  234. head = text[:SIZE]
  235. if "-*-" in head:
  236. match = self._emacs_oneliner_vars_pat.search(head)
  237. if match:
  238. emacs_vars_str = match.group(1)
  239. assert '\n' not in emacs_vars_str
  240. emacs_var_strs = [s.strip() for s in emacs_vars_str.split(';')
  241. if s.strip()]
  242. if len(emacs_var_strs) == 1 and ':' not in emacs_var_strs[0]:
  243. # While not in the spec, this form is allowed by emacs:
  244. # -*- Tcl -*-
  245. # where the implied "variable" is "mode". This form
  246. # is only allowed if there are no other variables.
  247. emacs_vars["mode"] = emacs_var_strs[0].strip()
  248. else:
  249. for emacs_var_str in emacs_var_strs:
  250. try:
  251. variable, value = emacs_var_str.strip().split(':', 1)
  252. except ValueError:
  253. log.debug("emacs variables error: malformed -*- "
  254. "line: %r", emacs_var_str)
  255. continue
  256. # Lowercase the variable name because Emacs allows "Mode"
  257. # or "mode" or "MoDe", etc.
  258. emacs_vars[variable.lower()] = value.strip()
  259. tail = text[-SIZE:]
  260. if "Local Variables" in tail:
  261. match = self._emacs_local_vars_pat.search(tail)
  262. if match:
  263. prefix = match.group("prefix")
  264. suffix = match.group("suffix")
  265. lines = match.group("content").splitlines(0)
  266. #print "prefix=%r, suffix=%r, content=%r, lines: %s"\
  267. # % (prefix, suffix, match.group("content"), lines)
  268. # Validate the Local Variables block: proper prefix and suffix
  269. # usage.
  270. for i, line in enumerate(lines):
  271. if not line.startswith(prefix):
  272. log.debug("emacs variables error: line '%s' "
  273. "does not use proper prefix '%s'"
  274. % (line, prefix))
  275. return {}
  276. # Don't validate suffix on last line. Emacs doesn't care,
  277. # neither should we.
  278. if i != len(lines)-1 and not line.endswith(suffix):
  279. log.debug("emacs variables error: line '%s' "
  280. "does not use proper suffix '%s'"
  281. % (line, suffix))
  282. return {}
  283. # Parse out one emacs var per line.
  284. continued_for = None
  285. for line in lines[:-1]: # no var on the last line ("PREFIX End:")
  286. if prefix: line = line[len(prefix):] # strip prefix
  287. if suffix: line = line[:-len(suffix)] # strip suffix
  288. line = line.strip()
  289. if continued_for:
  290. variable = continued_for
  291. if line.endswith('\\'):
  292. line = line[:-1].rstrip()
  293. else:
  294. continued_for = None
  295. emacs_vars[variable] += ' ' + line
  296. else:
  297. try:
  298. variable, value = line.split(':', 1)
  299. except ValueError:
  300. log.debug("local variables error: missing colon "
  301. "in local variables entry: '%s'" % line)
  302. continue
  303. # Do NOT lowercase the variable name, because Emacs only
  304. # allows "mode" (and not "Mode", "MoDe", etc.) in this block.
  305. value = value.strip()
  306. if value.endswith('\\'):
  307. value = value[:-1].rstrip()
  308. continued_for = variable
  309. else:
  310. continued_for = None
  311. emacs_vars[variable] = value
  312. # Unquote values.
  313. for var, val in emacs_vars.items():
  314. if len(val) > 1 and (val.startswith('"') and val.endswith('"')
  315. or val.startswith('"') and val.endswith('"')):
  316. emacs_vars[var] = val[1:-1]
  317. return emacs_vars
  318. # Cribbed from a post by Bart Lateur:
  319. # <http://www.nntp.perl.org/group/perl.macperl.anyperl/154>
  320. _detab_re = re.compile(r'(.*?)\t', re.M)
  321. def _detab_sub(self, match):
  322. g1 = match.group(1)
  323. return g1 + (' ' * (self.tab_width - len(g1) % self.tab_width))
  324. def _detab(self, text):
  325. r"""Remove (leading?) tabs from a file.
  326. >>> m = Markdown()
  327. >>> m._detab("\tfoo")
  328. ' foo'
  329. >>> m._detab(" \tfoo")
  330. ' foo'
  331. >>> m._detab("\t foo")
  332. ' foo'
  333. >>> m._detab(" foo")
  334. ' foo'
  335. >>> m._detab(" foo\n\tbar\tblam")
  336. ' foo\n bar blam'
  337. """
  338. if '\t' not in text:
  339. return text
  340. return self._detab_re.subn(self._detab_sub, text)[0]
  341. _block_tags_a = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del'
  342. _strict_tag_block_re = re.compile(r"""
  343. ( # save in \1
  344. ^ # start of line (with re.M)
  345. <(%s) # start tag = \2
  346. \b # word break
  347. (.*\n)*? # any number of lines, minimally matching
  348. </\2> # the matching end tag
  349. [ \t]* # trailing spaces/tabs
  350. (?=\n+|\Z) # followed by a newline or end of document
  351. )
  352. """ % _block_tags_a,
  353. re.X | re.M)
  354. _block_tags_b = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math'
  355. _liberal_tag_block_re = re.compile(r"""
  356. ( # save in \1
  357. ^ # start of line (with re.M)
  358. <(%s) # start tag = \2
  359. \b # word break
  360. (.*\n)*? # any number of lines, minimally matching
  361. .*</\2> # the matching end tag
  362. [ \t]* # trailing spaces/tabs
  363. (?=\n+|\Z) # followed by a newline or end of document
  364. )
  365. """ % _block_tags_b,
  366. re.X | re.M)
  367. def _hash_html_block_sub(self, match, raw=False):
  368. html = match.group(1)
  369. if raw and self.safe_mode:
  370. html = self._sanitize_html(html)
  371. key = _hash_text(html)
  372. self.html_blocks[key] = html
  373. return "\n\n" + key + "\n\n"
  374. def _hash_html_blocks(self, text, raw=False):
  375. """Hashify HTML blocks
  376. We only want to do this for block-level HTML tags, such as headers,
  377. lists, and tables. That's because we still want to wrap <p>s around
  378. "paragraphs" that are wrapped in non-block-level tags, such as anchors,
  379. phrase emphasis, and spans. The list of tags we're looking for is
  380. hard-coded.
  381. @param raw {boolean} indicates if these are raw HTML blocks in
  382. the original source. It makes a difference in "safe" mode.
  383. """
  384. if '<' not in text:
  385. return text
  386. # Pass `raw` value into our calls to self._hash_html_block_sub.
  387. hash_html_block_sub = _curry(self._hash_html_block_sub, raw=raw)
  388. # First, look for nested blocks, e.g.:
  389. # <div>
  390. # <div>
  391. # tags for inner block must be indented.
  392. # </div>
  393. # </div>
  394. #
  395. # The outermost tags must start at the left margin for this to match, and
  396. # the inner nested divs must be indented.
  397. # We need to do this before the next, more liberal match, because the next
  398. # match will start at the first `<div>` and stop at the first `</div>`.
  399. text = self._strict_tag_block_re.sub(hash_html_block_sub, text)
  400. # Now match more liberally, simply from `\n<tag>` to `</tag>\n`
  401. text = self._liberal_tag_block_re.sub(hash_html_block_sub, text)
  402. # Special case just for <hr />. It was easier to make a special
  403. # case than to make the other regex more complicated.
  404. if "<hr" in text:
  405. _hr_tag_re = _hr_tag_re_from_tab_width(self.tab_width)
  406. text = _hr_tag_re.sub(hash_html_block_sub, text)
  407. # Special case for standalone HTML comments:
  408. if "<!--" in text:
  409. start = 0
  410. while True:
  411. # Delimiters for next comment block.
  412. try:
  413. start_idx = text.index("<!--", start)
  414. except ValueError, ex:
  415. break
  416. try:
  417. end_idx = text.index("-->", start_idx) + 3
  418. except ValueError, ex:
  419. break
  420. # Start position for next comment block search.
  421. start = end_idx
  422. # Validate whitespace before comment.
  423. if start_idx:
  424. # - Up to `tab_width - 1` spaces before start_idx.
  425. for i in range(self.tab_width - 1):
  426. if text[start_idx - 1] != ' ':
  427. break
  428. start_idx -= 1
  429. if start_idx == 0:
  430. break
  431. # - Must be preceded by 2 newlines or hit the start of
  432. # the document.
  433. if start_idx == 0:
  434. pass
  435. elif start_idx == 1 and text[0] == '\n':
  436. start_idx = 0 # to match minute detail of Markdown.pl regex
  437. elif text[start_idx-2:start_idx] == '\n\n':
  438. pass
  439. else:
  440. break
  441. # Validate whitespace after comment.
  442. # - Any number of spaces and tabs.
  443. while end_idx < len(text):
  444. if text[end_idx] not in ' \t':
  445. break
  446. end_idx += 1
  447. # - Must be following by 2 newlines or hit end of text.
  448. if text[end_idx:end_idx+2] not in ('', '\n', '\n\n'):
  449. continue
  450. # Escape and hash (must match `_hash_html_block_sub`).
  451. html = text[start_idx:end_idx]
  452. if raw and self.safe_mode:
  453. html = self._sanitize_html(html)
  454. key = _hash_text(html)
  455. self.html_blocks[key] = html
  456. text = text[:start_idx] + "\n\n" + key + "\n\n" + text[end_idx:]
  457. if "xml" in self.extras:
  458. # Treat XML processing instructions and namespaced one-liner
  459. # tags as if they were block HTML tags. E.g., if standalone
  460. # (i.e. are their own paragraph), the following do not get
  461. # wrapped in a <p> tag:
  462. # <?foo bar?>
  463. #
  464. # <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="chapter_1.md"/>
  465. _xml_oneliner_re = _xml_oneliner_re_from_tab_width(self.tab_width)
  466. text = _xml_oneliner_re.sub(hash_html_block_sub, text)
  467. return text
  468. def _strip_link_definitions(self, text):
  469. # Strips link definitions from text, stores the URLs and titles in
  470. # hash references.
  471. less_than_tab = self.tab_width - 1
  472. # Link defs are in the form:
  473. # [id]: url "optional title"
  474. _link_def_re = re.compile(r"""
  475. ^[ ]{0,%d}\[(.+)\]: # id = \1
  476. [ \t]*
  477. \n? # maybe *one* newline
  478. [ \t]*
  479. <?(.+?)>? # url = \2
  480. [ \t]*
  481. (?:
  482. \n? # maybe one newline
  483. [ \t]*
  484. (?<=\s) # lookbehind for whitespace
  485. ['"(]
  486. ([^\n]*) # title = \3
  487. ['")]
  488. [ \t]*
  489. )? # title is optional
  490. (?:\n+|\Z)
  491. """ % less_than_tab, re.X | re.M | re.U)
  492. return _link_def_re.sub(self._extract_link_def_sub, text)
  493. def _extract_link_def_sub(self, match):
  494. id, url, title = match.groups()
  495. key = id.lower() # Link IDs are case-insensitive
  496. self.urls[key] = self._encode_amps_and_angles(url)
  497. if title:
  498. self.titles[key] = title.replace('"', '&quot;')
  499. return ""
  500. def _extract_footnote_def_sub(self, match):
  501. id, text = match.groups()
  502. text = _dedent(text, skip_first_line=not text.startswith('\n')).strip()
  503. normed_id = re.sub(r'\W', '-', id)
  504. # Ensure footnote text ends with a couple newlines (for some
  505. # block gamut matches).
  506. self.footnotes[normed_id] = text + "\n\n"
  507. return ""
  508. def _strip_footnote_definitions(self, text):
  509. """A footnote definition looks like this:
  510. [^note-id]: Text of the note.
  511. May include one or more indented paragraphs.
  512. Where,
  513. - The 'note-id' can be pretty much anything, though typically it
  514. is the number of the footnote.
  515. - The first paragraph may start on the next line, like so:
  516. [^note-id]:
  517. Text of the note.
  518. """
  519. less_than_tab = self.tab_width - 1
  520. footnote_def_re = re.compile(r'''
  521. ^[ ]{0,%d}\[\^(.+)\]: # id = \1
  522. [ \t]*
  523. ( # footnote text = \2
  524. # First line need not start with the spaces.
  525. (?:\s*.*\n+)
  526. (?:
  527. (?:[ ]{%d} | \t) # Subsequent lines must be indented.
  528. .*\n+
  529. )*
  530. )
  531. # Lookahead for non-space at line-start, or end of doc.
  532. (?:(?=^[ ]{0,%d}\S)|\Z)
  533. ''' % (less_than_tab, self.tab_width, self.tab_width),
  534. re.X | re.M)
  535. return footnote_def_re.sub(self._extract_footnote_def_sub, text)
  536. _hr_res = [
  537. re.compile(r"^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$", re.M),
  538. re.compile(r"^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$", re.M),
  539. re.compile(r"^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$", re.M),
  540. ]
  541. def _run_block_gamut(self, text):
  542. # These are all the transformations that form block-level
  543. # tags like paragraphs, headers, and list items.
  544. text = self._do_headers(text)
  545. # Do Horizontal Rules:
  546. hr = "\n<hr"+self.empty_element_suffix+"\n"
  547. for hr_re in self._hr_res:
  548. text = hr_re.sub(hr, text)
  549. text = self._do_lists(text)
  550. if "pyshell" in self.extras:
  551. text = self._prepare_pyshell_blocks(text)
  552. text = self._do_code_blocks(text)
  553. text = self._do_block_quotes(text)
  554. # We already ran _HashHTMLBlocks() before, in Markdown(), but that
  555. # was to escape raw HTML in the original Markdown source. This time,
  556. # we're escaping the markup we've just created, so that we don't wrap
  557. # <p> tags around block-level tags.
  558. text = self._hash_html_blocks(text)
  559. text = self._form_paragraphs(text)
  560. return text
  561. def _pyshell_block_sub(self, match):
  562. lines = match.group(0).splitlines(0)
  563. _dedentlines(lines)
  564. indent = ' ' * self.tab_width
  565. s = ('\n' # separate from possible cuddled paragraph
  566. + indent + ('\n'+indent).join(lines)
  567. + '\n\n')
  568. return s
  569. def _prepare_pyshell_blocks(self, text):
  570. """Ensure that Python interactive shell sessions are put in
  571. code blocks -- even if not properly indented.
  572. """
  573. if ">>>" not in text:
  574. return text
  575. less_than_tab = self.tab_width - 1
  576. _pyshell_block_re = re.compile(r"""
  577. ^([ ]{0,%d})>>>[ ].*\n # first line
  578. ^(\1.*\S+.*\n)* # any number of subsequent lines
  579. ^\n # ends with a blank line
  580. """ % less_than_tab, re.M | re.X)
  581. return _pyshell_block_re.sub(self._pyshell_block_sub, text)
  582. def _run_span_gamut(self, text):
  583. # These are all the transformations that occur *within* block-level
  584. # tags like paragraphs, headers, and list items.
  585. text = self._do_code_spans(text)
  586. text = self._escape_special_chars(text)
  587. # Process anchor and image tags.
  588. text = self._do_links(text)
  589. # Make links out of things like `<http://example.com/>`
  590. # Must come after _do_links(), because you can use < and >
  591. # delimiters in inline links like [this](<url>).
  592. text = self._do_auto_links(text)
  593. if "link-patterns" in self.extras:
  594. text = self._do_link_patterns(text)
  595. text = self._encode_amps_and_angles(text)
  596. text = self._do_italics_and_bold(text)
  597. # Do hard breaks:
  598. text = re.sub(r"\n", " <br%s\n" % self.empty_element_suffix, text)
  599. return text
  600. # "Sorta" because auto-links are identified as "tag" tokens.
  601. _sorta_html_tokenize_re = re.compile(r"""
  602. (
  603. # tag
  604. </?
  605. (?:\w+) # tag name
  606. (?:\s+(?:[\w-]+:)?[\w-]+=(?:".*?"|'.*?'))* # attributes
  607. \s*/?>
  608. |
  609. # auto-link (e.g., <http://www.activestate.com/>)
  610. <\w+[^>]*>
  611. |
  612. <!--.*?--> # comment
  613. |
  614. <\?.*?\?> # processing instruction
  615. )
  616. """, re.X)
  617. def _escape_special_chars(self, text):
  618. # Python markdown note: the HTML tokenization here differs from
  619. # that in Markdown.pl, hence the behaviour for subtle cases can
  620. # differ (I believe the tokenizer here does a better job because
  621. # it isn't susceptible to unmatched '<' and '>' in HTML tags).
  622. # Note, however, that '>' is not allowed in an auto-link URL
  623. # here.
  624. escaped = []
  625. is_html_markup = False
  626. for token in self._sorta_html_tokenize_re.split(text):
  627. if is_html_markup:
  628. # Within tags/HTML-comments/auto-links, encode * and _
  629. # so they don't conflict with their use in Markdown for
  630. # italics and strong. We're replacing each such
  631. # character with its corresponding MD5 checksum value;
  632. # this is likely overkill, but it should prevent us from
  633. # colliding with the escape values by accident.
  634. escaped.append(token.replace('*', g_escape_table['*'])
  635. .replace('_', g_escape_table['_']))
  636. else:
  637. escaped.append(self._encode_backslash_escapes(token))
  638. is_html_markup = not is_html_markup
  639. return ''.join(escaped)
  640. def _hash_html_spans(self, text):
  641. # Used for safe_mode.
  642. def _is_auto_link(s):
  643. if ':' in s and self._auto_link_re.match(s):
  644. return True
  645. elif '@' in s and self._auto_email_link_re.match(s):
  646. return True
  647. return False
  648. tokens = []
  649. is_html_markup = False
  650. for token in self._sorta_html_tokenize_re.split(text):
  651. if is_html_markup and not _is_auto_link(token):
  652. sanitized = self._sanitize_html(token)
  653. key = _hash_text(sanitized)
  654. self.html_spans[key] = sanitized
  655. tokens.append(key)
  656. else:
  657. tokens.append(token)
  658. is_html_markup = not is_html_markup
  659. return ''.join(tokens)
  660. def _unhash_html_spans(self, text):
  661. for key, sanitized in self.html_spans.items():
  662. text = text.replace(key, sanitized)
  663. return text
  664. def _sanitize_html(self, s):
  665. if self.safe_mode == "replace":
  666. return self.html_removed_text
  667. elif self.safe_mode == "escape":
  668. replacements = [
  669. ('&', '&amp;'),
  670. ('<', '&lt;'),
  671. ('>', '&gt;'),
  672. ]
  673. for before, after in replacements:
  674. s = s.replace(before, after)
  675. return s
  676. else:
  677. raise MarkdownError("invalid value for 'safe_mode': %r (must be "
  678. "'escape' or 'replace')" % self.safe_mode)
  679. _tail_of_inline_link_re = re.compile(r'''
  680. # Match tail of: [text](/url/) or [text](/url/ "title")
  681. \( # literal paren
  682. [ \t]*
  683. (?P<url> # \1
  684. <.*?>
  685. |
  686. .*?
  687. )
  688. [ \t]*
  689. ( # \2
  690. (['"]) # quote char = \3
  691. (?P<title>.*?)
  692. \3 # matching quote
  693. )? # title is optional
  694. \)
  695. ''', re.X | re.S)
  696. _tail_of_reference_link_re = re.compile(r'''
  697. # Match tail of: [text][id]
  698. [ ]? # one optional space
  699. (?:\n[ ]*)? # one optional newline followed by spaces
  700. \[
  701. (?P<id>.*?)
  702. \]
  703. ''', re.X | re.S)
  704. def _do_links(self, text):
  705. """Turn Markdown link shortcuts into XHTML <a> and <img> tags.
  706. This is a combination of Markdown.pl's _DoAnchors() and
  707. _DoImages(). They are done together because that simplified the
  708. approach. It was necessary to use a different approach than
  709. Markdown.pl because of the lack of atomic matching support in
  710. Python's regex engine used in $g_nested_brackets.
  711. """
  712. MAX_LINK_TEXT_SENTINEL = 3000 # markdown2 issue 24
  713. # `anchor_allowed_pos` is used to support img links inside
  714. # anchors, but not anchors inside anchors. An anchor's start
  715. # pos must be `>= anchor_allowed_pos`.
  716. anchor_allowed_pos = 0
  717. curr_pos = 0
  718. while True: # Handle the next link.
  719. # The next '[' is the start of:
  720. # - an inline anchor: [text](url "title")
  721. # - a reference anchor: [text][id]
  722. # - an inline img: ![text](url "title")
  723. # - a reference img: ![text][id]
  724. # - a footnote ref: [^id]
  725. # (Only if 'footnotes' extra enabled)
  726. # - a footnote defn: [^id]: ...
  727. # (Only if 'footnotes' extra enabled) These have already
  728. # been stripped in _strip_footnote_definitions() so no
  729. # need to watch for them.
  730. # - a link definition: [id]: url "title"
  731. # These have already been stripped in
  732. # _strip_link_definitions() so no need to watch for them.
  733. # - not markup: [...anything else...
  734. try:
  735. start_idx = text.index('[', curr_pos)
  736. except ValueError:
  737. break
  738. text_length = len(text)
  739. # Find the matching closing ']'.
  740. # Markdown.pl allows *matching* brackets in link text so we
  741. # will here too. Markdown.pl *doesn't* currently allow
  742. # matching brackets in img alt text -- we'll differ in that
  743. # regard.
  744. bracket_depth = 0
  745. for p in range(start_idx+1, min(start_idx+MAX_LINK_TEXT_SENTINEL,
  746. text_length)):
  747. ch = text[p]
  748. if ch == ']':
  749. bracket_depth -= 1
  750. if bracket_depth < 0:
  751. break
  752. elif ch == '[':
  753. bracket_depth += 1
  754. else:
  755. # Closing bracket not found within sentinel length.
  756. # This isn't markup.
  757. curr_pos = start_idx + 1
  758. continue
  759. link_text = text[start_idx+1:p]
  760. # Possibly a footnote ref?
  761. if "footnotes" in self.extras and link_text.startswith("^"):
  762. normed_id = re.sub(r'\W', '-', link_text[1:])
  763. if normed_id in self.footnotes:
  764. self.footnote_ids.append(normed_id)
  765. result = '<sup class="footnote-ref" id="fnref-%s">' \
  766. '<a href="#fn-%s">%s</a></sup>' \
  767. % (normed_id, normed_id, len(self.footnote_ids))
  768. text = text[:start_idx] + result + text[p+1:]
  769. else:
  770. # This id isn't defined, leave the markup alone.
  771. curr_pos = p+1
  772. continue
  773. # Now determine what this is by the remainder.
  774. p += 1
  775. if p == text_length:
  776. return text
  777. # Inline anchor or img?
  778. if text[p] == '(': # attempt at perf improvement
  779. match = self._tail_of_inline_link_re.match(text, p)
  780. if match:
  781. # Handle an inline anchor or img.
  782. is_img = start_idx > 0 and text[start_idx-1] == "!"
  783. if is_img:
  784. start_idx -= 1
  785. url, title = match.group("url"), match.group("title")
  786. if url and url[0] == '<':
  787. url = url[1:-1] # '<url>' -> 'url'
  788. # We've got to encode these to avoid conflicting
  789. # with italics/bold.
  790. url = url.replace('*', g_escape_table['*']) \
  791. .replace('_', g_escape_table['_'])
  792. if title:
  793. title_str = ' title="%s"' \
  794. % title.replace('*', g_escape_table['*']) \
  795. .replace('_', g_escape_table['_']) \
  796. .replace('"', '&quot;')
  797. else:
  798. title_str = ''
  799. if is_img:
  800. result = '<img src="%s" class="thumbnail" alt="%s"%s%s' \
  801. % (url, link_text.replace('"', '&quot;'),
  802. title_str, self.empty_element_suffix)
  803. curr_pos = start_idx + len(result)
  804. text = text[:start_idx] + result + text[match.end():]
  805. elif start_idx >= anchor_allowed_pos:
  806. result_head = '<a href="%s"%s>' % (url, title_str)
  807. result = '%s%s</a>' % (result_head, link_text)
  808. # <img> allowed from curr_pos on, <a> from
  809. # anchor_allowed_pos on.
  810. curr_pos = start_idx + len(result_head)
  811. anchor_allowed_pos = start_idx + len(result)
  812. text = text[:start_idx] + result + text[match.end():]
  813. else:
  814. # Anchor not allowed here.
  815. curr_pos = start_idx + 1
  816. continue
  817. # Reference anchor or img?
  818. else:
  819. match = self._tail_of_reference_link_re.match(text, p)
  820. if match:
  821. # Handle a reference-style anchor or img.
  822. is_img = start_idx > 0 and text[start_idx-1] == "!"
  823. if is_img:
  824. start_idx -= 1
  825. link_id = match.group("id").lower()
  826. if not link_id:
  827. link_id = link_text.lower() # for links like [this][]
  828. if link_id in self.urls:
  829. url = self.urls[link_id]
  830. # We've got to encode these to avoid conflicting
  831. # with italics/bold.
  832. url = url.replace('*', g_escape_table['*']) \
  833. .replace('_', g_escape_table['_'])
  834. title = self.titles.get(link_id)
  835. if title:
  836. title = title.replace('*', g_escape_table['*']) \
  837. .replace('_', g_escape_table['_'])
  838. title_str = ' title="%s"' % title
  839. else:
  840. title_str = ''
  841. if is_img:
  842. result = '<img src="%s" class="thumbnail" ' \
  843. 'alt="%s"%s%s' \
  844. % (url, link_text.replace('"', '&quot;'),
  845. title_str, self.empty_element_suffix)
  846. curr_pos = start_idx + len(result)
  847. text = text[:start_idx] + result + text[match.end():]
  848. elif start_idx >= anchor_allowed_pos:
  849. result = '<a href="%s"%s>%s</a>' \
  850. % (url, title_str, link_text)
  851. result_head = '<a href="%s"%s>' % (url, title_str)
  852. result = '%s%s</a>' % (result_head, link_text)
  853. # <img> allowed from curr_pos on, <a> from
  854. # anchor_allowed_pos on.
  855. curr_pos = start_idx + len(result_head)
  856. anchor_allowed_pos = start_idx + len(result)
  857. text = text[:start_idx] + result + text[match.end():]
  858. else:
  859. # Anchor not allowed here.
  860. curr_pos = start_idx + 1
  861. else:
  862. # This id isn't defined, leave the markup alone.
  863. curr_pos = match.end()
  864. continue
  865. # Otherwise, it isn't markup.
  866. curr_pos = start_idx + 1
  867. return text
  868. _setext_h_re = re.compile(r'^(.+)[ \t]*\n(=+|-+)[ \t]*\n+', re.M)
  869. def _setext_h_sub(self, match):
  870. n = {"=": 1, "-": 2}[match.group(2)[0]]
  871. demote_headers = self.extras.get("demote-headers")
  872. if demote_headers:
  873. n = min(n + demote_headers, 6)
  874. return "<h%d>%s</h%d>\n\n" \
  875. % (n, self._run_span_gamut(match.group(1)), n)
  876. _atx_h_re = re.compile(r'''
  877. ^(\#{1,6}) # \1 = string of #'s
  878. [ \t]*
  879. (.+?) # \2 = Header text
  880. [ \t]*
  881. (?<!\\) # ensure not an escaped trailing '#'
  882. \#* # optional closing #'s (not counted)
  883. \n+
  884. ''', re.X | re.M)
  885. def _atx_h_sub(self, match):
  886. n = len(match.group(1))
  887. demote_headers = self.extras.get("demote-headers")
  888. if demote_headers:
  889. n = min(n + demote_headers, 6)
  890. return "<h%d>%s</h%d>\n\n" \
  891. % (n, self._run_span_gamut(match.group(2)), n)
  892. def _do_headers(self, text):
  893. # Setext-style headers:
  894. # Header 1
  895. # ========
  896. #
  897. # Header 2
  898. # --------
  899. text = self._setext_h_re.sub(self._setext_h_sub, text)
  900. # atx-style headers:
  901. # # Header 1
  902. # ## Header 2
  903. # ## Header 2 with closing hashes ##
  904. # ...
  905. # ###### Header 6
  906. text = self._atx_h_re.sub(self._atx_h_sub, text)
  907. return text
  908. _marker_ul_chars = '*+-'
  909. _marker_any = r'(?:[%s]|\d+\.)' % _marker_ul_chars
  910. _marker_ul = '(?:[%s])' % _marker_ul_chars
  911. _marker_ol = r'(?:\d+\.)'
  912. def _list_sub(self, match):
  913. lst = match.group(1)
  914. lst_type = match.group(3) in self._marker_ul_chars and "ul" or "ol"
  915. result = self._process_list_items(lst)
  916. if self.list_level:
  917. return "<%s>\n%s</%s>\n" % (lst_type, result, lst_type)
  918. else:
  919. return "<%s>\n%s</%s>\n\n" % (lst_type, result, lst_type)
  920. def _do_lists(self, text):
  921. # Form HTML ordered (numbered) and unordered (bulleted) lists.
  922. for marker_pat in (self._marker_ul, self._marker_ol):
  923. # Re-usable pattern to match any entire ul or ol list:
  924. less_than_tab = self.tab_width - 1
  925. whole_list = r'''
  926. ( # \1 = whole list
  927. ( # \2
  928. [ ]{0,%d}
  929. (%s) # \3 = first list item marker
  930. [ \t]+
  931. )
  932. (?:.+?)
  933. ( # \4
  934. \Z
  935. |
  936. \n{2,}
  937. (?=\S)
  938. (?! # Negative lookahead for another list item marker
  939. [ \t]*
  940. %s[ \t]+
  941. )
  942. )
  943. )
  944. ''' % (less_than_tab, marker_pat, marker_pat)
  945. # We use a different prefix before nested lists than top-level lists.
  946. # See extended comment in _process_list_items().
  947. #
  948. # Note: There's a bit of duplication here. My original implementation
  949. # created a scalar regex pattern as the conditional result of the test on
  950. # $g_list_level, and then only ran the $text =~ s{...}{...}egmx
  951. # substitution once, using the scalar as the pattern. This worked,
  952. # everywhere except when running under MT on my hosting account at Pair
  953. # Networks. There, this caused all rebuilds to be killed by the reaper (or
  954. # perhaps they crashed, but that seems incredibly unlikely given that the
  955. # same script on the same server ran fine *except* under MT. I've spent
  956. # more time trying to figure out why this is happening than I'd like to
  957. # admit. My only guess, backed up by the fact that this workaround works,
  958. # is that Perl optimizes the substition when it can figure out that the
  959. # pattern will never change, and when this optimization isn't on, we run
  960. # afoul of the reaper. Thus, the slightly redundant code to that uses two
  961. # static s/// patterns rather than one conditional pattern.
  962. if self.list_level:
  963. sub_list_re = re.compile("^"+whole_list, re.X | re.M | re.S)
  964. text = sub_list_re.sub(self._list_sub, text)
  965. else:
  966. list_re = re.compile(r"(?:(?<=\n\n)|\A\n?)"+whole_list,
  967. re.X | re.M | re.S)
  968. text = list_re.sub(self._list_sub, text)
  969. return text
  970. _list_item_re = re.compile(r'''
  971. (\n)? # leading line = \1
  972. (^[ \t]*) # leading whitespace = \2
  973. (%s) [ \t]+ # list marker = \3
  974. ((?:.+?) # list item text = \4
  975. (\n{1,2})) # eols = \5
  976. (?= \n* (\Z | \2 (%s) [ \t]+))
  977. ''' % (_marker_any, _marker_any),
  978. re.M | re.X | re.S)
  979. _last_li_endswith_two_eols = False
  980. def _list_item_sub(self, match):
  981. item = match.group(4)
  982. leading_line = match.group(1)
  983. leading_space = match.group(2)
  984. if leading_line or "\n\n" in item or self._last_li_endswith_two_eols:
  985. item = self._run_block_gamut(self._outdent(item))
  986. else:
  987. # Recursion for sub-lists:
  988. item = self._do_lists(self._outdent(item))
  989. if item.endswith('\n'):
  990. item = item[:-1]
  991. item = self._run_span_gamut(item)
  992. self._last_li_endswith_two_eols = (len(match.group(5)) == 2)
  993. return "<li>%s</li>\n" % item
  994. def _process_list_items(self, list_str):
  995. # Process the contents of a single ordered or unordered list,
  996. # splitting it into individual list items.
  997. # The $g_list_level global keeps track of when we're inside a list.
  998. # Each time we enter a list, we increment it; when we leave a list,
  999. # we decrement. If it's zero, we're not in a list anymore.
  1000. #
  1001. # We do this because when we're not inside a list, we want to treat
  1002. # something like this:
  1003. #
  1004. # I recommend upgrading to version
  1005. # 8. Oops, now this line is treated
  1006. # as a sub-list.
  1007. #
  1008. # As a single paragraph, despite the fact that the second line starts
  1009. # with a digit-period-space sequence.
  1010. #
  1011. # Whereas when we're inside a list (or sub-list), that line will be
  1012. # treated as the start of a sub-list. What a kludge, huh? This is
  1013. # an aspect of Markdown's syntax that's hard to parse perfectly
  1014. # without resorting to mind-reading. Perhaps the solution is to
  1015. # change the syntax rules such that sub-lists must start with a
  1016. # starting cardinal number; e.g. "1." or "a.".
  1017. self.list_level += 1
  1018. self._last_li_endswith_two_eols = False
  1019. list_str = list_str.rstrip('\n') + '\n'
  1020. list_str = self._list_item_re.sub(self._list_item_sub, list_str)
  1021. self.list_level -= 1
  1022. return list_str
  1023. def _get_pygments_lexer(self, lexer_name):
  1024. try:
  1025. from pygments import lexers, util
  1026. except ImportError:
  1027. return None
  1028. try:
  1029. return lexers.get_lexer_by_name(lexer_name)
  1030. except util.ClassNotFound:
  1031. return None
  1032. def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
  1033. import pygments
  1034. import pygments.formatters
  1035. class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
  1036. def _wrap_code(self, inner):
  1037. """A function for use in a Pygments Formatter which
  1038. wraps in <code> tags.
  1039. """
  1040. yield 0, "<code>"
  1041. for tup in inner:
  1042. yield tup
  1043. yield 0, "</code>"
  1044. def wrap(self, source, outfile):
  1045. """Return the source with a code, pre, and div."""
  1046. return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
  1047. formatter = HtmlCodeFormatter(cssclass="codehilite", **formatter_opts)
  1048. return pygments.highlight(codeblock, lexer, formatter)
  1049. def _code_block_sub(self, match):
  1050. codeblock = match.group(1)
  1051. codeblock = self._outdent(codeblock)
  1052. codeblock = self._detab(codeblock)
  1053. codeblock = codeblock.lstrip('\n') # trim leading newlines
  1054. codeblock = codeblock.rstrip() # trim trailing whitespace
  1055. if "code-color" in self.extras and codeblock.startswith(":::"):
  1056. lexer_name, rest = codeblock.split('\n', 1)
  1057. lexer_name = lexer_name[3:].strip()
  1058. lexer = self._get_pygments_lexer(lexer_name)
  1059. codeblock = rest.lstrip("\n") # Remove lexer declaration line.
  1060. if lexer:
  1061. formatter_opts = self.extras['code-color'] or {}
  1062. colored = self._color_with_pygments(codeblock, lexer,
  1063. **formatter_opts)
  1064. return "\n\n%s\n\n" % colored
  1065. codeblock = self._encode_code(codeblock)
  1066. return "\n\n<pre><code>%s\n</code></pre>\n\n" % codeblock
  1067. def _do_code_blocks(self, text):
  1068. """Process Markdown `<pre><code>` blocks."""
  1069. code_block_re = re.compile(r'''
  1070. (?:\n\n|\A)
  1071. ( # $1 = the code block -- one or more lines, starting with a space/tab
  1072. (?:
  1073. (?:[ ]{%d} | \t) # Lines must start with a tab or a tab-width of spaces
  1074. .*\n+
  1075. )+
  1076. )
  1077. ((?=^[ ]{0,%d}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
  1078. ''' % (self.tab_width, self.tab_width),
  1079. re.M | re.X)
  1080. return code_block_re.sub(self._code_block_sub, text)
  1081. # Rules for a code span:
  1082. # - backslash escapes are not interpreted in a code span
  1083. # - to include one or or a run of more backticks the delimiters must
  1084. # be a longer run of backticks
  1085. # - cannot start or end a code span with a backtick; pad with a
  1086. # space and that space will be removed in the emitted HTML
  1087. # See `test/tm-cases/escapes.text` for a number of edge-case
  1088. # examples.
  1089. _code_span_re = re.compile(r'''
  1090. (?<!\\)
  1091. (`+) # \1 = Opening run of `
  1092. (?!`) # See Note A test/tm-cases/escapes.text
  1093. (.+?) # \2 = The code block
  1094. (?<!`)
  1095. \1 # Matching closer
  1096. (?!`)
  1097. ''', re.X | re.S)
  1098. def _code_span_sub(self, match):
  1099. c = match.group(2).strip(" \t")
  1100. c = self._encode_code(c)
  1101. return "<code>%s</code>" % c
  1102. def _do_code_spans(self, text):
  1103. # * Backtick quotes are used for <code></code> spans.
  1104. #
  1105. # * You can use multiple backticks as the delimiters if you want to
  1106. # include literal backticks in the code span. So, this input:
  1107. #
  1108. # Just type ``foo `bar` baz`` at the prompt.
  1109. #
  1110. # Will translate to:
  1111. #
  1112. # <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
  1113. #
  1114. # There's no arbitrary limit to the number of backticks you
  1115. # can use as delimters. If you need three consecutive backticks
  1116. # in your code, use four for delimiters, etc.
  1117. #
  1118. # * You can use spaces to get literal backticks at the edges:
  1119. #
  1120. # ... type `` `bar` `` ...
  1121. #
  1122. # Turns to:
  1123. #
  1124. # ... type <code>`bar`</code> ...
  1125. return self._code_span_re.sub(self._code_span_sub, text)
  1126. def _encode_code(self, text):
  1127. """Encode/escape certain characters inside Markdown code runs.
  1128. The point is that in code, these characters are literals,
  1129. and lose their special Markdown meanings.
  1130. """
  1131. replacements = [
  1132. # Encode all ampersands; HTML entities are not
  1133. # entities within a Markdown code span.
  1134. ('&', '&amp;'),
  1135. # Do the angle bracket song and dance:
  1136. ('<', '&lt;'),
  1137. ('>', '&gt;'),
  1138. # Now, escape characters that are magic in Markdown:
  1139. ('*', g_escape_table['*']),
  1140. ('_', g_escape_table['_']),
  1141. ('{', g_escape_table['{']),
  1142. ('}', g_escape_table['}']),
  1143. ('[', g_escape_table['[']),
  1144. (']', g_escape_table[']']),
  1145. ('\\', g_escape_table['\\']),
  1146. ]
  1147. for before, after in replacements:
  1148. text = text.replace(before, after)
  1149. return text
  1150. _strong_re = re.compile(r"(\*\*|__)(?=\S)(.+?[*_]*)(?<=\S)\1", re.S)
  1151. _em_re = re.compile(r"(\*|_)(?=\S)(.+?)(?<=\S)\1", re.S)
  1152. _code_friendly_strong_re = re.compile(r"\*\*(?=\S)(.+?[*_]*)(?<=\S)\*\*", re.S)
  1153. _code_friendly_em_re = re.compile(r"\*(?=\S)(.+?)(?<=\S)\*", re.S)
  1154. def _do_italics_and_bold(self, text):
  1155. # <strong> must go first:
  1156. if "code-friendly" in self.extras:
  1157. text = self._code_friendly_strong_re.sub(r"<strong>\1</strong>", text)
  1158. text = self._code_friendly_em_re.sub(r"<em>\1</em>", text)
  1159. else:
  1160. text = self._strong_re.sub(r"<strong>\2</strong>", text)
  1161. text = self._em_re.sub(r"<em>\2</em>", text)
  1162. return text
  1163. _block_quote_re = re.compile(r'''
  1164. ( # Wrap whole match in \1
  1165. (
  1166. ^[ \t]*>[ \t]? # '>' at the start of a line
  1167. .+\n # rest of the first line
  1168. (.+\n)* # subsequent consecutive lines
  1169. \n* # blanks
  1170. )+
  1171. )
  1172. ''', re.M | re.X)
  1173. _bq_one_level_re = re.compile('^[ \t]*>[ \t]?', re.M);
  1174. _html_pre_block_re = re.compile(r'(\s*<pre>.+?</pre>)', re.S)
  1175. def _dedent_two_spaces_sub(self, match):
  1176. return re.sub(r'(?m)^ ', '', match.group(1))
  1177. def _block_quote_sub(self, match):
  1178. bq = match.group(1)
  1179. bq = self._bq_one_level_re.sub('', bq) # trim one level of quoting
  1180. bq = self._ws_only_line_re.sub('', bq) # trim whitespace-only lines
  1181. bq = self._run_block_gamut(bq) # recurse
  1182. bq = re.sub('(?m)^', ' ', bq)
  1183. # These leading spaces screw with <pre> content, so we need to fix that:
  1184. bq = self._html_pre_block_re.sub(self._dedent_two_spaces_sub, bq)
  1185. return "<blockquote>\n%s\n</blockquote>\n\n" % bq
  1186. def _do_block_quotes(self, text):
  1187. if '>' not in text:
  1188. return text
  1189. return self._block_quote_re.sub(self._block_quote_sub, text)
  1190. def _form_paragraphs(self, text):
  1191. # Strip leading and trailing lines:
  1192. text = text.strip('\n')
  1193. # Wrap <p> tags.
  1194. grafs = re.split(r"\n{2,}", text)
  1195. for i, graf in enumerate(grafs):
  1196. if graf in self.html_blocks:
  1197. # Unhashify HTML blocks
  1198. grafs[i] = self.html_blocks[graf]
  1199. else:
  1200. # Wrap <p> tags.
  1201. graf = self._run_span_gamut(graf)
  1202. grafs[i] = "<p>" + graf.lstrip(" \t") + "</p>"
  1203. return "\n\n".join(grafs)
  1204. def _add_footnotes(self, text):
  1205. if self.footnotes:
  1206. footer = [
  1207. '<div class="footnotes">',
  1208. '<hr' + self.empty_element_suffix,
  1209. '<ol>',
  1210. ]
  1211. for i, id in enumerate(self.footnote_ids):
  1212. if i != 0:
  1213. footer.append('')
  1214. footer.append('<li id="fn-%s">' % id)
  1215. footer.append(self._run_block_gamut(self.footnotes[id]))
  1216. backlink = ('<a href="#fnref-%s" '
  1217. 'class="footnoteBackLink" '
  1218. 'title="Jump back to footnote %d in the text.">'
  1219. '&#8617;</a>' % (id, i+1))
  1220. if footer[-1].endswith("</p>"):
  1221. footer[-1] = footer[-1][:-len("</p>")] \
  1222. + '&nbsp;' + backlink + "</p>"
  1223. else:
  1224. footer.append("\n<p>%s</p>" % backlink)
  1225. footer.append('</li>')
  1226. footer.append('</ol>')
  1227. footer.append('</div>')
  1228. return text + '\n\n' + '\n'.join(footer)
  1229. else:
  1230. return text
  1231. # Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
  1232. # http://bumppo.net/projects/amputator/
  1233. _ampersand_re = re.compile(r'&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)')
  1234. _naked_lt_re = re.compile(r'<(?![a-z/?\$!])', re.I)
  1235. _naked_gt_re = re.compile(r'''(?<![a-z?!/'"-])>''', re.I)
  1236. def _encode_amps_and_angles(self, text):
  1237. # Smart processing for ampersands and angle brackets that need
  1238. # to be encoded.
  1239. text = self._ampersand_re.sub('&amp;', text)
  1240. # Encode naked <'s
  1241. text = self._naked_lt_re.sub('&lt;', text)
  1242. # Encode naked >'s
  1243. # Note: Other markdown implementations (e.g. Markdown.pl, PHP
  1244. # Markdown) don't do this.
  1245. text = self._naked_gt_re.sub('&gt;', text)
  1246. return text
  1247. def _encode_backslash_escapes(self, text):
  1248. for ch, escape in g_escape_table.items():
  1249. text = text.replace("\\"+ch, escape)
  1250. return text
  1251. _auto_link_re = re.compile(r'<((https?|ftp):[^\'">\s]+)>', re.I)
  1252. def _auto_link_sub(self, match):
  1253. g1 = match.group(1)
  1254. return '<a href="%s">%s</a>' % (g1, g1)
  1255. _auto_email_link_re = re.compile(r"""
  1256. <
  1257. (?:mailto:)?
  1258. (
  1259. [-.\w]+
  1260. \@
  1261. [-\w]+(\.[-\w]+)*\.[a-z]+
  1262. )
  1263. >
  1264. """, re.I | re.X | re.U)
  1265. def _auto_email_link_sub(self, match):
  1266. return self._encode_email_address(
  1267. self._unescape_special_chars(match.group(1)))
  1268. def _do_auto_links(self, text):
  1269. text = self._auto_link_re.sub(self._auto_link_sub, text)
  1270. text = self._auto_email_link_re.sub(self._auto_email_link_sub, text)
  1271. return text
  1272. def _encode_email_address(self, addr):
  1273. # Input: an email address, e.g. "foo@example.com"
  1274. #
  1275. # Output: the email address as a mailto link, with each character
  1276. # of the address encoded as either a decimal or hex entity, in
  1277. # the hopes of foiling most address harvesting spam bots. E.g.:
  1278. #
  1279. # <a href="&#x6D;&#97;&#105;&#108;&#x74;&#111;:&#102;&#111;&#111;&#64;&#101;
  1280. # x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;">&#102;&#111;&#111;
  1281. # &#64;&#101;x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;</a>
  1282. #
  1283. # Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
  1284. # mailing list: <http://tinyurl.com/yu7ue>
  1285. chars = [_xml_encode_email_char_at_random(ch)
  1286. for ch in "mailto:" + addr]
  1287. # Strip the mailto: from the visible part.
  1288. addr = '<a href="%s">%s</a>' \
  1289. % (''.join(chars), ''.join(chars[7:]))
  1290. return addr
  1291. def _do_link_patterns(self, text):
  1292. """Caveat emptor: there isn't much guarding against link
  1293. patterns being formed inside other standard Markdown links, e.g.
  1294. inside a [link def][like this].
  1295. Dev Notes: *Could* consider prefixing regexes with a negative
  1296. lookbehind assertion to attempt to guard against this.
  1297. """
  1298. link_from_hash = {}
  1299. for regex, repl in self.link_patterns:
  1300. replacements = []
  1301. for match in regex.finditer(text):
  1302. if hasattr(repl, "__call__"):
  1303. href = repl(match)
  1304. else:
  1305. href = match.expand(repl)
  1306. replacements.append((match.span(), href))
  1307. for (start, end), href in reversed(replacements):
  1308. escaped_href = (
  1309. href.replace('"', '&quot;') # b/c of attr quote
  1310. # To avoid markdown <em> and <strong>:
  1311. .replace('*', g_escape_table['*'])
  1312. .replace('_', g_escape_table['_']))
  1313. link = '<a href="%s">%s</a>' % (escaped_href, text[start:end])
  1314. hash = md5(link).hexdigest()
  1315. link_from_hash[hash] = link
  1316. text = text[:start] + hash + text[end:]
  1317. for hash, link in link_from_hash.items():
  1318. text = text.replace(hash, link)
  1319. return text
  1320. def _unescape_special_chars(self, text):
  1321. # Swap back in all the special characters we've hidden.
  1322. for ch, hash in g_escape_table.items():
  1323. text = text.replace(hash, ch)
  1324. return text
  1325. def _outdent(self, text):
  1326. # Remove one level of line-leading tabs or spaces
  1327. return self._outdent_re.sub('', text)
  1328. class MarkdownWithExtras(Markdown):
  1329. """A markdowner class that enables most extras:
  1330. - footnotes
  1331. - code-color (only has effect if 'pygments' Python module on path)
  1332. These are not included:
  1333. - pyshell (specific to Python-related documenting)
  1334. - code-friendly (because it *disables* part of the syntax)
  1335. - link-patterns (because you need to specify some actual
  1336. link-patterns anyway)
  1337. """
  1338. extras = ["footnotes", "code-color"]
  1339. #---- internal support functions
  1340. # From http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549
  1341. def _curry(*args, **kwargs):
  1342. function, args = args[0], args[1:]
  1343. def result(*rest, **kwrest):
  1344. combined = kwargs.copy()
  1345. combined.update(kwrest)
  1346. return function(*args + rest, **combined)
  1347. return result
  1348. # Recipe: regex_from_encoded_pattern (1.0)
  1349. def _regex_from_encoded_pattern(s):
  1350. """'foo' -> re.compile(re.escape('foo'))
  1351. '/foo/' -> re.compile('foo')
  1352. '/foo/i' -> re.compile('foo', re.I)
  1353. """
  1354. if s.startswith('/') and s.rfind('/') != 0:
  1355. # Parse it: /PATTERN/FLAGS
  1356. idx = s.rfind('/')
  1357. pattern, flags_str = s[1:idx], s[idx+1:]
  1358. flag_from_char = {
  1359. "i": re.IGNORECASE,
  1360. "l": re.LOCALE,
  1361. "s": re.DOTALL,
  1362. "m": re.MULTILINE,
  1363. "u": re.UNICODE,
  1364. }
  1365. flags = 0
  1366. for char in flags_str:
  1367. try:
  1368. flags |= flag_from_char[char]
  1369. except KeyError:
  1370. raise ValueError("unsupported regex flag: '%s' in '%s' "
  1371. "(must be one of '%s')"
  1372. % (char, s, ''.join(flag_from_char.keys())))
  1373. return re.compile(s[1:idx], flags)
  1374. else: # not an encoded regex
  1375. return re.compile(re.escape(s))
  1376. # Recipe: dedent (0.1.2)
  1377. def _dedentlines(lines, tabsize=8, skip_first_line=False):
  1378. """_dedentlines(lines, tabsize=8, skip_first_line=False) -> dedented lines
  1379. "lines" is a list of lines to dedent.
  1380. "tabsize" is the tab width to use for indent width calculations.
  1381. "skip_first_line" is a boolean indicating if the first line should
  1382. be skipped for calculating the indent width and for dedenting.
  1383. This is sometimes useful for docstrings and similar.
  1384. Same as dedent() except operates on a sequence of lines. Note: the
  1385. lines list is modified **in-place**.
  1386. """
  1387. DEBUG = False
  1388. if DEBUG:
  1389. print "dedent: dedent(..., tabsize=%d, skip_first_line=%r)"\
  1390. % (tabsize, skip_first_line)
  1391. indents = []
  1392. margin = None
  1393. for i, line in enumerate(lines):
  1394. if i == 0 and skip_first_line: continue
  1395. indent = 0
  1396. for ch in line:
  1397. if ch == ' ':
  1398. indent += 1
  1399. elif ch == '\t':
  1400. indent += tabsize - (indent % tabsize)
  1401. elif ch in '\r\n':
  1402. continue # skip all-whitespace lines
  1403. else:
  1404. break
  1405. else:
  1406. continue # skip all-whitespace lines
  1407. if DEBUG: print "dedent: indent=%d: %r" % (indent, line)
  1408. if margin is None:
  1409. margin = indent
  1410. else:
  1411. margin = min(margin, indent)
  1412. if DEBUG: print "dedent: margin=%r" % margin
  1413. if margin is not None and margin > 0:
  1414. for i, line in enumerate(lines):
  1415. if i == 0 and skip_first_line: continue
  1416. removed = 0
  1417. for j, ch in enumerate(line):
  1418. if ch == ' ':
  1419. removed += 1
  1420. elif ch == '\t':
  1421. removed += tabsize - (removed % tabsize)
  1422. elif ch in '\r\n':
  1423. if DEBUG: print "dedent: %r: EOL -> strip up to EOL" % line
  1424. lines[i] = lines[i][j:]
  1425. break
  1426. else:
  1427. raise ValueError("unexpected non-whitespace char %r in "
  1428. "line %r while removing %d-space margin"
  1429. % (ch, line, margin))
  1430. if DEBUG:
  1431. print "dedent: %r: %r -> removed %d/%d"\
  1432. % (line, ch, removed, margin)
  1433. if removed == margin:
  1434. lines[i] = lines[i][j+1:]
  1435. break
  1436. elif removed > margin:
  1437. lines[i] = ' '*(removed-margin) + lines[i][j+1:]
  1438. break
  1439. else:
  1440. if removed:
  1441. lines[i] = lines[i][removed:]
  1442. return lines
  1443. def _dedent(text, tabsize=8, skip_first_line=False):
  1444. """_dedent(text, tabsize=8, skip_first_line=False) -> dedented text
  1445. "text" is the text to dedent.
  1446. "tabsize" is the tab width to use for indent width calculations.
  1447. "skip_first_line" is a boolean indicating if the first line should
  1448. be skipped for calculating the indent width and for dedenting.
  1449. This is sometimes useful for docstrings and similar.
  1450. textwrap.dedent(s), but don't expand tabs to spaces
  1451. """
  1452. lines = text.splitlines(1)
  1453. _dedentlines(lines, tabsize=tabsize, skip_first_line=skip_first_line)
  1454. return ''.join(lines)
  1455. class _memoized(object):
  1456. """Decorator that caches a function's return value each time it is called.
  1457. If called later with the same arguments, the cached value is returned, and
  1458. not re-evaluated.
  1459. http://wiki.python.org/moin/PythonDecoratorLibrary
  1460. """
  1461. def __init__(self, func):
  1462. self.func = func
  1463. self.cache = {}
  1464. def __call__(self, *args):
  1465. try:
  1466. return self.cache[args]
  1467. except KeyError:
  1468. self.cache[args] = value = self.func(*args)
  1469. return value
  1470. except TypeError:
  1471. # uncachable -- for instance, passing a list as an argument.
  1472. # Better to not cache than to blow up entirely.
  1473. return self.func(*args)
  1474. def __repr__(self):
  1475. """Return the function's docstring."""
  1476. return self.func.__doc__
  1477. def _xml_oneliner_re_from_tab_width(tab_width):
  1478. """Standalone XML processing instruction regex."""
  1479. return re.compile(r"""
  1480. (?:
  1481. (?<=\n\n) # Starting after a blank line
  1482. | # or
  1483. \A\n? # the beginning of the doc
  1484. )
  1485. ( # save in $1
  1486. [ ]{0,%d}
  1487. (?:
  1488. <\?\w+\b\s+.*?\?> # XML processing instruction
  1489. |
  1490. <\w+:\w+\b\s+.*?/> # namespaced single tag
  1491. )
  1492. [ \t]*
  1493. (?=\n{2,}|\Z) # followed by a blank line or end of document
  1494. )
  1495. """ % (tab_width - 1), re.X)
  1496. _xml_oneliner_re_from_tab_width = _memoized(_xml_oneliner_re_from_tab_width)
  1497. def _hr_tag_re_from_tab_width(tab_width):
  1498. return re.compile(r"""
  1499. (?:
  1500. (?<=\n\n) # Starting after a blank line
  1501. | # or
  1502. \A\n? # the beginning of the doc
  1503. )
  1504. ( # save in \1
  1505. [ ]{0,%d}
  1506. <(hr) # start tag = \2
  1507. \b # word break
  1508. ([^<>])*? #
  1509. /?> # the matching end tag
  1510. [ \t]*
  1511. (?=\n{2,}|\Z) # followed by a blank line or end of document
  1512. )
  1513. """ % (tab_width - 1), re.X)
  1514. _hr_tag_re_from_tab_width = _memoized(_hr_tag_re_from_tab_width)
  1515. def _xml_encode_email_char_at_random(ch):
  1516. r = random()
  1517. # Roughly 10% raw, 45% hex, 45% dec.
  1518. # '@' *must* be encoded. I [John Gruber] insist.
  1519. # Issue 26: '_' must be encoded.
  1520. if r > 0.9 and ch not in "@_":
  1521. return ch
  1522. elif r < 0.45:
  1523. # The [1:] is to drop leading '0': 0x63 -> x63
  1524. return '&#%s;' % hex(ord(ch))[1:]
  1525. else:
  1526. return '&#%s;' % ord(ch)
  1527. def _hash_text(text):
  1528. return 'md5:'+md5(text.encode("utf-8")).hexdigest()
  1529. #---- mainline
  1530. class _NoReflowFormatter(optparse.IndentedHelpFormatter):
  1531. """An optparse formatter that does NOT reflow the description."""
  1532. def format_description(self, description):
  1533. return description or ""
  1534. def _test():
  1535. import doctest
  1536. doctest.testmod()
  1537. def main(argv=None):
  1538. if argv is None:
  1539. argv = sys.argv
  1540. if not logging.root.handlers:
  1541. logging.basicConfig()
  1542. usage = "usage: %prog [PATHS...]"
  1543. version = "%prog "+__version__
  1544. parser = optparse.OptionParser(prog="markdown2", usage=usage,
  1545. version=version, description=cmdln_desc,
  1546. formatter=_NoReflowFormatter())
  1547. parser.add_option("-v", "--verbose", dest="log_level",
  1548. action="store_const", const=logging.DEBUG,
  1549. help="more verbose output")
  1550. parser.add_option("--encoding",
  1551. help="specify encoding of text content")
  1552. parser.add_option("--html4tags", action="store_true", default=False,
  1553. help="use HTML 4 style for empty element tags")
  1554. parser.add_option("-s", "--safe", metavar="MODE", dest="safe_mode",
  1555. help="sanitize literal HTML: 'escape' escapes "
  1556. "HTML meta chars, 'replace' replaces with an "
  1557. "[HTML_REMOVED] note")
  1558. parser.add_option("-x", "--extras", action="append",
  1559. help="Turn on specific extra features (not part of "
  1560. "the core Markdown spec). Supported values: "
  1561. "'code-friendly' disables _/__ for emphasis; "
  1562. "'code-color' adds code-block syntax coloring; "
  1563. "'link-patterns' adds auto-linking based on patterns; "
  1564. "'footnotes' adds the footnotes syntax;"
  1565. "'xml' passes one-liner processing instructions and namespaced XML tags;"
  1566. "'pyshell' to put unindented Python interactive shell sessions in a <code> block.")
  1567. parser.add_option("--use-file-vars",
  1568. help="Look for and use Emacs-style 'markdown-extras' "
  1569. "file var to turn on extras. See "
  1570. "<http://code.google.com/p/python-markdown2/wiki/Extras>.")
  1571. parser.add_option("--link-patterns-file",
  1572. help="path to a link pattern file")
  1573. parser.add_option("--self-test", action="store_true",
  1574. help="run internal self-tests (some doctests)")
  1575. parser.add_option("--compare", action="store_true",
  1576. help="run against Markdown.pl as well (for testing)")
  1577. parser.set_defaults(log_level=logging.INFO, compare=False,
  1578. encoding="utf-8", safe_mode=None, use_file_vars=False)
  1579. opts, paths = parser.parse_args()
  1580. log.setLevel(opts.log_level)
  1581. if opts.self_test:
  1582. return _test()
  1583. if opts.extras:
  1584. extras = {}
  1585. for s in opts.extras:
  1586. splitter = re.compile("[,;: ]+")
  1587. for e in splitter.split(s):
  1588. if '=' in e:
  1589. ename, earg = e.split('=', 1)
  1590. try:
  1591. earg = int(earg)
  1592. except ValueError:
  1593. pass
  1594. else:
  1595. ename, earg = e, None
  1596. extras[ename] = earg
  1597. else:
  1598. extras = None
  1599. if opts.link_patterns_file:
  1600. link_patterns = []
  1601. f = open(opts.link_patterns_file)
  1602. try:
  1603. for i, line in enumerate(f.readlines()):
  1604. if not line.strip(): continue
  1605. if line.lstrip().startswith("#"): continue
  1606. try:
  1607. pat, href = line.rstrip().rsplit(None, 1)
  1608. except ValueError:
  1609. raise MarkdownError("%s:%d: invalid link pattern line: %r"
  1610. % (opts.link_patterns_file, i+1, line))
  1611. link_patterns.append(
  1612. (_regex_from_encoded_pattern(pat), href))
  1613. finally:
  1614. f.close()
  1615. else:
  1616. link_patterns = None
  1617. from os.path import join, dirname, abspath, exists
  1618. markdown_pl = join(dirname(dirname(abspath(__file__))), "test",
  1619. "Markdown.pl")
  1620. for path in paths:
  1621. if opts.compare:
  1622. print "==== Markdown.pl ===="
  1623. perl_cmd = 'perl %s "%s"' % (markdown_pl, path)
  1624. o = os.popen(perl_cmd)
  1625. perl_html = o.read()
  1626. o.close()
  1627. sys.stdout.write(perl_html)
  1628. print "==== markdown2.py ===="
  1629. html = markdown_path(path, encoding=opts.encoding,
  1630. html4tags=opts.html4tags,
  1631. safe_mode=opts.safe_mode,
  1632. extras=extras, link_patterns=link_patterns,
  1633. use_file_vars=opts.use_file_vars)
  1634. sys.stdout.write(
  1635. html.encode(sys.stdout.encoding or "utf-8", 'xmlcharrefreplace'))
  1636. if opts.compare:
  1637. test_dir = join(dirname(dirname(abspath(__file__))), "test")
  1638. if exists(join(test_dir, "test_markdown2.py")):
  1639. sys.path.insert(0, test_dir)
  1640. from test_markdown2 import norm_html_from_html
  1641. norm_html = norm_html_from_html(html)
  1642. norm_perl_html = norm_html_from_html(perl_html)
  1643. else:
  1644. norm_html = html
  1645. norm_perl_html = perl_html
  1646. print "==== match? %r ====" % (norm_perl_html == norm_html)
  1647. if __name__ == "__main__":
  1648. sys.exit( main(sys.argv) )