home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1408 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  4.3 KB  |  107 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import re
  8. from calibre.constants import preferred_encoding
  9. from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag, NavigableString, CData, Comment, Declaration, ProcessingInstruction
  10. from calibre import prepare_string_for_xml
  11. from calibre.utils.html2text import html2text
  12. from calibre.ebooks.markdown import markdown
  13. lost_cr_pat = re.compile('([a-z])([\\.\\?!])([A-Z])')
  14. lost_cr_exception_pat = re.compile('(Ph\\.D)|(D\\.Phil)|((Dr|Mr|Mrs|Ms)\\.[A-Z])')
  15. sanitize_pat = re.compile('<script|<table|<tr|<td|<th|<style|<iframe', re.IGNORECASE)
  16.  
  17. def comments_to_html(comments):
  18.     if not comments:
  19.         return u'<p></p>'
  20.     if not isinstance(comments, unicode):
  21.         comments = comments.decode(preferred_encoding, 'replace')
  22.     
  23.     if '<' not in comments:
  24.         comments = prepare_string_for_xml(comments)
  25.         parts = [ u'<p class="description">%s</p>' % x.replace(u'\n', u'<br />') for x in comments.split('\n\n') ]
  26.         return '\n'.join(parts)
  27.     comments = lost_cr_exception_pat.sub((lambda m: m.group().replace('.', '.\r')), comments)
  28.     for lost_cr in lost_cr_pat.finditer(comments):
  29.         comments = comments.replace(lost_cr.group(), '%s%s\n\n%s' % (lost_cr.group(1), lost_cr.group(2), lost_cr.group(3)))
  30.     
  31.     comments = comments.replace(u'\r', u'')
  32.     comments = comments.replace(u'\n\n', u'<p>')
  33.     comments = comments.replace(u'\n', '<br />')
  34.     comments = comments.replace('--', '—')
  35.     soup = BeautifulSoup(comments)
  36.     result = BeautifulSoup()
  37.     rtc = 0
  38.     open_pTag = False
  39.     all_tokens = list(soup.contents)
  40.     for token in all_tokens:
  41.         if type(token) is NavigableString:
  42.             if not open_pTag:
  43.                 pTag = Tag(result, 'p')
  44.                 open_pTag = True
  45.                 ptc = 0
  46.             
  47.             pTag.insert(ptc, prepare_string_for_xml(token))
  48.             ptc += 1
  49.             continue
  50.         if type(token) in (CData, Comment, Declaration, ProcessingInstruction):
  51.             continue
  52.             continue
  53.         if token.name in ('br', 'b', 'i', 'em', 'strong', 'span', 'font', 'a', 'hr'):
  54.             if not open_pTag:
  55.                 pTag = Tag(result, 'p')
  56.                 open_pTag = True
  57.                 ptc = 0
  58.             
  59.             pTag.insert(ptc, token)
  60.             ptc += 1
  61.             continue
  62.         if open_pTag:
  63.             result.insert(rtc, pTag)
  64.             rtc += 1
  65.             open_pTag = False
  66.             ptc = 0
  67.         
  68.         result.insert(rtc, token)
  69.         rtc += 1
  70.     
  71.     if open_pTag:
  72.         result.insert(rtc, pTag)
  73.     
  74.     for p in result.findAll('p'):
  75.         p['class'] = 'description'
  76.     
  77.     for t in result.findAll(text = True):
  78.         t.replaceWith(prepare_string_for_xml(unicode(t)))
  79.     
  80.     return result.renderContents(encoding = None)
  81.  
  82.  
  83. def sanitize_comments_html(html):
  84.     text = html2text(html)
  85.     md = markdown.Markdown(safe_mode = True)
  86.     return md.convert(text)
  87.  
  88.  
  89. def test():
  90.     for pat, val in [
  91.         ('lineone\n\nlinetwo', '<p class="description">lineone</p>\n<p class="description">linetwo</p>'),
  92.         ('a <b>b&c</b>\nf', '<p class="description">a <b>b&c;</b><br />f</p>'),
  93.         ('a <?xml asd> b\n\ncd', '<p class="description">a  b</p><p class="description">cd</p>')]:
  94.         print 
  95.         print 'Testing: %r' % pat
  96.         cval = comments_to_html(pat)
  97.         print 'Value: %r' % cval
  98.         if comments_to_html(pat) != val:
  99.             print 'FAILED'
  100.             break
  101.             continue
  102.     
  103.  
  104. if __name__ == '__main__':
  105.     test()
  106.  
  107.