home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import sys
- import os
- import tempfile
- from calibre.ebooks.rtf2xml import copy, border_parse
-
- class ParagraphDef:
-
- def __init__(self, in_file, bug_handler, default_font, copy = None, run_level = 1):
- self._ParagraphDef__file = in_file
- self._ParagraphDef__bug_handler = bug_handler
- self._ParagraphDef__default_font = default_font
- self._ParagraphDef__copy = copy
- self._ParagraphDef__run_level = run_level
- self._ParagraphDef__write_to = tempfile.mktemp()
-
-
- def __initiate_values(self):
- self._ParagraphDef__token_dict = {
- 'par-end___': 'para',
- 'par-def___': 'paragraph-definition',
- 'keep-w-nex': 'keep-with-next',
- 'widow-cntl': 'widow-control',
- 'adjust-rgt': 'adjust-right',
- 'language__': 'language',
- 'right-inde': 'right-indent',
- 'fir-ln-ind': 'first-line-indent',
- 'left-inden': 'left-indent',
- 'space-befo': 'space-before',
- 'space-afte': 'space-after',
- 'line-space': 'line-spacing',
- 'default-ta': 'default-tab',
- 'align_____': 'align',
- 'widow-cntr': 'widow-control',
- 'style-shet': 'stylesheet',
- 'based-on__': 'based-on-style',
- 'next-style': 'next-style',
- 'char-style': 'character-style',
- 'para-style': 'name',
- 'picture___': 'pict',
- 'obj-class_': 'obj_class',
- 'mac-pic___': 'mac-pict',
- 'section___': 'section-new',
- 'sect-defin': 'section-reset',
- 'sect-note_': 'endnotes-in-section',
- 'list-text_': 'list-text',
- 'list-text_': 'list-text',
- 'list______': 'list',
- 'list-lev-d': 'list-level-definition',
- 'list-cardi': 'list-cardinal-numbering',
- 'list-decim': 'list-decimal-numbering',
- 'list-up-al': 'list-uppercase-alphabetic-numbering',
- 'list-up-ro': 'list-uppercae-roman-numbering',
- 'list-ord__': 'list-ordinal-numbering',
- 'list-ordte': 'list-ordinal-text-numbering',
- 'list-bulli': 'list-bullet',
- 'list-simpi': 'list-simple',
- 'list-conti': 'list-continue',
- 'list-hang_': 'list-hang',
- 'list-level': 'level',
- 'list-id___': 'list-id',
- 'list-start': 'list-start',
- 'nest-level': 'nest-level',
- 'list-level': 'list-level',
- 'footnote__': 'footnote',
- 'type______': 'type',
- 'toc_______': 'anchor-toc',
- 'book-mk-st': 'bookmark-start',
- 'book-mk-en': 'bookmark-end',
- 'index-mark': 'anchor-index',
- 'place_____': 'place',
- 'field_____': 'field',
- 'field-inst': 'field-instruction',
- 'field-rslt': 'field-result',
- 'datafield_': 'data-field',
- 'font-table': 'font-table',
- 'colr-table': 'color-table',
- 'lovr-table': 'list-override-table',
- 'listtable_': 'list-table',
- 'revi-table': 'revision-table',
- 'hidden____': 'hidden',
- 'italics___': 'italics',
- 'bold______': 'bold',
- 'strike-thr': 'strike-through',
- 'shadow____': 'shadow',
- 'outline___': 'outline',
- 'small-caps': 'small-caps',
- 'caps______': 'caps',
- 'dbl-strike': 'double-strike-through',
- 'emboss____': 'emboss',
- 'engrave___': 'engrave',
- 'subscript_': 'subscript',
- 'superscrip': 'superscipt',
- 'font-style': 'font-style',
- 'font-color': 'font-color',
- 'font-size_': 'font-size',
- 'font-up___': 'superscript',
- 'font-down_': 'subscript',
- 'red_______': 'red',
- 'blue______': 'blue',
- 'green_____': 'green',
- 'row-def___': 'row-definition',
- 'cell______': 'cell',
- 'row_______': 'row',
- 'in-table__': 'in-table',
- 'columns___': 'columns',
- 'row-pos-le': 'row-position-left',
- 'cell-posit': 'cell-position',
- 'underlined': 'underlined',
- 'bor-t-r-hi': 'border-table-row-horizontal-inside',
- 'bor-t-r-vi': 'border-table-row-vertical-inside',
- 'bor-t-r-to': 'border-table-row-top',
- 'bor-t-r-le': 'border-table-row-left',
- 'bor-t-r-bo': 'border-table-row-bottom',
- 'bor-t-r-ri': 'border-table-row-right',
- 'bor-cel-bo': 'border-cell-bottom',
- 'bor-cel-to': 'border-cell-top',
- 'bor-cel-le': 'border-cell-left',
- 'bor-cel-ri': 'border-cell-right',
- 'bor-par-bo': 'border-paragraph-bottom',
- 'bor-par-to': 'border-paragraph-top',
- 'bor-par-le': 'border-paragraph-left',
- 'bor-par-ri': 'border-paragraph-right',
- 'bor-par-bo': 'border-paragraph-box',
- 'bor-for-ev': 'border-for-every-paragraph',
- 'bor-outsid': 'border-outisde',
- 'bor-none__': 'border',
- 'bdr-single': 'single',
- 'bdr-doubtb': 'double-thickness-border',
- 'bdr-shadow': 'shadowed-border',
- 'bdr-double': 'double-border',
- 'bdr-dotted': 'dotted-border',
- 'bdr-dashed': 'dashed',
- 'bdr-hair__': 'hairline',
- 'bdr-inset_': 'inset',
- 'bdr-das-sm': 'dash-small',
- 'bdr-dot-sm': 'dot-dash',
- 'bdr-dot-do': 'dot-dot-dash',
- 'bdr-outset': 'outset',
- 'bdr-trippl': 'tripple',
- 'bdr-thsm__': 'thick-thin-small',
- 'bdr-htsm__': 'thin-thick-small',
- 'bdr-hthsm_': 'thin-thick-thin-small',
- 'bdr-thm__': 'thick-thin-medium',
- 'bdr-htm__': 'thin-thick-medium',
- 'bdr-hthm_': 'thin-thick-thin-medium',
- 'bdr-thl__': 'thick-thin-large',
- 'bdr-hthl_': 'think-thick-think-large',
- 'bdr-wavy_': 'wavy',
- 'bdr-d-wav': 'double-wavy',
- 'bdr-strip': 'striped',
- 'bdr-embos': 'emboss',
- 'bdr-engra': 'engrave',
- 'bdr-frame': 'frame',
- 'bdr-li-wid': 'line-width' }
- self._ParagraphDef__tabs_dict = {
- 'cw<pf<tab-stop__': self._ParagraphDef__tab_stop_func,
- 'cw<pf<tab-center': self._ParagraphDef__tab_type_func,
- 'cw<pf<tab-right_': self._ParagraphDef__tab_type_func,
- 'cw<pf<tab-dec___': self._ParagraphDef__tab_type_func,
- 'cw<pf<leader-dot': self._ParagraphDef__tab_leader_func,
- 'cw<pf<leader-hyp': self._ParagraphDef__tab_leader_func,
- 'cw<pf<leader-und': self._ParagraphDef__tab_leader_func,
- 'cw<pf<tab-bar-st': self._ParagraphDef__tab_bar_func }
- self._ParagraphDef__tab_type_dict = {
- 'cw<pf<tab-center': 'center',
- 'cw<pf<tab-right_': 'right',
- 'cw<pf<tab-dec___': 'decimal',
- 'cw<pf<leader-dot': 'leader-dot',
- 'cw<pf<leader-hyp': 'leader-hyphen',
- 'cw<pf<leader-und': 'leader-underline' }
- self._ParagraphDef__border_obj = border_parse.BorderParse()
- self._ParagraphDef__style_num_strings = []
- self._ParagraphDef__body_style_strings = []
- self._ParagraphDef__state = 'before_1st_para_def'
- self._ParagraphDef__att_val_dict = { }
- self._ParagraphDef__start_marker = 'mi<mk<pard-start\n'
- self._ParagraphDef__start2_marker = 'mi<mk<pardstart_\n'
- self._ParagraphDef__end2_marker = 'mi<mk<pardend___\n'
- self._ParagraphDef__end_marker = 'mi<mk<pard-end__\n'
- self._ParagraphDef__text_string = ''
- self._ParagraphDef__state_dict = {
- 'before_1st_para_def': self._ParagraphDef__before_1st_para_def_func,
- 'collect_tokens': self._ParagraphDef__collect_tokens_func,
- 'after_para_def': self._ParagraphDef__after_para_def_func,
- 'in_paragraphs': self._ParagraphDef__in_paragraphs_func,
- 'after_para_end': self._ParagraphDef__after_para_end_func }
- self._ParagraphDef__collect_tokens_dict = {
- 'mi<mk<para-start': self._ParagraphDef__end_para_def_func,
- 'cw<pf<par-def___': self._ParagraphDef__para_def_in_para_def_func,
- 'cw<tb<cell______': self._ParagraphDef__empty_table_element_func,
- 'cw<tb<row_______': self._ParagraphDef__empty_table_element_func }
- self._ParagraphDef__after_para_def_dict = {
- 'mi<mk<para-start': self._ParagraphDef__start_para_after_def_func,
- 'cw<pf<par-def___': self._ParagraphDef__found_para_def_func,
- 'cw<tb<cell______': self._ParagraphDef__empty_table_element_func,
- 'cw<tb<row_______': self._ParagraphDef__empty_table_element_func }
- self._ParagraphDef__in_paragraphs_dict = {
- 'mi<mk<para-end__': self._ParagraphDef__found_para_end_func }
- self._ParagraphDef__after_para_end_dict = {
- 'mi<mk<para-start': self._ParagraphDef__continue_block_func,
- 'mi<mk<para-end__': self._ParagraphDef__continue_block_func,
- 'cw<pf<par-def___': self._ParagraphDef__new_para_def_func,
- 'mi<mk<body-close': self._ParagraphDef__stop_block_func,
- 'mi<mk<par-in-fld': self._ParagraphDef__stop_block_func,
- 'cw<tb<cell______': self._ParagraphDef__stop_block_func,
- 'cw<tb<row-def___': self._ParagraphDef__stop_block_func,
- 'cw<tb<row_______': self._ParagraphDef__stop_block_func,
- 'mi<mk<sect-close': self._ParagraphDef__stop_block_func,
- 'mi<mk<sect-start': self._ParagraphDef__stop_block_func,
- 'mi<mk<header-beg': self._ParagraphDef__stop_block_func,
- 'mi<mk<header-end': self._ParagraphDef__stop_block_func,
- 'mi<mk<head___clo': self._ParagraphDef__stop_block_func,
- 'mi<mk<fldbk-end_': self._ParagraphDef__stop_block_func,
- 'mi<mk<lst-txbeg_': self._ParagraphDef__stop_block_func }
-
-
- def __before_1st_para_def_func(self, line):
- if self._ParagraphDef__token_info == 'cw<pf<par-def___':
- self._ParagraphDef__found_para_def_func()
- else:
- self._ParagraphDef__write_obj.write(line)
-
-
- def __found_para_def_func(self):
- self._ParagraphDef__state = 'collect_tokens'
- self._ParagraphDef__reset_dict()
-
-
- def __collect_tokens_func(self, line):
- action = self._ParagraphDef__collect_tokens_dict.get(self._ParagraphDef__token_info)
- if action:
- action(line)
- elif line[0:2] != 'cw':
- self._ParagraphDef__write_obj.write(line)
- self._ParagraphDef__state = 'after_para_def'
- elif line[0:5] == 'cw<bd':
- self._ParagraphDef__parse_border(line)
- else:
- action = self._ParagraphDef__tabs_dict.get(self._ParagraphDef__token_info)
- if action:
- action(line)
- else:
- token = self._ParagraphDef__token_dict.get(line[6:16])
- if token:
- self._ParagraphDef__att_val_dict[token] = line[20:-1]
-
-
-
- def __tab_stop_func(self, line):
- self._ParagraphDef__att_val_dict['tabs'] += '%s:' % self._ParagraphDef__tab_type
- self._ParagraphDef__att_val_dict['tabs'] += '%s;' % line[20:-1]
- self._ParagraphDef__tab_type = 'left'
-
-
- def __tab_type_func(self, line):
- type = self._ParagraphDef__tab_type_dict.get(self._ParagraphDef__token_info)
- if type != None:
- self._ParagraphDef__tab_type = type
- elif self._ParagraphDef__run_level > 3:
- msg = 'no entry for %s\n' % self._ParagraphDef__token_info
- raise self._ParagraphDef__bug_handler, msg
-
-
-
- def __tab_leader_func(self, line):
- leader = self._ParagraphDef__tab_type_dict.get(self._ParagraphDef__token_info)
- if leader != None:
- self._ParagraphDef__att_val_dict['tabs'] += '%s^' % leader
- elif self._ParagraphDef__run_level > 3:
- msg = 'no entry for %s\n' % self._ParagraphDef__token_info
- raise self._ParagraphDef__bug_handler, msg
-
-
-
- def __tab_bar_func(self, line):
- self._ParagraphDef__att_val_dict['tabs'] += 'bar:%s;' % line[20:-1]
- self._ParagraphDef__tab_type = 'left'
-
-
- def __parse_border(self, line):
- border_dict = self._ParagraphDef__border_obj.parse_border(line)
- self._ParagraphDef__att_val_dict.update(border_dict)
-
-
- def __para_def_in_para_def_func(self, line):
- self._ParagraphDef__state = 'collect_tokens'
- self._ParagraphDef__reset_dict()
-
-
- def __end_para_def_func(self, line):
- self._ParagraphDef__write_para_def_beg()
- self._ParagraphDef__write_obj.write(line)
- self._ParagraphDef__state = 'in_paragraphs'
-
-
- def __start_para_after_def_func(self, line):
- self._ParagraphDef__write_para_def_beg()
- self._ParagraphDef__write_obj.write(line)
- self._ParagraphDef__state = 'in_paragraphs'
-
-
- def __after_para_def_func(self, line):
- action = self._ParagraphDef__after_para_def_dict.get(self._ParagraphDef__token_info)
- if self._ParagraphDef__token_info == 'cw<pf<par-def___':
- self._ParagraphDef__found_para_def_func()
- elif action:
- action(line)
- else:
- self._ParagraphDef__write_obj.write(line)
-
-
- def __in_paragraphs_func(self, line):
- action = self._ParagraphDef__in_paragraphs_dict.get(self._ParagraphDef__token_info)
- if action:
- action(line)
- else:
- self._ParagraphDef__write_obj.write(line)
-
-
- def __found_para_end_func(self, line):
- self._ParagraphDef__state = 'after_para_end'
- self._ParagraphDef__write_obj.write(line)
-
-
- def __after_para_end_func(self, line):
- self._ParagraphDef__text_string += line
- action = self._ParagraphDef__after_para_end_dict.get(self._ParagraphDef__token_info)
- if action:
- action(line)
-
-
-
- def __continue_block_func(self, line):
- self._ParagraphDef__state = 'in_paragraphs'
- self._ParagraphDef__write_obj.write(self._ParagraphDef__text_string)
- self._ParagraphDef__text_string = ''
-
-
- def __new_para_def_func(self, line):
- self._ParagraphDef__write_para_def_end_func()
- self._ParagraphDef__found_para_def_func()
-
-
- def __stop_block_func(self, line):
- self._ParagraphDef__write_para_def_end_func()
- self._ParagraphDef__state = 'after_para_def'
-
-
- def __write_para_def_end_func(self):
- self._ParagraphDef__write_obj.write(self._ParagraphDef__end2_marker)
- self._ParagraphDef__write_obj.write('mi<tg<close_____<paragraph-definition\n')
- self._ParagraphDef__write_obj.write(self._ParagraphDef__end_marker)
- self._ParagraphDef__write_obj.write(self._ParagraphDef__text_string)
- self._ParagraphDef__text_string = ''
- keys = self._ParagraphDef__att_val_dict.keys()
- if 'font-style' in keys:
- self._ParagraphDef__write_obj.write('mi<mk<font-end__\n')
-
- if 'caps' in keys:
- self._ParagraphDef__write_obj.write('mi<mk<caps-end__\n')
-
-
-
- def __get_num_of_style(self):
- my_string = ''
- new_style = 0
- ignore_values = [
- 'style-num',
- 'nest-level',
- 'in-table']
- keys = self._ParagraphDef__att_val_dict.keys()
- keys.sort()
- for key in keys:
- if key in ignore_values:
- continue
-
- my_string += '%s:%s' % (key, self._ParagraphDef__att_val_dict[key])
-
- if my_string in self._ParagraphDef__style_num_strings:
- num = self._ParagraphDef__style_num_strings.index(my_string)
- num += 1
- else:
- self._ParagraphDef__style_num_strings.append(my_string)
- num = len(self._ParagraphDef__style_num_strings)
- new_style = 1
- num = '%04d' % num
- self._ParagraphDef__att_val_dict['style-num'] = 's' + str(num)
- if new_style:
- self._ParagraphDef__write_body_styles()
-
-
-
- def __write_body_styles(self):
- style_string = ''
- style_string += 'mi<tg<empty-att_<paragraph-style-in-body'
- style_string += '<name>%s' % self._ParagraphDef__att_val_dict['name']
- style_string += '<style-number>%s' % self._ParagraphDef__att_val_dict['style-num']
- tabs_list = [
- 'tabs-left',
- 'tabs-right',
- 'tabs-decimal',
- 'tabs-center',
- 'tabs-bar',
- 'tabs']
- if self._ParagraphDef__att_val_dict['tabs'] != '':
- the_value = self._ParagraphDef__att_val_dict['tabs']
- style_string += '<%s>%s' % ('tabs', the_value)
-
- keys = self._ParagraphDef__att_val_dict.keys()
- keys.sort()
- for key in keys:
- if key != 'name' and key != 'style-num' and key != 'in-table' and key not in tabs_list:
- style_string += '<%s>%s' % (key, self._ParagraphDef__att_val_dict[key])
- continue
-
- style_string += '\n'
- self._ParagraphDef__body_style_strings.append(style_string)
-
-
- def __write_para_def_beg(self):
- self._ParagraphDef__get_num_of_style()
- table = self._ParagraphDef__att_val_dict.get('in-table')
- if table:
- self._ParagraphDef__write_obj.write('mi<mk<in-table__\n')
- else:
- self._ParagraphDef__write_obj.write('mi<mk<not-in-tbl\n')
- left_indent = self._ParagraphDef__att_val_dict.get('left-indent')
- if left_indent:
- self._ParagraphDef__write_obj.write('mi<mk<left_inden<%s\n' % left_indent)
-
- is_list = self._ParagraphDef__att_val_dict.get('list-id')
- if is_list:
- self._ParagraphDef__write_obj.write('mi<mk<list-id___<%s\n' % is_list)
- else:
- self._ParagraphDef__write_obj.write('mi<mk<no-list___\n')
- self._ParagraphDef__write_obj.write('mi<mk<style-name<%s\n' % self._ParagraphDef__att_val_dict['name'])
- self._ParagraphDef__write_obj.write(self._ParagraphDef__start_marker)
- self._ParagraphDef__write_obj.write('mi<tg<open-att__<paragraph-definition')
- self._ParagraphDef__write_obj.write('<name>%s' % self._ParagraphDef__att_val_dict['name'])
- self._ParagraphDef__write_obj.write('<style-number>%s' % self._ParagraphDef__att_val_dict['style-num'])
- tabs_list = [
- 'tabs-left',
- 'tabs-right',
- 'tabs-decimal',
- 'tabs-center',
- 'tabs-bar',
- 'tabs']
- if self._ParagraphDef__att_val_dict['tabs'] != '':
- the_value = self._ParagraphDef__att_val_dict['tabs']
- self._ParagraphDef__write_obj.write('<%s>%s' % ('tabs', the_value))
-
- keys = self._ParagraphDef__att_val_dict.keys()
- keys.sort()
- for key in keys:
- if key != 'name' and key != 'style-num' and key != 'in-table' and key not in tabs_list:
- self._ParagraphDef__write_obj.write('<%s>%s' % (key, self._ParagraphDef__att_val_dict[key]))
- continue
-
- self._ParagraphDef__write_obj.write('\n')
- self._ParagraphDef__write_obj.write(self._ParagraphDef__start2_marker)
- if 'font-style' in keys:
- face = self._ParagraphDef__att_val_dict['font-style']
- self._ParagraphDef__write_obj.write('mi<mk<font______<%s\n' % face)
-
- if 'caps' in keys:
- value = self._ParagraphDef__att_val_dict['caps']
- self._ParagraphDef__write_obj.write('mi<mk<caps______<%s\n' % value)
-
-
-
- def __empty_table_element_func(self, line):
- self._ParagraphDef__write_obj.write('mi<mk<in-table__\n')
- self._ParagraphDef__write_obj.write(line)
- self._ParagraphDef__state = 'after_para_def'
-
-
- def __reset_dict(self):
- self._ParagraphDef__att_val_dict.clear()
- self._ParagraphDef__att_val_dict['name'] = 'Normal'
- self._ParagraphDef__att_val_dict['font-style'] = self._ParagraphDef__default_font
- self._ParagraphDef__tab_type = 'left'
- self._ParagraphDef__att_val_dict['tabs-left'] = ''
- self._ParagraphDef__att_val_dict['tabs-right'] = ''
- self._ParagraphDef__att_val_dict['tabs-center'] = ''
- self._ParagraphDef__att_val_dict['tabs-decimal'] = ''
- self._ParagraphDef__att_val_dict['tabs-bar'] = ''
- self._ParagraphDef__att_val_dict['tabs'] = ''
-
-
- def make_paragraph_def(self):
- self._ParagraphDef__initiate_values()
- read_obj = open(self._ParagraphDef__file, 'r')
- self._ParagraphDef__write_obj = open(self._ParagraphDef__write_to, 'w')
- line_to_read = 1
- while line_to_read:
- line_to_read = read_obj.readline()
- line = line_to_read
- self._ParagraphDef__token_info = line[:16]
- action = self._ParagraphDef__state_dict.get(self._ParagraphDef__state)
- if action == None:
- sys.stderr.write('no no matching state in module sections.py\n')
- sys.stderr.write(self._ParagraphDef__state + '\n')
-
- action(line)
- read_obj.close()
- self._ParagraphDef__write_obj.close()
- copy_obj = copy.Copy(bug_handler = self._ParagraphDef__bug_handler)
- if self._ParagraphDef__copy:
- copy_obj.copy_file(self._ParagraphDef__write_to, 'paragraphs_def.data')
-
- copy_obj.rename(self._ParagraphDef__write_to, self._ParagraphDef__file)
- os.remove(self._ParagraphDef__write_to)
- return self._ParagraphDef__body_style_strings
-
-
-