]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_1.py
File format bump after r39982 (renaming of the japanese encodings for pLaTeX).
[lyx.git] / lib / lyx2lyx / lyx_2_1.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 The LyX team
5 #
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.
10 #
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.
15 #
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
19
20 """ Convert files to the file format generated by lyx 2.1"""
21
22 import re, string
23 import unicodedata
24 import sys, os
25
26 # Uncomment only what you need to import, please.
27
28 from parser_tools import find_token, find_end_of_inset, get_value
29
30 #from parser_tools import find_token, find_end_of, find_tokens, \
31   #find_token_exact, find_end_of_inset, find_end_of_layout, \
32   #find_token_backwards, is_in_inset, get_value, get_quoted_value, \
33   #del_token, check_token, get_option_value
34
35 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
36
37 #from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
38 #  put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
39 #  revert_font_attrs, hex2ratio, str2bool
40
41 ####################################################################
42 # Private helper functions
43
44 #def remove_option(lines, m, option):
45     #''' removes option from line m. returns whether we did anything '''
46     #l = lines[m].find(option)
47     #if l == -1:
48         #return False
49     #val = lines[m][l:].split('"')[1]
50     #lines[m] = lines[m][:l - 1] + lines[m][l+len(option + '="' + val + '"'):]
51     #return True
52
53
54 ###############################################################################
55 ###
56 ### Conversion and reversion routines
57 ###
58 ###############################################################################
59
60 def revert_visible_space(document):
61     "Revert InsetSpace visible into its ERT counterpart"
62     i = 0
63     while True:
64       i = find_token(document.body, "\\begin_inset space \\textvisiblespace{}", i)
65       if i == -1:
66         return
67       end = find_end_of_inset(document.body, i)
68       subst = put_cmd_in_ert("\\textvisiblespace{}")
69       document.body[i:end + 1] = subst
70
71
72 def convert_undertilde(document):
73     " Load undertilde automatically "
74     i = find_token(document.header, "\\use_mathdots" , 0)
75     if i != -1:
76       document.header.insert(i + 1, "\\use_undertilde 1")
77
78
79 def revert_undertilde(document):
80     " Load undertilde if used in the document "
81     undertilde = find_token(document.header, "\\use_undertilde" , 0)
82     if undertilde == -1:
83       document.warning("No \\use_undertilde line. Assuming auto.")
84     else:
85       val = get_value(document.header, "\\use_undertilde", undertilde)
86       del document.header[undertilde]
87       try:
88         usetilde = int(val)
89       except:
90         document.warning("Invalid \\use_undertilde value: " + val + ". Assuming auto.")
91         # probably usedots has not been changed, but be safe.
92         usetilde = 1
93
94       if usetilde == 0:
95         # do not load case
96         return
97       if usetilde == 2:
98         # force load case
99         add_to_preamble(document, ["\\usepackage{undertilde}"])
100         return
101     
102     # so we are in the auto case. we want to load undertilde if \utilde is used.
103     i = 0
104     while True:
105       i = find_token(document.body, '\\begin_inset Formula', i)
106       if i == -1:
107         return
108       j = find_end_of_inset(document.body, i)
109       if j == -1:
110         document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
111         i += 1
112         continue
113       code = "\n".join(document.body[i:j])
114       if code.find("\\utilde") != -1:
115         add_to_preamble(document, ["\\@ifundefined{utilde}{\\usepackage{undertilde}}"])
116         return
117       i = j
118
119
120 def revert_negative_space(document):
121     "Revert InsetSpace negmedspace and negthickspace into its TeX-code counterpart"
122     i = 0
123     j = 0
124     reverted = False
125     while True:
126       i = find_token(document.body, "\\begin_inset space \\negmedspace{}", i)
127       if i == -1:
128         j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
129         if j == -1:
130           # load amsmath in the preamble if not already loaded if we are at the end of checking
131           if reverted == True:
132             i = find_token(document.header, "\\use_amsmath 2", 0)
133             if i == -1:
134               add_to_preamble(document, ["\\@ifundefined{negthickspace}{\\usepackage{amsmath}}"])
135           return
136       if i == -1:
137         return
138       end = find_end_of_inset(document.body, i)
139       subst = put_cmd_in_ert("\\negmedspace{}")
140       document.body[i:end + 1] = subst
141       j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
142       if j == -1:
143         return
144       end = find_end_of_inset(document.body, j)
145       subst = put_cmd_in_ert("\\negthickspace{}")
146       document.body[j:end + 1] = subst
147       reverted = True
148
149
150 def revert_math_spaces(document):
151     "Revert formulas with protected custom space and protected hfills to TeX-code"
152     i = 0
153     while True:
154       i = find_token(document.body, "\\begin_inset Formula", i)
155       if i == -1:
156         return
157       j = document.body[i].find("\\hspace*")
158       if j != -1:
159         end = find_end_of_inset(document.body, i)
160         subst = put_cmd_in_ert(document.body[i][21:])
161         document.body[i:end + 1] = subst
162       i = i + 1
163
164
165 def convert_japanese_encodings(document):
166     " Rename the japanese encodings to names understood by platex "
167     jap_enc_dict = {
168         "EUC-JP-pLaTeX": "euc",
169         "JIS-pLaTeX":    "jis",
170         "SJIS-pLaTeX":   "sjis"
171     }
172     i = find_token(document.header, "\\inputencoding" , 0)
173     if i == -1:
174         return
175     val = get_value(document.header, "\\inputencoding", i)
176     if val in jap_enc_dict.keys():
177         document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
178
179
180 def revert_japanese_encodings(document):
181     " Revert the japanese encodings name changes "
182     jap_enc_dict = {
183         "euc":  "EUC-JP-pLaTeX",
184         "jis":  "JIS-pLaTeX",
185         "sjis": "SJIS-pLaTeX"
186     }
187     i = find_token(document.header, "\\inputencoding" , 0)
188     if i == -1:
189         return
190     val = get_value(document.header, "\\inputencoding", i)
191     if val in jap_enc_dict.keys():
192         document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
193
194
195 ##
196 # Conversion hub
197 #
198
199 supported_versions = ["2.1.0","2.1"]
200 convert = [
201            [414, []],
202            [415, [convert_undertilde]],
203            [416, []],
204            [417, [convert_japanese_encodings]],
205           ]
206
207 revert =  [
208            [416, [revert_japanese_encodings]],
209            [415, [revert_negative_space,revert_math_spaces]],
210            [414, [revert_undertilde]],
211            [413, [revert_visible_space]]
212           ]
213
214
215 if __name__ == "__main__":
216     pass