1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # Copyright (C) 2018 The LyX team
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 """ Convert files to the file format generated by lyx 2.4"""
25 # Uncomment only what you need to import, please.
27 from parser_tools import (find_end_of_inset, find_end_of_layout, find_token,
28 get_bool_value, get_value, get_quoted_value)
29 # del_token, del_value, del_complete_lines,
30 # find_complete_lines, find_end_of,
31 # find_re, find_substring, find_token_backwards,
32 # get_containing_inset, get_containing_layout,
33 # is_in_inset, set_bool_value
34 # find_tokens, find_token_exact, check_token, get_option_value
36 from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
37 # revert_font_attrs, insert_to_preamble, latex_length
38 # get_ert, lyx2latex, lyx2verbatim, length_in_bp, convert_info_insets
39 # revert_flex_inset, hex2ratio, str2bool
41 ####################################################################
42 # Private helper functions
46 ###############################################################################
48 ### Conversion and reversion routines
50 ###############################################################################
53 def convert_lst_literalparam(document):
54 " Add param literal to include inset "
58 i = find_token(document.body, '\\begin_inset CommandInset include', i)
61 j = find_end_of_inset(document.body, i)
63 document.warning("Malformed LyX document: Can't find end of command inset at line %d" % i)
66 while i < j and document.body[i].strip() != '':
68 document.body.insert(i, "literal \"true\"")
71 def revert_lst_literalparam(document):
72 " Remove param literal from include inset "
76 i = find_token(document.body, '\\begin_inset CommandInset include', i)
79 j = find_end_of_inset(document.body, i)
81 document.warning("Malformed LyX document: Can't find end of include inset at line %d" % i)
84 k = find_token(document.body, 'literal', i, j)
91 def revert_paratype(document):
92 " Revert ParaType font definitions to LaTeX "
94 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
96 i1 = find_token(document.header, "\\font_roman \"PTSerif-TLF\"", 0)
97 i2 = find_token(document.header, "\\font_sans \"default\"", 0)
98 i3 = find_token(document.header, "\\font_typewriter \"default\"", 0)
99 j = find_token(document.header, "\\font_sans \"PTSans-TLF\"", 0)
100 sfval = get_value(document.header, "\\font_sf_scale", 0)
105 sfoption = "scaled=" + format(float(sfval) / 100, '.2f')
106 k = find_token(document.header, "\\font_typewriter \"PTMono-TLF\"", 0)
107 ttval = get_value(document.header, "\\font_tt_scale", 0)
112 ttoption = "scaled=" + format(float(ttval) / 100, '.2f')
113 if i1 != -1 and i2 != -1 and i3!= -1:
114 add_to_preamble(document, ["\\usepackage{paratype}"])
117 add_to_preamble(document, ["\\usepackage{PTSerif}"])
118 document.header[i1] = document.header[i1].replace("PTSerif-TLF", "default")
121 add_to_preamble(document, ["\\usepackage[" + sfoption + "]{PTSans}"])
123 add_to_preamble(document, ["\\usepackage{PTSans}"])
124 document.header[j] = document.header[j].replace("PTSans-TLF", "default")
127 add_to_preamble(document, ["\\usepackage[" + ttoption + "]{PTMono}"])
129 add_to_preamble(document, ["\\usepackage{PTMono}"])
130 document.header[k] = document.header[k].replace("PTMono-TLF", "default")
133 def revert_xcharter(document):
134 " Revert XCharter font definitions to LaTeX "
136 i = find_token(document.header, "\\font_roman \"xcharter\"", 0)
140 # replace unsupported font setting
141 document.header[i] = document.header[i].replace("xcharter", "default")
142 # no need for preamble code with system fonts
143 if get_bool_value(document.header, "\\use_non_tex_fonts"):
146 # transfer old style figures setting to package options
147 j = find_token(document.header, "\\font_osf true")
150 document.header[j] = "\\font_osf false"
154 add_to_preamble(document, ["\\usepackage%s{XCharter}"%options])
157 def revert_lscape(document):
158 " Reverts the landscape environment (Landscape module) to TeX-code "
160 if not "landscape" in document.get_module_list():
165 i = find_token(document.body, "\\begin_inset Flex Landscape", i)
168 j = find_end_of_inset(document.body, i)
170 document.warning("Malformed LyX document: Can't find end of Landscape inset")
174 if document.body[i] == "\\begin_inset Flex Landscape (Floating)":
175 document.body[j - 2 : j + 1] = put_cmd_in_ert("\\end{landscape}}")
176 document.body[i : i + 4] = put_cmd_in_ert("\\afterpage{\\begin{landscape}")
177 add_to_preamble(document, ["\\usepackage{afterpage}"])
179 document.body[j - 2 : j + 1] = put_cmd_in_ert("\\end{landscape}")
180 document.body[i : i + 4] = put_cmd_in_ert("\\begin{landscape}")
182 add_to_preamble(document, ["\\usepackage{pdflscape}"])
186 def convert_fontenc(document):
187 " Convert default fontenc setting "
189 i = find_token(document.header, "\\fontencoding global", 0)
193 document.header[i] = document.header[i].replace("global", "auto")
196 def revert_fontenc(document):
197 " Revert default fontenc setting "
199 i = find_token(document.header, "\\fontencoding auto", 0)
203 document.header[i] = document.header[i].replace("auto", "global")
206 def revert_nospellcheck(document):
207 " Remove nospellcheck font info param "
211 i = find_token(document.body, '\\nospellcheck', i)
217 def revert_floatpclass(document):
218 " Remove float placement params 'document' and 'class' "
221 i = find_token(document.header, "\\float_placement class", 0)
223 del document.header[i]
227 i = find_token(document.body, '\\begin_inset Float', i)
230 j = find_end_of_inset(document.body, i)
231 k = find_token(document.body, 'placement class', i, i + 2)
233 k = find_token(document.body, 'placement document', i, i + 2)
241 def revert_floatalignment(document):
242 " Remove float alignment params "
245 i = find_token(document.header, "\\float_alignment", 0)
248 galignment = get_value(document.header, "\\float_alignment", i)
249 del document.header[i]
253 i = find_token(document.body, '\\begin_inset Float', i)
256 j = find_end_of_inset(document.body, i)
258 document.warning("Malformed LyX document: Can't find end of inset at line " + str(i))
260 k = find_token(document.body, 'alignment', i, i + 4)
264 alignment = get_value(document.body, "alignment", k)
265 if alignment == "document":
266 alignment = galignment
268 l = find_token(document.body, "\\begin_layout Plain Layout", i, j)
270 document.warning("Can't find float layout!")
274 if alignment == "left":
275 alcmd = put_cmd_in_ert("\\raggedright{}")
276 elif alignment == "center":
277 alcmd = put_cmd_in_ert("\\centering{}")
278 elif alignment == "right":
279 alcmd = put_cmd_in_ert("\\raggedleft{}")
281 document.body[l+1:l+1] = alcmd
285 def revert_tuftecite(document):
286 " Revert \cite commands in tufte classes "
288 tufte = ["tufte-book", "tufte-handout"]
289 if document.textclass not in tufte:
294 i = find_token(document.body, "\\begin_inset CommandInset citation", i)
297 j = find_end_of_inset(document.body, i)
299 document.warning("Can't find end of citation inset at line %d!!" %(i))
302 k = find_token(document.body, "LatexCommand", i, j)
304 document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
307 cmd = get_value(document.body, "LatexCommand", k)
311 pre = get_quoted_value(document.body, "before", i, j)
312 post = get_quoted_value(document.body, "after", i, j)
313 key = get_quoted_value(document.body, "key", i, j)
315 document.warning("Citation inset at line %d does not have a key!" %(i))
317 # Replace command with ERT
320 res += "[" + pre + "]"
322 res += "[" + post + "]"
325 res += "{" + key + "}"
326 document.body[i:j+1] = put_cmd_in_ert([res])
330 def revert_stretchcolumn(document):
331 " We remove the column varwidth flags or everything else will become a mess. "
334 i = find_token(document.body, "\\begin_inset Tabular", i)
337 j = find_end_of_inset(document.body, i + 1)
339 document.warning("Malformed LyX document: Could not find end of tabular.")
341 for k in range(i, j):
342 if re.search('^<column.*varwidth="[^"]+".*>$', document.body[k]):
343 document.warning("Converting 'tabularx'/'xltabular' table to normal table.")
344 document.body[k] = document.body[k].replace(' varwidth="true"', '')
352 supported_versions = ["2.4.0", "2.4"]
354 [545, [convert_lst_literalparam]],
359 [550, [convert_fontenc]],
367 [553, [revert_stretchcolumn]],
368 [552, [revert_tuftecite]],
369 [551, [revert_floatpclass, revert_floatalignment]],
370 [550, [revert_nospellcheck]],
371 [549, [revert_fontenc]],
372 [548, []],# dummy format change
373 [547, [revert_lscape]],
374 [546, [revert_xcharter]],
375 [545, [revert_paratype]],
376 [544, [revert_lst_literalparam]]
380 if __name__ == "__main__":