######################################################################### # # # # # 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 os, re from ebook_converter.ebooks.rtf2xml import copy from ebook_converter.ptempfile import better_mktemp from . import open_for_read, open_for_write class HeadingsToSections: """ """ def __init__(self, in_file, bug_handler, copy=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.__write_to = better_mktemp() def __initiate_values(self): """ Required: Nothing Return: Nothing Logic: The self.__end_list is a list of tokens that will force a list to end. Likewise, the self.__end_lines is a list of lines that forces a list to end. """ self.__state = "default" self.__all_sections = [] self.__chunk = '' self.__state_dict={ 'default' : self.__default_func, 'in_table' : self.__in_table_func, 'in_list' : self.__in_list_func, 'after_body' : self.__after_body_func, } self.__list_depth = 0 self.__end_list = [ 'mi(\d+)') def __close_lists(self): """ Required: Nothing Return: Nothing Logic: Reverse the list of dictionaries. Iterate through the list and get the indent for each list. If the current indent is less than or equal to the indent in the dictionary, close that level. Keep track of how many levels you close. Reduce the list by that many levels. Reverse the list again. """ current_indent = self.__left_indent self.__all_lists.reverse() num_levels_closed = 0 for the_dict in self.__all_lists: list_indent = the_dict.get('left-indent') if current_indent <= list_indent: self.__write_end_item() self.__write_end_list() num_levels_closed += 1 self.__all_lists = self.__all_lists[num_levels_closed:] self.__all_lists.reverse() def __close_sections(self, current_level): self.__all_sections.reverse() num_levels_closed = 0 for level in self.__all_sections: if current_level <= level: self.__write_end_section() num_levels_closed += 1 self.__all_sections = self.__all_sections[num_levels_closed:] self.__all_sections.reverse() def __write_start_section(self, current_level, name): section_num = '' for the_num in self.__section_num: section_num += '%s.' % the_num section_num = section_num[:-1] num_in_level = len(self.__all_sections) num_in_level = self.__section_num[num_in_level] level = len(self.__all_sections) self.__write_obj.write( 'mi%s%s%s' '%s\n' % (section_num, num_in_level, level, name) ) def __write_end_section(self): self.__write_obj.write('mi