mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-22 10:00:33 +00:00
Approaching null diff for 2.16
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5088 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
78f1f6125c
commit
4125f05d3e
@ -17,7 +17,7 @@
|
||||
|
||||
import getopt, sys, string, re
|
||||
from error import error, warning
|
||||
from parser_tools import set_comment, set_format
|
||||
from parser_tools import set_comment, set_format, check_token
|
||||
|
||||
version = "0.0.2"
|
||||
|
||||
@ -113,6 +113,8 @@ def lyxformat(fmt):
|
||||
def read_file(file, header, body):
|
||||
"""Reads a file into the header and body parts"""
|
||||
fmt = None
|
||||
preamble = 0
|
||||
|
||||
while 1:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
@ -120,7 +122,12 @@ def read_file(file, header, body):
|
||||
sys.exit(3)
|
||||
|
||||
line = line[:-1]
|
||||
if not line:
|
||||
if check_token(line, '\\begin_preamble'):
|
||||
preamble = 1
|
||||
if check_token(line, '\\end_preamble'):
|
||||
preamble = 0
|
||||
|
||||
if not line and not preamble:
|
||||
break
|
||||
|
||||
header.append(line)
|
||||
|
@ -25,6 +25,11 @@ def update_tabular(lines):
|
||||
i = find_re(lines, lyxtable_re, i)
|
||||
if i == -1:
|
||||
break
|
||||
prop_dict = {"family" : "default", "series" : "default",
|
||||
"shape" : "default", "size" : "default",
|
||||
"emph" : "default", "bar" : "default",
|
||||
"noun" : "default", "latex" : "default", "color" : "default"}
|
||||
|
||||
# remove \LyXTable
|
||||
lines[i] = lines[i][:-9]
|
||||
i = i + 1
|
||||
@ -38,7 +43,7 @@ def update_tabular(lines):
|
||||
|
||||
lines[i] = '<LyXTabular version="1" rows="%s" columns="%s">' % (head[0],head[1])
|
||||
i = i +1
|
||||
lines.insert(i, '<Features rotate="%s" islongtable="%s" endhead="0" endfirsthead="0" endfoot="0" endlastfoot="0">' % (head[2], head[3]))
|
||||
lines.insert(i, '<Features rotate="%s" islongtable="%s" endhead="%s" endfirsthead="%s" endfoot="%s" endlastfoot="%s">' % (head[2],head[3],head[4],head[5],head[6],head[7]))
|
||||
|
||||
i = i +1
|
||||
|
||||
@ -52,24 +57,13 @@ def update_tabular(lines):
|
||||
del lines[i]
|
||||
|
||||
cell_info = []
|
||||
cell_location = []
|
||||
ncells = 0
|
||||
l = 0
|
||||
for j in range(rows):
|
||||
tmp = []
|
||||
for k in range(columns):
|
||||
cell_info.append(string.split(lines[i]))
|
||||
if lines[i][0] != "2":
|
||||
tmp.append(l)
|
||||
ncells = ncells + 1
|
||||
del lines[i]
|
||||
l = l + 1
|
||||
cell_location.append(tmp)
|
||||
|
||||
for j in range(rows):
|
||||
for k in cell_location[j]:
|
||||
sys.stderr.write("%d\t" % (k))
|
||||
sys.stderr.write("\n")
|
||||
|
||||
del lines[i]
|
||||
if not lines[i]:
|
||||
@ -88,21 +82,32 @@ def update_tabular(lines):
|
||||
else:
|
||||
tmp.append('<Column alignment="%s" valignment="0" leftline="%s" rightline="%s" width=%s special=%s>' % (column_info[k][0],column_info[k][1], column_info[k][2], column_info[k][3], column_info[k][4]))
|
||||
m = j*columns + k
|
||||
sys.stderr.write("%d\t%d\t%d\n" % (j,k,m))
|
||||
tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%s" rightline="%s" rotate="%s" usebox="0" width=%s special=%s>' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],cell_info[m][4],cell_info[m][5],cell_info[m][6],cell_info[m][7],cell_info[m][8]))
|
||||
|
||||
leftline = int(cell_info[m][4]) or int(column_info[k][1])
|
||||
if cell_info[m][0] == '1':
|
||||
n = m + 1
|
||||
while n < rows * columns - 1 and cell_info[n][0] == '2':
|
||||
n = n + 1
|
||||
rightline = int(cell_info[n][5]) or int(column_info[k][2])
|
||||
else:
|
||||
# not a multicolumn main cell
|
||||
# rightline = int(cell_info[m][5]) or int(column_info[k][2])
|
||||
rightline = int(column_info[k][2])
|
||||
|
||||
tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%d" rightline="%d" rotate="%s" usebox="0" width=%s special=%s>' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],leftline,rightline,cell_info[m][6],cell_info[m][7],cell_info[m][8]))
|
||||
tmp.append('\\begin_inset Text')
|
||||
tmp.append('')
|
||||
tmp.append('\\layout Standard')
|
||||
tmp.append('')
|
||||
|
||||
if m not in cell_location[j]:
|
||||
if cell_info[m][0] == '2':
|
||||
tmp.append('\\end_inset ')
|
||||
tmp.append('</Cell>')
|
||||
tmp.append('</Column>')
|
||||
continue
|
||||
|
||||
if l == ncells -1:
|
||||
end = find_tokens(lines, ['\\layout','\\the_end'], i)
|
||||
end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i)
|
||||
if end == -1:
|
||||
sys.stderr.write("Malformed lyx file\n")
|
||||
else:
|
||||
@ -112,14 +117,14 @@ def update_tabular(lines):
|
||||
paragraph.append(lines[i])
|
||||
del lines[i]
|
||||
end = end -1
|
||||
tmp.extend(set_paragraph_properties(paragraph))
|
||||
tmp.extend(set_paragraph_properties(paragraph, prop_dict))
|
||||
else:
|
||||
paragraph = []
|
||||
while lines[i] != '\\newline ':
|
||||
paragraph.append(lines[i])
|
||||
del lines[i]
|
||||
del lines[i]
|
||||
tmp.extend(set_paragraph_properties(paragraph))
|
||||
tmp.extend(set_paragraph_properties(paragraph, prop_dict))
|
||||
|
||||
tmp.append('\\end_inset ')
|
||||
tmp.append('</Cell>')
|
||||
@ -130,6 +135,8 @@ def update_tabular(lines):
|
||||
tmp.append('</LyXTabular>')
|
||||
tmp.append('')
|
||||
tmp.append('\\end_inset ')
|
||||
tmp.append('')
|
||||
tmp.append('')
|
||||
tail = lines[i:]
|
||||
lines[i:] = []
|
||||
lines.extend(tmp)
|
||||
@ -138,11 +145,11 @@ def update_tabular(lines):
|
||||
i = i + len(tmp)
|
||||
|
||||
prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
|
||||
def set_paragraph_properties(lines):
|
||||
def set_paragraph_properties(lines, prop_dict):
|
||||
# we need to preserve the order of options
|
||||
properties = ["family","series","shape","size",
|
||||
"emph","bar","noun","latex","color"]
|
||||
prop_value = {"family":"roman", "series" : "medium",
|
||||
prop_value = {"family" : "default", "series" : "medium",
|
||||
"shape" : "up", "size" : "normal",
|
||||
"emph" : "off", "bar" : "no",
|
||||
"noun" : "off", "latex" : "no_latex", "color" : "none"}
|
||||
@ -151,39 +158,86 @@ def set_paragraph_properties(lines):
|
||||
end = 0
|
||||
i = 0
|
||||
n = len(lines)
|
||||
|
||||
|
||||
#skip empty lines
|
||||
while i<n and lines[i] == "":
|
||||
i = i + 1
|
||||
start = i
|
||||
|
||||
#catch open char properties
|
||||
while i<n and lines[i][:1] == "\\":
|
||||
result = prop_exp.match(lines[i])
|
||||
sys.stderr.write(lines[i]+"\n")
|
||||
# sys.stderr.write(lines[i]+"\n")
|
||||
prop = result.group(1)
|
||||
if prop not in properties:
|
||||
sys.stderr.write("Unknown property: %s\n" % (prop))
|
||||
break
|
||||
else:
|
||||
prop_value[prop] = result.group(2)
|
||||
prop_dict[prop] = result.group(2)
|
||||
i = i + 1
|
||||
end = i
|
||||
|
||||
aux = []
|
||||
insert = 0
|
||||
for prop in properties:
|
||||
if prop == "color":
|
||||
aux.append("\\%s %s" % (prop, prop_value[prop]))
|
||||
elif prop == "family":
|
||||
if prop_value[prop] != "roman":
|
||||
aux.append("\\%s %s " % (prop, prop_value[prop]))
|
||||
else:
|
||||
aux.append("\\%s %s " % (prop, prop_value[prop]))
|
||||
|
||||
if prop_dict[prop] != 'default':
|
||||
insert = 1
|
||||
if prop == "color":
|
||||
aux.append("\\%s %s" % (prop, prop_dict[prop]))
|
||||
elif prop != "family" or prop_dict[prop] != "roman":
|
||||
aux.append("\\%s %s " % (prop, prop_dict[prop]))
|
||||
|
||||
return lines[:start] + aux[:] + lines[end:]
|
||||
# remove final char properties
|
||||
n = len(lines)
|
||||
while n:
|
||||
n = n - 1
|
||||
if not lines[n]:
|
||||
del lines[n]
|
||||
continue
|
||||
|
||||
if lines[n][:1] == '\\':
|
||||
result = prop_exp.match(lines[n])
|
||||
prop = result.group(1)
|
||||
if prop in properties:
|
||||
prop_dict[prop] = result.group(2)
|
||||
del lines[n]
|
||||
continue
|
||||
|
||||
if check_token(lines[n],'\\end_inset'):
|
||||
# ensure proper newlines after inset end
|
||||
lines.append('')
|
||||
lines.append('')
|
||||
break
|
||||
|
||||
#debug_list('*' * 10 + ' begin ' + '*' * 10, lines[start:])
|
||||
if not lines[start:] and not lines[end:]:
|
||||
return []
|
||||
|
||||
result = lines[:start] + aux[:] + lines[end:]
|
||||
if insert and result[0] != '':
|
||||
return [''] + result[:]
|
||||
|
||||
return result[:]
|
||||
|
||||
def debug_list(title, list):
|
||||
sys.stderr.write(title+'\n')
|
||||
for line in list:
|
||||
sys.stderr.write(line+'\n')
|
||||
|
||||
def update_language(header):
|
||||
i = find_token(header, "\\language", 0)
|
||||
if i == -1:
|
||||
# no language, should emit a warning
|
||||
header.append('\\language english')
|
||||
return
|
||||
# FIXME: find the document default language in user preferences
|
||||
header[i] = '\\language english'
|
||||
return
|
||||
|
||||
def convert(header,body):
|
||||
sys.stderr.write("%d\n" % len(body))
|
||||
update_tabular(body)
|
||||
sys.stderr.write("%d\n" % len(body))
|
||||
update_language(header)
|
||||
|
||||
if __name__ == "__main__":
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user