diff options
author | Waylan Limberg <waylan@gmail.com> | 2008-11-05 19:20:43 -0500 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2008-11-05 19:20:43 -0500 |
commit | e968bbf38cc6570da2ccc3c7e87d99a36758544c (patch) | |
tree | 35969c6c8b0e1e84462bac600d35465a2b0d1a01 | |
parent | 36abb1d12c0def034725b840ac200b6813cf5828 (diff) | |
download | markdown-e968bbf38cc6570da2ccc3c7e87d99a36758544c.tar.gz markdown-e968bbf38cc6570da2ccc3c7e87d99a36758544c.tar.bz2 markdown-e968bbf38cc6570da2ccc3c7e87d99a36758544c.zip |
Fixed Definition List extension to use a regex rather than a strict string match. Whitespcae is now mor eflexable and better matches other implementations. Also refactored all definition specific code into one method.
-rw-r--r-- | markdown_extensions/def_list.py | 90 |
1 files changed, 44 insertions, 46 deletions
diff --git a/markdown_extensions/def_list.py b/markdown_extensions/def_list.py index 2f1a92d..831e840 100644 --- a/markdown_extensions/def_list.py +++ b/markdown_extensions/def_list.py @@ -19,9 +19,11 @@ Copyright 2008 - [Waylan Limberg](http://achinghead.com) """ -import markdown +import markdown, re from markdown import etree, CORE_RE +DEF_RE = re.compile(r'^[ ]{0,3}:[ ]{1,3}(.*)$') + class DefListParser(markdown.MarkdownParser): """ Subclass of MarkdownParser which adds definition list parsing. """ @@ -97,22 +99,10 @@ class DefListParser(markdown.MarkdownParser): self._MarkdownParser__processHR(parent_elem) lines = paragraph[1:] + lines elif paragraph: - terms, defs, paragraph = self._getDefs(paragraph) - if defs: - if not terms: - # The previous paragraph must be the terms - c = parent_elem.getchildren() - if c and c[-1].tag == "p" and c[-1].text: - terms = c[-1].text.split("\n") - parent_elem.remove(c[-1]) - looseList = 1 - # check for extra paragraphs of a def - extradef, lines = self.detectTabbed(lines) - if extradef: - looseList = 1 - defs[-1].extend(extradef) - # process the terms and defs - self._processDef(parent_elem, terms, defs, looseList) + paragraph, lines, looseList = self._processDefs(parent_elem, + paragraph, + lines, + looseList) if len(paragraph): self._MarkdownParser__processParagraph(parent_elem, paragraph, @@ -123,45 +113,53 @@ class DefListParser(markdown.MarkdownParser): lines = lines[1:] # skip the first (blank) line - def _getDefs(self, lines): + def _processDefs(self, parentElem, paragraph, lines, looseList): + """ Check a paragraph for definition lists and process. """ terms = [] defs = [] i = 0 - while i < len(lines): - if lines[i].startswith(': '): - d = self._getDef(lines[i:]) + while i < len(paragraph): + m = DEF_RE.match(paragraph[i]) + if m: + d, theRest = self.detectTabbed(paragraph[i+1:]) + d.insert(0, m.group(1)) if d: defs.append(d) i += len(d) else: - terms.append(lines[i]) + terms.append(paragraph[i]) i += 1 if defs: - return terms, defs, [] - else: - return None, None, terms - - def _getDef(self, lines): - if lines[0].startswith(': '): - Def, theRest = self.detectTabbed(lines[1:]) - Def.insert(0, lines[0][4:]) - return Def - return [] - - def _processDef(self, parentElem, terms, defs, looseList): - children = parentElem.getchildren() - if children and children[-1].tag == "dl": - dl = children[-1] + if not terms: + # The previous paragraph must contain the terms + c = parentElem.getchildren() + if c and c[-1].tag == "p" and c[-1].text: + terms = c[-1].text.split("\n") + parentElem.remove(c[-1]) + looseList = 1 + # check for extra paragraphs of a def + extradef, lines = self.detectTabbed(lines) + if extradef: + looseList = 1 + defs[-1].extend(extradef) + # Build a tree from the terms and defs + c = parentElem.getchildren() + if c and c[-1].tag == "dl": + dl = c[-1] + else: + dl = etree.SubElement(parentElem, "dl") + for term in terms: + dt = etree.SubElement(dl, "dt") + dt.text = term + for d in defs: + dd = etree.SubElement(dl, "dd") + self.parseChunk(dd, d, looseList = looseList) + return [], lines, looseList else: - dl = etree.SubElement(parentElem, "dl") - for term in terms: - dt = etree.SubElement(dl, "dt") - dt.text = term - for d in defs: - dd = etree.SubElement(dl, "dd") - self.parseChunk(dd, d, looseList = looseList) - - def _MarkdownParser__processParagraph(self, parentElem, paragraph, inList, looseList): + return terms, lines, looseList + + def _MarkdownParser__processParagraph(self, parentElem, paragraph, + inList, looseList): if ((parentElem.tag == 'li' or parentElem.tag == 'dd') and not (looseList or parentElem.getchildren())): |