1 # This document is part of lyx2lyx
2 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 """Convert files to the file format generated by lyx 1.1.5"""
22 from parser_tools import find_re, find_token, find_token_backwards
24 ####################################################################
25 # Private helper functions
28 def get_layout(line, default_layout):
29 "Get the line layout, beware of the empty layout."
36 ####################################################################
38 math_env = ["\\[", "\\begin{eqnarray*}", "\\begin{eqnarray}", "\\begin{equation}"]
41 def replace_protected_separator(document):
42 "Replace protected separator."
46 i = find_token(lines, "\\protected_separator", i)
49 j = find_token_backwards(lines, "\\layout", i)
50 # if j == -1: print error
51 layout = get_layout(lines[j], document.default_layout)
53 if layout == "LyX-Code":
55 while lines[i] == "\\protected_separator ":
59 lines[i - 1] = lines[i - 1] + result + lines[i]
61 lines[i - 1] = lines[i - 1] + "\\SpecialChar ~"
66 def merge_formula_inset(document):
67 "Merge formula insets."
71 i = find_token(lines, "\\begin_inset Formula", i)
74 if lines[i + 1] in math_env:
75 lines[i] = lines[i] + lines[i + 1]
80 def update_tabular(document):
81 "Update from tabular format 4 to 5 if necessary."
83 lyxtable_re = re.compile(r".*\\LyXTable$")
86 i = find_re(lines, lyxtable_re, i)
94 lines[i] = "multicol5"
96 rows = int(lines[i].split()[0])
97 columns = int(lines[i].split()[1])
100 for j in range(columns):
101 col_info = lines[i].split()
102 if len(col_info) == 3:
103 lines[i] = lines[i] + '"" ""'
105 lines[i] = " ".join(col_info[:3]) + ' "%s" ""' % col_info[3]
109 lines[i] = lines[i] + ' "" ""'
113 def update_toc(document):
114 "Update table of contents."
115 lines = document.body
118 i = find_token(lines, "\\begin_inset LatexCommand \\tableofcontents", i)
121 lines[i] = lines[i] + "{}"
125 def remove_cursor(document):
127 lines = document.body
128 i = find_token(lines, "\\cursor", 0)
133 def remove_vcid(document):
134 "Remove \\lyxvcid and \\lyxrcsid."
135 lines = document.header
136 i = find_token(lines, "\\lyxvcid", 0)
139 i = find_token(lines, "\\lyxrcsid", 0)
144 def first_layout(document):
145 "Fix first layout, if empty use the default layout."
146 lines = document.body
147 while lines[0] == "":
149 if lines[0][:7] != "\\layout":
150 lines[:0] = ["\\layout %s" % document.default_layout, ""]
153 def remove_space_in_units(document):
154 "Remove space in units."
155 lines = document.header
156 margins = ["\\topmargin", "\\rightmargin", "\\leftmargin", "\\bottommargin"]
158 unit_rexp = re.compile(r"[^ ]* (.*) (.*)")
160 for margin in margins:
163 i = find_token(lines, margin, i)
167 result = unit_rexp.search(lines[i])
169 lines[i] = margin + " " + result.group(1) + result.group(2)
173 def latexdel_getargs(document, i):
174 "Get arguments from latexdel insets."
175 lines = document.body
177 # play safe, clean empty lines
183 j = find_token(lines, "\\end_inset", i)
188 document.warning("Unexpected end of inset.")
189 j = find_token(lines, "\\begin_inset LatexDel }{", i)
191 ref = " ".join(lines[i:j])
194 # play safe, clean empty lines
200 j = find_token(lines, "\\end_inset", i - 1)
204 document.warning("Unexpected end of inset.")
205 j = find_token(lines, "\\begin_inset LatexDel }", i)
206 label = " ".join(lines[i:j])
212 def update_ref(document):
213 "Update reference inset."
214 lines = document.body
217 i = find_token(lines, "\\begin_inset LatexCommand", i)
221 if lines[i].split()[-1] == "\\ref{":
223 ref, label = latexdel_getargs(document, i)
224 lines[i - 1] = f"{lines[i - 1][:-1]}[{ref}]{{{label}}}"
229 def update_latexdel(document):
230 "Remove latexdel insets."
231 lines = document.body
233 latexdel_re = re.compile(r".*\\begin_inset LatexDel")
235 i = find_re(lines, latexdel_re, i)
238 lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
240 j = lines[i].find("\\begin_inset")
241 lines.insert(i + 1, lines[i][j:])
242 lines[i] = lines[i][:j].strip()
245 if lines[i].split()[-1] in ("\\url{", "\\htmlurl{"):
248 ref, label = latexdel_getargs(document, i)
249 lines[i - 1] = f"{lines[i-1][:-1]}[{label}]{{{ref}}}"
254 supported_versions = ["1.1.5", "1.1.5fix1", "1.1.5fix2", "1.1"]
263 replace_protected_separator,
266 remove_space_in_units,
275 if __name__ == "__main__":