######################################################################### # # # # # copyright 2002 Paul Henry Tremblay # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # # General Public License for more details. # # # # # ######################################################################### import sys, os from ebook_converter.ebooks.rtf2xml import copy, override_table, list_table from ebook_converter.ptempfile import better_mktemp from . import open_for_read, open_for_write class PreambleDiv: """ Break the preamble into divisions. """ def __init__(self, in_file, bug_handler, copy=None, no_namespace=None, run_level=1, ): """ Required: 'file' Optional: 'copy'-- whether to make a copy of result for debugging 'temp_dir' --where to output temporary results (default is directory from which the script is run.) Returns: nothing """ self.__file = in_file self.__bug_handler = bug_handler self.__copy = copy self.__no_namespace = no_namespace self.__write_to = better_mktemp() self.__run_level = run_level def __initiate_values(self): """ Set values, including those for the dictionary. """ self.__all_lists = {} self.__page = { 'margin-top' : 72, 'margin-bottom' : 72, 'margin-left' : 90, 'margin-right' : 90, 'gutter' : 0, } self.__cb_count = '' self.__ob_count = '' self.__state = 'preamble' self.__rtf_final = '' self.__close_group_count = '' self.__found_font_table = 0 self.__list_table_final = '' self.__override_table_final = '' self.__revision_table_final = '' self.__doc_info_table_final = '' self.__state_dict = { 'default' : self.__default_func, 'rtf_header' : self.__rtf_head_func, 'preamble' : self.__preamble_func, 'font_table' : self.__font_table_func, 'color_table' : self.__color_table_func, 'style_sheet' : self.__style_sheet_func, 'list_table' : self.__list_table_func, 'override_table' : self.__override_table_func, 'revision_table' : self.__revision_table_func, 'doc_info' : self.__doc_info_func, 'body' : self.__body_func, 'ignore' : self.__ignore_func, 'cw%s' % (key, self.__page[key]) ) self.__write_obj.write('\n') # mi%s' % (key, self.__section[key]) ) self.__write_obj.write('\n') def __section_func(self, line): """ Add info pertaining to section to the self.__section dictionary, to be printed out later. """ info = self.__translate_sec.get(line[6:16]) if info is None: sys.stderr.write('woops!\n') else: self.__section[info] = 'true' def __body_func(self, line): self.__write_obj.write(line) def __default_func(self, line): # either in preamble or in body pass def __para_def_func(self, line): # if self.__ob_group == 1 # this tells dept of group if self.__cb_count == '0002': self.__state = 'body' self.__write_preamble() self.__write_obj.write(line) def __text_func(self, line): """ If the cb_count is less than 1, you have hit the body For older RTF Newer RTF should never have to use this function """ if self.__cb_count == '': cb_count = '0002' else: cb_count = self.__cb_count # ignore previous lines # should be # if self.__ob_group == 1 # this tells dept of group if cb_count == '0002': self.__state = 'body' self.__write_preamble() self.__write_obj.write(line) def __row_def_func(self, line): # if self.__ob_group == 1 # this tells dept of group if self.__cb_count == '0002': self.__state = 'body' self.__write_preamble() self.__write_obj.write(line) def __new_section_func(self, line): """ This is new. The start of a section marks the end of the preamble """ if self.__cb_count == '0002': self.__state = 'body' self.__write_preamble() else: sys.stderr.write('module is preamble_div\n') sys.stderr.write('method is __new_section_func\n') sys.stderr.write('bracket count should be 2?\n') self.__write_obj.write(line) def __write_preamble(self): """ Write all the strings, which represent all the data in the preamble. Write a body and section beginning. """ if self.__no_namespace: self.__write_obj.write( 'mihttp://rtf2xml.sourceforge.net/\n') self.__write_obj.write('mi1\n') # self.__print_sec_info() # self.__write_obj.write('mi