home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1348 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.4 KB  |  94 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. lost_cr_pat = re.compile('([a-z])([\\.\\?!])([A-Z])')
  12.  
  13. def comments_to_html(comments):
  14.     if not isinstance(comments, unicode):
  15.         comments = comments.decode(preferred_encoding, 'replace')
  16.     
  17.     if '<' not in comments:
  18.         comments = prepare_string_for_xml(comments)
  19.         parts = [ u'<p class="description">%s</p>' % x.replace(u'\n', u'<br />') for x in comments.split('\n\n') ]
  20.         return '\n'.join(parts)
  21.     for lost_cr in lost_cr_pat.finditer(comments):
  22.         comments = comments.replace(lost_cr.group(), '%s%s\n\n%s' % (lost_cr.group(1), lost_cr.group(2), lost_cr.group(3)))
  23.     
  24.     comments = comments.replace(u'\r', u'')
  25.     comments = comments.replace(u'\n\n', u'<p>')
  26.     comments = comments.replace(u'\n', '<br />')
  27.     comments = comments.replace('--', '—')
  28.     soup = BeautifulSoup(comments)
  29.     result = BeautifulSoup()
  30.     rtc = 0
  31.     open_pTag = False
  32.     all_tokens = list(soup.contents)
  33.     for token in all_tokens:
  34.         if type(token) is NavigableString:
  35.             if not open_pTag:
  36.                 pTag = Tag(result, 'p')
  37.                 open_pTag = True
  38.                 ptc = 0
  39.             
  40.             pTag.insert(ptc, prepare_string_for_xml(token))
  41.             ptc += 1
  42.             continue
  43.         if type(token) in (CData, Comment, Declaration, ProcessingInstruction):
  44.             continue
  45.             continue
  46.         if token.name in ('br', 'b', 'i', 'em', 'strong', 'span', 'font', 'a', 'hr'):
  47.             if not open_pTag:
  48.                 pTag = Tag(result, 'p')
  49.                 open_pTag = True
  50.                 ptc = 0
  51.             
  52.             pTag.insert(ptc, token)
  53.             ptc += 1
  54.             continue
  55.         if open_pTag:
  56.             result.insert(rtc, pTag)
  57.             rtc += 1
  58.             open_pTag = False
  59.             ptc = 0
  60.         
  61.         result.insert(rtc, token)
  62.         rtc += 1
  63.     
  64.     if open_pTag:
  65.         result.insert(rtc, pTag)
  66.     
  67.     for p in result.findAll('p'):
  68.         p['class'] = 'description'
  69.     
  70.     for t in result.findAll(text = True):
  71.         t.replaceWith(prepare_string_for_xml(unicode(t)))
  72.     
  73.     return result.renderContents(encoding = None)
  74.  
  75.  
  76. def test():
  77.     for pat, val in [
  78.         ('lineone\n\nlinetwo', '<p class="description">lineone</p>\n<p class="description">linetwo</p>'),
  79.         ('a <b>b&c</b>\nf', '<p class="description">a <b>b&c;</b><br />f</p>'),
  80.         ('a <?xml asd> b\n\ncd', '<p class="description">a  b</p><p class="description">cd</p>')]:
  81.         print 
  82.         print 'Testing: %r' % pat
  83.         cval = comments_to_html(pat)
  84.         print 'Value: %r' % cval
  85.         if comments_to_html(pat) != val:
  86.             print 'FAILED'
  87.             break
  88.             continue
  89.     
  90.  
  91. if __name__ == '__main__':
  92.     test()
  93.  
  94.