1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 The LyX team
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 """ Convert files to the file format generated by lyx 2.1"""
26 # Uncomment only what you need to import, please.
28 from parser_tools import find_token, find_end_of_inset, get_value, \
31 #from parser_tools import find_token, find_end_of, find_tokens, \
32 #find_token_exact, find_end_of_inset, find_end_of_layout, \
33 #find_token_backwards, is_in_inset, get_value, get_quoted_value, \
34 #del_token, check_token, get_option_value
36 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
38 #from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
39 # put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
40 # revert_font_attrs, hex2ratio, str2bool
42 ####################################################################
43 # Private helper functions
45 #def remove_option(lines, m, option):
46 #''' removes option from line m. returns whether we did anything '''
47 #l = lines[m].find(option)
50 #val = lines[m][l:].split('"')[1]
51 #lines[m] = lines[m][:l - 1] + lines[m][l+len(option + '="' + val + '"'):]
55 ###############################################################################
57 ### Conversion and reversion routines
59 ###############################################################################
61 def revert_visible_space(document):
62 "Revert InsetSpace visible into its ERT counterpart"
65 i = find_token(document.body, "\\begin_inset space \\textvisiblespace{}", i)
68 end = find_end_of_inset(document.body, i)
69 subst = put_cmd_in_ert("\\textvisiblespace{}")
70 document.body[i:end + 1] = subst
73 def convert_undertilde(document):
74 " Load undertilde automatically "
75 i = find_token(document.header, "\\use_mathdots" , 0)
77 document.header.insert(i + 1, "\\use_undertilde 1")
80 def revert_undertilde(document):
81 " Load undertilde if used in the document "
82 undertilde = find_token(document.header, "\\use_undertilde" , 0)
84 document.warning("No \\use_undertilde line. Assuming auto.")
86 val = get_value(document.header, "\\use_undertilde", undertilde)
87 del document.header[undertilde]
91 document.warning("Invalid \\use_undertilde value: " + val + ". Assuming auto.")
92 # probably usedots has not been changed, but be safe.
100 add_to_preamble(document, ["\\usepackage{undertilde}"])
103 # so we are in the auto case. we want to load undertilde if \utilde is used.
106 i = find_token(document.body, '\\begin_inset Formula', i)
109 j = find_end_of_inset(document.body, i)
111 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
114 code = "\n".join(document.body[i:j])
115 if code.find("\\utilde") != -1:
116 add_to_preamble(document, ["\\@ifundefined{utilde}{\\usepackage{undertilde}}"])
121 def revert_negative_space(document):
122 "Revert InsetSpace negmedspace and negthickspace into its TeX-code counterpart"
127 i = find_token(document.body, "\\begin_inset space \\negmedspace{}", i)
129 j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
131 # load amsmath in the preamble if not already loaded if we are at the end of checking
133 i = find_token(document.header, "\\use_amsmath 2", 0)
135 add_to_preamble(document, ["\\@ifundefined{negthickspace}{\\usepackage{amsmath}}"])
139 end = find_end_of_inset(document.body, i)
140 subst = put_cmd_in_ert("\\negmedspace{}")
141 document.body[i:end + 1] = subst
142 j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
145 end = find_end_of_inset(document.body, j)
146 subst = put_cmd_in_ert("\\negthickspace{}")
147 document.body[j:end + 1] = subst
151 def revert_math_spaces(document):
152 "Revert formulas with protected custom space and protected hfills to TeX-code"
155 i = find_token(document.body, "\\begin_inset Formula", i)
158 j = document.body[i].find("\\hspace*")
160 end = find_end_of_inset(document.body, i)
161 subst = put_cmd_in_ert(document.body[i][21:])
162 document.body[i:end + 1] = subst
166 def convert_japanese_encodings(document):
167 " Rename the japanese encodings to names understood by platex "
169 "EUC-JP-pLaTeX": "euc",
171 "SJIS-pLaTeX": "sjis"
173 i = find_token(document.header, "\\inputencoding" , 0)
176 val = get_value(document.header, "\\inputencoding", i)
177 if val in jap_enc_dict.keys():
178 document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
181 def revert_japanese_encodings(document):
182 " Revert the japanese encodings name changes "
184 "euc": "EUC-JP-pLaTeX",
186 "sjis": "SJIS-pLaTeX"
188 i = find_token(document.header, "\\inputencoding" , 0)
191 val = get_value(document.header, "\\inputencoding", i)
192 if val in jap_enc_dict.keys():
193 document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
196 def revert_justification(document):
197 " Revert the \\justification buffer param"
198 if not del_token(document.header, '\\justification', 0):
199 document.warning("Malformed LyX document: Missing \\justification.")
207 supported_versions = ["2.1.0","2.1"]
210 [415, [convert_undertilde]],
212 [417, [convert_japanese_encodings]],
217 [417, [revert_justification]],
218 [416, [revert_japanese_encodings]],
219 [415, [revert_negative_space,revert_math_spaces]],
220 [414, [revert_undertilde]],
221 [413, [revert_visible_space]]
225 if __name__ == "__main__":