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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. FN_BACKLINK_TEXT = 'zz1337820767766393qq'
  5. import re
  6. import markdown
  7. import random
  8.  
  9. class FootnoteExtension(markdown.Extension):
  10.     DEF_RE = re.compile('(\\ ?\\ ?\\ ?)\\[\\^([^\\]]*)\\]:\\s*(.*)')
  11.     SHORT_USE_RE = re.compile('\\[\\^([^\\]]*)\\]', re.M)
  12.     
  13.     def __init__(self, configs):
  14.         self.config = {
  15.             'PLACE_MARKER': [
  16.                 '///Footnotes Go Here///',
  17.                 'The text string that marks where the footnotes go'] }
  18.         for key, value in configs:
  19.             self.config[key][0] = value
  20.         
  21.         self.reset()
  22.  
  23.     
  24.     def extendMarkdown(self, md, md_globals):
  25.         self.md = md
  26.         md.registerExtension(self)
  27.         index = md.preprocessors.index(md_globals['REFERENCE_PREPROCESSOR'])
  28.         preprocessor = FootnotePreprocessor(self)
  29.         preprocessor.md = md
  30.         md.preprocessors.insert(index, preprocessor)
  31.         FOOTNOTE_RE = '\\[\\^([^\\]]*)\\]'
  32.         index = md.inlinePatterns.index(md_globals['IMAGE_REFERENCE_PATTERN'])
  33.         md.inlinePatterns.insert(index, FootnotePattern(FOOTNOTE_RE, self))
  34.         postprocessor = FootnotePostprocessor(self)
  35.         postprocessor.extension = self
  36.         md.postprocessors.append(postprocessor)
  37.         textPostprocessor = FootnoteTextPostprocessor(self)
  38.         md.textPostprocessors.append(textPostprocessor)
  39.  
  40.     
  41.     def reset(self):
  42.         self.footnote_suffix = '-' + str(int(random.random() * 1000000000))
  43.         self.used_footnotes = { }
  44.         self.footnotes = { }
  45.  
  46.     
  47.     def findFootnotesPlaceholder(self, doc):
  48.         
  49.         def findFootnotePlaceholderFn(node = None, indent = (0,)):
  50.             if node.type == 'text':
  51.                 if node.value.find(self.getConfig('PLACE_MARKER')) > -1:
  52.                     return True
  53.             
  54.  
  55.         fn_div_list = doc.find(findFootnotePlaceholderFn)
  56.         if fn_div_list:
  57.             return fn_div_list[0]
  58.  
  59.     
  60.     def setFootnote(self, id, text):
  61.         self.footnotes[id] = text
  62.  
  63.     
  64.     def makeFootnoteId(self, num):
  65.         return 'fn%d%s' % (num, self.footnote_suffix)
  66.  
  67.     
  68.     def makeFootnoteRefId(self, num):
  69.         return 'fnr%d%s' % (num, self.footnote_suffix)
  70.  
  71.     
  72.     def makeFootnotesDiv(self, doc):
  73.         if not self.footnotes.keys():
  74.             return None
  75.         div = doc.createElement('div')
  76.         div.setAttribute('class', 'footnote')
  77.         hr = doc.createElement('hr')
  78.         div.appendChild(hr)
  79.         ol = doc.createElement('ol')
  80.         div.appendChild(ol)
  81.         footnotes = [ (self.used_footnotes[id], id) for id in self.footnotes.keys() ]
  82.         footnotes.sort()
  83.         for i, id in footnotes:
  84.             li = doc.createElement('li')
  85.             li.setAttribute('id', self.makeFootnoteId(i))
  86.             self.md._processSection(li, self.footnotes[id].split('\n'), looseList = 1)
  87.             backlink = doc.createElement('a')
  88.             backlink.setAttribute('href', '#' + self.makeFootnoteRefId(i))
  89.             backlink.setAttribute('class', 'footnoteBackLink')
  90.             backlink.setAttribute('title', 'Jump back to footnote %d in the text' % 1)
  91.             backlink.appendChild(doc.createTextNode(FN_BACKLINK_TEXT))
  92.             ol.appendChild(li)
  93.         
  94.         return div
  95.  
  96.  
  97.  
  98. class FootnotePreprocessor:
  99.     
  100.     def __init__(self, footnotes):
  101.         self.footnotes = footnotes
  102.  
  103.     
  104.     def run(self, lines):
  105.         self.blockGuru = markdown.BlockGuru()
  106.         lines = self._handleFootnoteDefinitions(lines)
  107.         text = '\n'.join(lines)
  108.         self.footnotes.SHORT_USE_RE.sub(self.recordFootnoteUse, text)
  109.         return text.split('\n')
  110.  
  111.     
  112.     def recordFootnoteUse(self, match):
  113.         id = match.group(1)
  114.         id = id.strip()
  115.         nextNum = len(self.footnotes.used_footnotes.keys()) + 1
  116.         self.footnotes.used_footnotes[id] = nextNum
  117.  
  118.     
  119.     def _handleFootnoteDefinitions(self, lines):
  120.         (i, id, footnote) = self._findFootnoteDefinition(lines)
  121.         if id:
  122.             plain = lines[:i]
  123.             (detabbed, theRest) = self.blockGuru.detectTabbed(lines[i + 1:])
  124.             self.footnotes.setFootnote(id, footnote + '\n' + '\n'.join(detabbed))
  125.             more_plain = self._handleFootnoteDefinitions(theRest)
  126.             return plain + [
  127.                 ''] + more_plain
  128.         return lines
  129.  
  130.     
  131.     def _findFootnoteDefinition(self, lines):
  132.         counter = 0
  133.         for line in lines:
  134.             m = self.footnotes.DEF_RE.match(line)
  135.             if m:
  136.                 return (counter, m.group(2), m.group(3))
  137.             counter += 1
  138.         
  139.         return (counter, None, None)
  140.  
  141.  
  142.  
  143. class FootnotePattern(markdown.Pattern):
  144.     
  145.     def __init__(self, pattern, footnotes):
  146.         markdown.Pattern.__init__(self, pattern)
  147.         self.footnotes = footnotes
  148.  
  149.     
  150.     def handleMatch(self, m, doc):
  151.         sup = doc.createElement('sup')
  152.         a = doc.createElement('a')
  153.         sup.appendChild(a)
  154.         id = m.group(2)
  155.         num = self.footnotes.used_footnotes[id]
  156.         sup.setAttribute('id', self.footnotes.makeFootnoteRefId(num))
  157.         a.setAttribute('href', '#' + self.footnotes.makeFootnoteId(num))
  158.         a.appendChild(doc.createTextNode(str(num)))
  159.         return sup
  160.  
  161.  
  162.  
  163. class FootnotePostprocessor(markdown.Postprocessor):
  164.     
  165.     def __init__(self, footnotes):
  166.         self.footnotes = footnotes
  167.  
  168.     
  169.     def run(self, doc):
  170.         footnotesDiv = self.footnotes.makeFootnotesDiv(doc)
  171.         if footnotesDiv:
  172.             fnPlaceholder = self.extension.findFootnotesPlaceholder(doc)
  173.             if fnPlaceholder:
  174.                 fnPlaceholder.parent.replaceChild(fnPlaceholder, footnotesDiv)
  175.             else:
  176.                 doc.documentElement.appendChild(footnotesDiv)
  177.         
  178.  
  179.  
  180.  
  181. class FootnoteTextPostprocessor(markdown.Postprocessor):
  182.     
  183.     def __init__(self, footnotes):
  184.         self.footnotes = footnotes
  185.  
  186.     
  187.     def run(self, text):
  188.         return text.replace(FN_BACKLINK_TEXT, '↩')
  189.  
  190.  
  191.  
  192. def makeExtension(configs = None):
  193.     return FootnoteExtension(configs = configs)
  194.  
  195.