]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_3.py
rename buffer parameter math_number_before to math_numbering_side
[lyx.git] / lib / lyx2lyx / lyx_2_3.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # Copyright (C) 2016 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.3"""
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, find_token_backwards, find_end_of_layout, \
28     find_token, find_end_of_inset, get_value,  get_bool_value, \
29     get_containing_layout, get_quoted_value, del_token, find_re
30 #  find_tokens, find_token_exact, is_in_inset, \
31 #  check_token, get_option_value
32
33 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, revert_font_attrs, \
34     insert_to_preamble
35 #  get_ert, lyx2latex, \
36 #  lyx2verbatim, length_in_bp, convert_info_insets
37 #  latex_length, revert_flex_inset, hex2ratio, str2bool
38
39 ####################################################################
40 # Private helper functions
41
42
43
44 ###############################################################################
45 ###
46 ### Conversion and reversion routines
47 ###
48 ###############################################################################
49
50 def convert_microtype(document):
51     " Add microtype settings. "
52     i = find_token(document.header, "\\font_tt_scale" , 0)
53     if i == -1:
54         document.warning("Malformed LyX document: Can't find \\font_tt_scale.")
55         i = len(document.header) - 1
56
57     j = find_token(document.preamble, "\\usepackage{microtype}", 0)
58     if j == -1:
59         document.header.insert(i + 1, "\\use_microtype false")
60     else:
61         document.header.insert(i + 1, "\\use_microtype true")
62         del document.preamble[j]
63
64
65 def revert_microtype(document):
66     " Remove microtype settings. "
67     i = find_token(document.header, "\\use_microtype", 0)
68     if i == -1:
69         return
70     use_microtype = get_bool_value(document.header, "\\use_microtype" , i)
71     del document.header[i]
72     if use_microtype:
73         add_to_preamble(document, ["\\usepackage{microtype}"])
74
75
76 def convert_dateinset(document):
77     ' Convert date external inset to ERT '
78     i = 0
79     while True:
80         i = find_token(document.body, "\\begin_inset External", i)
81         if i == -1:
82             return
83         j = find_end_of_inset(document.body, i)
84         if j == -1:
85             document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
86             i += 1
87             continue
88         if get_value(document.body, 'template', i, j) == "Date":
89             document.body[i : j + 1] = put_cmd_in_ert("\\today ")
90         i += 1
91         continue
92
93
94 def convert_inputenc(document):
95     " Replace no longer supported input encoding settings. "
96     i = find_token(document.header, "\\inputenc", 0)
97     if i == -1:
98         return
99     if get_value(document.header, "\\inputencoding", i) == "pt254":
100         document.header[i] = "\\inputencoding pt154"
101
102
103 def convert_ibranches(document):
104     ' Add "inverted 0" to branch insets'
105     i = 0
106     while True:
107         i = find_token(document.body, "\\begin_inset Branch", i)
108         if i == -1:
109             return
110         document.body.insert(i + 1, "inverted 0")
111         i += 1
112
113
114 def revert_ibranches(document):
115     ' Convert inverted branches to explicit anti-branches'
116     # Get list of branches
117     ourbranches = {}
118     i = 0
119     while True:
120         i = find_token(document.header, "\\branch", i)
121         if i == -1:
122             break
123         branch = document.header[i][8:].strip()
124         if document.header[i+1].startswith("\\selected "):
125             #document.warning(document.header[i+1])
126             #document.warning(document.header[i+1][10])
127             selected = int(document.header[i+1][10])
128         else:
129             document.warning("Malformed LyX document: No selection indicator for branch " + branch)
130             selected = 1
131
132         # the value tells us whether the branch is selected
133         ourbranches[document.header[i][8:].strip()] = selected
134         i += 1
135
136     # Figure out what inverted branches, if any, have been used
137     # and convert them to "Anti-OldBranch"
138     ibranches = {}
139     i = 0
140     while True:
141         i = find_token(document.body, "\\begin_inset Branch", i)
142         if i == -1:
143             break
144         if not document.body[i+1].startswith("inverted "):
145             document.warning("Malformed LyX document: Missing 'inverted' tag!")
146             i += 1
147             continue
148         inverted = document.body[i+1][9]
149         #document.warning(document.body[i+1])
150
151         if inverted == "1":
152             branch = document.body[i][20:].strip()
153             #document.warning(branch)
154             if not branch in ibranches:
155                 antibranch = "Anti-" + branch
156                 while antibranch in ibranches:
157                     antibranch = "x" + antibranch
158                 ibranches[branch] = antibranch
159             else:
160                 antibranch = ibranches[branch]
161             #document.warning(antibranch)
162             document.body[i] = "\\begin_inset Branch " + antibranch
163
164         # remove "inverted" key
165         del document.body[i+1]
166         i += 1
167
168     # now we need to add the new branches to the header
169     for old, new in ibranches.items():
170         i = find_token(document.header, "\\branch " + old, 0)
171         if i == -1:
172             document.warning("Can't find branch %s even though we found it before!" % (old))
173             continue
174         j = find_token(document.header, "\\end_branch", i)
175         if j == -1:
176             document.warning("Malformed LyX document! Can't find end of branch " + old)
177             continue
178         # ourbranches[old] - 1 inverts the selection status of the old branch
179         lines = ["\\branch " + new,
180                  "\\selected " + str(ourbranches[old] - 1)]
181         # these are the old lines telling us color, etc.
182         lines += document.header[i+2 : j+1]
183         document.header[i:i] = lines
184
185
186 def revert_beamer_article_styles(document):
187     " Include (scr)article styles in beamer article "
188
189     beamer_articles = ["article-beamer", "scrarticle-beamer"]
190     if document.textclass not in beamer_articles:
191         return
192
193     inclusion = "article.layout"
194     if document.textclass == "scrarticle-beamer":
195         inclusion = "scrartcl.layout"
196
197     i = find_token(document.header, "\\begin_local_layout", 0)
198     if i == -1:
199         k = find_token(document.header, "\\language", 0)
200         if k == -1:
201             # this should not happen
202             document.warning("Malformed LyX document! No \\language header found!")
203             return
204         document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
205         i = k - 1
206
207     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
208     if j == -1:
209         # this should not happen
210         document.warning("Malformed LyX document: Can't find end of local layout!")
211         return
212
213     document.header[i+1 : i+1] = [
214         "### Inserted by lyx2lyx (more [scr]article styles) ###",
215         "Input " + inclusion,
216         "Input beamer.layout",
217         "Provides geometry 0",
218         "Provides hyperref 0",
219         "DefaultFont",
220         "     Family                Roman",
221         "     Series                Medium",
222         "     Shape                 Up",
223         "     Size                  Normal",
224         "     Color                 None",
225         "EndFont",
226         "Preamble",
227         "     \\usepackage{beamerarticle,pgf}",
228         "     % this default might be overridden by plain title style",
229         "     \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
230         "     \\AtBeginDocument{",
231         "             \\let\\origtableofcontents=\\tableofcontents",
232         "             \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
233         "             \\def\\gobbletableofcontents#1{\\origtableofcontents}",
234         "     }",
235         "EndPreamble",
236         "### End of insertion by lyx2lyx (more [scr]article styles) ###"
237     ]
238
239
240 def convert_beamer_article_styles(document):
241     " Remove included (scr)article styles in beamer article "
242
243     beamer_articles = ["article-beamer", "scrarticle-beamer"]
244     if document.textclass not in beamer_articles:
245         return
246
247     i = find_token(document.header, "\\begin_local_layout", 0)
248     if i == -1:
249         return
250
251     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
252     if j == -1:
253         # this should not happen
254         document.warning("Malformed LyX document: Can't find end of local layout!")
255         return
256
257     k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
258     if k != -1:
259         l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
260         if l == -1:
261             # this should not happen
262             document.warning("End of lyx2lyx local layout insertion not found!")
263             return
264
265         if k == i + 1 and l == j - 1:
266             # that was all the local layout there was
267             document.header[i : j + 1] = []
268         else:
269             document.header[k : l + 1] = []
270
271
272 def revert_bosnian(document):
273     "Set the document language to English but assure Bosnian output"
274
275     if document.language == "bosnian":
276         document.language = "english"
277         i = find_token(document.header, "\\language bosnian", 0)
278         if i != -1:
279             document.header[i] = "\\language english"
280         j = find_token(document.header, "\\language_package default", 0)
281         if j != -1:
282             document.header[j] = "\\language_package babel"
283         k = find_token(document.header, "\\options", 0)
284         if k != -1:
285             document.header[k] = document.header[k].replace("\\options", "\\options bosnian,")
286         else:
287             l = find_token(document.header, "\\use_default_options", 0)
288             document.header.insert(l + 1, "\\options bosnian")
289
290
291 def revert_friulan(document):
292     "Set the document language to English but assure Friulan output"
293
294     if document.language == "friulan":
295         document.language = "english"
296         i = find_token(document.header, "\\language friulan", 0)
297         if i != -1:
298             document.header[i] = "\\language english"
299         j = find_token(document.header, "\\language_package default", 0)
300         if j != -1:
301             document.header[j] = "\\language_package babel"
302         k = find_token(document.header, "\\options", 0)
303         if k != -1:
304             document.header[k] = document.header[k].replace("\\options", "\\options friulan,")
305         else:
306             l = find_token(document.header, "\\use_default_options", 0)
307             document.header.insert(l + 1, "\\options friulan")
308
309
310 def revert_macedonian(document):
311     "Set the document language to English but assure Macedonian output"
312
313     if document.language == "macedonian":
314         document.language = "english"
315         i = find_token(document.header, "\\language macedonian", 0)
316         if i != -1:
317             document.header[i] = "\\language english"
318         j = find_token(document.header, "\\language_package default", 0)
319         if j != -1:
320             document.header[j] = "\\language_package babel"
321         k = find_token(document.header, "\\options", 0)
322         if k != -1:
323             document.header[k] = document.header[k].replace("\\options", "\\options macedonian,")
324         else:
325             l = find_token(document.header, "\\use_default_options", 0)
326             document.header.insert(l + 1, "\\options macedonian")
327
328
329 def revert_piedmontese(document):
330     "Set the document language to English but assure Piedmontese output"
331
332     if document.language == "piedmontese":
333         document.language = "english"
334         i = find_token(document.header, "\\language piedmontese", 0)
335         if i != -1:
336             document.header[i] = "\\language english"
337         j = find_token(document.header, "\\language_package default", 0)
338         if j != -1:
339             document.header[j] = "\\language_package babel"
340         k = find_token(document.header, "\\options", 0)
341         if k != -1:
342             document.header[k] = document.header[k].replace("\\options", "\\options piedmontese,")
343         else:
344             l = find_token(document.header, "\\use_default_options", 0)
345             document.header.insert(l + 1, "\\options piedmontese")
346
347
348 def revert_romansh(document):
349     "Set the document language to English but assure Romansh output"
350
351     if document.language == "romansh":
352         document.language = "english"
353         i = find_token(document.header, "\\language romansh", 0)
354         if i != -1:
355             document.header[i] = "\\language english"
356         j = find_token(document.header, "\\language_package default", 0)
357         if j != -1:
358             document.header[j] = "\\language_package babel"
359         k = find_token(document.header, "\\options", 0)
360         if k != -1:
361             document.header[k] = document.header[k].replace("\\options", "\\options romansh,")
362         else:
363             l = find_token(document.header, "\\use_default_options", 0)
364             document.header.insert(l + 1, "\\options romansh")
365
366
367 def revert_amharic(document):
368     "Set the document language to English but assure Amharic output"
369
370     if document.language == "amharic":
371         document.language = "english"
372         i = find_token(document.header, "\\language amharic", 0)
373         if i != -1:
374             document.header[i] = "\\language english"
375         j = find_token(document.header, "\\language_package default", 0)
376         if j != -1:
377             document.header[j] = "\\language_package default"
378         add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{amharic}}"])
379         document.body[2 : 2] = ["\\begin_layout Standard",
380                                 "\\begin_inset ERT", "status open", "",
381                                 "\\begin_layout Plain Layout", "", "",
382                                 "\\backslash",
383                                 "resetdefaultlanguage{amharic}",
384                                 "\\end_layout", "", "\\end_inset", "", "",
385                                 "\\end_layout", ""]
386
387
388 def revert_asturian(document):
389     "Set the document language to English but assure Asturian output"
390
391     if document.language == "asturian":
392         document.language = "english"
393         i = find_token(document.header, "\\language asturian", 0)
394         if i != -1:
395             document.header[i] = "\\language english"
396         j = find_token(document.header, "\\language_package default", 0)
397         if j != -1:
398             document.header[j] = "\\language_package default"
399         add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{asturian}}"])
400         document.body[2 : 2] = ["\\begin_layout Standard",
401                                 "\\begin_inset ERT", "status open", "",
402                                 "\\begin_layout Plain Layout", "", "",
403                                 "\\backslash",
404                                 "resetdefaultlanguage{asturian}",
405                                 "\\end_layout", "", "\\end_inset", "", "",
406                                 "\\end_layout", ""]
407
408
409 def revert_kannada(document):
410     "Set the document language to English but assure Kannada output"
411
412     if document.language == "kannada":
413         document.language = "english"
414         i = find_token(document.header, "\\language kannada", 0)
415         if i != -1:
416             document.header[i] = "\\language english"
417         j = find_token(document.header, "\\language_package default", 0)
418         if j != -1:
419             document.header[j] = "\\language_package default"
420         add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{kannada}}"])
421         document.body[2 : 2] = ["\\begin_layout Standard",
422                                 "\\begin_inset ERT", "status open", "",
423                                 "\\begin_layout Plain Layout", "", "",
424                                 "\\backslash",
425                                 "resetdefaultlanguage{kannada}",
426                                 "\\end_layout", "", "\\end_inset", "", "",
427                                 "\\end_layout", ""]
428
429
430 def revert_khmer(document):
431     "Set the document language to English but assure Khmer output"
432
433     if document.language == "khmer":
434         document.language = "english"
435         i = find_token(document.header, "\\language khmer", 0)
436         if i != -1:
437             document.header[i] = "\\language english"
438         j = find_token(document.header, "\\language_package default", 0)
439         if j != -1:
440             document.header[j] = "\\language_package default"
441         add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{khmer}}"])
442         document.body[2 : 2] = ["\\begin_layout Standard",
443                                 "\\begin_inset ERT", "status open", "",
444                                 "\\begin_layout Plain Layout", "", "",
445                                 "\\backslash",
446                                 "resetdefaultlanguage{khmer}",
447                                 "\\end_layout", "", "\\end_inset", "", "",
448                                 "\\end_layout", ""]
449
450
451 def revert_urdu(document):
452     "Set the document language to English but assure Urdu output"
453
454     if document.language == "urdu":
455         document.language = "english"
456         i = find_token(document.header, "\\language urdu", 0)
457         if i != -1:
458             document.header[i] = "\\language english"
459         j = find_token(document.header, "\\language_package default", 0)
460         if j != -1:
461             document.header[j] = "\\language_package default"
462         add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{urdu}}"])
463         document.body[2 : 2] = ["\\begin_layout Standard",
464                                 "\\begin_inset ERT", "status open", "",
465                                 "\\begin_layout Plain Layout", "", "",
466                                 "\\backslash",
467                                 "resetdefaultlanguage{urdu}",
468                                 "\\end_layout", "", "\\end_inset", "", "",
469                                 "\\end_layout", ""]
470
471
472 def revert_syriac(document):
473     "Set the document language to English but assure Syriac output"
474
475     if document.language == "syriac":
476         document.language = "english"
477         i = find_token(document.header, "\\language syriac", 0)
478         if i != -1:
479             document.header[i] = "\\language english"
480         j = find_token(document.header, "\\language_package default", 0)
481         if j != -1:
482             document.header[j] = "\\language_package default"
483         add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{syriac}}"])
484         document.body[2 : 2] = ["\\begin_layout Standard",
485                                 "\\begin_inset ERT", "status open", "",
486                                 "\\begin_layout Plain Layout", "", "",
487                                 "\\backslash",
488                                 "resetdefaultlanguage{syriac}",
489                                 "\\end_layout", "", "\\end_inset", "", "",
490                                 "\\end_layout", ""]
491
492
493 def revert_quotes(document):
494     " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
495
496     # First handle verbatim insets
497     i = 0
498     j = 0
499     while i < len(document.body):
500         words = document.body[i].split()
501         if len(words) > 1 and words[0] == "\\begin_inset" and \
502            ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
503             j = find_end_of_inset(document.body, i)
504             if j == -1:
505                 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
506                 i += 1
507                 continue
508             while True:
509                 k = find_token(document.body, '\\begin_inset Quotes', i, j)
510                 if k == -1:
511                     i += 1
512                     break
513                 l = find_end_of_inset(document.body, k)
514                 if l == -1:
515                     document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
516                     i = k
517                     continue
518                 replace = "\""
519                 if document.body[k].endswith("s"):
520                     replace = "'"
521                 document.body[k:l+1] = [replace]
522         else:
523             i += 1
524             continue
525
526     # Now verbatim layouts
527     i = 0
528     j = 0
529     while i < len(document.body):
530         words = document.body[i].split()
531         if len(words) > 1 and words[0] == "\\begin_layout" and \
532            words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
533             j = find_end_of_layout(document.body, i)
534             if j == -1:
535                 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
536                 i += 1
537                 continue
538             while True:
539                 k = find_token(document.body, '\\begin_inset Quotes', i, j)
540                 if k == -1:
541                     i += 1
542                     break
543                 l = find_end_of_inset(document.body, k)
544                 if l == -1:
545                     document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
546                     i = k
547                     continue
548                 replace = "\""
549                 if document.body[k].endswith("s"):
550                     replace = "'"
551                 document.body[k:l+1] = [replace]
552         else:
553             i += 1
554             continue
555
556     # Now handle Hebrew
557     if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
558         return
559
560     i = 0
561     j = 0
562     while True:
563         k = find_token(document.body, '\\begin_inset Quotes', i)
564         if k == -1:
565             return
566         l = find_end_of_inset(document.body, k)
567         if l == -1:
568             document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
569             i = k
570             continue
571         hebrew = False
572         parent = get_containing_layout(document.body, k)
573         ql = find_token_backwards(document.body, "\\lang", k)
574         if ql == -1 or ql < parent[1]:
575             hebrew = document.language == "hebrew"
576         elif document.body[ql] == "\\lang hebrew":
577             hebrew = True
578         if hebrew:
579             replace = "\""
580             if document.body[k].endswith("s"):
581                 replace = "'"
582             document.body[k:l+1] = [replace]
583         i = l
584
585
586 def revert_iopart(document):
587     " Input new styles via local layout "
588     if document.textclass != "iopart":
589         return
590
591     i = find_token(document.header, "\\begin_local_layout", 0)
592     if i == -1:
593         k = find_token(document.header, "\\language", 0)
594         if k == -1:
595             # this should not happen
596             document.warning("Malformed LyX document! No \\language header found!")
597             return
598         document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
599         i = k-1
600
601     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
602     if j == -1:
603         # this should not happen
604         document.warning("Malformed LyX document! Can't find end of local layout!")
605         return
606
607     document.header[i+1 : i+1] = [
608         "### Inserted by lyx2lyx (stdlayouts) ###",
609         "Input stdlayouts.inc",
610         "### End of insertion by lyx2lyx (stdlayouts) ###"
611     ]
612
613
614 def convert_iopart(document):
615     " Remove local layout we added, if it is there "
616     if document.textclass != "iopart":
617         return
618
619     i = find_token(document.header, "\\begin_local_layout", 0)
620     if i == -1:
621         return
622
623     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
624     if j == -1:
625         # this should not happen
626         document.warning("Malformed LyX document! Can't find end of local layout!")
627         return
628
629     k = find_token(document.header, "### Inserted by lyx2lyx (stdlayouts) ###", i, j)
630     if k != -1:
631         l = find_token(document.header, "### End of insertion by lyx2lyx (stdlayouts) ###", i, j)
632         if l == -1:
633             # this should not happen
634             document.warning("End of lyx2lyx local layout insertion not found!")
635             return
636         if k == i + 1 and l == j - 1:
637             # that was all the local layout there was
638             document.header[i : j + 1] = []
639         else:
640             document.header[k : l + 1] = []
641
642
643 def convert_quotestyle(document):
644     " Convert \\quotes_language to \\quotes_style "
645     i = find_token(document.header, "\\quotes_language", 0)
646     if i == -1:
647         document.warning("Malformed LyX document! Can't find \\quotes_language!")
648         return
649     val = get_value(document.header, "\\quotes_language", i)
650     document.header[i] = "\\quotes_style " + val
651
652
653 def revert_quotestyle(document):
654     " Revert \\quotes_style to \\quotes_language "
655     i = find_token(document.header, "\\quotes_style", 0)
656     if i == -1:
657         document.warning("Malformed LyX document! Can't find \\quotes_style!")
658         return
659     val = get_value(document.header, "\\quotes_style", i)
660     document.header[i] = "\\quotes_language " + val
661
662
663 def revert_plainquote(document):
664     " Revert plain quote insets "
665
666     # First, revert style setting
667     i = find_token(document.header, "\\quotes_style plain", 0)
668     if i != -1:
669         document.header[i] = "\\quotes_style english"
670
671     # now the insets
672     i = 0
673     j = 0
674     while True:
675         k = find_token(document.body, '\\begin_inset Quotes q', i)
676         if k == -1:
677             return
678         l = find_end_of_inset(document.body, k)
679         if l == -1:
680             document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
681             i = k
682             continue
683         replace = "\""
684         if document.body[k].endswith("s"):
685             replace = "'"
686         document.body[k:l+1] = [replace]
687         i = l
688
689
690 def convert_frenchquotes(document):
691     " Convert french quote insets to swiss "
692
693     # First, revert style setting
694     i = find_token(document.header, "\\quotes_style french", 0)
695     if i != -1:
696         document.header[i] = "\\quotes_style swiss"
697
698     # now the insets
699     i = 0
700     while True:
701         i = find_token(document.body, '\\begin_inset Quotes f', i)
702         if i == -1:
703             return
704         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
705         newval = val.replace("f", "c", 1)
706         document.body[i] = document.body[i].replace(val, newval)
707         i += 1
708
709
710 def revert_swissquotes(document):
711     " Revert swiss quote insets to french "
712
713     # First, revert style setting
714     i = find_token(document.header, "\\quotes_style swiss", 0)
715     if i != -1:
716         document.header[i] = "\\quotes_style french"
717
718     # now the insets
719     i = 0
720     while True:
721         i = find_token(document.body, '\\begin_inset Quotes c', i)
722         if i == -1:
723             return
724         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
725         newval = val.replace("c", "f", 1)
726         document.body[i] = document.body[i].replace(val, newval)
727         i += 1
728
729
730 def revert_britishquotes(document):
731     " Revert british quote insets to english "
732
733     # First, revert style setting
734     i = find_token(document.header, "\\quotes_style british", 0)
735     if i != -1:
736         document.header[i] = "\\quotes_style english"
737
738     # now the insets
739     i = 0
740     while True:
741         i = find_token(document.body, '\\begin_inset Quotes b', i)
742         if i == -1:
743             return
744         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
745         newval = val.replace("b", "e", 1)
746         if val[2] == "d":
747             # opening mark
748             newval = newval.replace("d", "s")
749         else:
750             # closing mark
751             newval = newval.replace("s", "d")
752         document.body[i] = document.body[i].replace(val, newval)
753         i += 1
754
755
756 def revert_swedishgquotes(document):
757     " Revert swedish quote insets "
758
759     # First, revert style setting
760     i = find_token(document.header, "\\quotes_style swedishg", 0)
761     if i != -1:
762         document.header[i] = "\\quotes_style danish"
763
764     # now the insets
765     i = 0
766     while True:
767         i = find_token(document.body, '\\begin_inset Quotes w', i)
768         if i == -1:
769             return
770         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
771         if val[2] == "d":
772             # outer marks
773             newval = val.replace("w", "a", 1).replace("r", "l")
774         else:
775             # inner marks
776             newval = val.replace("w", "s", 1)
777         document.body[i] = document.body[i].replace(val, newval)
778         i += 1
779
780
781 def revert_frenchquotes(document):
782     " Revert french inner quote insets "
783
784     i = 0
785     while True:
786         i = find_token(document.body, '\\begin_inset Quotes f', i)
787         if i == -1:
788             return
789         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
790         if val[2] == "s":
791             # inner marks
792             newval = val.replace("f", "e", 1).replace("s", "d")
793             document.body[i] = document.body[i].replace(val, newval)
794         i += 1
795
796
797 def revert_frenchinquotes(document):
798     " Revert inner frenchin quote insets "
799
800     # First, revert style setting
801     i = find_token(document.header, "\\quotes_style frenchin", 0)
802     if i != -1:
803         document.header[i] = "\\quotes_style french"
804
805     # now the insets
806     i = 0
807     while True:
808         i = find_token(document.body, '\\begin_inset Quotes i', i)
809         if i == -1:
810             return
811         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
812         newval = val.replace("i", "f", 1)
813         if val[2] == "s":
814             # inner marks
815             newval = newval.replace("s", "d")
816         document.body[i] = document.body[i].replace(val, newval)
817         i += 1
818
819
820 def revert_russianquotes(document):
821     " Revert russian quote insets "
822
823     # First, revert style setting
824     i = find_token(document.header, "\\quotes_style russian", 0)
825     if i != -1:
826         document.header[i] = "\\quotes_style french"
827
828     # now the insets
829     i = 0
830     while True:
831         i = find_token(document.body, '\\begin_inset Quotes r', i)
832         if i == -1:
833             return
834         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
835         newval = val
836         if val[2] == "s":
837             # inner marks
838             newval = val.replace("r", "g", 1).replace("s", "d")
839         else:
840             # outer marks
841             newval = val.replace("r", "f", 1)
842         document.body[i] = document.body[i].replace(val, newval)
843         i += 1
844
845
846 def revert_dynamicquotes(document):
847     " Revert dynamic quote insets "
848
849     # First, revert header
850     i = find_token(document.header, "\\dynamic_quotes", 0)
851     if i != -1:
852         del document.header[i]
853
854     # Get global style
855     style = "english"
856     i = find_token(document.header, "\\quotes_style", 0)
857     if i == -1:
858         document.warning("Malformed document! Missing \\quotes_style")
859     else:
860         style = get_value(document.header, "\\quotes_style", i)
861
862     s = "e"
863     if style == "english":
864         s = "e"
865     elif style == "swedish":
866         s = "s"
867     elif style == "german":
868         s = "g"
869     elif style == "polish":
870         s = "p"
871     elif style == "swiss":
872         s = "c"
873     elif style == "danish":
874         s = "a"
875     elif style == "plain":
876         s = "q"
877     elif style == "british":
878         s = "b"
879     elif style == "swedishg":
880         s = "w"
881     elif style == "french":
882         s = "f"
883     elif style == "frenchin":
884         s = "i"
885     elif style == "russian":
886         s = "r"
887
888     # now transform the insets
889     i = 0
890     while True:
891         i = find_token(document.body, '\\begin_inset Quotes x', i)
892         if i == -1:
893             return
894         document.body[i] = document.body[i].replace("x", s)
895         i += 1
896
897
898 def revert_cjkquotes(document):
899     " Revert cjk quote insets "
900
901     # Get global style
902     style = "english"
903     i = find_token(document.header, "\\quotes_style", 0)
904     if i == -1:
905         document.warning("Malformed document! Missing \\quotes_style")
906     else:
907         style = get_value(document.header, "\\quotes_style", i)
908
909     global_cjk = style.find("cjk") != -1
910
911     if global_cjk:
912         document.header[i] = "\\quotes_style english"
913         # transform dynamic insets
914         s = "j"
915         if style == "cjkangle":
916             s = "k"
917         i = 0
918         while True:
919             i = find_token(document.body, '\\begin_inset Quotes x', i)
920             if i == -1:
921                 break
922             document.body[i] = document.body[i].replace("x", s)
923             i += 1
924
925     cjk_langs = ["chinese-simplified", "chinese-traditional", "japanese", "japanese-cjk", "korean"]
926
927     i = 0
928     j = 0
929     while True:
930         k = find_token(document.body, '\\begin_inset Quotes j', i)
931         if k == -1:
932             break
933         l = find_end_of_inset(document.body, k)
934         if l == -1:
935             document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
936             i = k
937             continue
938         cjk = False
939         parent = get_containing_layout(document.body, k)
940         ql = find_token_backwards(document.body, "\\lang", k)
941         if ql == -1 or ql < parent[1]:
942             cjk = document.language in cjk_langs
943         elif document.body[ql].split()[1] in cjk_langs:
944             cjk = True
945         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
946         replace = []
947         if val[2] == "s":
948             # inner marks
949             if val[1] == "l":
950                 # inner opening mark
951                 if cjk:
952                     replace = [u"\u300E"]
953                 else:
954                     replace = ["\\begin_inset Formula $\\llceil$", "\\end_inset"]
955             else:
956                 # inner closing mark
957                 if cjk:
958                     replace = [u"\u300F"]
959                 else:
960                     replace = ["\\begin_inset Formula $\\rrfloor$", "\\end_inset"]
961         else:
962             # outer marks
963             if val[1] == "l":
964                 # outer opening mark
965                 if cjk:
966                     replace = [u"\u300C"]
967                 else:
968                     replace = ["\\begin_inset Formula $\\lceil$", "\\end_inset"]
969             else:
970                 # outer closing mark
971                 if cjk:
972                     replace = [u"\u300D"]
973                 else:
974                     replace = ["\\begin_inset Formula $\\rfloor$", "\\end_inset"]
975
976         document.body[k:l+1] = replace
977         i = l
978
979     i = 0
980     j = 0
981     while True:
982         k = find_token(document.body, '\\begin_inset Quotes k', i)
983         if k == -1:
984             return
985         l = find_end_of_inset(document.body, k)
986         if l == -1:
987             document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
988             i = k
989             continue
990         cjk = False
991         parent = get_containing_layout(document.body, k)
992         ql = find_token_backwards(document.body, "\\lang", k)
993         if ql == -1 or ql < parent[1]:
994             cjk = document.language in cjk_langs
995         elif document.body[ql].split()[1] in cjk_langs:
996             cjk = True
997         val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
998         replace = []
999         if val[2] == "s":
1000             # inner marks
1001             if val[1] == "l":
1002                 # inner opening mark
1003                 if cjk:
1004                     replace = [u"\u3008"]
1005                 else:
1006                     replace = ["\\begin_inset Formula $\\langle$", "\\end_inset"]
1007             else:
1008                 # inner closing mark
1009                 if cjk:
1010                     replace = [u"\u3009"]
1011                 else:
1012                     replace = ["\\begin_inset Formula $\\rangle$", "\\end_inset"]
1013         else:
1014             # outer marks
1015             if val[1] == "l":
1016                 # outer opening mark
1017                 if cjk:
1018                     replace = [u"\u300A"]
1019                 else:
1020                     replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$", "\\end_inset"]
1021             else:
1022                 # outer closing mark
1023                 if cjk:
1024                     replace = [u"\u300B"]
1025                 else:
1026                     replace = ["\\begin_inset Formula $\\rangle\\kern -2.5pt\\rangle$", "\\end_inset"]
1027
1028         document.body[k:l+1] = replace
1029         i = l
1030
1031
1032 def revert_crimson(document):
1033     " Revert native Cochineal/Crimson font definition to LaTeX "
1034
1035     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1036         preamble = ""
1037         i = find_token(document.header, "\\font_roman \"cochineal\"", 0)
1038         if i != -1:
1039             osf = False
1040             j = find_token(document.header, "\\font_osf true", 0)
1041             if j != -1:
1042                 osf = True
1043             preamble = "\\usepackage"
1044             if osf:
1045                 document.header[j] = "\\font_osf false"
1046                 preamble += "[proportional,osf]"
1047             preamble += "{cochineal}"
1048             add_to_preamble(document, [preamble])
1049             document.header[i] = document.header[i].replace("cochineal", "default")
1050
1051
1052 def revert_cochinealmath(document):
1053     " Revert cochineal newtxmath definitions to LaTeX "
1054
1055     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1056         i = find_token(document.header, "\\font_math \"cochineal-ntxm\"", 0)
1057         if i != -1:
1058             add_to_preamble(document, "\\usepackage[cochineal]{newtxmath}")
1059             document.header[i] = document.header[i].replace("cochineal-ntxm", "auto")
1060
1061
1062 def revert_labelonly(document):
1063     " Revert labelonly tag for InsetRef "
1064     i = 0
1065     while (True):
1066         i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1067         if i == -1:
1068             return
1069         j = find_end_of_inset(document.body, i)
1070         if j == -1:
1071             document.warning("Can't find end of reference inset at line %d!!" %(i))
1072             i += 1
1073             continue
1074         k = find_token(document.body, "LatexCommand labelonly", i, j)
1075         if k == -1:
1076             i = j
1077             continue
1078         label = get_quoted_value(document.body, "reference", i, j)
1079         if not label:
1080             document.warning("Can't find label for reference at line %d!" %(i))
1081             i = j + 1
1082             continue
1083         document.body[i:j+1] = put_cmd_in_ert([label])
1084         i += 1
1085
1086
1087 def revert_plural_refs(document):
1088     " Revert plural and capitalized references "
1089     i = find_token(document.header, "\\use_refstyle 1", 0)
1090     use_refstyle = (i != 0)
1091
1092     i = 0
1093     while (True):
1094         i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1095         if i == -1:
1096             return
1097         j = find_end_of_inset(document.body, i)
1098         if j == -1:
1099             document.warning("Can't find end of reference inset at line %d!!" %(i))
1100             i += 1
1101             continue
1102
1103         plural = caps = suffix = False
1104         k = find_token(document.body, "LaTeXCommand formatted", i, j)
1105         if k != -1 and use_refstyle:
1106             plural = get_bool_value(document.body, "plural", i, j, False)
1107             caps   = get_bool_value(document.body, "caps", i, j, False)
1108             label  = get_quoted_value(document.body, "reference", i, j)
1109             if label:
1110                 try:
1111                     (prefix, suffix) = label.split(":", 1)
1112                 except:
1113                     document.warning("No `:' separator in formatted reference at line %d!" % (i))
1114             else:
1115                 document.warning("Can't find label for reference at line %d!" % (i))
1116
1117         # this effectively tests also for use_refstyle and a formatted reference
1118         # we do this complicated test because we would otherwise do this erasure
1119         # over and over and over
1120         if not ((plural or caps) and suffix):
1121             del_token(document.body, "plural", i, j)
1122             del_token(document.body, "caps", i, j - 1) # since we deleted a line
1123             i = j - 1
1124             continue
1125
1126         if caps:
1127             prefix = prefix[0].title() + prefix[1:]
1128         cmd = "\\" + prefix + "ref"
1129         if plural:
1130             cmd += "[s]"
1131         cmd += "{" + suffix + "}"
1132         document.body[i:j+1] = put_cmd_in_ert([cmd])
1133         i += 1
1134
1135
1136 def revert_noprefix(document):
1137     " Revert labelonly tags with 'noprefix' set "
1138     i = 0
1139     while (True):
1140         i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1141         if i == -1:
1142             return
1143         j = find_end_of_inset(document.body, i)
1144         if j == -1:
1145             document.warning("Can't find end of reference inset at line %d!!" %(i))
1146             i += 1
1147             continue
1148         k = find_token(document.body, "LatexCommand labelonly", i, j)
1149         if k == -1:
1150             i = j
1151             continue
1152         noprefix = get_bool_value(document.body, "noprefix", i, j)
1153         if not noprefix:
1154             del_token(document.body, "noprefix", i, j)
1155             i = j
1156             continue
1157         label = get_quoted_value(document.body, "reference", i, j)
1158         if not label:
1159             document.warning("Can't find label for reference at line %d!" %(i))
1160             i = j + 1
1161             continue
1162         try:
1163             (prefix, suffix) = label.split(":", 1)
1164         except:
1165             document.warning("No `:' separator in formatted reference at line %d!" % (i))
1166             # we'll leave this as an ordinary labelonly reference
1167             del_token(document.body, "noprefix", i, j)
1168             i = j
1169             continue
1170         document.body[i:j+1] = put_cmd_in_ert([suffix])
1171         i += 1
1172
1173
1174 def revert_biblatex(document):
1175     " Revert biblatex support "
1176
1177     #
1178     # Header
1179     #
1180
1181     # 1. Get cite engine
1182     engine = "basic"
1183     i = find_token(document.header, "\\cite_engine", 0)
1184     if i == -1:
1185         document.warning("Malformed document! Missing \\cite_engine")
1186     else:
1187         engine = get_value(document.header, "\\cite_engine", i)
1188
1189     # 2. Store biblatex state and revert to natbib
1190     biblatex = False
1191     if engine in ["biblatex", "biblatex-natbib"]:
1192         biblatex = True
1193         document.header[i] = "\\cite_engine natbib"
1194
1195     # 3. Store and remove new document headers
1196     bibstyle = ""
1197     i = find_token(document.header, "\\biblatex_bibstyle", 0)
1198     if i != -1:
1199         bibstyle = get_value(document.header, "\\biblatex_bibstyle", i)
1200         del document.header[i]
1201
1202     citestyle = ""
1203     i = find_token(document.header, "\\biblatex_citestyle", 0)
1204     if i != -1:
1205         citestyle = get_value(document.header, "\\biblatex_citestyle", i)
1206         del document.header[i]
1207
1208     biblio_options = ""
1209     i = find_token(document.header, "\\biblio_options", 0)
1210     if i != -1:
1211         biblio_options = get_value(document.header, "\\biblio_options", i)
1212         del document.header[i]
1213
1214     if biblatex:
1215         bbxopts = "[natbib=true"
1216         if bibstyle != "":
1217             bbxopts += ",bibstyle=" + bibstyle
1218         if citestyle != "":
1219             bbxopts += ",citestyle=" + citestyle
1220         if biblio_options != "":
1221             bbxopts += "," + biblio_options
1222         bbxopts += "]"
1223         add_to_preamble(document, "\\usepackage" + bbxopts + "{biblatex}")
1224
1225     #
1226     # Body
1227     #
1228
1229     # 1. Bibtex insets
1230     i = 0
1231     bibresources = []
1232     while (True):
1233         i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1234         if i == -1:
1235             break
1236         j = find_end_of_inset(document.body, i)
1237         if j == -1:
1238             document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1239             i += 1
1240             continue
1241         bibs = get_quoted_value(document.body, "bibfiles", i, j)
1242         opts = get_quoted_value(document.body, "biblatexopts", i, j)
1243         # store resources
1244         if bibs:
1245             bibresources += bibs.split(",")
1246         else:
1247             document.warning("Can't find bibfiles for bibtex inset at line %d!" %(i))
1248         # remove biblatexopts line
1249         k = find_token(document.body, "biblatexopts", i, j)
1250         if k != -1:
1251             del document.body[k]
1252         # Re-find inset end line
1253         j = find_end_of_inset(document.body, i)
1254         # Insert ERT \\printbibliography and wrap bibtex inset to a Note
1255         if biblatex:
1256             pcmd = "printbibliography"
1257             if opts:
1258                 pcmd += "[" + opts + "]"
1259             repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1260                     "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1261                     "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1262                     "status open", "", "\\begin_layout Plain Layout" ]
1263             repl += document.body[i:j+1]
1264             repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1265             document.body[i:j+1] = repl
1266             j += 27
1267
1268         i = j + 1
1269
1270     if biblatex:
1271         for b in bibresources:
1272             add_to_preamble(document, "\\addbibresource{" + b + ".bib}")
1273
1274     # 2. Citation insets
1275
1276     # Specific citation insets used in biblatex that need to be reverted to ERT
1277     new_citations = {
1278         "Cite" : "Cite",
1279         "citebyear" : "citeyear",
1280         "citeyear" : "cite*",
1281         "Footcite" : "Smartcite",
1282         "footcite" : "smartcite",
1283         "Autocite" : "Autocite",
1284         "autocite" : "autocite",
1285         "citetitle" : "citetitle",
1286         "citetitle*" : "citetitle*",
1287         "fullcite" : "fullcite",
1288         "footfullcite" : "footfullcite",
1289         "supercite" : "supercite",
1290         "citeauthor" : "citeauthor",
1291         "citeauthor*" : "citeauthor*",
1292         "Citeauthor" : "Citeauthor",
1293         "Citeauthor*" : "Citeauthor*"
1294         }
1295
1296     # All commands accepted by LyX < 2.3. Everything else throws an error.
1297     old_citations = [ "cite", "nocite", "citet", "citep", "citealt", "citealp",\
1298                       "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*",\
1299                       "citealt*", "citealp*", "citeauthor*", "Citet",  "Citep",\
1300                       "Citealt",  "Citealp",  "Citeauthor", "Citet*", "Citep*",\
1301                       "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",\
1302                       "footcitet", "footcitep", "footcitealt", "footcitealp",\
1303                       "footciteauthor", "footciteyear", "footciteyearpar",\
1304                       "citefield", "citetitle", "cite*" ]
1305
1306     i = 0
1307     while (True):
1308         i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1309         if i == -1:
1310             break
1311         j = find_end_of_inset(document.body, i)
1312         if j == -1:
1313             document.warning("Can't find end of citation inset at line %d!!" %(i))
1314             i += 1
1315             continue
1316         k = find_token(document.body, "LatexCommand", i, j)
1317         if k == -1:
1318             document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1319             i = j + 1
1320             continue
1321         cmd = get_value(document.body, "LatexCommand", k)
1322         if biblatex and cmd in list(new_citations.keys()):
1323             pre = get_quoted_value(document.body, "before", i, j)
1324             post = get_quoted_value(document.body, "after", i, j)
1325             key = get_quoted_value(document.body, "key", i, j)
1326             if not key:
1327                 document.warning("Citation inset at line %d does not have a key!" %(i))
1328                 key = "???"
1329             # Replace known new commands with ERT
1330             res = "\\" + new_citations[cmd]
1331             if pre:
1332                 res += "[" + pre + "]"
1333             if post:
1334                 res += "[" + post + "]"
1335             elif pre:
1336                 res += "[]"
1337             res += "{" + key + "}"
1338             document.body[i:j+1] = put_cmd_in_ert([res])
1339         elif cmd not in old_citations:
1340             # Reset unknown commands to cite. This is what LyX does as well
1341             # (but LyX 2.2 would break on unknown commands)
1342             document.body[k] = "LatexCommand cite"
1343             document.warning("Reset unknown cite command '%s' with cite" % cmd)
1344         i = j + 1
1345
1346     # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
1347     if biblatex:
1348         i = find_token(document.header, "\\begin_local_layout", 0)
1349         if i == -1:
1350             k = find_token(document.header, "\\language", 0)
1351             if k == -1:
1352                 # this should not happen
1353                 document.warning("Malformed LyX document! No \\language header found!")
1354                 return
1355             document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1356             i = k-1
1357
1358         j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1359         if j == -1:
1360             # this should not happen
1361             document.warning("Malformed LyX document! Can't find end of local layout!")
1362             return
1363
1364         document.header[i+1 : i+1] = [
1365             "### Inserted by lyx2lyx (biblatex emulation) ###",
1366             "Provides natbib 1",
1367             "### End of insertion by lyx2lyx (biblatex emulation) ###"
1368         ]
1369
1370
1371 def revert_citekeyonly(document):
1372     " Revert keyonly cite command to ERT "
1373
1374     i = 0
1375     while (True):
1376         i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1377         if i == -1:
1378             break
1379         j = find_end_of_inset(document.body, i)
1380         if j == -1:
1381             document.warning("Can't find end of citation inset at line %d!!" %(i))
1382             i += 1
1383             continue
1384         k = find_token(document.body, "LatexCommand", i, j)
1385         if k == -1:
1386             document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1387             i = j + 1
1388             continue
1389         cmd = get_value(document.body, "LatexCommand", k)
1390         if cmd != "keyonly":
1391             i = j + 1
1392             continue
1393
1394         key = get_quoted_value(document.body, "key", i, j)
1395         if not key:
1396             document.warning("Citation inset at line %d does not have a key!" %(i))
1397         # Replace known new commands with ERT
1398         document.body[i:j+1] = put_cmd_in_ert([key])
1399         i = j + 1
1400
1401
1402
1403 def revert_bibpackopts(document):
1404     " Revert support for natbib/jurabib package options "
1405
1406     engine = "basic"
1407     i = find_token(document.header, "\\cite_engine", 0)
1408     if i == -1:
1409         document.warning("Malformed document! Missing \\cite_engine")
1410     else:
1411         engine = get_value(document.header, "\\cite_engine", i)
1412
1413     biblatex = False
1414     if engine not in ["natbib", "jurabib"]:
1415         return
1416
1417     i = find_token(document.header, "\\biblio_options", 0)
1418     if i == -1:
1419         # Nothing to do if we have no options
1420         return
1421
1422     biblio_options = get_value(document.header, "\\biblio_options", i)
1423     del document.header[i]
1424
1425     if not biblio_options:
1426         # Nothing to do for empty options
1427         return
1428
1429     i = find_token(document.header, "\\begin_local_layout", 0)
1430     if i == -1:
1431         k = find_token(document.header, "\\language", 0)
1432         if k == -1:
1433             # this should not happen
1434             document.warning("Malformed LyX document! No \\language header found!")
1435             return
1436         document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1437         i = k - 1
1438
1439     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1440     if j == -1:
1441         # this should not happen
1442         document.warning("Malformed LyX document! Can't find end of local layout!")
1443         return
1444
1445     document.header[i+1 : i+1] = [
1446         "### Inserted by lyx2lyx (bibliography package options) ###",
1447         "PackageOptions " + engine + " " + biblio_options,
1448         "### End of insertion by lyx2lyx (bibliography package options) ###"
1449     ]
1450
1451
1452 def revert_qualicites(document):
1453     " Revert qualified citation list commands to ERT "
1454
1455     # Citation insets that support qualified lists, with their LaTeX code
1456     ql_citations = {
1457         "cite" : "cites",
1458         "Cite" : "Cites",
1459         "citet" : "textcites",
1460         "Citet" : "Textcites",
1461         "citep" : "parencites",
1462         "Citep" : "Parencites",
1463         "Footcite" : "Smartcites",
1464         "footcite" : "smartcites",
1465         "Autocite" : "Autocites",
1466         "autocite" : "autocites",
1467         }
1468
1469     # Get cite engine
1470     engine = "basic"
1471     i = find_token(document.header, "\\cite_engine", 0)
1472     if i == -1:
1473         document.warning("Malformed document! Missing \\cite_engine")
1474     else:
1475         engine = get_value(document.header, "\\cite_engine", i)
1476
1477     biblatex = engine in ["biblatex", "biblatex-natbib"]
1478
1479     i = 0
1480     while (True):
1481         i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1482         if i == -1:
1483             break
1484         j = find_end_of_inset(document.body, i)
1485         if j == -1:
1486             document.warning("Can't find end of citation inset at line %d!!" %(i))
1487             i += 1
1488             continue
1489         pres = find_token(document.body, "pretextlist", i, j)
1490         posts = find_token(document.body, "posttextlist", i, j)
1491         if pres == -1 and posts == -1:
1492             # nothing to do.
1493             i = j + 1
1494             continue
1495         pretexts = get_quoted_value(document.body, "pretextlist", pres)
1496         posttexts = get_quoted_value(document.body, "posttextlist", posts)
1497         k = find_token(document.body, "LatexCommand", i, j)
1498         if k == -1:
1499             document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1500             i = j + 1
1501             continue
1502         cmd = get_value(document.body, "LatexCommand", k)
1503         if biblatex and cmd in list(ql_citations.keys()):
1504             pre = get_quoted_value(document.body, "before", i, j)
1505             post = get_quoted_value(document.body, "after", i, j)
1506             key = get_quoted_value(document.body, "key", i, j)
1507             if not key:
1508                 document.warning("Citation inset at line %d does not have a key!" %(i))
1509                 key = "???"
1510             keys = key.split(",")
1511             prelist = pretexts.split("\t")
1512             premap = dict()
1513             for pp in prelist:
1514                 ppp = pp.split(" ", 1)
1515                 premap[ppp[0]] = ppp[1]
1516             postlist = posttexts.split("\t")
1517             postmap = dict()
1518             for pp in postlist:
1519                 ppp = pp.split(" ", 1)
1520                 postmap[ppp[0]] = ppp[1]
1521             # Replace known new commands with ERT
1522             if "(" in pre or ")" in pre:
1523                 pre = "{" + pre + "}"
1524             if "(" in post or ")" in post:
1525                 post = "{" + post + "}"
1526             res = "\\" + ql_citations[cmd]
1527             if pre:
1528                 res += "(" + pre + ")"
1529             if post:
1530                 res += "(" + post + ")"
1531             elif pre:
1532                 res += "()"
1533             for kk in keys:
1534                 if premap.get(kk, "") != "":
1535                     res += "[" + premap[kk] + "]"
1536                 if postmap.get(kk, "") != "":
1537                     res += "[" + postmap[kk] + "]"
1538                 elif premap.get(kk, "") != "":
1539                     res += "[]"
1540                 res += "{" + kk + "}"
1541             document.body[i:j+1] = put_cmd_in_ert([res])
1542         else:
1543             # just remove the params
1544             del document.body[posttexts]
1545             del document.body[pretexts]
1546             i += 1
1547
1548
1549 command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
1550 def convert_literalparam(document):
1551     " Add param literal "
1552
1553     for inset in command_insets:
1554         i = 0
1555         while True:
1556             i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1557             if i == -1:
1558                 break
1559             j = find_end_of_inset(document.body, i)
1560             if j == -1:
1561                 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1562                 i += 1
1563                 continue
1564             while i < j and document.body[i].strip() != '':
1565                 i += 1
1566             # href is already fully latexified. Here we can switch off literal.
1567             if inset == "href":
1568                 document.body.insert(i, "literal \"false\"")
1569             else:
1570                 document.body.insert(i, "literal \"true\"")
1571
1572
1573
1574 def revert_literalparam(document):
1575     " Remove param literal "
1576
1577     for inset in command_insets:
1578         i = 0
1579         while True:
1580             i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1581             if i == -1:
1582                 break
1583             j = find_end_of_inset(document.body, i)
1584             if j == -1:
1585                 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1586                 i += 1
1587                 continue
1588             k = find_token(document.body, 'literal', i, j)
1589             if k == -1:
1590                 i += 1
1591                 continue
1592             del document.body[k]
1593
1594
1595
1596 def revert_multibib(document):
1597     " Revert multibib support "
1598
1599     # 1. Get cite engine
1600     engine = "basic"
1601     i = find_token(document.header, "\\cite_engine", 0)
1602     if i == -1:
1603         document.warning("Malformed document! Missing \\cite_engine")
1604     else:
1605         engine = get_value(document.header, "\\cite_engine", i)
1606
1607     # 2. Do we use biblatex?
1608     biblatex = False
1609     if engine in ["biblatex", "biblatex-natbib"]:
1610         biblatex = True
1611
1612     # 3. Store and remove multibib document header
1613     multibib = ""
1614     i = find_token(document.header, "\\multibib", 0)
1615     if i != -1:
1616         multibib = get_value(document.header, "\\multibib", i)
1617         del document.header[i]
1618
1619     if not multibib:
1620         return
1621
1622     # 4. The easy part: Biblatex
1623     if biblatex:
1624         i = find_token(document.header, "\\biblio_options", 0)
1625         if i == -1:
1626             k = find_token(document.header, "\\use_bibtopic", 0)
1627             if k == -1:
1628                 # this should not happen
1629                 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1630                 return
1631             document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
1632         else:
1633             biblio_options = get_value(document.header, "\\biblio_options", i)
1634             if biblio_options:
1635                 biblio_options += ","
1636             biblio_options += "refsection=" + multibib
1637             document.header[i] = "\\biblio_options " + biblio_options
1638
1639         # Bibtex insets
1640         i = 0
1641         while (True):
1642             i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1643             if i == -1:
1644                 break
1645             j = find_end_of_inset(document.body, i)
1646             if j == -1:
1647                 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1648                 i += 1
1649                 continue
1650             btprint = get_quoted_value(document.body, "btprint", i, j)
1651             if btprint != "bibbysection":
1652                 i += 1
1653                 continue
1654             opts = get_quoted_value(document.body, "biblatexopts", i, j)
1655             # change btprint line
1656             k = find_token(document.body, "btprint", i, j)
1657             if k != -1:
1658                 document.body[k] = "btprint \"btPrintCited\""
1659             # Insert ERT \\bibbysection and wrap bibtex inset to a Note
1660             pcmd = "bibbysection"
1661             if opts:
1662                 pcmd += "[" + opts + "]"
1663             repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1664                     "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1665                     "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1666                     "status open", "", "\\begin_layout Plain Layout" ]
1667             repl += document.body[i:j+1]
1668             repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1669             document.body[i:j+1] = repl
1670             j += 27
1671
1672             i = j + 1
1673         return
1674
1675     # 5. More tricky: Bibtex/Bibtopic
1676     k = find_token(document.header, "\\use_bibtopic", 0)
1677     if k == -1:
1678         # this should not happen
1679         document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1680         return
1681     document.header[k] = "\\use_bibtopic true"
1682
1683     # Possible units. This assumes that the LyX name follows the std,
1684     # which might not always be the case. But it's as good as we can get.
1685     units = {
1686         "part" : "Part",
1687         "chapter" : "Chapter",
1688         "section" : "Section",
1689         "subsection" : "Subsection",
1690         }
1691
1692     if multibib not in units.keys():
1693         document.warning("Unknown multibib value `%s'!" % nultibib)
1694         return
1695     unit = units[multibib]
1696     btunit = False
1697     i = 0
1698     while (True):
1699         i = find_token(document.body, "\\begin_layout " + unit, i)
1700         if i == -1:
1701             break
1702         if btunit:
1703             document.body[i-1 : i-1] = ["\\begin_layout Standard",
1704                                 "\\begin_inset ERT", "status open", "",
1705                                 "\\begin_layout Plain Layout", "", "",
1706                                 "\\backslash",
1707                                 "end{btUnit}", "\\end_layout",
1708                                 "\\begin_layout Plain Layout", "",
1709                                 "\\backslash",
1710                                 "begin{btUnit}"
1711                                 "\\end_layout", "", "\\end_inset", "", "",
1712                                 "\\end_layout", ""]
1713             i += 21
1714         else:
1715             document.body[i-1 : i-1] = ["\\begin_layout Standard",
1716                                 "\\begin_inset ERT", "status open", "",
1717                                 "\\begin_layout Plain Layout", "", "",
1718                                 "\\backslash",
1719                                 "begin{btUnit}"
1720                                 "\\end_layout", "", "\\end_inset", "", "",
1721                                 "\\end_layout", ""]
1722             i += 16
1723         btunit = True
1724         i += 1
1725
1726     if btunit:
1727         i = find_token(document.body, "\\end_body", i)
1728         document.body[i-1 : i-1] = ["\\begin_layout Standard",
1729                                 "\\begin_inset ERT", "status open", "",
1730                                 "\\begin_layout Plain Layout", "", "",
1731                                 "\\backslash",
1732                                 "end{btUnit}"
1733                                 "\\end_layout", "", "\\end_inset", "", "",
1734                                 "\\end_layout", ""]
1735
1736
1737 def revert_chapterbib(document):
1738     " Revert chapterbib support "
1739
1740     # 1. Get cite engine
1741     engine = "basic"
1742     i = find_token(document.header, "\\cite_engine", 0)
1743     if i == -1:
1744         document.warning("Malformed document! Missing \\cite_engine")
1745     else:
1746         engine = get_value(document.header, "\\cite_engine", i)
1747
1748     # 2. Do we use biblatex?
1749     biblatex = False
1750     if engine in ["biblatex", "biblatex-natbib"]:
1751         biblatex = True
1752
1753     # 3. Store multibib document header value
1754     multibib = ""
1755     i = find_token(document.header, "\\multibib", 0)
1756     if i != -1:
1757         multibib = get_value(document.header, "\\multibib", i)
1758
1759     if not multibib or multibib != "child":
1760         # nothing to do
1761         return
1762
1763     # 4. remove multibib header
1764     del document.header[i]
1765
1766     # 5. Biblatex
1767     if biblatex:
1768         # find include insets
1769         i = 0
1770         while (True):
1771             i = find_token(document.body, "\\begin_inset CommandInset include", i)
1772             if i == -1:
1773                 break
1774             j = find_end_of_inset(document.body, i)
1775             if j == -1:
1776                 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1777                 i += 1
1778                 continue
1779             parent = get_containing_layout(document.body, i)
1780             parbeg = parent[1]
1781
1782             # Insert ERT \\newrefsection before inset
1783             beg = ["\\begin_layout Standard",
1784                    "\\begin_inset ERT", "status open", "",
1785                    "\\begin_layout Plain Layout", "", "",
1786                    "\\backslash",
1787                    "newrefsection"
1788                    "\\end_layout", "", "\\end_inset", "", "",
1789                    "\\end_layout", ""]
1790             document.body[parbeg-1:parbeg-1] = beg
1791             j += len(beg)
1792             i = j + 1
1793         return
1794
1795     # 6. Bibtex/Bibtopic
1796     i = find_token(document.header, "\\use_bibtopic", 0)
1797     if i == -1:
1798         # this should not happen
1799         document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1800         return
1801     if get_value(document.header, "\\use_bibtopic", i) == "true":
1802         # find include insets
1803         i = 0
1804         while (True):
1805             i = find_token(document.body, "\\begin_inset CommandInset include", i)
1806             if i == -1:
1807                 break
1808             j = find_end_of_inset(document.body, i)
1809             if j == -1:
1810                 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1811                 i += 1
1812                 continue
1813             parent = get_containing_layout(document.body, i)
1814             parbeg = parent[1]
1815             parend = parent[2]
1816
1817             # Insert wrap inset into \\begin{btUnit}...\\end{btUnit}
1818             beg = ["\\begin_layout Standard",
1819                    "\\begin_inset ERT", "status open", "",
1820                    "\\begin_layout Plain Layout", "", "",
1821                    "\\backslash",
1822                    "begin{btUnit}"
1823                    "\\end_layout", "", "\\end_inset", "", "",
1824                    "\\end_layout", ""]
1825             end = ["\\begin_layout Standard",
1826                    "\\begin_inset ERT", "status open", "",
1827                    "\\begin_layout Plain Layout", "", "",
1828                    "\\backslash",
1829                    "end{btUnit}"
1830                    "\\end_layout", "", "\\end_inset", "", "",
1831                    "\\end_layout", ""]
1832             document.body[parend+1:parend+1] = end
1833             document.body[parbeg-1:parbeg-1] = beg
1834             j += len(beg) + len(end)
1835             i = j + 1
1836         return
1837
1838     # 7. Chapterbib proper
1839     add_to_preamble(document, ["\\usepackage{chapterbib}"])
1840
1841
1842 def convert_dashligatures(document):
1843     " Remove a zero-length space (U+200B) after en- and em-dashes. "
1844
1845     i = find_token(document.header, "\\use_microtype", 0)
1846     if i != -1:
1847         if document.initial_format > 474 and document.initial_format < 509:
1848             # This was created by LyX 2.2
1849             document.header[i+1:i+1] = ["\\use_dash_ligatures false"]
1850         else:
1851             # This was created by LyX 2.1 or earlier
1852             document.header[i+1:i+1] = ["\\use_dash_ligatures true"]
1853
1854     i = 0
1855     while i < len(document.body):
1856         words = document.body[i].split()
1857         # Skip some document parts where dashes are not converted
1858         if len(words) > 1 and words[0] == "\\begin_inset" and \
1859            words[1] in ["CommandInset", "ERT", "External", "Formula", \
1860                         "FormulaMacro", "Graphics", "IPA", "listings"]:
1861             j = find_end_of_inset(document.body, i)
1862             if j == -1:
1863                 document.warning("Malformed LyX document: Can't find end of " \
1864                                  + words[1] + " inset at line " + str(i))
1865                 i += 1
1866             else:
1867                 i = j
1868             continue
1869         if len(words) > 0 and words[0] in ["\\leftindent", \
1870                 "\\paragraph_spacing", "\\align", "\\labelwidthstring"]:
1871             i += 1
1872             continue
1873
1874         start = 0
1875         while True:
1876             j = document.body[i].find(u"\u2013", start) # en-dash
1877             k = document.body[i].find(u"\u2014", start) # em-dash
1878             if j == -1 and k == -1:
1879                 break
1880             if j == -1 or (k != -1 and k < j):
1881                 j = k
1882             after = document.body[i][j+1:]
1883             if after.startswith(u"\u200B"):
1884                 document.body[i] = document.body[i][:j+1] + after[1:]
1885             else:
1886                 if len(after) == 0 and document.body[i+1].startswith(u"\u200B"):
1887                     document.body[i+1] = document.body[i+1][1:]
1888                     break
1889             start = j+1
1890         i += 1
1891
1892
1893 def revert_dashligatures(document):
1894     " Remove font ligature settings for en- and em-dashes. "
1895     i = find_token(document.header, "\\use_dash_ligatures", 0)
1896     if i == -1:
1897         return
1898     use_dash_ligatures = get_bool_value(document.header, "\\use_dash_ligatures", i)
1899     del document.header[i]
1900     use_non_tex_fonts = False
1901     i = find_token(document.header, "\\use_non_tex_fonts", 0)
1902     if i != -1:
1903         use_non_tex_fonts = get_bool_value(document.header, "\\use_non_tex_fonts", i)
1904     if not use_dash_ligatures or use_non_tex_fonts:
1905         return
1906
1907     # Add a zero-length space (U+200B) after en- and em-dashes
1908     i = 0
1909     while i < len(document.body):
1910         words = document.body[i].split()
1911         # Skip some document parts where dashes are not converted
1912         if len(words) > 1 and words[0] == "\\begin_inset" and \
1913            words[1] in ["CommandInset", "ERT", "External", "Formula", \
1914                         "FormulaMacro", "Graphics", "IPA", "listings"]:
1915             j = find_end_of_inset(document.body, i)
1916             if j == -1:
1917                 document.warning("Malformed LyX document: Can't find end of " \
1918                                  + words[1] + " inset at line " + str(i))
1919                 i += 1
1920             else:
1921                 i = j
1922             continue
1923         if len(words) > 0 and words[0] in ["\\leftindent", \
1924                 "\\paragraph_spacing", "\\align", "\\labelwidthstring"]:
1925             i += 1
1926             continue
1927
1928         start = 0
1929         while True:
1930             j = document.body[i].find(u"\u2013", start) # en-dash
1931             k = document.body[i].find(u"\u2014", start) # em-dash
1932             if j == -1 and k == -1:
1933                 break
1934             if j == -1 or (k != -1 and k < j):
1935                 j = k
1936             after = document.body[i][j+1:]
1937             document.body[i] = document.body[i][:j+1] + u"\u200B" + after
1938             start = j+1
1939         i += 1
1940
1941
1942 def revert_noto(document):
1943     " Revert Noto font definitions to LaTeX "
1944
1945     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1946         preamble = ""
1947         i = find_token(document.header, "\\font_roman \"NotoSerif-TLF\"", 0)
1948         if i != -1:
1949             add_to_preamble(document, ["\\renewcommand{\\rmdefault}{NotoSerif-TLF}"])
1950             document.header[i] = document.header[i].replace("NotoSerif-TLF", "default")
1951         i = find_token(document.header, "\\font_sans \"NotoSans-TLF\"", 0)
1952         if i != -1:
1953             add_to_preamble(document, ["\\renewcommand{\\sfdefault}{NotoSans-TLF}"])
1954             document.header[i] = document.header[i].replace("NotoSans-TLF", "default")
1955         i = find_token(document.header, "\\font_typewriter \"NotoMono-TLF\"", 0)
1956         if i != -1:
1957             add_to_preamble(document, ["\\renewcommand{\\ttdefault}{NotoMono-TLF}"])
1958             document.header[i] = document.header[i].replace("NotoMono-TLF", "default")
1959
1960
1961 def revert_xout(document):
1962   " Reverts \\xout font attribute "
1963   changed = revert_font_attrs(document.body, "\\xout", "\\xout")
1964   if changed == True:
1965     insert_to_preamble(document, \
1966         ['%  for proper cross-out',
1967         '\\PassOptionsToPackage{normalem}{ulem}',
1968         '\\usepackage{ulem}'])
1969
1970
1971 def convert_mathindent(document):
1972     " add the \\is_math_indent tag "
1973     # check if the document uses the class option "fleqn"
1974     k = find_token(document.header, "\\quotes_style", 0)
1975     regexp = re.compile(r'^.*fleqn.*')
1976     i = find_re(document.header, regexp, 0)
1977     if i != -1:
1978         document.header.insert(k, "\\is_math_indent 1")
1979         # delete the found option
1980         document.header[i] = document.header[i].replace(",fleqn", "")
1981         document.header[i] = document.header[i].replace(", fleqn", "")
1982         document.header[i] = document.header[i].replace("fleqn,", "")
1983         j = find_re(document.header, regexp, 0)
1984         if i == j:
1985             # then we have fleqn as the only option
1986             del document.header[i]
1987     else:
1988         document.header.insert(k, "\\is_math_indent 0")
1989
1990
1991 def revert_mathindent(document):
1992     " Define mathindent if set in the document "
1993     # first output the length
1994     regexp = re.compile(r'(\\math_indentation)')
1995     i = find_re(document.header, regexp, 0)
1996     if i != -1:
1997         value = get_value(document.header, "\\math_indentation" , i).split()[0]
1998         add_to_preamble(document, ["\\setlength{\\mathindent}{" + value + '}'])
1999         del document.header[i]
2000     # now set the document class option
2001     regexp = re.compile(r'(\\is_math_indent 1)')
2002     i = find_re(document.header, regexp, 0)
2003     if i == -1:
2004         regexp = re.compile(r'(\\is_math_indent)')
2005         j = find_re(document.header, regexp, 0)
2006         del document.header[j]
2007     else:
2008         k = find_token(document.header, "\\options", 0)
2009         if k != -1:
2010             document.header[k] = document.header[k].replace("\\options", "\\options fleqn,")
2011             del document.header[i]
2012         else:
2013             l = find_token(document.header, "\\use_default_options", 0)
2014             document.header.insert(l, "\\options fleqn")
2015             del document.header[i + 1]
2016
2017
2018 def revert_baselineskip(document):
2019   " Revert baselineskips to TeX code "
2020   i = 0
2021   vspaceLine = 0
2022   hspaceLine = 0
2023   while True:
2024     regexp = re.compile(r'^.*baselineskip%.*$')
2025     i = find_re(document.body, regexp, i)
2026     if i == -1:
2027       return
2028     vspaceLine = find_token(document.body, "\\begin_inset VSpace", i)
2029     if  vspaceLine == i:
2030       # output VSpace inset as TeX code
2031       # first read out the values
2032       beg = document.body[i].rfind("VSpace ");
2033       end = document.body[i].rfind("baselineskip%");
2034       baselineskip = float(document.body[i][beg + 7:end]);
2035       # we store the value in percent, thus divide by 100
2036       baselineskip = baselineskip/100;
2037       baselineskip = str(baselineskip);
2038       # check if it is the starred version
2039       if document.body[i].find('*') != -1:
2040         star = '*'
2041       else:
2042         star = ''
2043       # now output TeX code
2044       endInset = find_end_of_inset(document.body, i)
2045       if endInset == -1:
2046         document.warning("Malformed LyX document: Missing '\\end_inset' of VSpace inset.")
2047         return
2048       else:
2049         document.body[vspaceLine: endInset + 1] = put_cmd_in_ert("\\vspace" + star + '{' + baselineskip + "\\baselineskip}")
2050     hspaceLine = find_token(document.body, "\\begin_inset space \\hspace", i - 1)
2051     document.warning("hspaceLine: " + str(hspaceLine))
2052     document.warning("i: " + str(i))
2053     if  hspaceLine == i - 1:
2054       # output space inset as TeX code
2055       # first read out the values
2056       beg = document.body[i].rfind("\\length ");
2057       end = document.body[i].rfind("baselineskip%");
2058       baselineskip = float(document.body[i][beg + 7:end]);
2059       document.warning("baselineskip: " + str(baselineskip))
2060       # we store the value in percent, thus divide by 100
2061       baselineskip = baselineskip/100;
2062       baselineskip = str(baselineskip);
2063       # check if it is the starred version
2064       if document.body[i-1].find('*') != -1:
2065         star = '*'
2066       else:
2067         star = ''
2068       # now output TeX code
2069       endInset = find_end_of_inset(document.body, i)
2070       if endInset == -1:
2071         document.warning("Malformed LyX document: Missing '\\end_inset' of space inset.")
2072         return
2073       else:
2074         document.body[hspaceLine: endInset + 1] = put_cmd_in_ert("\\hspace" + star + '{' + baselineskip + "\\baselineskip}")
2075
2076     i = i + 1
2077
2078
2079 def revert_rotfloat(document):
2080   " Revert placement options for rotated floats "
2081   i = 0
2082   j = 0
2083   k = 0
2084   while True:
2085     i = find_token(document.body, "sideways true", i)
2086     if i != -1:
2087       regexp = re.compile(r'^.*placement.*$')
2088       j = find_re(document.body, regexp, i-2)
2089       if j == -1:
2090           return
2091       if j != i-2:
2092           i = i + 1
2093           continue
2094     else:
2095       return
2096     # we found a sideways float with placement options
2097     # at first store the placement
2098     beg = document.body[i-2].rfind(" ");
2099     placement = document.body[i-2][beg+1:]
2100     # check if the option'H' is used
2101     if placement.find("H") != -1:
2102       add_to_preamble(document, ["\\usepackage{float}"])
2103     # now check if it is a starred type
2104     if document.body[i-1].find("wide true") != -1:
2105       star = '*'
2106     else:
2107       star = ''
2108     # store the float type
2109     beg = document.body[i-3].rfind(" ");
2110     fType = document.body[i-3][beg+1:]
2111     # now output TeX code
2112     endInset = find_end_of_inset(document.body, i-3)
2113     if endInset == -1:
2114       document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
2115       return
2116     else:
2117       document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
2118       document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
2119       add_to_preamble(document, ["\\usepackage{rotfloat}"])
2120
2121     i = i + 1
2122
2123
2124 def convert_allowbreak(document):
2125     " Zero widths Space-inset -> \SpecialChar allowbreak. "
2126     body = "\n".join(document.body)
2127     body = body.replace("\\begin_inset space \hspace{}\n"
2128                         "\\length 0dd\n"
2129                         "\\end_inset\n\n",
2130                         "\\SpecialChar allowbreak\n")
2131     document.body = body.split("\n")
2132
2133
2134 def revert_allowbreak(document):
2135     " \SpecialChar allowbreak -> Zero widths Space-inset. "
2136     body = "\n".join(document.body)
2137     body = body.replace("\\SpecialChar allowbreak\n",
2138                         "\\begin_inset space \hspace{}\n"
2139                         "\\length 0dd\n"
2140                         "\\end_inset\n\n")
2141     document.body = body.split("\n")
2142
2143
2144 def convert_mathnumberpos(document):
2145     " add the \\math_number_before tag "
2146     # check if the document uses the class option "leqno"
2147     k = find_token(document.header, "\\quotes_style", 0)
2148     regexp = re.compile(r'^.*leqno.*')
2149     i = find_re(document.header, regexp, 0)
2150     if i != -1:
2151         document.header.insert(k, "\\math_number_before 1")
2152         # delete the found option
2153         document.header[i] = document.header[i].replace(",leqno", "")
2154         document.header[i] = document.header[i].replace(", leqno", "")
2155         document.header[i] = document.header[i].replace("leqno,", "")
2156         j = find_re(document.header, regexp, 0)
2157         if i == j:
2158             # then we have leqno as the only option
2159             del document.header[i]
2160     else:
2161         document.header.insert(k, "\\math_number_before 0")
2162
2163
2164 def revert_mathnumberpos(document):
2165     " add the document class option leqno"
2166     regexp = re.compile(r'(\\math_number_before 1)')
2167     i = find_re(document.header, regexp, 0)
2168     if i == -1:
2169         regexp = re.compile(r'(\\math_number_before)')
2170         j = find_re(document.header, regexp, 0)
2171         del document.header[j]
2172     else:
2173         k = find_token(document.header, "\\options", 0)
2174         if k != -1:
2175             document.header[k] = document.header[k].replace("\\options", "\\options leqno,")
2176             del document.header[i]
2177         else:
2178             l = find_token(document.header, "\\use_default_options", 0)
2179             document.header.insert(l, "\\options leqno")
2180             del document.header[i + 1]
2181
2182
2183 def convert_mathnumberingname(document):
2184     " rename the \\math_number_before tag to \\math_numbering_side "
2185     document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
2186     regexp = re.compile(r'(\\math_number_before 1)')
2187     i = find_re(document.header, regexp, 0)
2188     if i != -1:
2189         document.header[i] = "\\math_numbering_side left"
2190     regexp = re.compile(r'(\\math_number_before 0)')
2191     i = find_re(document.header, regexp, 0)
2192     if i != -1:
2193         document.header[i] = "\\math_numbering_side default"
2194     # check if the document uses the class option "reqno"
2195     k = find_token(document.header, "\\math_numbering_side", 0)
2196     regexp = re.compile(r'^.*reqno.*')
2197     i = find_re(document.header, regexp, 0)
2198     if i != -1:
2199         document.header[k] = "\\math_numbering_side right"
2200         # delete the found option
2201         document.header[i] = document.header[i].replace(",reqno", "")
2202         document.header[i] = document.header[i].replace(", reqno", "")
2203         document.header[i] = document.header[i].replace("reqno,", "")
2204         j = find_re(document.header, regexp, 0)
2205         if i == j:
2206             # then we have reqno as the only option
2207             del document.header[i]
2208
2209
2210 def revert_mathnumberingname(document):
2211     " rename the \\math_numbering_side tag back to \\math_number_before "
2212     # just rename
2213     regexp = re.compile(r'(\\math_numbering_side left)')
2214     i = find_re(document.header, regexp, 0)
2215     if i != -1:
2216         document.header[i] = "\\math_number_before 1"
2217     # add the option reqno and delete the tag
2218     regexp = re.compile(r'(\\math_numbering_side right)')
2219     i = find_re(document.header, regexp, 0)
2220     if i != -1:
2221         document.header[i] = "\\math_number_before 0"
2222         k = find_token(document.header, "\\options", 0)
2223         if k != -1:
2224             document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
2225         else:
2226             l = find_token(document.header, "\\use_default_options", 0)
2227             document.header.insert(l, "\\options reqno")
2228     # add the math_number_before tag   
2229     regexp = re.compile(r'(\\math_numbering_side default)')
2230     i = find_re(document.header, regexp, 0)
2231     if i != -1:
2232         document.header[i] = "\\math_number_before 0"
2233
2234
2235 ##
2236 # Conversion hub
2237 #
2238
2239 supported_versions = ["2.3.0", "2.3"]
2240 convert = [
2241            [509, [convert_microtype]],
2242            [510, [convert_dateinset]],
2243            [511, [convert_ibranches]],
2244            [512, [convert_beamer_article_styles]],
2245            [513, []],
2246            [514, []],
2247            [515, []],
2248            [516, [convert_inputenc]],
2249            [517, []],
2250            [518, [convert_iopart]],
2251            [519, [convert_quotestyle]],
2252            [520, []],
2253            [521, [convert_frenchquotes]],
2254            [522, []],
2255            [523, []],
2256            [524, []],
2257            [525, []],
2258            [526, []],
2259            [527, []],
2260            [528, []],
2261            [529, []],
2262            [530, []],
2263            [531, []],
2264            [532, [convert_literalparam]],
2265            [533, []],
2266            [534, []],
2267            [535, [convert_dashligatures]],
2268            [536, []],
2269            [537, []],
2270            [538, [convert_mathindent]],
2271            [539, []],
2272            [540, []],
2273            [541, [convert_allowbreak]],
2274            [542, [convert_mathnumberpos]],
2275            [543, [convert_mathnumberingname]]
2276           ]
2277
2278 revert =  [
2279            [542, [revert_mathnumberingname]],
2280            [541, [revert_mathnumberpos]],
2281            [540, [revert_allowbreak]],
2282            [539, [revert_rotfloat]],
2283            [538, [revert_baselineskip]],
2284            [537, [revert_mathindent]],
2285            [536, [revert_xout]],
2286            [535, [revert_noto]],
2287            [534, [revert_dashligatures]],
2288            [533, [revert_chapterbib]],
2289            [532, [revert_multibib]],
2290            [531, [revert_literalparam]],
2291            [530, [revert_qualicites]],
2292            [529, [revert_bibpackopts]],
2293            [528, [revert_citekeyonly]],
2294            [527, [revert_biblatex]],
2295            [526, [revert_noprefix]],
2296            [525, [revert_plural_refs]],
2297            [524, [revert_labelonly]],
2298            [523, [revert_crimson, revert_cochinealmath]],
2299            [522, [revert_cjkquotes]],
2300            [521, [revert_dynamicquotes]],
2301            [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
2302            [519, [revert_plainquote]],
2303            [518, [revert_quotestyle]],
2304            [517, [revert_iopart]],
2305            [516, [revert_quotes]],
2306            [515, []],
2307            [514, [revert_urdu, revert_syriac]],
2308            [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
2309            [512, [revert_bosnian, revert_friulan, revert_macedonian, revert_piedmontese, revert_romansh]],
2310            [511, [revert_beamer_article_styles]],
2311            [510, [revert_ibranches]],
2312            [509, []],
2313            [508, [revert_microtype]]
2314           ]
2315
2316
2317 if __name__ == "__main__":
2318     pass