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_token, get_value)
28 # del_token, del_value, del_complete_lines,
29 # find_complete_lines, find_end_of, find_end_of_layout,
30 # find_re, find_substring, find_token_backwards,
31 # get_containing_inset, get_containing_layout, get_bool_value, 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 (add_to_preamble)
36 # put_cmd_in_ert, 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)
100 document.warning("sfval: " + str(sfval))
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}"])
120 add_to_preamble(document, ["\\usepackage[" + sfoption + "]{PTSans}"])
122 add_to_preamble(document, ["\\usepackage{PTSans}"])
125 add_to_preamble(document, ["\\usepackage[" + ttoption + "]{PTMono}"])
127 add_to_preamble(document, ["\\usepackage{PTMono}"])
133 supported_versions = ["2.4.0", "2.4"]
135 [545, [convert_lst_literalparam]],
140 [545, [revert_paratype]],
141 [544, [revert_lst_literalparam]]
145 if __name__ == "__main__":