######################################################################### # # # # # 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. # # # # You should have received a copy of the GNU General Public License # # along with this program; if not, write to the Free Software # # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA # # 02111-1307 USA # # # # # ######################################################################### import sys, os, tempfile, re from libprs500.ebooks.rtf2xml import field_strings, copy 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 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 = tempfile.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() read_obj = open(self.__file) self.__write_obj = open(self.__write_to, 'w') line_to_read = '1' while line_to_read: line_to_read = read_obj.readline() line = line_to_read self.__token_info = line[:16] if self.__token_info == 'ob