home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_941 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.8 KB  |  68 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import markdown
  5.  
  6. class TablePattern(markdown.Pattern):
  7.     
  8.     def __init__(self, md):
  9.         markdown.Pattern.__init__(self, '^\\|([^\\n]*)\\|(\\n|$)')
  10.         self.md = md
  11.  
  12.     
  13.     def handleMatch(self, m, doc):
  14.         tr = doc.createElement('tr')
  15.         tr.appendChild(doc.createTextNode('\n'))
  16.         for t in m.group(2).split('|'):
  17.             if len(t) >= 2 and t.startswith('*') and t.endswith('*'):
  18.                 td = doc.createElement('th')
  19.                 t = t[1:-1]
  20.             else:
  21.                 td = doc.createElement('td')
  22.             for n in self.md._handleInline(t):
  23.                 if type(n) == unicode:
  24.                     td.appendChild(doc.createTextNode(n))
  25.                     continue
  26.                 td.appendChild(n)
  27.             
  28.             tr.appendChild(td)
  29.             tr.appendChild(doc.createTextNode('\n'))
  30.         
  31.         return tr
  32.  
  33.  
  34.  
  35. class TablePostprocessor:
  36.     
  37.     def run(self, doc):
  38.         
  39.         def test_for_p(element):
  40.             if element.type == 'element':
  41.                 pass
  42.             return element.nodeName == 'p'
  43.  
  44.         for element in doc.find(test_for_p):
  45.             for node in element.childNodes:
  46.                 if node.type == 'text' and node.value.strip() == '':
  47.                     continue
  48.                 
  49.                 if node.type == 'element' and node.nodeName == 'tr':
  50.                     element.nodeName = 'table'
  51.                 
  52.             
  53.         
  54.  
  55.  
  56.  
  57. class TableExtension(markdown.Extension):
  58.     
  59.     def extendMarkdown(self, md, md_globals):
  60.         md.inlinePatterns.insert(0, TablePattern(md))
  61.         md.postprocessors.append(TablePostprocessor())
  62.  
  63.  
  64.  
  65. def makeExtension(configs):
  66.     return TableExtension(configs)
  67.  
  68.