]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_1_6_3.py
Support for multiple bibliographies setting "per child"
[lyx.git] / lib / lyx2lyx / lyx_1_1_6_3.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
19 """ Convert files to the file format generated by lyx 1.1.6, fix3 and fix4"""
20
21 import re
22 from parser_tools import find_token, find_re
23
24 def bool_table(item):
25     " Convert 0, 1 to false, true."
26     if item == "0":
27         return "false"
28     # should emit a warning if item != "1"
29     return "true"
30
31
32 align_vertical = {"0": "top", "1": "bottom", "2": "center"}
33 align_table = {"0": "top", "2": "left", "4": "right", "8": "center"}
34 use_table = {"0": "none", "1": "parbox"}
35 table_meta_re = re.compile(r'<LyXTabular version="?1"? rows="?(\d*)"? columns="?(\d*)"?>')
36
37 def update_tabular(document):
38     " Update tabular format to version 2 (xml like syntax)."
39     regexp = re.compile(r'^\\begin_inset\s+Tabular')
40     lines = document.body
41     i=0
42     while True:
43         i = find_re(lines, regexp, i)
44         if i == -1:
45             break
46
47         i = i +1
48
49         # scan table header meta-info
50         res = table_meta_re.match( lines[i] )
51         if res:
52             val = res.groups()
53             lines[i] = '<lyxtabular version="2" rows="%s" columns="%s">' % val
54
55         j = find_token(lines, '</LyXTabular>', i) + 1
56         if j == 0:
57             document.warning( "Error: Bad lyx format i=%d j=%d" % (i,j))
58             break
59
60         new_table = table_update(lines[i:j])
61         lines[i:j] = new_table
62         i = i + len(new_table)
63
64
65 col_re = re.compile(r'<column alignment="?(\d)"? valignment="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? width="(.*)" special="(.*)">')
66 cell_re = re.compile(r'<cell multicolumn="?(\d)"? alignment="?(\d)"? valignment="?(\d)"? topline="?(\d)"? bottomline="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? rotate="?(\d)"? usebox="?(\d)"? width="(.*)" special="(.*)">')
67 features_re = re.compile(r'<features rotate="?(\d)"? islongtable="?(\d)"? endhead="?(-?\d)"? endfirsthead="?(-?\d)"? endfoot="?(-?\d)"? endlastfoot="?(-?\d)"?>')
68 row_re = re.compile(r'<row topline="?(\d)"? bottomline="?(\d)"? newpage="?(\d)"?>')
69
70 def table_update(lines):
71     " Update table's internal content to format 2."
72     lines[1] = lines[1].replace('<Features', '<features')
73     res = features_re.match( lines[1] )
74     if res:
75         val = res.groups()
76         lines[1] = '<features rotate="%s" islongtable="%s" endhead="%s" endfirsthead="%s" endfoot="%s" endlastfoot="%s">' % (bool_table(val[0]), bool_table(val[1]), val[2], val[3], val[4], val[5])
77
78     if lines[2]=="":
79         del lines[2]
80     i = 2
81     col_info = []
82     while i < len(lines):
83         lines[i] = lines[i].replace('<Cell', '<cell')
84         lines[i] = lines[i].replace('</Cell', '</cell')
85         lines[i] = lines[i].replace('<Row', '<row')
86         lines[i] = lines[i].replace('</Row', '</row')
87         lines[i] = lines[i].replace('<Column', '<column')
88         lines[i] = lines[i].replace('</Column', '</column')
89         lines[i] = lines[i].replace('</LyXTabular', '</lyxtabular')
90         k = lines[i].find ('<column ')
91         if k != -1:
92             col_info.append(lines[i])
93             del lines[i]
94             continue
95
96         if lines[i] == '</column>' or lines[i] == '<column>':
97             del lines[i]
98             continue
99
100         res = cell_re.match(lines[i])
101         if res:
102             val = res.groups()
103             lines[i] = '<cell multicolumn="%s" alignment="%s" valignment="%s" topline="%s" bottomline="%s" leftline="%s" rightline="%s" rotate="%s" usebox="%s" width="%s" special="%s">' % ( val[0], align_table[val[1]], align_vertical[val[2]], bool_table(val[3]), bool_table(val[4]), bool_table(val[5]), bool_table(val[6]), bool_table(val[7]), use_table[val[8]], val[9], val[10])
104
105         res = row_re.match(lines[i])
106         if res:
107             val = res.groups()
108             lines[i] = '<row topline="%s" bottomline="%s" newpage="%s">' % (bool_table(val[0]), bool_table(val[1]), bool_table(val[2]))
109
110         i = i + 1
111
112     j = len(col_info)
113     for i in range(j):
114         res = col_re.match(col_info[i])
115         if res:
116             val = res.groups()
117             col_info[i] = '<column alignment="%s" valignment="%s" leftline="%s" rightline="%s" width="%s" special="%s">' \
118                           % ( align_table[val[0]], align_vertical[val[1]], bool_table(val[2]), bool_table(val[3]), val[4],val[5])
119
120     return lines[:2] + col_info + lines[2:]
121
122
123 supported_versions = ["1.1.6fix3","1.1.6fix4","1.1"]
124 convert = [[218, [update_tabular]]]
125 revert  = []
126
127
128 if __name__ == "__main__":
129     pass