home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import re
- from calibre import prepare_string_for_xml, isbytestring
- from calibre.ebooks.markdown import markdown
- from calibre.ebooks.metadata.opf2 import OPFCreator
- __license__ = 'GPL v3'
- __copyright__ = '2009, John Schember <john@nachtimwald.com>'
- __docformat__ = 'restructuredtext en'
- 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>'
-
- def convert_basic(txt, title = '', epub_split_size_kb = 0):
- if isbytestring(txt):
- txt = txt.decode('utf-8', 'replace')
-
- txt = []([ line.strip() for line in txt.splitlines() ])
- txt = re.sub('[ ]{2,}', ' ', txt)
- txt = re.sub('^\\s+(?=.)', '', txt)
- txt = re.sub('(?<=.)\\s+$', '', txt)
- txt = re.sub('\n{3,}', '\n\n', txt)
- chars = list(range(8)) + [
- 11,
- 14,
- 15] + list(range(16, 25))
- illegal_chars = re.compile(u'|'.join(map(unichr, chars)))
- txt = illegal_chars.sub('', txt)
- if epub_split_size_kb > 0:
- length_byte = len(txt)
- chunk_size = long(length_byte / (int(length_byte / (epub_split_size_kb * 1024)) + 2))
- if len((filter,)((lambda x: len(x) > chunk_size), txt.split('\n\n'))):
- txt = []([ split_string_separator(line, chunk_size) for line in txt.split('\n\n') ])
-
-
- if isbytestring(txt):
- txt = txt.decode('utf-8')
-
- lines = []
- for line in txt.split('\n\n'):
- if line.strip():
- lines.append(u'<p>%s</p>' % prepare_string_for_xml(line.replace('\n', ' ')))
- continue
-
- return HTML_TEMPLATE % (title, u'\n'.join(lines))
-
-
- def convert_markdown(txt, title = '', disable_toc = False):
- md = markdown.Markdown(extensions = [
- 'footnotes',
- 'tables',
- 'toc'], extension_configs = {
- 'toc': {
- 'disable_toc': disable_toc } }, safe_mode = False)
- return HTML_TEMPLATE % (title, md.convert(txt))
-
-
- def separate_paragraphs_single_line(txt):
- txt = txt.replace('\r\n', '\n')
- txt = txt.replace('\r', '\n')
- txt = re.sub(u'(?<=.)\n(?=.)', '\n\n', txt)
- return txt
-
-
- def separate_paragraphs_print_formatted(txt):
- txt = re.sub(u'(?miu)^(\t+|[ ]{2,})(?=.)', '\n\t', txt)
- return txt
-
-
- def preserve_spaces(txt):
- txt = txt.replace(' ', ' ')
- txt = txt.replace('\t', ' ')
- return txt
-
-
- def opf_writer(path, opf_name, manifest, spine, mi):
- opf = OPFCreator(path, mi)
- opf.create_manifest(manifest)
- opf.create_spine(spine)
-
- try:
- opffile = _[1]
- opf.render(opffile)
- finally:
- pass
-
-
-
- def split_string_separator(txt, size):
- return txt
-
-