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, get_bool_value, get_value)
28 # del_token, del_value, del_complete_lines,
29 # find_complete_lines, find_end_of,
30 # find_re, find_substring, find_token_backwards,
31 # get_containing_inset, get_containing_layout, get_value,
32 # get_quoted_value, is_in_inset, set_bool_value
33 # find_tokens, find_token_exact, check_token, get_option_value
35 from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
36 # revert_font_attrs, insert_to_preamble, latex_length
37 # get_ert, lyx2latex, lyx2verbatim, length_in_bp, convert_info_insets
38 # revert_flex_inset, hex2ratio, str2bool
40 ####################################################################
41 # Private helper functions
45 ###############################################################################
47 ### Conversion and reversion routines
49 ###############################################################################
52 def convert_lst_literalparam(document):
53 " Add param literal to include inset "
57 i = find_token(document.body, '\\begin_inset CommandInset include', i)
60 j = find_end_of_inset(document.body, i)
62 document.warning("Malformed LyX document: Can't find end of command inset at line %d" % i)
65 while i < j and document.body[i].strip() != '':
67 document.body.insert(i, "literal \"true\"")
70 def revert_lst_literalparam(document):
71 " Remove param literal from include inset "
75 i = find_token(document.body, '\\begin_inset CommandInset include', i)
78 j = find_end_of_inset(document.body, i)
80 document.warning("Malformed LyX document: Can't find end of include inset at line %d" % i)
83 k = find_token(document.body, 'literal', i, j)
90 def revert_paratype(document):
91 " Revert ParaType font definitions to LaTeX "
93 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
95 i1 = find_token(document.header, "\\font_roman \"PTSerif-TLF\"", 0)
96 i2 = find_token(document.header, "\\font_sans \"default\"", 0)
97 i3 = find_token(document.header, "\\font_typewriter \"default\"", 0)
98 j = find_token(document.header, "\\font_sans \"PTSans-TLF\"", 0)
99 sfval = get_value(document.header, "\\font_sf_scale", 0)
104 sfoption = "scaled=" + format(float(sfval) / 100, '.2f')
105 k = find_token(document.header, "\\font_typewriter \"PTMono-TLF\"", 0)
106 ttval = get_value(document.header, "\\font_tt_scale", 0)
111 ttoption = "scaled=" + format(float(ttval) / 100, '.2f')
112 if i1 != -1 and i2 != -1 and i3!= -1:
113 add_to_preamble(document, ["\\usepackage{paratype}"])
116 add_to_preamble(document, ["\\usepackage{PTSerif}"])
117 document.header[i1] = document.header[i1].replace("PTSerif-TLF", "default")
120 add_to_preamble(document, ["\\usepackage[" + sfoption + "]{PTSans}"])
122 add_to_preamble(document, ["\\usepackage{PTSans}"])
123 document.header[j] = document.header[j].replace("PTSans-TLF", "default")
126 add_to_preamble(document, ["\\usepackage[" + ttoption + "]{PTMono}"])
128 add_to_preamble(document, ["\\usepackage{PTMono}"])
129 document.header[k] = document.header[k].replace("PTMono-TLF", "default")
132 def revert_xcharter(document):
133 " Revert XCharter font definitions to LaTeX "
135 i = find_token(document.header, "\\font_roman \"xcharter\"", 0)
139 # replace unsupported font setting
140 document.header[i] = document.header[i].replace("xcharter", "default")
141 # no need for preamble code with system fonts
142 if get_bool_value(document.header, "\\use_non_tex_fonts"):
145 # transfer old style figures setting to package options
146 j = find_token(document.header, "\\font_osf true")
149 document.header[j] = "\\font_osf false"
153 add_to_preamble(document, ["\\usepackage%s{XCharter}"%options])
156 def revert_lscape(document):
157 " Reverts the landscape environment (Landscape module) to TeX-code "
159 if not "landscape" in document.get_module_list():
164 i = find_token(document.body, "\\begin_inset Flex Landscape", i)
167 j = find_end_of_inset(document.body, i)
169 document.warning("Malformed LyX document: Can't find end of Landscape inset")
173 if document.body[i] == "\\begin_inset Flex Landscape (Floating)":
174 document.body[j - 2 : j + 1] = put_cmd_in_ert("\\end{landscape}}")
175 document.body[i : i + 4] = put_cmd_in_ert("\\afterpage{\\begin{landscape}")
176 add_to_preamble(document, ["\\usepackage{afterpage}"])
178 document.body[j - 2 : j + 1] = put_cmd_in_ert("\\end{landscape}")
179 document.body[i : i + 4] = put_cmd_in_ert("\\begin{landscape}")
181 add_to_preamble(document, ["\\usepackage{pdflscape}"])
189 supported_versions = ["2.4.0", "2.4"]
191 [545, [convert_lst_literalparam]],
199 [548, []],# dummy format change
200 [547, [revert_lscape]],
201 [546, [revert_xcharter]],
202 [545, [revert_paratype]],
203 [544, [revert_lst_literalparam]]
207 if __name__ == "__main__":