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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import markdown
  6. DEFAULT_TITLE = None
  7.  
  8. def extract_alphanumeric(in_str = None):
  9.     out_str = []
  10.     for x in in_str.title():
  11.         if x.isalnum():
  12.             out_str.append(x)
  13.             continue
  14.     
  15.     return ''.join(out_str)
  16.  
  17.  
  18. class TitlePostprocessor(markdown.Postprocessor):
  19.     
  20.     def __init__(self, extension):
  21.         self.extension = extension
  22.  
  23.     
  24.     def run(self, doc):
  25.         titleElement = self.extension.createTitle(doc)
  26.         if titleElement:
  27.             doc.documentElement.insertChild(0, titleElement)
  28.         
  29.  
  30.  
  31.  
  32. class TocExtension(markdown.Extension):
  33.     
  34.     def __init__(self, configs = { }):
  35.         self.TOC_INCLUDE_MARKER = '///Table of Contents///'
  36.         self.TOC_TITLE = 'Table Of Contents'
  37.         self.auto_toc_heading_type = 2
  38.         self.toc_heading_type = 3
  39.         self.configs = configs
  40.  
  41.     
  42.     def extendMarkdown(self, md, md_globals):
  43.         md.postprocessors.append(TocPostprocessor(self))
  44.  
  45.     
  46.     def findTocPlaceholder(self, doc):
  47.         
  48.         def findTocPlaceholderFn(node = None, indent = (0,)):
  49.             if node.type == 'text':
  50.                 if node.value.find(self.TOC_INCLUDE_MARKER) > -1:
  51.                     return True
  52.             
  53.  
  54.         toc_div_list = doc.find(findTocPlaceholderFn)
  55.         if toc_div_list:
  56.             return toc_div_list[0]
  57.  
  58.     
  59.     def createTocDiv(self, doc):
  60.         headers_compiled_re = re.compile('h[123456]', re.IGNORECASE)
  61.         
  62.         def findHeadersFn(element = (None,)):
  63.             if element.type == 'element':
  64.                 if headers_compiled_re.match(element.nodeName):
  65.                     return True
  66.             
  67.  
  68.         headers_doc_list = doc.find(findHeadersFn)
  69.         generated_anchor_id = 0
  70.         headers_list = []
  71.         min_header_size_found = 6
  72.         for element in headers_doc_list:
  73.             heading_title = element.childNodes[0].value
  74.             if heading_title.strip() != '':
  75.                 heading_type = int(element.nodeName[-1:])
  76.                 if heading_type == self.auto_toc_heading_type:
  77.                     min_header_size_found = min(min_header_size_found, heading_type)
  78.                 
  79.                 html_anchor_name = extract_alphanumeric(heading_title) + '__MD_autoTOC_%d' % generated_anchor_id
  80.                 html_anchor = doc.createElement('a')
  81.                 html_anchor.setAttribute('name', html_anchor_name)
  82.                 element.appendChild(html_anchor)
  83.                 headers_list.append((heading_type, heading_title, html_anchor_name))
  84.                 generated_anchor_id = generated_anchor_id + 1
  85.                 continue
  86.         
  87.         if headers_list != []:
  88.             toc_doc_list = doc.createElement('ul')
  89.             for heading_type, heading_title, html_anchor_name in headers_list:
  90.                 if heading_type == self.auto_toc_heading_type:
  91.                     toc_doc_entry = doc.createElement('li')
  92.                     toc_doc_link = doc.createElement('a')
  93.                     toc_doc_link.setAttribute('href', '#' + html_anchor_name)
  94.                     toc_doc_text = doc.createTextNode(heading_title)
  95.                     toc_doc_link.appendChild(toc_doc_text)
  96.                     toc_doc_entry.appendChild(toc_doc_link)
  97.                     toc_doc_list.appendChild(toc_doc_entry)
  98.                     continue
  99.             
  100.             div = doc.createElement('div')
  101.             div.setAttribute('class', 'toc')
  102.             if self.TOC_TITLE:
  103.                 toc_header = doc.createElement('h%d' % self.toc_heading_type)
  104.                 toc_header_text = doc.createTextNode(self.TOC_TITLE)
  105.                 toc_header.appendChild(toc_header_text)
  106.                 div.appendChild(toc_header)
  107.             
  108.             div.appendChild(toc_doc_list)
  109.             return div
  110.  
  111.  
  112.  
  113. class TocPostprocessor(markdown.Postprocessor):
  114.     
  115.     def __init__(self, toc):
  116.         self.toc = toc
  117.  
  118.     
  119.     def run(self, doc):
  120.         tocPlaceholder = self.toc.findTocPlaceholder(doc)
  121.         if self.toc.configs.get('disable_toc', False):
  122.             if tocPlaceholder:
  123.                 tocPlaceholder.parent.replaceChild(tocPlaceholder, '')
  124.             
  125.         else:
  126.             tocDiv = self.toc.createTocDiv(doc)
  127.             if tocDiv:
  128.                 if tocPlaceholder:
  129.                     tocPlaceholder.parent.replaceChild(tocPlaceholder, tocDiv)
  130.                 else:
  131.                     doc.documentElement.appendChild(tocDiv)
  132.             
  133.  
  134.  
  135.  
  136. def makeExtension(configs = { }):
  137.     return TocExtension(configs = configs)
  138.  
  139.