home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import re
- import markdown
- DEFAULT_TITLE = None
-
- def extract_alphanumeric(in_str = None):
- out_str = []
- for x in in_str.title():
- if x.isalnum():
- out_str.append(x)
- continue
-
- return ''.join(out_str)
-
-
- class TitlePostprocessor(markdown.Postprocessor):
-
- def __init__(self, extension):
- self.extension = extension
-
-
- def run(self, doc):
- titleElement = self.extension.createTitle(doc)
- if titleElement:
- doc.documentElement.insertChild(0, titleElement)
-
-
-
-
- class TocExtension(markdown.Extension):
-
- def __init__(self, configs = { }):
- self.TOC_INCLUDE_MARKER = '///Table of Contents///'
- self.TOC_TITLE = 'Table Of Contents'
- self.auto_toc_heading_type = 2
- self.toc_heading_type = 3
- self.configs = configs
-
-
- def extendMarkdown(self, md, md_globals):
- md.postprocessors.append(TocPostprocessor(self))
-
-
- def findTocPlaceholder(self, doc):
-
- def findTocPlaceholderFn(node = None, indent = (0,)):
- if node.type == 'text':
- if node.value.find(self.TOC_INCLUDE_MARKER) > -1:
- return True
-
-
- toc_div_list = doc.find(findTocPlaceholderFn)
- if toc_div_list:
- return toc_div_list[0]
-
-
- def createTocDiv(self, doc):
- headers_compiled_re = re.compile('h[123456]', re.IGNORECASE)
-
- def findHeadersFn(element = (None,)):
- if element.type == 'element':
- if headers_compiled_re.match(element.nodeName):
- return True
-
-
- headers_doc_list = doc.find(findHeadersFn)
- generated_anchor_id = 0
- headers_list = []
- min_header_size_found = 6
- for element in headers_doc_list:
- heading_title = element.childNodes[0].value
- if heading_title.strip() != '':
- heading_type = int(element.nodeName[-1:])
- if heading_type == self.auto_toc_heading_type:
- min_header_size_found = min(min_header_size_found, heading_type)
-
- html_anchor_name = extract_alphanumeric(heading_title) + '__MD_autoTOC_%d' % generated_anchor_id
- html_anchor = doc.createElement('a')
- html_anchor.setAttribute('name', html_anchor_name)
- element.appendChild(html_anchor)
- headers_list.append((heading_type, heading_title, html_anchor_name))
- generated_anchor_id = generated_anchor_id + 1
- continue
-
- if headers_list != []:
- toc_doc_list = doc.createElement('ul')
- for heading_type, heading_title, html_anchor_name in headers_list:
- if heading_type == self.auto_toc_heading_type:
- toc_doc_entry = doc.createElement('li')
- toc_doc_link = doc.createElement('a')
- toc_doc_link.setAttribute('href', '#' + html_anchor_name)
- toc_doc_text = doc.createTextNode(heading_title)
- toc_doc_link.appendChild(toc_doc_text)
- toc_doc_entry.appendChild(toc_doc_link)
- toc_doc_list.appendChild(toc_doc_entry)
- continue
-
- div = doc.createElement('div')
- div.setAttribute('class', 'toc')
- if self.TOC_TITLE:
- toc_header = doc.createElement('h%d' % self.toc_heading_type)
- toc_header_text = doc.createTextNode(self.TOC_TITLE)
- toc_header.appendChild(toc_header_text)
- div.appendChild(toc_header)
-
- div.appendChild(toc_doc_list)
- return div
-
-
-
- class TocPostprocessor(markdown.Postprocessor):
-
- def __init__(self, toc):
- self.toc = toc
-
-
- def run(self, doc):
- tocPlaceholder = self.toc.findTocPlaceholder(doc)
- if self.toc.configs.get('disable_toc', False):
- if tocPlaceholder:
- tocPlaceholder.parent.replaceChild(tocPlaceholder, '')
-
- else:
- tocDiv = self.toc.createTocDiv(doc)
- if tocDiv:
- if tocPlaceholder:
- tocPlaceholder.parent.replaceChild(tocPlaceholder, tocDiv)
- else:
- doc.documentElement.appendChild(tocDiv)
-
-
-
-
- def makeExtension(configs = { }):
- return TocExtension(configs = configs)
-
-