]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_4.py
377f6e7e18fe26b53aa9007d74b597d1aa2ef8a7
[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_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
34
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
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         document.warning("sfval: " + str(sfval))
101         # cutoff " 100"
102         sfval = sfval[:-4]
103         sfoption = ""
104         if sfval != "100":
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)
108         # cutoff " 100"
109         ttval = ttval[:-4]
110         ttoption = ""
111         if ttval != "100":
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}"])
115         else:
116             if i1!= -1: 
117                 add_to_preamble(document, ["\\usepackage{PTSerif}"])
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             if k!= -1: 
124                 if ttoption != "":
125                         add_to_preamble(document, ["\\usepackage[" + ttoption + "]{PTMono}"])
126                 else:
127                         add_to_preamble(document, ["\\usepackage{PTMono}"])
128
129
130 def revert_xcharter(document):
131     " Revert XCharter font definitions to LaTeX "
132
133     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
134         preamble = ""
135         i1 = find_token(document.header, "\\font_roman \"xcharter\"", 0)
136         if i1 != -1:
137             add_to_preamble(document, ["\\usepackage{XCharter}"])
138
139
140 ##
141 # Conversion hub
142 #
143
144 supported_versions = ["2.4.0", "2.4"]
145 convert = [
146            [545, [convert_lst_literalparam]],
147            [546, []],
148            [547, []]
149           ]
150
151 revert =  [
152            [546, [revert_xcharter]],
153            [545, [revert_paratype]],
154            [544, [revert_lst_literalparam]]
155           ]
156
157
158 if __name__ == "__main__":
159     pass