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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
  9. from calibre.ebooks.txt.processor import convert_basic, convert_markdown, separate_paragraphs_single_line, separate_paragraphs_print_formatted, preserve_spaces
  10. from calibre import _ent_pat, xml_entity_to_unicode
  11.  
  12. class TXTInput(InputFormatPlugin):
  13.     name = 'TXT Input'
  14.     author = 'John Schember'
  15.     description = 'Convert TXT files to HTML'
  16.     file_types = set([
  17.         'txt'])
  18.     options = set([
  19.         OptionRecommendation(name = 'single_line_paras', recommended_value = False, help = _('Normally calibre treats blank lines as paragraph markers. With this option it will assume that every line represents a paragraph instead.')),
  20.         OptionRecommendation(name = 'print_formatted_paras', recommended_value = False, help = _('Normally calibre treats blank lines as paragraph markers. With this option it will assume that every line starting with an indent (either a tab or 2+ spaces) represents a paragraph. Paragraphs end when the next line that starts with an indent is reached.')),
  21.         OptionRecommendation(name = 'preserve_spaces', recommended_value = False, help = _('Normally extra spaces are condensed into a single space. With this option all spaces will be displayed.')),
  22.         OptionRecommendation(name = 'markdown', recommended_value = False, help = _('Run the text input through the markdown pre-processor. To learn more about markdown see') + ' http://daringfireball.net/projects/markdown/'),
  23.         OptionRecommendation(name = 'markdown_disable_toc', recommended_value = False, help = _('Do not insert a Table of Contents into the output text.'))])
  24.     
  25.     def convert(self, stream, options, file_ext, log, accelerators):
  26.         ienc = None if stream.encoding else 'utf-8'
  27.         if options.input_encoding:
  28.             ienc = options.input_encoding
  29.         
  30.         log.debug('Reading text from file...')
  31.         txt = stream.read().decode(ienc, 'replace')
  32.         if options.single_line_paras:
  33.             txt = separate_paragraphs_single_line(txt)
  34.         
  35.         if options.print_formatted_paras:
  36.             txt = separate_paragraphs_print_formatted(txt)
  37.         
  38.         if options.preserve_spaces:
  39.             txt = preserve_spaces(txt)
  40.         
  41.         txt = _ent_pat.sub(xml_entity_to_unicode, txt)
  42.         if options.markdown:
  43.             log.debug('Running text though markdown conversion...')
  44.             
  45.             try:
  46.                 html = convert_markdown(txt, disable_toc = options.markdown_disable_toc)
  47.             except RuntimeError:
  48.                 raise ValueError('This txt file has malformed markup, it cannot be converted by calibre. See http://daringfireball.net/projects/markdown/syntax')
  49.             except:
  50.                 None<EXCEPTION MATCH>RuntimeError
  51.             
  52.  
  53.         None<EXCEPTION MATCH>RuntimeError
  54.         flow_size = getattr(options, 'flow_size', 0)
  55.         html = convert_basic(txt, epub_split_size_kb = flow_size)
  56.         plugin_for_input_format = plugin_for_input_format
  57.         import calibre.customize.ui
  58.         html_input = plugin_for_input_format('html')
  59.         for opt in html_input.options:
  60.             setattr(options, opt.option.name, opt.recommended_value)
  61.         
  62.         options.input_encoding = 'utf-8'
  63.         base = os.getcwdu()
  64.         if hasattr(stream, 'name'):
  65.             base = os.path.dirname(stream.name)
  66.         
  67.         htmlfile = open(os.path.join(base, 'temp_calibre_txt_input_to_html.html'), 'wb')
  68.         htmlfile.write(html.encode('utf-8'))
  69.         htmlfile.close()
  70.         cwd = os.getcwdu()
  71.         odi = options.debug_pipeline
  72.         options.debug_pipeline = None
  73.         oeb = html_input(open(htmlfile.name, 'rb'), options, 'html', log, { }, cwd)
  74.         options.debug_pipeline = odi
  75.         os.remove(htmlfile.name)
  76.         return oeb
  77.  
  78.  
  79.