]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyx_2_3.py
Introduce "inverted" branch insets: These are branches whose content
[features.git] / lib / lyx2lyx / lyx_2_3.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2016 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 """ Convert files to the file format generated by lyx 2.3"""
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, find_tokens, \
29 #  find_token_exact, find_end_of_inset, find_end_of_layout, \
30 #  find_token_backwards, is_in_inset, get_value, get_quoted_value, \
31 #  del_token, check_token, get_option_value
32
33 from parser_tools import find_token, find_end_of_inset, get_value
34
35 #from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert, lyx2latex, \
36 #  lyx2verbatim, length_in_bp, convert_info_insets
37 #  insert_to_preamble, latex_length, revert_flex_inset, \
38 #  revert_font_attrs, hex2ratio, str2bool
39
40 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
41
42 ####################################################################
43 # Private helper functions
44
45
46
47 ###############################################################################
48 ###
49 ### Conversion and reversion routines
50 ###
51 ###############################################################################
52
53 def convert_microtype(document):
54     " Add microtype settings. "
55     i = find_token(document.header, "\\font_tt_scale" , 0)
56     if i == -1:
57         document.warning("Malformed LyX document: Can't find \\font_tt_scale.")
58         return;
59     j = find_token(document.preamble, "\\usepackage{microtype}", 0)
60     if j == -1:
61         document.header.insert(i + 1, "\\use_microtype 0")
62     else:
63         document.header.insert(i + 1, "\\use_microtype 1")
64         del document.preamble[j]
65
66
67 def revert_microtype(document):
68     " Remove microtype settings. "
69     i = find_token(document.header, "\\use_microtype", 0)
70     if i == -1:
71         return
72     value = get_value(document.header, "\\use_microtype" , i).split()[0]
73     del document.header[i]
74     if value == "1":
75         add_to_preamble(document, ["\\usepackage{microtype}"])
76
77
78 def convert_dateinset(document):
79     ' Convert date external inset to ERT '
80     i = 0
81     while True:
82         i = find_token(document.body, "\\begin_inset External", i)
83         if i == -1:
84             return
85         j = find_end_of_inset(document.body, i)
86         if j == -1:
87             document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
88             i += 1
89             continue
90         if get_value(document.body, 'template', i, j) == "Date":
91             document.body[i : j + 1] = put_cmd_in_ert("\\today ")
92         i += 1
93         continue
94
95
96 def convert_ibranches(document):
97     ' Add "inverted 0" to branch insets'
98     i = 0
99     while True:
100         i = find_token(document.body, "\\begin_inset Branch", i)
101         if i == -1:
102             return
103         document.body.insert(i + 1, "inverted 0")
104         i += 1
105
106
107 def revert_ibranches(document):
108     ' Convert inverted branches to explicit anti-branches'
109     # Get list of branches
110     ourbranches = {}
111     i = 0
112     while True:
113         i = find_token(document.header, "\\branch", i)
114         if i == -1:
115             break
116         branch = document.header[i][8:].strip()
117         if document.header[i+1].startswith("\\selected "):
118             #document.warning(document.header[i+1])
119             #document.warning(document.header[i+1][10])
120             selected = int(document.header[i+1][10])
121         else:
122             document.warning("Malformed LyX document: No selection indicator for branch " + branch)
123             selected = 1
124             
125         # the value tells us whether the branch is selected
126         ourbranches[document.header[i][8:].strip()] = selected
127         i += 1
128
129     # Figure out what inverted branches, if any, have been used
130     # and convert them to "Anti-OldBranch"
131     ibranches = {}
132     i = 0
133     while True:
134         i = find_token(document.body, "\\begin_inset Branch", i)
135         if i == -1:
136             break
137         if not document.body[i+1].startswith("inverted "):
138             document.warning("Malformed LyX document: Missing 'inverted' tag!")
139             i += 1
140             continue
141         inverted = document.body[i+1][9]
142         #document.warning(document.body[i+1])
143
144         if inverted == "1":
145             branch = document.body[i][20:].strip()
146             #document.warning(branch)
147             if not branch in ibranches:
148                 antibranch = "Anti-" + branch
149                 while antibranch in ibranches:
150                     antibranch = "x" + antibranch
151                 ibranches[branch] = antibranch
152             else:
153                 antibranch = ibranches[branch]
154             #document.warning(antibranch)
155             document.body[i] = "\\begin_inset Branch " + antibranch
156
157         # remove "inverted" key
158         del document.body[i+1]
159         i += 1
160
161     # now we need to add the new branches to the header
162     for old, new in ibranches.iteritems():
163         i = find_token(document.header, "\\branch " + old, 0)
164         if i == -1:
165             document.warning("Can't find branch %s even though we found it before!" % (old))
166             continue
167         j = find_token(document.header, "\\end_branch", i)
168         if j == -1:
169             document.warning("Malformed LyX document! Can't find end of branch " + old)
170             continue
171         # ourbranches[old] - 1 inverts the selection status of the old branch
172         lines = ["\\branch " + new,
173                  "\\selected " + str(ourbranches[old] - 1)]
174         # these are the old lines telling us color, etc.
175         lines += document.header[i+2 : j+1]
176         document.header[i:i] = lines
177         
178
179 ##
180 # Conversion hub
181 #
182
183 supported_versions = ["2.3.0", "2.3"]
184 convert = [
185            [509, [convert_microtype]],
186            [510, [convert_dateinset]],
187            [511, [convert_ibranches]]
188           ]
189
190 revert =  [
191            [510, [revert_ibranches]],
192            [509, []],
193            [508, [revert_microtype]]
194           ]
195
196
197 if __name__ == "__main__":
198     pass