######################################################################### # # # # # 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 from ebook_converter.ptempfile import better_mktemp from . import open_for_read, open_for_write class Header: """ Two public methods are available. The first separates all of the headers and footers from the body and puts them at the bottom of the text, where they are easier to process. The second joins those headers and footers to the proper places in the body. """ def __init__(self, in_file , bug_handler, copy=None, run_level=1, ): self.__file = in_file self.__bug_handler = bug_handler self.__copy = copy self.__write_to = better_mktemp() self.__found_a_header = False def __in_header_func(self, line): """ Handle all tokens that are part of header """ if self.__cb_count == self.__header_bracket_count: self.__in_header = False self.__write_obj.write(line) self.__write_to_head_obj.write( 'mi%s\n' % (type) ) else: sys.stderr.write( 'module is header\n' 'method is __found_header\n' 'no dict entry\n' 'line is %s' % line) self.__write_to_head_obj.write( 'minone\n' ) def __default_sep(self, line): """ Handle all tokens that are not header tokens """ if self.__token_info[3:5] == 'hf': self.__found_header(line) self.__write_obj.write(line) def __initiate_sep_values(self): """ initiate counters for separate_footnotes method. """ self.__bracket_count=0 self.__ob_count = 0 self.__cb_count = 0 self.__header_bracket_count = 0 self.__in_header = False self.__header_count = 0 self.__head_dict = { 'head-left_' : ('header-left'), 'head-right' : ('header-right'), 'foot-left_' : ('footer-left'), 'foot-right' : ('footer-right'), 'head-first' : ('header-first'), 'foot-first' : ('footer-first'), 'header____' : ('header'), 'footer____' : ('footer'), } def separate_headers(self): """ Separate all the footnotes in an RTF file and put them at the bottom, where they are easier to process. Each time a footnote is found, print all of its contents to a temporary file. Close both the main and temporary file. Print the footnotes from the temporary file to the bottom of the main file. """ self.__initiate_sep_values() self.__header_holder = better_mktemp() with open_for_read(self.__file) as read_obj: with open_for_write(self.__write_to) as self.__write_obj: with open_for_write(self.__header_holder) as self.__write_to_head_obj: for line in read_obj: self.__token_info = line[:16] # keep track of opening and closing brackets if self.__token_info == 'ob