mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
PML Input: New handling of \t and \T tags. \T's that do not start the line are ignored. \t's that start and end the line use a margin for the text block
This commit is contained in:
commit
c21d927caa
@ -70,7 +70,7 @@ class PML_HTMLizer(object):
|
|||||||
'c': ('<div style="text-align: center; margin: auto;">', '</div>'),
|
'c': ('<div style="text-align: center; margin: auto;">', '</div>'),
|
||||||
'r': ('<div style="text-align: right;">', '</div>'),
|
'r': ('<div style="text-align: right;">', '</div>'),
|
||||||
't': ('<div style="margin-left: 5%;">', '</div>'),
|
't': ('<div style="margin-left: 5%;">', '</div>'),
|
||||||
'T': ('<div style="margin-left: %s;">', '</div>'),
|
'T': ('<div style="text-indent: %s;">', '</div>'),
|
||||||
'i': ('<span style="font-style: italic;">', '</span>'),
|
'i': ('<span style="font-style: italic;">', '</span>'),
|
||||||
'u': ('<span style="text-decoration: underline;">', '</span>'),
|
'u': ('<span style="text-decoration: underline;">', '</span>'),
|
||||||
'd': ('<span style="text-decoration: line-through;">', '</span>'),
|
'd': ('<span style="text-decoration: line-through;">', '</span>'),
|
||||||
@ -499,7 +499,13 @@ class PML_HTMLizer(object):
|
|||||||
self.toc = []
|
self.toc = []
|
||||||
self.file_name = file_name
|
self.file_name = file_name
|
||||||
|
|
||||||
indent_state = {'t': False, 'T': False}
|
# t: Are we in an open \t tag set?
|
||||||
|
# T: Are we in an open \T?
|
||||||
|
# st: Did the \t start the line?
|
||||||
|
# sT: Did the \T start the line?
|
||||||
|
# et: Did the \t end the line?
|
||||||
|
indent_state = {'t': False, 'T': False, 'st': False, 'sT': False, 'et': False}
|
||||||
|
basic_indent = False
|
||||||
adv_indent_val = ''
|
adv_indent_val = ''
|
||||||
# Keep track of the number of empty lines
|
# Keep track of the number of empty lines
|
||||||
# between paragraphs. When we reach a set number
|
# between paragraphs. When we reach a set number
|
||||||
@ -512,8 +518,26 @@ class PML_HTMLizer(object):
|
|||||||
for line in pml.splitlines():
|
for line in pml.splitlines():
|
||||||
parsed = []
|
parsed = []
|
||||||
empty = True
|
empty = True
|
||||||
|
|
||||||
basic_indent = indent_state['t']
|
basic_indent = indent_state['t']
|
||||||
adv_indent = indent_state['T']
|
indent_state['T'] = False
|
||||||
|
# Determine if the \t starts the line or if we are
|
||||||
|
# in an open \t block.
|
||||||
|
if line.lstrip().startswith('\\t') or basic_indent:
|
||||||
|
basic_indent = True
|
||||||
|
indent_state['st'] = True
|
||||||
|
else:
|
||||||
|
indent_state['st'] = False
|
||||||
|
# Determine if the \T starts the line.
|
||||||
|
if line.lstrip().startswith('\\T'):
|
||||||
|
indent_state['sT'] = True
|
||||||
|
else:
|
||||||
|
indent_state['sT'] = False
|
||||||
|
# Determine if the \t ends the line.
|
||||||
|
if line.rstrip().endswith('\\t'):
|
||||||
|
indent_state['et'] = True
|
||||||
|
else:
|
||||||
|
indent_state['et'] = False
|
||||||
|
|
||||||
# Must use StringIO, cStringIO does not support unicode
|
# Must use StringIO, cStringIO does not support unicode
|
||||||
line = StringIO.StringIO(line)
|
line = StringIO.StringIO(line)
|
||||||
@ -575,13 +599,10 @@ class PML_HTMLizer(object):
|
|||||||
empty = False
|
empty = False
|
||||||
text = '<hr width="%s" />' % self.code_value(line)
|
text = '<hr width="%s" />' % self.code_value(line)
|
||||||
elif c == 't':
|
elif c == 't':
|
||||||
indent_state[c] = not indent_state[c]
|
indent_state['t'] = not indent_state['t']
|
||||||
if indent_state[c]:
|
|
||||||
basic_indent = True
|
|
||||||
elif c == 'T':
|
elif c == 'T':
|
||||||
# Ensure we only store the value on the first T set for the line.
|
# Ensure we only store the value on the first T set for the line.
|
||||||
if not indent_state['T']:
|
if not indent_state['T']:
|
||||||
adv_indent = True
|
|
||||||
adv_indent_val = self.code_value(line)
|
adv_indent_val = self.code_value(line)
|
||||||
else:
|
else:
|
||||||
# We detected a T previously on this line.
|
# We detected a T previously on this line.
|
||||||
@ -610,10 +631,23 @@ class PML_HTMLizer(object):
|
|||||||
text = self.end_line()
|
text = self.end_line()
|
||||||
parsed.append(text)
|
parsed.append(text)
|
||||||
|
|
||||||
|
# Basic indent will be set if the \t starts the line or
|
||||||
|
# if we are in a continuing \t block.
|
||||||
if basic_indent:
|
if basic_indent:
|
||||||
|
# if the \t started the line and either it ended the line or the \t
|
||||||
|
# block is still open use a left margin.
|
||||||
|
if indent_state['st'] and (indent_state['et'] or indent_state['t']):
|
||||||
parsed.insert(0, self.STATES_TAGS['t'][0])
|
parsed.insert(0, self.STATES_TAGS['t'][0])
|
||||||
parsed.append(self.STATES_TAGS['t'][1])
|
parsed.append(self.STATES_TAGS['t'][1])
|
||||||
elif adv_indent:
|
# Use a text indent instead of a margin.
|
||||||
|
# This handles cases such as:
|
||||||
|
# \tO\tne upon a time...
|
||||||
|
else:
|
||||||
|
parsed.insert(0, self.STATES_TAGS['T'][0] % '5%')
|
||||||
|
parsed.append(self.STATES_TAGS['T'][1])
|
||||||
|
# \t will override \T's on the line.
|
||||||
|
# We only handle \T's that started the line.
|
||||||
|
elif indent_state['T'] and indent_state['sT']:
|
||||||
parsed.insert(0, self.STATES_TAGS['T'][0] % adv_indent_val)
|
parsed.insert(0, self.STATES_TAGS['T'][0] % adv_indent_val)
|
||||||
parsed.append(self.STATES_TAGS['T'][1])
|
parsed.append(self.STATES_TAGS['T'][1])
|
||||||
indent_state['T'] = False
|
indent_state['T'] = False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user