diff options
author | Toshio Kuratomi <toshio@fedoraproject.org> | 2010-07-05 14:22:22 -0400 |
---|---|---|
committer | Toshio Kuratomi <toshio@fedoraproject.org> | 2010-07-05 14:22:22 -0400 |
commit | b50560edc707241b236f9d6c3f33edcf09d7cf33 (patch) | |
tree | cbb7d24f0784d05730bd3c637bb9e163c933c047 /markdown/blockprocessors.py | |
parent | cf1e33c216b1ea5ce18f660025744d9f8b66fb7c (diff) | |
download | markdown-b50560edc707241b236f9d6c3f33edcf09d7cf33.tar.gz markdown-b50560edc707241b236f9d6c3f33edcf09d7cf33.tar.bz2 markdown-b50560edc707241b236f9d6c3f33edcf09d7cf33.zip |
Break cyclic import of markdown. This allows people to embed markdown
if they desire.
Diffstat (limited to 'markdown/blockprocessors.py')
-rw-r--r-- | markdown/blockprocessors.py | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index 42693c6..3aeef34 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -13,8 +13,8 @@ as they need to alter how markdown blocks are parsed. """ import re -import markdown -from markdown import CRITICAL, message +import misc +from misc_logging import CRITICAL, message class BlockProcessor: """ Base class for block processors. @@ -42,8 +42,8 @@ class BlockProcessor: newtext = [] lines = text.split('\n') for line in lines: - if line.startswith(' '*markdown.TAB_LENGTH): - newtext.append(line[markdown.TAB_LENGTH:]) + if line.startswith(' '*misc.TAB_LENGTH): + newtext.append(line[misc.TAB_LENGTH:]) elif not line.strip(): newtext.append('') else: @@ -54,8 +54,8 @@ class BlockProcessor: """ Remove a tab from front of lines but allowing dedented lines. """ lines = text.split('\n') for i in range(len(lines)): - if lines[i].startswith(' '*markdown.TAB_LENGTH*level): - lines[i] = lines[i][markdown.TAB_LENGTH*level:] + if lines[i].startswith(' '*misc.TAB_LENGTH*level): + lines[i] = lines[i][misc.TAB_LENGTH*level:] return '\n'.join(lines) def test(self, parent, block): @@ -114,12 +114,12 @@ class ListIndentProcessor(BlockProcessor): """ - INDENT_RE = re.compile(r'^(([ ]{%s})+)'% markdown.TAB_LENGTH) + INDENT_RE = re.compile(r'^(([ ]{%s})+)'% misc.TAB_LENGTH) ITEM_TYPES = ['li'] LIST_TYPES = ['ul', 'ol'] def test(self, parent, block): - return block.startswith(' '*markdown.TAB_LENGTH) and \ + return block.startswith(' '*misc.TAB_LENGTH) and \ not self.parser.state.isstate('detabbed') and \ (parent.tag in self.ITEM_TYPES or \ (len(parent) and parent[-1] and \ @@ -154,7 +154,7 @@ class ListIndentProcessor(BlockProcessor): # If the parent li has text, that text needs to be moved to a p # The p must be 'inserted' at beginning of list in the event # that other children already exist i.e.; a nested sublist. - p = markdown.etree.Element('p') + p = misc.etree.Element('p') p.text = sibling[-1].text sibling[-1].text = '' sibling[-1].insert(0, p) @@ -162,10 +162,10 @@ class ListIndentProcessor(BlockProcessor): else: self.create_item(sibling, block) self.parser.state.reset() - + def create_item(self, parent, block): """ Create a new li and parse the block with it as the parent. """ - li = markdown.etree.SubElement(parent, 'li') + li = misc.etree.SubElement(parent, 'li') self.parser.parseBlocks(li, [block]) def get_level(self, parent, block): @@ -173,7 +173,7 @@ class ListIndentProcessor(BlockProcessor): # Get indent level m = self.INDENT_RE.match(block) if m: - indent_level = len(m.group(1))/markdown.TAB_LENGTH + indent_level = len(m.group(1))/misc.TAB_LENGTH else: indent_level = 0 if self.parser.state.isstate('list'): @@ -200,7 +200,7 @@ class CodeBlockProcessor(BlockProcessor): """ Process code blocks. """ def test(self, parent, block): - return block.startswith(' '*markdown.TAB_LENGTH) + return block.startswith(' '*misc.TAB_LENGTH) def run(self, parent, blocks): sibling = self.lastChild(parent) @@ -213,13 +213,13 @@ class CodeBlockProcessor(BlockProcessor): # linebreaks removed from the split into a list. code = sibling[0] block, theRest = self.detab(block) - code.text = markdown.AtomicString('%s\n%s\n' % (code.text, block.rstrip())) + code.text = misc.AtomicString('%s\n%s\n' % (code.text, block.rstrip())) else: # This is a new codeblock. Create the elements and insert text. - pre = markdown.etree.SubElement(parent, 'pre') - code = markdown.etree.SubElement(pre, 'code') + pre = misc.etree.SubElement(parent, 'pre') + code = misc.etree.SubElement(pre, 'code') block, theRest = self.detab(block) - code.text = markdown.AtomicString('%s\n' % block.rstrip()) + code.text = misc.AtomicString('%s\n' % block.rstrip()) if theRest: # This block contained unindented line(s) after the first indented # line. Insert these lines as the first block of the master blocks @@ -250,7 +250,7 @@ class BlockQuoteProcessor(BlockProcessor): quote = sibling else: # This is a new blockquote. Create a new parent element. - quote = markdown.etree.SubElement(parent, 'blockquote') + quote = misc.etree.SubElement(parent, 'blockquote') # Recursively parse block with blockquote as parent. # change parser state so blockquotes embedded in lists use p tags self.parser.state.set('blockquote') @@ -295,13 +295,13 @@ class OListProcessor(BlockProcessor): # since it's possible there are other children for this sibling, # we can't just SubElement the p, we need to insert it as the # first item - p = markdown.etree.Element('p') + p = misc.etree.Element('p') p.text = lst[-1].text lst[-1].text = '' lst[-1].insert(0, p) # parse first block differently as it gets wrapped in a p. - li = markdown.etree.SubElement(lst, 'li') + li = misc.etree.SubElement(lst, 'li') self.parser.state.set('looselist') firstitem = items.pop(0) self.parser.parseBlocks(li, [firstitem]) @@ -315,17 +315,17 @@ class OListProcessor(BlockProcessor): lst = parent else: # This is a new list so create parent with appropriate tag. - lst = markdown.etree.SubElement(parent, self.TAG) + lst = misc.etree.SubElement(parent, self.TAG) self.parser.state.set('list') # Loop through items in block, recursively parsing each with the # appropriate parent. for item in items: - if item.startswith(' '*markdown.TAB_LENGTH): + if item.startswith(' '*misc.TAB_LENGTH): # Item is indented. Parse with last item as parent self.parser.parseBlocks(lst[-1], [item]) else: # New item. Create li and parse with it as parent - li = markdown.etree.SubElement(lst, 'li') + li = misc.etree.SubElement(lst, 'li') self.parser.parseBlocks(li, [item]) self.parser.state.reset() @@ -339,7 +339,7 @@ class OListProcessor(BlockProcessor): items.append(m.group(3)) elif self.INDENT_RE.match(line): # This is an indented (possibly nested) item. - if items[-1].startswith(' '*markdown.TAB_LENGTH): + if items[-1].startswith(' '*misc.TAB_LENGTH): # Previous item was indented. Append to that item. items[-1] = '%s\n%s' % (items[-1], line) else: @@ -378,7 +378,7 @@ class HashHeaderProcessor(BlockProcessor): # recursively parse this lines as a block. self.parser.parseBlocks(parent, [before]) # Create header using named groups from RE - h = markdown.etree.SubElement(parent, 'h%d' % len(m.group('level'))) + h = misc.etree.SubElement(parent, 'h%d' % len(m.group('level'))) h.text = m.group('header').strip() if after: # Insert remaining lines as first block for future parsing. @@ -404,7 +404,7 @@ class SetextHeaderProcessor(BlockProcessor): level = 1 else: level = 2 - h = markdown.etree.SubElement(parent, 'h%d' % level) + h = misc.etree.SubElement(parent, 'h%d' % level) h.text = lines[0].strip() if len(lines) > 2: # Block contains additional lines. Add to master blocks for later. @@ -437,7 +437,7 @@ class HRProcessor(BlockProcessor): # Recursively parse lines before hr so they get parsed first. self.parser.parseBlocks(parent, ['\n'.join(prelines)]) # create hr - hr = markdown.etree.SubElement(parent, 'hr') + hr = misc.etree.SubElement(parent, 'hr') # check for lines in block after hr. lines = lines[len(prelines)+1:] if len(lines): @@ -465,7 +465,7 @@ class EmptyBlockProcessor(BlockProcessor): if sibling and sibling.tag == 'pre' and sibling[0] and \ sibling[0].tag == 'code': # Last block is a codeblock. Append to preserve whitespace. - sibling[0].text = markdown.AtomicString('%s/n/n/n' % sibling[0].text ) + sibling[0].text = misc.AtomicString('%s/n/n/n' % sibling[0].text ) class ParagraphProcessor(BlockProcessor): @@ -486,5 +486,5 @@ class ParagraphProcessor(BlockProcessor): parent.text = block.lstrip() else: # Create a regular paragraph - p = markdown.etree.SubElement(parent, 'p') + p = misc.etree.SubElement(parent, 'p') p.text = block.lstrip() |