######################################################################### # # # # # 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, re from ebook_converter.ebooks.rtf2xml import field_strings, copy from ebook_converter.ptempfile import better_mktemp from . import open_for_read, open_for_write class FieldsSmall: """ ================= Purpose ================= Write tags for bookmarks, index and toc entry fields in a tokenized file. This module does not handle toc or index tables. (This module won't be any use to you unless you use it as part of the other modules.) ----------- Method ----------- Look for the beginning of a bookmark, index, or toc entry. When such a token is found, store the opeing bracket count in a variable. Collect all the text until the closing bracket entry is found. Send the string to the module field_strings to process it. Write the processed string to the output file. """ def __init__(self, in_file, bug_handler, copy=None, run_level=1, ): """ Required: 'file'--file to parse 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() self.__run_level = run_level def __initiate_values(self): """ Initiate all values. """ self.__string_obj = field_strings.FieldStrings(bug_handler=self.__bug_handler) self.__state = 'before_body' self.__text_string = '' self.__marker = 'mi%s' '%snone\n' % (type, my_string)) return my_changed_string def __found_toc_index_func(self, line, tag): """ Requires: line --the line to parse Returns: nothing Logic: This function is called when a toc or index entry is found. The opening bracket count is stored in the beginning bracket count. The state is changed to 'toc_index.' """ self.__beg_bracket_count = self.__ob_count self.__cb_count = 0 self.__state = 'toc_index' self.__tag = tag def __toc_index_func(self, line): """ Requires: line --the line to parse Returns: nothing Logic: This function handles all lines within a toc or index entry. It adds each line to a string until the end of the entry is found. It processes the string with the fields_string module, and prints out the result. """ if self.__beg_bracket_count == self.__cb_count: self.__state = 'body' type = self.__tag if type == 'index': my_string = self.__parse_index_func( self.__text_string) elif type == 'toc': my_string = self.__parse_toc_func( self.__text_string) self.__write_obj.write(self.__marker) self.__write_obj.write(my_string) self.__text_string = '' self.__write_obj.write(line) else: self.__text_string += line def fix_fields(self): """ Requires: nothing Returns: nothing (changes the original file) Logic: Read one line in at a time. Determine what action to take based on the state. If the state is before the body, look for the beginning of the body. The other two states are toc_index (for toc and index entries) and bookmark. """ self.__initiate_values() with open_for_read(self.__file) as read_obj: with open_for_write(self.__write_to) as self.__write_obj: for line in read_obj: self.__token_info = line[:16] if self.__token_info == 'ob