######################################################################### # # # # # 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, border_parse from ebook_converter.ptempfile import better_mktemp from . import open_for_read, open_for_write """ States. 1. 'not_in_table' 1. 'cw 0: if self.__state[-1] == 'in_row_def': self.__state.pop() # added [{]] at the *end* of each /cell. Get rid of extra one self.__cell_list.pop() widths = self.__row_dict.get('widths') if widths: width_list = widths.split(',') num_cells = len(width_list) self.__row_dict['number-of-cells'] = num_cells def __in_row_def_func(self, line): """ Requires: line --line to parse Returns: nothing Logic: In the text that defines a row. If a control word is found, handle the control word with another method. Check for states that will end this state. While in the row definition, certain tokens can end a row or end a table. If a paragrah definition (pard-start) is found, and the you are already in a table, start of a row. """ if self.__token_info == 'cw 0 and self.__state[-1] == 'in_table': self.__start_row_func(line) self.__start_cell_func(line) self.__write_obj.write(line) elif self.__token_info == 'mi 0 and self.__state[-1] != 'in_table': self.__start_table_func(line) self.__write_obj.write(line) else: self.__write_obj.write(line) def __handle_row_token(self, line): """ Requires: line -- line to parse Returns: ? Logic: the tokens in the row definition contain the following information: 1. row borders. 2. cell borders for all cells in the row. 3. cell postions for all cells in the row. Put all information about row borders into a row dictionary. Put all information about cell borders into into the dictionary in the last item in the cell list. ([{border:something, width:something}, {border:something, width:something}]) cw attributes for key=> value pop the self.__cell_list. Otherwise, print out a cell tag. """ self.__state.append('in_cell') # self.__cell_list = [] if len(self.__cell_list) > 0: self.__write_obj.write('mi%s' % (key, cell_dict[key])) self.__write_obj.write('\n') # self.__cell_list.pop() self.__cell_list.pop(0) # self.__cell_list = self.__cell_list[1:] else: self.__write_obj.write('mi attributes for key=> value """ self.__state.append('in_row') self.__write_obj.write('mi%s' % (key, self.__row_dict[key])) self.__write_obj.write('\n') self.__cells_in_row = 0 self.__rows_in_table += 1 def __found_cell_position(self, line): """ needs: line: current line returns: nothing logic: Calculate the cell width. If the cell is the first cell, you should add the left cell position to it. (This value is often negative.) Next, set the new last_cell_position to the current cell position. """ # cw 1: if self.__state[-1] == 'in_cell': self.__state.pop() self.__write_obj.write('mi 1 and self.__state[-1] == 'in_row': self.__state.pop() self.__write_obj.write('mi self.__max_number_cells_in_row: self.__max_number_cells_in_row = self.__cells_in_row self.__list_of_cells_in_row.append(self.__cells_in_row) def __empty_cell(self, line): """ Required: line -- line of text Returns: nothing Logic: Write an empty tag with attributes if there are attributes. Otherwise, writen an empty tag with cell as element. """ if len(self.__cell_list) > 0: self.__write_obj.write('mi%s' % (key, cell_dict[key])) self.__write_obj.write('\n') else: self.__write_obj.write('mi max: mode = item max = num_of_values return mode def make_table(self): """ Requires: nothing Returns: A dictionary of values for the beginning of the table. Logic: Read one line in at a time. Determine what action to take based on the state. """ self.__initiate_values() read_obj = open_for_read(self.__file) self.__write_obj = open_for_write(self.__write_to) line_to_read = 1 while line_to_read: line_to_read = read_obj.readline() line = line_to_read self.__token_info = line[:16] action = self.__state_dict.get(self.__state[-1]) # print self.__state[-1] if action is None: sys.stderr.write('No matching state in module table.py\n') sys.stderr.write(self.__state[-1] + '\n') action(line) read_obj.close() self.__write_obj.close() copy_obj = copy.Copy(bug_handler=self.__bug_handler) if self.__copy: copy_obj.copy_file(self.__write_to, "table.data") copy_obj.rename(self.__write_to, self.__file) os.remove(self.__write_to) return self.__table_data