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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. from calibre import prepare_string_for_xml, isbytestring
  7. from calibre.ebooks.markdown import markdown
  8. from calibre.ebooks.metadata.opf2 import OPFCreator
  9. __license__ = 'GPL v3'
  10. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  11. __docformat__ = 'restructuredtext en'
  12. HTML_TEMPLATE = u'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s</title></head><body>\n%s\n</body></html>'
  13.  
  14. def convert_basic(txt, title = '', epub_split_size_kb = 0):
  15.     if isbytestring(txt):
  16.         txt = txt.decode('utf-8', 'replace')
  17.     
  18.     txt = []([ line.strip() for line in txt.splitlines() ])
  19.     txt = re.sub('[ ]{2,}', ' ', txt)
  20.     txt = re.sub('^\\s+(?=.)', '', txt)
  21.     txt = re.sub('(?<=.)\\s+$', '', txt)
  22.     txt = re.sub('\n{3,}', '\n\n', txt)
  23.     chars = list(range(8)) + [
  24.         11,
  25.         14,
  26.         15] + list(range(16, 25))
  27.     illegal_chars = re.compile(u'|'.join(map(unichr, chars)))
  28.     txt = illegal_chars.sub('', txt)
  29.     if epub_split_size_kb > 0:
  30.         length_byte = len(txt)
  31.         chunk_size = long(length_byte / (int(length_byte / (epub_split_size_kb * 1024)) + 2))
  32.         if len((filter,)((lambda x: len(x) > chunk_size), txt.split('\n\n'))):
  33.             txt = []([ split_string_separator(line, chunk_size) for line in txt.split('\n\n') ])
  34.         
  35.     
  36.     if isbytestring(txt):
  37.         txt = txt.decode('utf-8')
  38.     
  39.     lines = []
  40.     for line in txt.split('\n\n'):
  41.         if line.strip():
  42.             lines.append(u'<p>%s</p>' % prepare_string_for_xml(line.replace('\n', ' ')))
  43.             continue
  44.     
  45.     return HTML_TEMPLATE % (title, u'\n'.join(lines))
  46.  
  47.  
  48. def convert_markdown(txt, title = '', disable_toc = False):
  49.     md = markdown.Markdown(extensions = [
  50.         'footnotes',
  51.         'tables',
  52.         'toc'], extension_configs = {
  53.         'toc': {
  54.             'disable_toc': disable_toc } }, safe_mode = False)
  55.     return HTML_TEMPLATE % (title, md.convert(txt))
  56.  
  57.  
  58. def separate_paragraphs_single_line(txt):
  59.     txt = txt.replace('\r\n', '\n')
  60.     txt = txt.replace('\r', '\n')
  61.     txt = re.sub(u'(?<=.)\n(?=.)', '\n\n', txt)
  62.     return txt
  63.  
  64.  
  65. def separate_paragraphs_print_formatted(txt):
  66.     txt = re.sub(u'(?miu)^(\t+|[ ]{2,})(?=.)', '\n\t', txt)
  67.     return txt
  68.  
  69.  
  70. def preserve_spaces(txt):
  71.     txt = txt.replace(' ', ' ')
  72.     txt = txt.replace('\t', ' ')
  73.     return txt
  74.  
  75.  
  76. def opf_writer(path, opf_name, manifest, spine, mi):
  77.     opf = OPFCreator(path, mi)
  78.     opf.create_manifest(manifest)
  79.     opf.create_spine(spine)
  80.     
  81.     try:
  82.         opffile = _[1]
  83.         opf.render(opffile)
  84.     finally:
  85.         pass
  86.  
  87.  
  88.  
  89. def split_string_separator(txt, size):
  90.     return txt
  91.  
  92.