]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_4.py
Add \nospellcheck font property
[lyx.git] / lib / lyx2lyx / lyx_2_4.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # Copyright (C) 2018 The LyX team
4 #
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.
9 #
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.
14 #
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.
18
19 """ Convert files to the file format generated by lyx 2.4"""
20
21 import re, string
22 import unicodedata
23 import sys, os
24
25 # Uncomment only what you need to import, please.
26
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
34
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
39
40 ####################################################################
41 # Private helper functions
42
43
44
45 ###############################################################################
46 ###
47 ### Conversion and reversion routines
48 ###
49 ###############################################################################
50
51   
52 def convert_lst_literalparam(document):
53     " Add param literal to include inset "
54
55     i = 0
56     while True:
57         i = find_token(document.body, '\\begin_inset CommandInset include', i)
58         if i == -1:
59             break
60         j = find_end_of_inset(document.body, i)
61         if j == -1:
62             document.warning("Malformed LyX document: Can't find end of command inset at line %d" % i)
63             i += 1
64             continue
65         while i < j and document.body[i].strip() != '':
66             i += 1
67         document.body.insert(i, "literal \"true\"")
68
69
70 def revert_lst_literalparam(document):
71     " Remove param literal from include inset "
72
73     i = 0
74     while True:
75         i = find_token(document.body, '\\begin_inset CommandInset include', i)
76         if i == -1:
77             break
78         j = find_end_of_inset(document.body, i)
79         if j == -1:
80             document.warning("Malformed LyX document: Can't find end of include inset at line %d" % i)
81             i += 1
82             continue
83         k = find_token(document.body, 'literal', i, j)
84         if k == -1:
85             i += 1
86             continue
87         del document.body[k]
88
89
90 def revert_paratype(document):
91     " Revert ParaType font definitions to LaTeX "
92
93     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
94         preamble = ""
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         # cutoff " 100"
101         sfval = sfval[:-4]
102         sfoption = ""
103         if sfval != "100":
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)
107         # cutoff " 100"
108         ttval = ttval[:-4]
109         ttoption = ""
110         if ttval != "100":
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}"])
114         else:
115             if i1!= -1: 
116                 add_to_preamble(document, ["\\usepackage{PTSerif}"])
117                 document.header[i1] = document.header[i1].replace("PTSerif-TLF", "default")
118             if j!= -1: 
119                 if sfoption != "":
120                         add_to_preamble(document, ["\\usepackage[" + sfoption + "]{PTSans}"])
121                 else:
122                         add_to_preamble(document, ["\\usepackage{PTSans}"])
123                 document.header[j] = document.header[j].replace("PTSans-TLF", "default")
124             if k!= -1: 
125                 if ttoption != "":
126                         add_to_preamble(document, ["\\usepackage[" + ttoption + "]{PTMono}"])
127                 else:
128                         add_to_preamble(document, ["\\usepackage{PTMono}"])    
129                 document.header[k] = document.header[k].replace("PTMono-TLF", "default")
130
131
132 def revert_xcharter(document):
133     " Revert XCharter font definitions to LaTeX "
134
135     i = find_token(document.header, "\\font_roman \"xcharter\"", 0)
136     if i == -1:
137         return
138
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"):
143         return
144
145     # transfer old style figures setting to package options
146     j = find_token(document.header, "\\font_osf true")
147     if j != -1:
148         options = "[osf]"
149         document.header[j] = "\\font_osf false"
150     else:
151         options = ""
152     if i != -1:
153         add_to_preamble(document, ["\\usepackage%s{XCharter}"%options])
154
155
156 def revert_lscape(document):
157     " Reverts the landscape environment (Landscape module) to TeX-code "
158
159     if not "landscape" in document.get_module_list():
160         return
161
162     i = 0
163     while True:
164         i = find_token(document.body, "\\begin_inset Flex Landscape", i)
165         if i == -1:
166             return
167         j = find_end_of_inset(document.body, i)
168         if j == -1:
169             document.warning("Malformed LyX document: Can't find end of Landscape inset")
170             i += 1
171             continue
172
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}"])
177         else:
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}")
180
181         add_to_preamble(document, ["\\usepackage{pdflscape}"])
182         # no need to reset i
183
184
185 def convert_fontenc(document):
186     " Convert default fontenc setting "
187
188     i = find_token(document.header, "\\fontencoding global", 0)
189     if i == -1:
190         return
191
192     document.header[i] = document.header[i].replace("global", "auto")
193
194
195 def revert_fontenc(document):
196     " Revert default fontenc setting "
197
198     i = find_token(document.header, "\\fontencoding auto", 0)
199     if i == -1:
200         return
201
202     document.header[i] = document.header[i].replace("auto", "global")
203
204
205 def revert_nospellcheck(document):
206     " Remove nospellcheck font info param "
207
208     i = 0
209     while True:
210         i = find_token(document.body, '\\nospellcheck', i)
211         if i == -1:
212             return
213         del document.body[i]
214
215
216 ##
217 # Conversion hub
218 #
219
220 supported_versions = ["2.4.0", "2.4"]
221 convert = [
222            [545, [convert_lst_literalparam]],
223            [546, []],
224            [547, []],
225            [548, []],
226            [549, []],
227            [550, [convert_fontenc]],
228            [551, []]
229           ]
230
231 revert =  [
232            [549, [revert_nospellcheck]],
233            [549, [revert_fontenc]],
234            [548, []],# dummy format change
235            [547, [revert_lscape]],
236            [546, [revert_xcharter]],
237            [545, [revert_paratype]],
238            [544, [revert_lst_literalparam]]
239           ]
240
241
242 if __name__ == "__main__":
243     pass