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 """ States. 1. default 1. an open bracket ends this state. 2. Text print out text. Print out any groups_in_waiting. 3. closed bracket. Close groups 2. after an open bracket 1. The lack of a control word ends this state. 2. paragraph end -- close out all tags 3. footnote beg -- close out all tags """ class Inline: """ Make inline tags within lists. Logic: """ 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.__run_level = run_level self.__write_to = better_mktemp() def __initiate_values(self): """ Initiate all values. """ self.__state_dict = { 'default': self.__default_func, 'after_open_bracket': self.__after_open_bracket_func, } self.__default_dict = { 'ob ci 'annotation' : 'annotation', 'blue______' : 'blue', 'bold______' : 'bold', 'caps______' : 'caps', 'char-style' : 'character-style', 'dbl-strike' : 'double-strike-through', 'emboss____' : 'emboss', 'engrave___' : 'engrave', 'font-color' : 'font-color', 'font-down_' : 'subscript', 'font-size_' : 'font-size', 'font-style' : 'font-style', 'font-up___' : 'superscript', 'footnot-mk' : 'footnote-marker', 'green_____' : 'green', 'hidden____' : 'hidden', 'italics___' : 'italics', 'outline___' : 'outline', 'red_______' : 'red', 'shadow____' : 'shadow', 'small-caps' : 'small-caps', 'strike-thr' : 'strike-through', 'subscript_' : 'subscript', 'superscrip' : 'superscript', 'underlined' : 'underlined', } self.__caps_list = ['false'] def __set_list_func(self, line): """ Requires: line--line of text Returns: nothing Logic: """ if self.__place == 'in_list': if self.__token_info == 'mi 3: msg = 'self.__inline_list is %s\n' % self.__inline_list raise self.__bug_handler(msg) self.__write_obj.write('error\n') self.__groups_in_waiting[0] = 0 return for the_dict in inline_list: if the_dict['contains_inline']: the_keys = the_dict.keys() if 'font-style' in the_keys: face = the_dict['font-style'] self.__write_obj.write('mi%s' % (the_key, the_dict[the_key])) self.__write_obj.write('\n') self.__groups_in_waiting[0] = 0 def __end_para_func(self, line): """ Requires: line -- line of text Returns: nothing Logic: Slice from the end the groups in waiting. Iterate through the list. If the dictionary contaings info, write a closing tag. """ if not self.__in_para: return if self.__groups_in_waiting[0] == 0: inline_list = self.__inline_list else: last_index = -1 * self.__groups_in_waiting[0] inline_list = self.__inline_list[0:last_index] for the_dict in inline_list: contains_info = the_dict.get('contains_inline') if contains_info: the_keys = the_dict.keys() if 'font-style' in the_keys: self.__write_obj.write('mi%s' % (the_key, the_dict[the_key])) self.__write_obj.write('\n') self.__groups_in_waiting[0] = 0 def __found_field_func(self, line): """ Just a default function to make sure I don't prematurely exit default state """ pass def form_tags(self): """ Requires: area--area to parse (list or non-list) Returns: nothing Logic: Read one line in at a time. Determine what action to take based on the state. """ 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: token = line[0:-1] self.__token_info = '' if token == 'tx