]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_3.py
Update fr.po
[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         noprefix = False
1150         if k != -1:
1151             noprefix = get_bool_value(document.body, "noprefix", i, j)
1152         if not noprefix:
1153             # either it was not a labelonly command, or else noprefix was not set.
1154             # in that case, we just delete the option.
1155             del_token(document.body, "noprefix", i, j)
1156             i = j
1157             continue
1158         label = get_quoted_value(document.body, "reference", i, j)
1159         if not label:
1160             document.warning("Can't find label for reference at line %d!" %(i))
1161             i = j + 1
1162             continue
1163         try:
1164             (prefix, suffix) = label.split(":", 1)
1165         except:
1166             document.warning("No `:' separator in formatted reference at line %d!" % (i))
1167             # we'll leave this as an ordinary labelonly reference
1168             del_token(document.body, "noprefix", i, j)
1169             i = j
1170             continue
1171         document.body[i:j+1] = put_cmd_in_ert([suffix])
1172         i += 1
1173
1174
1175 def revert_biblatex(document):
1176     " Revert biblatex support "
1177
1178     #
1179     # Header
1180     #
1181
1182     # 1. Get cite engine
1183     engine = "basic"
1184     i = find_token(document.header, "\\cite_engine", 0)
1185     if i == -1:
1186         document.warning("Malformed document! Missing \\cite_engine")
1187     else:
1188         engine = get_value(document.header, "\\cite_engine", i)
1189
1190     # 2. Store biblatex state and revert to natbib
1191     biblatex = False
1192     if engine in ["biblatex", "biblatex-natbib"]:
1193         biblatex = True
1194         document.header[i] = "\\cite_engine natbib"
1195
1196     # 3. Store and remove new document headers
1197     bibstyle = ""
1198     i = find_token(document.header, "\\biblatex_bibstyle", 0)
1199     if i != -1:
1200         bibstyle = get_value(document.header, "\\biblatex_bibstyle", i)
1201         del document.header[i]
1202
1203     citestyle = ""
1204     i = find_token(document.header, "\\biblatex_citestyle", 0)
1205     if i != -1:
1206         citestyle = get_value(document.header, "\\biblatex_citestyle", i)
1207         del document.header[i]
1208
1209     biblio_options = ""
1210     i = find_token(document.header, "\\biblio_options", 0)
1211     if i != -1:
1212         biblio_options = get_value(document.header, "\\biblio_options", i)
1213         del document.header[i]
1214
1215     if biblatex:
1216         bbxopts = "[natbib=true"
1217         if bibstyle != "":
1218             bbxopts += ",bibstyle=" + bibstyle
1219         if citestyle != "":
1220             bbxopts += ",citestyle=" + citestyle
1221         if biblio_options != "":
1222             bbxopts += "," + biblio_options
1223         bbxopts += "]"
1224         add_to_preamble(document, "\\usepackage" + bbxopts + "{biblatex}")
1225
1226     #
1227     # Body
1228     #
1229
1230     # 1. Bibtex insets
1231     i = 0
1232     bibresources = []
1233     while (True):
1234         i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1235         if i == -1:
1236             break
1237         j = find_end_of_inset(document.body, i)
1238         if j == -1:
1239             document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1240             i += 1
1241             continue
1242         bibs = get_quoted_value(document.body, "bibfiles", i, j)
1243         opts = get_quoted_value(document.body, "biblatexopts", i, j)
1244         # store resources
1245         if bibs:
1246             bibresources += bibs.split(",")
1247         else:
1248             document.warning("Can't find bibfiles for bibtex inset at line %d!" %(i))
1249         # remove biblatexopts line
1250         k = find_token(document.body, "biblatexopts", i, j)
1251         if k != -1:
1252             del document.body[k]
1253         # Re-find inset end line
1254         j = find_end_of_inset(document.body, i)
1255         # Insert ERT \\printbibliography and wrap bibtex inset to a Note
1256         if biblatex:
1257             pcmd = "printbibliography"
1258             if opts:
1259                 pcmd += "[" + opts + "]"
1260             repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1261                     "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1262                     "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1263                     "status open", "", "\\begin_layout Plain Layout" ]
1264             repl += document.body[i:j+1]
1265             repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1266             document.body[i:j+1] = repl
1267             j += 27
1268
1269         i = j + 1
1270
1271     if biblatex:
1272         for b in bibresources:
1273             add_to_preamble(document, "\\addbibresource{" + b + ".bib}")
1274
1275     # 2. Citation insets
1276
1277     # Specific citation insets used in biblatex that need to be reverted to ERT
1278     new_citations = {
1279         "Cite" : "Cite",
1280         "citebyear" : "citeyear",
1281         "citeyear" : "cite*",
1282         "Footcite" : "Smartcite",
1283         "footcite" : "smartcite",
1284         "Autocite" : "Autocite",
1285         "autocite" : "autocite",
1286         "citetitle" : "citetitle",
1287         "citetitle*" : "citetitle*",
1288         "fullcite" : "fullcite",
1289         "footfullcite" : "footfullcite",
1290         "supercite" : "supercite",
1291         "citeauthor" : "citeauthor",
1292         "citeauthor*" : "citeauthor*",
1293         "Citeauthor" : "Citeauthor",
1294         "Citeauthor*" : "Citeauthor*"
1295         }
1296
1297     # All commands accepted by LyX < 2.3. Everything else throws an error.
1298     old_citations = [ "cite", "nocite", "citet", "citep", "citealt", "citealp",\
1299                       "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*",\
1300                       "citealt*", "citealp*", "citeauthor*", "Citet",  "Citep",\
1301                       "Citealt",  "Citealp",  "Citeauthor", "Citet*", "Citep*",\
1302                       "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",\
1303                       "footcitet", "footcitep", "footcitealt", "footcitealp",\
1304                       "footciteauthor", "footciteyear", "footciteyearpar",\
1305                       "citefield", "citetitle", "cite*" ]
1306
1307     i = 0
1308     while (True):
1309         i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1310         if i == -1:
1311             break
1312         j = find_end_of_inset(document.body, i)
1313         if j == -1:
1314             document.warning("Can't find end of citation inset at line %d!!" %(i))
1315             i += 1
1316             continue
1317         k = find_token(document.body, "LatexCommand", i, j)
1318         if k == -1:
1319             document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1320             i = j + 1
1321             continue
1322         cmd = get_value(document.body, "LatexCommand", k)
1323         if biblatex and cmd in list(new_citations.keys()):
1324             pre = get_quoted_value(document.body, "before", i, j)
1325             post = get_quoted_value(document.body, "after", i, j)
1326             key = get_quoted_value(document.body, "key", i, j)
1327             if not key:
1328                 document.warning("Citation inset at line %d does not have a key!" %(i))
1329                 key = "???"
1330             # Replace known new commands with ERT
1331             res = "\\" + new_citations[cmd]
1332             if pre:
1333                 res += "[" + pre + "]"
1334             if post:
1335                 res += "[" + post + "]"
1336             elif pre:
1337                 res += "[]"
1338             res += "{" + key + "}"
1339             document.body[i:j+1] = put_cmd_in_ert([res])
1340         elif cmd not in old_citations:
1341             # Reset unknown commands to cite. This is what LyX does as well
1342             # (but LyX 2.2 would break on unknown commands)
1343             document.body[k] = "LatexCommand cite"
1344             document.warning("Reset unknown cite command '%s' with cite" % cmd)
1345         i = j + 1
1346
1347     # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
1348     if biblatex:
1349         i = find_token(document.header, "\\begin_local_layout", 0)
1350         if i == -1:
1351             k = find_token(document.header, "\\language", 0)
1352             if k == -1:
1353                 # this should not happen
1354                 document.warning("Malformed LyX document! No \\language header found!")
1355                 return
1356             document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1357             i = k-1
1358
1359         j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1360         if j == -1:
1361             # this should not happen
1362             document.warning("Malformed LyX document! Can't find end of local layout!")
1363             return
1364
1365         document.header[i+1 : i+1] = [
1366             "### Inserted by lyx2lyx (biblatex emulation) ###",
1367             "Provides natbib 1",
1368             "### End of insertion by lyx2lyx (biblatex emulation) ###"
1369         ]
1370
1371
1372 def revert_citekeyonly(document):
1373     " Revert keyonly cite command to ERT "
1374
1375     i = 0
1376     while (True):
1377         i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1378         if i == -1:
1379             break
1380         j = find_end_of_inset(document.body, i)
1381         if j == -1:
1382             document.warning("Can't find end of citation inset at line %d!!" %(i))
1383             i += 1
1384             continue
1385         k = find_token(document.body, "LatexCommand", i, j)
1386         if k == -1:
1387             document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1388             i = j + 1
1389             continue
1390         cmd = get_value(document.body, "LatexCommand", k)
1391         if cmd != "keyonly":
1392             i = j + 1
1393             continue
1394
1395         key = get_quoted_value(document.body, "key", i, j)
1396         if not key:
1397             document.warning("Citation inset at line %d does not have a key!" %(i))
1398         # Replace known new commands with ERT
1399         document.body[i:j+1] = put_cmd_in_ert([key])
1400         i = j + 1
1401
1402
1403
1404 def revert_bibpackopts(document):
1405     " Revert support for natbib/jurabib package options "
1406
1407     engine = "basic"
1408     i = find_token(document.header, "\\cite_engine", 0)
1409     if i == -1:
1410         document.warning("Malformed document! Missing \\cite_engine")
1411     else:
1412         engine = get_value(document.header, "\\cite_engine", i)
1413
1414     biblatex = False
1415     if engine not in ["natbib", "jurabib"]:
1416         return
1417
1418     i = find_token(document.header, "\\biblio_options", 0)
1419     if i == -1:
1420         # Nothing to do if we have no options
1421         return
1422
1423     biblio_options = get_value(document.header, "\\biblio_options", i)
1424     del document.header[i]
1425
1426     if not biblio_options:
1427         # Nothing to do for empty options
1428         return
1429
1430     i = find_token(document.header, "\\begin_local_layout", 0)
1431     if i == -1:
1432         k = find_token(document.header, "\\language", 0)
1433         if k == -1:
1434             # this should not happen
1435             document.warning("Malformed LyX document! No \\language header found!")
1436             return
1437         document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
1438         i = k - 1
1439
1440     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
1441     if j == -1:
1442         # this should not happen
1443         document.warning("Malformed LyX document! Can't find end of local layout!")
1444         return
1445
1446     document.header[i+1 : i+1] = [
1447         "### Inserted by lyx2lyx (bibliography package options) ###",
1448         "PackageOptions " + engine + " " + biblio_options,
1449         "### End of insertion by lyx2lyx (bibliography package options) ###"
1450     ]
1451
1452
1453 def revert_qualicites(document):
1454     " Revert qualified citation list commands to ERT "
1455
1456     # Citation insets that support qualified lists, with their LaTeX code
1457     ql_citations = {
1458         "cite" : "cites",
1459         "Cite" : "Cites",
1460         "citet" : "textcites",
1461         "Citet" : "Textcites",
1462         "citep" : "parencites",
1463         "Citep" : "Parencites",
1464         "Footcite" : "Smartcites",
1465         "footcite" : "smartcites",
1466         "Autocite" : "Autocites",
1467         "autocite" : "autocites",
1468         }
1469
1470     # Get cite engine
1471     engine = "basic"
1472     i = find_token(document.header, "\\cite_engine", 0)
1473     if i == -1:
1474         document.warning("Malformed document! Missing \\cite_engine")
1475     else:
1476         engine = get_value(document.header, "\\cite_engine", i)
1477
1478     biblatex = engine in ["biblatex", "biblatex-natbib"]
1479
1480     i = 0
1481     while (True):
1482         i = find_token(document.body, "\\begin_inset CommandInset citation", i)
1483         if i == -1:
1484             break
1485         j = find_end_of_inset(document.body, i)
1486         if j == -1:
1487             document.warning("Can't find end of citation inset at line %d!!" %(i))
1488             i += 1
1489             continue
1490         pres = find_token(document.body, "pretextlist", i, j)
1491         posts = find_token(document.body, "posttextlist", i, j)
1492         if pres == -1 and posts == -1:
1493             # nothing to do.
1494             i = j + 1
1495             continue
1496         pretexts = get_quoted_value(document.body, "pretextlist", pres)
1497         posttexts = get_quoted_value(document.body, "posttextlist", posts)
1498         k = find_token(document.body, "LatexCommand", i, j)
1499         if k == -1:
1500             document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
1501             i = j + 1
1502             continue
1503         cmd = get_value(document.body, "LatexCommand", k)
1504         if biblatex and cmd in list(ql_citations.keys()):
1505             pre = get_quoted_value(document.body, "before", i, j)
1506             post = get_quoted_value(document.body, "after", i, j)
1507             key = get_quoted_value(document.body, "key", i, j)
1508             if not key:
1509                 document.warning("Citation inset at line %d does not have a key!" %(i))
1510                 key = "???"
1511             keys = key.split(",")
1512             prelist = pretexts.split("\t")
1513             premap = dict()
1514             for pp in prelist:
1515                 ppp = pp.split(" ", 1)
1516                 premap[ppp[0]] = ppp[1]
1517             postlist = posttexts.split("\t")
1518             postmap = dict()
1519             for pp in postlist:
1520                 ppp = pp.split(" ", 1)
1521                 postmap[ppp[0]] = ppp[1]
1522             # Replace known new commands with ERT
1523             if "(" in pre or ")" in pre:
1524                 pre = "{" + pre + "}"
1525             if "(" in post or ")" in post:
1526                 post = "{" + post + "}"
1527             res = "\\" + ql_citations[cmd]
1528             if pre:
1529                 res += "(" + pre + ")"
1530             if post:
1531                 res += "(" + post + ")"
1532             elif pre:
1533                 res += "()"
1534             for kk in keys:
1535                 if premap.get(kk, "") != "":
1536                     res += "[" + premap[kk] + "]"
1537                 if postmap.get(kk, "") != "":
1538                     res += "[" + postmap[kk] + "]"
1539                 elif premap.get(kk, "") != "":
1540                     res += "[]"
1541                 res += "{" + kk + "}"
1542             document.body[i:j+1] = put_cmd_in_ert([res])
1543         else:
1544             # just remove the params
1545             del document.body[posttexts]
1546             del document.body[pretexts]
1547             i += 1
1548
1549
1550 command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
1551 def convert_literalparam(document):
1552     " Add param literal "
1553
1554     for inset in command_insets:
1555         i = 0
1556         while True:
1557             i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1558             if i == -1:
1559                 break
1560             j = find_end_of_inset(document.body, i)
1561             if j == -1:
1562                 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1563                 i += 1
1564                 continue
1565             while i < j and document.body[i].strip() != '':
1566                 i += 1
1567             # href is already fully latexified. Here we can switch off literal.
1568             if inset == "href":
1569                 document.body.insert(i, "literal \"false\"")
1570             else:
1571                 document.body.insert(i, "literal \"true\"")
1572
1573
1574
1575 def revert_literalparam(document):
1576     " Remove param literal "
1577
1578     for inset in command_insets:
1579         i = 0
1580         while True:
1581             i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
1582             if i == -1:
1583                 break
1584             j = find_end_of_inset(document.body, i)
1585             if j == -1:
1586                 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
1587                 i += 1
1588                 continue
1589             k = find_token(document.body, 'literal', i, j)
1590             if k == -1:
1591                 i += 1
1592                 continue
1593             del document.body[k]
1594
1595
1596
1597 def revert_multibib(document):
1598     " Revert multibib support "
1599
1600     # 1. Get cite engine
1601     engine = "basic"
1602     i = find_token(document.header, "\\cite_engine", 0)
1603     if i == -1:
1604         document.warning("Malformed document! Missing \\cite_engine")
1605     else:
1606         engine = get_value(document.header, "\\cite_engine", i)
1607
1608     # 2. Do we use biblatex?
1609     biblatex = False
1610     if engine in ["biblatex", "biblatex-natbib"]:
1611         biblatex = True
1612
1613     # 3. Store and remove multibib document header
1614     multibib = ""
1615     i = find_token(document.header, "\\multibib", 0)
1616     if i != -1:
1617         multibib = get_value(document.header, "\\multibib", i)
1618         del document.header[i]
1619
1620     if not multibib:
1621         return
1622
1623     # 4. The easy part: Biblatex
1624     if biblatex:
1625         i = find_token(document.header, "\\biblio_options", 0)
1626         if i == -1:
1627             k = find_token(document.header, "\\use_bibtopic", 0)
1628             if k == -1:
1629                 # this should not happen
1630                 document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1631                 return
1632             document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
1633         else:
1634             biblio_options = get_value(document.header, "\\biblio_options", i)
1635             if biblio_options:
1636                 biblio_options += ","
1637             biblio_options += "refsection=" + multibib
1638             document.header[i] = "\\biblio_options " + biblio_options
1639
1640         # Bibtex insets
1641         i = 0
1642         while (True):
1643             i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
1644             if i == -1:
1645                 break
1646             j = find_end_of_inset(document.body, i)
1647             if j == -1:
1648                 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1649                 i += 1
1650                 continue
1651             btprint = get_quoted_value(document.body, "btprint", i, j)
1652             if btprint != "bibbysection":
1653                 i += 1
1654                 continue
1655             opts = get_quoted_value(document.body, "biblatexopts", i, j)
1656             # change btprint line
1657             k = find_token(document.body, "btprint", i, j)
1658             if k != -1:
1659                 document.body[k] = "btprint \"btPrintCited\""
1660             # Insert ERT \\bibbysection and wrap bibtex inset to a Note
1661             pcmd = "bibbysection"
1662             if opts:
1663                 pcmd += "[" + opts + "]"
1664             repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
1665                     "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
1666                     "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
1667                     "status open", "", "\\begin_layout Plain Layout" ]
1668             repl += document.body[i:j+1]
1669             repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
1670             document.body[i:j+1] = repl
1671             j += 27
1672
1673             i = j + 1
1674         return
1675
1676     # 5. More tricky: Bibtex/Bibtopic
1677     k = find_token(document.header, "\\use_bibtopic", 0)
1678     if k == -1:
1679         # this should not happen
1680         document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1681         return
1682     document.header[k] = "\\use_bibtopic true"
1683
1684     # Possible units. This assumes that the LyX name follows the std,
1685     # which might not always be the case. But it's as good as we can get.
1686     units = {
1687         "part" : "Part",
1688         "chapter" : "Chapter",
1689         "section" : "Section",
1690         "subsection" : "Subsection",
1691         }
1692
1693     if multibib not in units.keys():
1694         document.warning("Unknown multibib value `%s'!" % nultibib)
1695         return
1696     unit = units[multibib]
1697     btunit = False
1698     i = 0
1699     while (True):
1700         i = find_token(document.body, "\\begin_layout " + unit, i)
1701         if i == -1:
1702             break
1703         if btunit:
1704             document.body[i-1 : i-1] = ["\\begin_layout Standard",
1705                                 "\\begin_inset ERT", "status open", "",
1706                                 "\\begin_layout Plain Layout", "", "",
1707                                 "\\backslash",
1708                                 "end{btUnit}", "\\end_layout",
1709                                 "\\begin_layout Plain Layout", "",
1710                                 "\\backslash",
1711                                 "begin{btUnit}"
1712                                 "\\end_layout", "", "\\end_inset", "", "",
1713                                 "\\end_layout", ""]
1714             i += 21
1715         else:
1716             document.body[i-1 : i-1] = ["\\begin_layout Standard",
1717                                 "\\begin_inset ERT", "status open", "",
1718                                 "\\begin_layout Plain Layout", "", "",
1719                                 "\\backslash",
1720                                 "begin{btUnit}"
1721                                 "\\end_layout", "", "\\end_inset", "", "",
1722                                 "\\end_layout", ""]
1723             i += 16
1724         btunit = True
1725         i += 1
1726
1727     if btunit:
1728         i = find_token(document.body, "\\end_body", i)
1729         document.body[i-1 : i-1] = ["\\begin_layout Standard",
1730                                 "\\begin_inset ERT", "status open", "",
1731                                 "\\begin_layout Plain Layout", "", "",
1732                                 "\\backslash",
1733                                 "end{btUnit}"
1734                                 "\\end_layout", "", "\\end_inset", "", "",
1735                                 "\\end_layout", ""]
1736
1737
1738 def revert_chapterbib(document):
1739     " Revert chapterbib support "
1740
1741     # 1. Get cite engine
1742     engine = "basic"
1743     i = find_token(document.header, "\\cite_engine", 0)
1744     if i == -1:
1745         document.warning("Malformed document! Missing \\cite_engine")
1746     else:
1747         engine = get_value(document.header, "\\cite_engine", i)
1748
1749     # 2. Do we use biblatex?
1750     biblatex = False
1751     if engine in ["biblatex", "biblatex-natbib"]:
1752         biblatex = True
1753
1754     # 3. Store multibib document header value
1755     multibib = ""
1756     i = find_token(document.header, "\\multibib", 0)
1757     if i != -1:
1758         multibib = get_value(document.header, "\\multibib", i)
1759
1760     if not multibib or multibib != "child":
1761         # nothing to do
1762         return
1763
1764     # 4. remove multibib header
1765     del document.header[i]
1766
1767     # 5. Biblatex
1768     if biblatex:
1769         # find include insets
1770         i = 0
1771         while (True):
1772             i = find_token(document.body, "\\begin_inset CommandInset include", i)
1773             if i == -1:
1774                 break
1775             j = find_end_of_inset(document.body, i)
1776             if j == -1:
1777                 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1778                 i += 1
1779                 continue
1780             parent = get_containing_layout(document.body, i)
1781             parbeg = parent[1]
1782
1783             # Insert ERT \\newrefsection before inset
1784             beg = ["\\begin_layout Standard",
1785                    "\\begin_inset ERT", "status open", "",
1786                    "\\begin_layout Plain Layout", "", "",
1787                    "\\backslash",
1788                    "newrefsection"
1789                    "\\end_layout", "", "\\end_inset", "", "",
1790                    "\\end_layout", ""]
1791             document.body[parbeg-1:parbeg-1] = beg
1792             j += len(beg)
1793             i = j + 1
1794         return
1795
1796     # 6. Bibtex/Bibtopic
1797     i = find_token(document.header, "\\use_bibtopic", 0)
1798     if i == -1:
1799         # this should not happen
1800         document.warning("Malformed LyX document! No \\use_bibtopic header found!")
1801         return
1802     if get_value(document.header, "\\use_bibtopic", i) == "true":
1803         # find include insets
1804         i = 0
1805         while (True):
1806             i = find_token(document.body, "\\begin_inset CommandInset include", i)
1807             if i == -1:
1808                 break
1809             j = find_end_of_inset(document.body, i)
1810             if j == -1:
1811                 document.warning("Can't find end of bibtex inset at line %d!!" %(i))
1812                 i += 1
1813                 continue
1814             parent = get_containing_layout(document.body, i)
1815             parbeg = parent[1]
1816             parend = parent[2]
1817
1818             # Insert wrap inset into \\begin{btUnit}...\\end{btUnit}
1819             beg = ["\\begin_layout Standard",
1820                    "\\begin_inset ERT", "status open", "",
1821                    "\\begin_layout Plain Layout", "", "",
1822                    "\\backslash",
1823                    "begin{btUnit}"
1824                    "\\end_layout", "", "\\end_inset", "", "",
1825                    "\\end_layout", ""]
1826             end = ["\\begin_layout Standard",
1827                    "\\begin_inset ERT", "status open", "",
1828                    "\\begin_layout Plain Layout", "", "",
1829                    "\\backslash",
1830                    "end{btUnit}"
1831                    "\\end_layout", "", "\\end_inset", "", "",
1832                    "\\end_layout", ""]
1833             document.body[parend+1:parend+1] = end
1834             document.body[parbeg-1:parbeg-1] = beg
1835             j += len(beg) + len(end)
1836             i = j + 1
1837         return
1838
1839     # 7. Chapterbib proper
1840     add_to_preamble(document, ["\\usepackage{chapterbib}"])
1841
1842
1843 def convert_dashligatures(document):
1844     "Set 'use_dash_ligatures' according to content."
1845     use_dash_ligatures = None
1846     # eventually remove preamble code from 2.3->2.2 conversion:
1847     for i, line in enumerate(document.preamble):
1848         if i > 1 and line == r'\renewcommand{\textemdash}{---}':
1849             if (document.preamble[i-1] == r'\renewcommand{\textendash}{--}'
1850                 and document.preamble[i-2] == '% Added by lyx2lyx'):
1851                 del document.preamble[i-2:i+1]
1852                 use_dash_ligatures = True
1853     if use_dash_ligatures is None:
1854         # Look for dashes:
1855         # (Documents by LyX 2.1 or older have "\twohyphens\n" or "\threehyphens\n"
1856         # as interim representation for dash ligatures in 2.2.)
1857         has_literal_dashes = False
1858         has_ligature_dashes = False
1859         j = 0
1860         for i, line in enumerate(document.body):
1861             # Skip some document parts where dashes are not converted
1862             if (i < j) or line.startswith("\\labelwidthstring"):
1863                 continue
1864             words = line.split()
1865             if (len(words) > 1 and words[0] == "\\begin_inset"
1866                 and (words[1] in ["CommandInset", "ERT", "External", "Formula",
1867                                  "FormulaMacro", "Graphics", "IPA", "listings"]
1868                      or ' '.join(words[1:]) == "Flex Code")):
1869                 j = find_end_of_inset(document.body, i)
1870                 if j == -1:
1871                     document.warning("Malformed LyX document: "
1872                         "Can't find end of %s inset at line %d" % (words[1],i))
1873                 continue
1874             if line == "\\begin_layout LyX-Code":
1875                 j = find_end_of_layout(document.body, i)
1876                 if j == -1:
1877                     document.warning("Malformed LyX document: "
1878                        "Can't find end of %s layout at line %d" % (words[1],i))
1879                 continue
1880             # literal dash followed by a word or no-break space:
1881             if re.search(u"[\u2013\u2014]([\w\u00A0]|$)", line,
1882                          flags=re.UNICODE):
1883                 has_literal_dashes = True
1884             # ligature dash followed by word or no-break space on next line:
1885             if re.search(u"(\\\\twohyphens|\\\\threehyphens)", line,
1886                             flags=re.UNICODE) and re.match(u"[\w\u00A0]",
1887                             document.body[i+1], flags=re.UNICODE):
1888                 has_ligature_dashes = True
1889         if has_literal_dashes and has_ligature_dashes:
1890             # TODO: insert a warning note in the document?
1891             document.warning('This document contained both literal and '
1892                 '"ligature" dashes.\n Line breaks may have changed. '
1893                 'See UserGuide chapter 3.9.1 for details.')
1894         elif has_literal_dashes:
1895             use_dash_ligatures = False
1896         elif has_ligature_dashes:
1897             use_dash_ligatures = True
1898     # insert the setting if there is a preferred value
1899     if use_dash_ligatures is not None:
1900         i = find_token(document.header, "\\use_microtype", 0)
1901         if i != -1:
1902             document.header.insert(i+1, "\\use_dash_ligatures %s"
1903                                 % str(use_dash_ligatures).lower())
1904
1905 def revert_dashligatures(document):
1906     """Remove font ligature settings for en- and em-dashes.
1907     Revert conversion of \twodashes or \threedashes to literal dashes."""
1908     i = find_token(document.header, "\\use_dash_ligatures", 0)
1909     if i == -1:
1910         return
1911     use_dash_ligatures = get_bool_value(document.header, "\\use_dash_ligatures", i)
1912     del document.header[i]
1913     if not use_dash_ligatures or document.backend != "latex":
1914         return
1915
1916     j = 0
1917     new_body = []
1918     for i, line in enumerate(document.body):
1919         # Skip some document parts where dashes are not converted
1920         if (i < j) or line.startswith("\\labelwidthstring"):
1921             new_body.append(line)
1922             continue
1923         words = line.split()
1924         if (len(words) > 1 and words[0] == "\\begin_inset"
1925             and (words[1] in ["CommandInset", "ERT", "External", "Formula",
1926                               "FormulaMacro", "Graphics", "IPA", "listings"]
1927                  or ' '.join(words[1:]) == "Flex Code")):
1928             j = find_end_of_inset(document.body, i)
1929             if j == -1:
1930                 document.warning("Malformed LyX document: Can't find end of "
1931                                  + words[1] + " inset at line " + str(i))
1932             new_body.append(line)
1933             continue
1934         if line == "\\begin_layout LyX-Code":
1935             j = find_end_of_layout(document.body, i)
1936             if j == -1:
1937                 document.warning("Malformed LyX document: "
1938                     "Can't find end of %s layout at line %d" % (words[1],i))
1939             new_body.append(line)
1940             continue
1941         # TODO: skip replacement in typewriter fonts
1942         line = line.replace(u'\u2013', '\\twohyphens\n')
1943         line = line.replace(u'\u2014', '\\threehyphens\n')
1944         lines = line.split('\n')
1945         new_body.extend(line.split('\n'))
1946     document.body = new_body
1947     # redefine the dash LICRs to use ligature dashes:
1948     add_to_preamble(document, [r'\renewcommand{\textendash}{--}',
1949                                r'\renewcommand{\textemdash}{---}'])
1950
1951
1952 def revert_noto(document):
1953     " Revert Noto font definitions to LaTeX "
1954
1955     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1956         preamble = ""
1957         i = find_token(document.header, "\\font_roman \"NotoSerif-TLF\"", 0)
1958         if i != -1:
1959             add_to_preamble(document, ["\\renewcommand{\\rmdefault}{NotoSerif-TLF}"])
1960             document.header[i] = document.header[i].replace("NotoSerif-TLF", "default")
1961         i = find_token(document.header, "\\font_sans \"NotoSans-TLF\"", 0)
1962         if i != -1:
1963             add_to_preamble(document, ["\\renewcommand{\\sfdefault}{NotoSans-TLF}"])
1964             document.header[i] = document.header[i].replace("NotoSans-TLF", "default")
1965         i = find_token(document.header, "\\font_typewriter \"NotoMono-TLF\"", 0)
1966         if i != -1:
1967             add_to_preamble(document, ["\\renewcommand{\\ttdefault}{NotoMono-TLF}"])
1968             document.header[i] = document.header[i].replace("NotoMono-TLF", "default")
1969
1970
1971 def revert_xout(document):
1972   " Reverts \\xout font attribute "
1973   changed = revert_font_attrs(document.body, "\\xout", "\\xout")
1974   if changed == True:
1975     insert_to_preamble(document, \
1976         ['%  for proper cross-out',
1977         '\\PassOptionsToPackage{normalem}{ulem}',
1978         '\\usepackage{ulem}'])
1979
1980
1981 def convert_mathindent(document):
1982     " add the \\is_math_indent tag "
1983     # check if the document uses the class option "fleqn"
1984     k = find_token(document.header, "\\quotes_style", 0)
1985     regexp = re.compile(r'^.*fleqn.*')
1986     i = find_re(document.header, regexp, 0)
1987     if i != -1:
1988         document.header.insert(k, "\\is_math_indent 1")
1989         # delete the found option
1990         document.header[i] = document.header[i].replace(",fleqn", "")
1991         document.header[i] = document.header[i].replace(", fleqn", "")
1992         document.header[i] = document.header[i].replace("fleqn,", "")
1993         j = find_re(document.header, regexp, 0)
1994         if i == j:
1995             # then we have fleqn as the only option
1996             del document.header[i]
1997     else:
1998         document.header.insert(k, "\\is_math_indent 0")
1999
2000
2001 def revert_mathindent(document):
2002     " Define mathindent if set in the document "
2003     # first output the length
2004     regexp = re.compile(r'(\\math_indentation)')
2005     i = find_re(document.header, regexp, 0)
2006     if i != -1:
2007         value = get_value(document.header, "\\math_indentation" , i).split()[0]
2008         if value != "default":
2009             add_to_preamble(document, ["\\setlength{\\mathindent}{" + value + '}'])
2010         del document.header[i]
2011     # now set the document class option
2012     regexp = re.compile(r'(\\is_math_indent 1)')
2013     i = find_re(document.header, regexp, 0)
2014     if i == -1:
2015         regexp = re.compile(r'(\\is_math_indent)')
2016         j = find_re(document.header, regexp, 0)
2017         del document.header[j]
2018     else:
2019         k = find_token(document.header, "\\options", 0)
2020         if k != -1:
2021             document.header[k] = document.header[k].replace("\\options", "\\options fleqn,")
2022             del document.header[i]
2023         else:
2024             l = find_token(document.header, "\\use_default_options", 0)
2025             document.header.insert(l, "\\options fleqn")
2026             del document.header[i + 1]
2027
2028
2029 def revert_baselineskip(document):
2030   " Revert baselineskips to TeX code "
2031   i = 0
2032   vspaceLine = 0
2033   hspaceLine = 0
2034   while True:
2035     regexp = re.compile(r'^.*baselineskip%.*$')
2036     i = find_re(document.body, regexp, i)
2037     if i == -1:
2038       return
2039     vspaceLine = find_token(document.body, "\\begin_inset VSpace", i)
2040     if  vspaceLine == i:
2041       # output VSpace inset as TeX code
2042       # first read out the values
2043       beg = document.body[i].rfind("VSpace ");
2044       end = document.body[i].rfind("baselineskip%");
2045       baselineskip = float(document.body[i][beg + 7:end]);
2046       # we store the value in percent, thus divide by 100
2047       baselineskip = baselineskip/100;
2048       baselineskip = str(baselineskip);
2049       # check if it is the starred version
2050       if document.body[i].find('*') != -1:
2051         star = '*'
2052       else:
2053         star = ''
2054       # now output TeX code
2055       endInset = find_end_of_inset(document.body, i)
2056       if endInset == -1:
2057         document.warning("Malformed LyX document: Missing '\\end_inset' of VSpace inset.")
2058         return
2059       else:
2060         document.body[vspaceLine: endInset + 1] = put_cmd_in_ert("\\vspace" + star + '{' + baselineskip + "\\baselineskip}")
2061     hspaceLine = find_token(document.body, "\\begin_inset space \\hspace", i - 1)
2062     document.warning("hspaceLine: " + str(hspaceLine))
2063     document.warning("i: " + str(i))
2064     if  hspaceLine == i - 1:
2065       # output space inset as TeX code
2066       # first read out the values
2067       beg = document.body[i].rfind("\\length ");
2068       end = document.body[i].rfind("baselineskip%");
2069       baselineskip = float(document.body[i][beg + 7:end]);
2070       document.warning("baselineskip: " + str(baselineskip))
2071       # we store the value in percent, thus divide by 100
2072       baselineskip = baselineskip/100;
2073       baselineskip = str(baselineskip);
2074       # check if it is the starred version
2075       if document.body[i-1].find('*') != -1:
2076         star = '*'
2077       else:
2078         star = ''
2079       # now output TeX code
2080       endInset = find_end_of_inset(document.body, i)
2081       if endInset == -1:
2082         document.warning("Malformed LyX document: Missing '\\end_inset' of space inset.")
2083         return
2084       else:
2085         document.body[hspaceLine: endInset + 1] = put_cmd_in_ert("\\hspace" + star + '{' + baselineskip + "\\baselineskip}")
2086
2087     i = i + 1
2088
2089
2090 def revert_rotfloat(document):
2091   " Revert placement options for rotated floats "
2092   i = 0
2093   j = 0
2094   k = 0
2095   while True:
2096     i = find_token(document.body, "sideways true", i)
2097     if i != -1:
2098       regexp = re.compile(r'^.*placement.*$')
2099       j = find_re(document.body, regexp, i-2)
2100       if j == -1:
2101           return
2102       if j != i-2:
2103           i = i + 1
2104           continue
2105     else:
2106       return
2107     # we found a sideways float with placement options
2108     # at first store the placement
2109     beg = document.body[i-2].rfind(" ");
2110     placement = document.body[i-2][beg+1:]
2111     # check if the option'H' is used
2112     if placement.find("H") != -1:
2113       add_to_preamble(document, ["\\usepackage{float}"])
2114     # now check if it is a starred type
2115     if document.body[i-1].find("wide true") != -1:
2116       star = '*'
2117     else:
2118       star = ''
2119     # store the float type
2120     beg = document.body[i-3].rfind(" ");
2121     fType = document.body[i-3][beg+1:]
2122     # now output TeX code
2123     endInset = find_end_of_inset(document.body, i-3)
2124     if endInset == -1:
2125       document.warning("Malformed LyX document: Missing '\\end_inset' of Float inset.")
2126       return
2127     else:
2128       document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
2129       document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
2130       add_to_preamble(document, ["\\usepackage{rotfloat}"])
2131
2132     i = i + 1
2133
2134
2135 def convert_allowbreak(document):
2136     " Zero widths Space-inset -> \SpecialChar allowbreak. "
2137     body = "\n".join(document.body)
2138     body = body.replace("\\begin_inset space \hspace{}\n"
2139                         "\\length 0dd\n"
2140                         "\\end_inset\n\n",
2141                         "\\SpecialChar allowbreak\n")
2142     document.body = body.split("\n")
2143
2144
2145 def revert_allowbreak(document):
2146     " \SpecialChar allowbreak -> Zero widths Space-inset. "
2147     body = "\n".join(document.body)
2148     body = body.replace("\\SpecialChar allowbreak\n",
2149                         "\n\\begin_inset space \hspace{}\n"
2150                         "\\length 0dd\n"
2151                         "\\end_inset\n\n")
2152     document.body = body.split("\n")
2153
2154
2155 def convert_mathnumberpos(document):
2156     " add the \\math_number_before tag "
2157     # check if the document uses the class option "leqno"
2158     k = find_token(document.header, "\\quotes_style", 0)
2159     m = find_token(document.header, "\\options", 0)
2160     regexp = re.compile(r'^.*leqno.*')
2161     i = find_re(document.header, regexp, 0)
2162     if i != -1 and i == m:
2163         document.header.insert(k, "\\math_number_before 1")
2164         # delete the found option
2165         document.header[i] = document.header[i].replace(",leqno", "")
2166         document.header[i] = document.header[i].replace(", leqno", "")
2167         document.header[i] = document.header[i].replace("leqno,", "")
2168         j = find_re(document.header, regexp, 0)
2169         if i == j:
2170             # then we have leqno as the only option
2171             del document.header[i]
2172     else:
2173         document.header.insert(k, "\\math_number_before 0")
2174
2175
2176 def revert_mathnumberpos(document):
2177     " add the document class option leqno"
2178     regexp = re.compile(r'(\\math_number_before 1)')
2179     i = find_re(document.header, regexp, 0)
2180     if i == -1:
2181         regexp = re.compile(r'(\\math_number_before)')
2182         j = find_re(document.header, regexp, 0)
2183         del document.header[j]
2184     else:
2185         k = find_token(document.header, "\\options", 0)
2186         if k != -1:
2187             document.header[k] = document.header[k].replace("\\options", "\\options leqno,")
2188             del document.header[i]
2189         else:
2190             l = find_token(document.header, "\\use_default_options", 0)
2191             document.header.insert(l, "\\options leqno")
2192             del document.header[i + 1]
2193
2194
2195 def convert_mathnumberingname(document):
2196     " rename the \\math_number_before tag to \\math_numbering_side "
2197     regexp = re.compile(r'(\\math_number_before 1)')
2198     i = find_re(document.header, regexp, 0)
2199     if i != -1:
2200         document.header[i] = "\\math_numbering_side left"
2201     regexp = re.compile(r'(\\math_number_before 0)')
2202     i = find_re(document.header, regexp, 0)
2203     if i != -1:
2204         document.header[i] = "\\math_numbering_side default"
2205     # check if the document uses the class option "reqno"
2206     k = find_token(document.header, "\\math_numbering_side", 0)
2207     m = find_token(document.header, "\\options", 0)
2208     regexp = re.compile(r'^.*reqno.*')
2209     i = find_re(document.header, regexp, 0)
2210     if i != -1 and i == m:
2211         document.header[k] = "\\math_numbering_side right"
2212         # delete the found option
2213         document.header[i] = document.header[i].replace(",reqno", "")
2214         document.header[i] = document.header[i].replace(", reqno", "")
2215         document.header[i] = document.header[i].replace("reqno,", "")
2216         j = find_re(document.header, regexp, 0)
2217         if i == j:
2218             # then we have reqno as the only option
2219             del document.header[i]
2220
2221
2222 def revert_mathnumberingname(document):
2223     " rename the \\math_numbering_side tag back to \\math_number_before "
2224     # just rename
2225     regexp = re.compile(r'(\\math_numbering_side left)')
2226     i = find_re(document.header, regexp, 0)
2227     if i != -1:
2228         document.header[i] = "\\math_number_before 1"
2229     # add the option reqno and delete the tag
2230     regexp = re.compile(r'(\\math_numbering_side right)')
2231     i = find_re(document.header, regexp, 0)
2232     if i != -1:
2233         document.header[i] = "\\math_number_before 0"
2234         k = find_token(document.header, "\\options", 0)
2235         if k != -1:
2236             document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
2237         else:
2238             l = find_token(document.header, "\\use_default_options", 0)
2239             document.header.insert(l, "\\options reqno")
2240     # add the math_number_before tag
2241     regexp = re.compile(r'(\\math_numbering_side default)')
2242     i = find_re(document.header, regexp, 0)
2243     if i != -1:
2244         document.header[i] = "\\math_number_before 0"
2245
2246
2247 def convert_minted(document):
2248     " add the \\use_minted tag "
2249     document.header.insert(-1, "\\use_minted 0")
2250
2251
2252 def revert_minted(document):
2253     " remove the \\use_minted tag "
2254     i = find_token(document.header, "\\use_minted", 0)
2255     if i != -1:
2256         document.header.pop(i)
2257
2258
2259 ##
2260 # Conversion hub
2261 #
2262
2263 supported_versions = ["2.3.0", "2.3"]
2264 convert = [
2265            [509, [convert_microtype]],
2266            [510, [convert_dateinset]],
2267            [511, [convert_ibranches]],
2268            [512, [convert_beamer_article_styles]],
2269            [513, []],
2270            [514, []],
2271            [515, []],
2272            [516, [convert_inputenc]],
2273            [517, []],
2274            [518, [convert_iopart]],
2275            [519, [convert_quotestyle]],
2276            [520, []],
2277            [521, [convert_frenchquotes]],
2278            [522, []],
2279            [523, []],
2280            [524, []],
2281            [525, []],
2282            [526, []],
2283            [527, []],
2284            [528, []],
2285            [529, []],
2286            [530, []],
2287            [531, []],
2288            [532, [convert_literalparam]],
2289            [533, []],
2290            [534, []],
2291            [535, [convert_dashligatures]],
2292            [536, []],
2293            [537, []],
2294            [538, [convert_mathindent]],
2295            [539, []],
2296            [540, []],
2297            [541, [convert_allowbreak]],
2298            [542, [convert_mathnumberpos]],
2299            [543, [convert_mathnumberingname]],
2300            [544, [convert_minted]]
2301           ]
2302
2303 revert =  [
2304            [543, [revert_minted]],
2305            [542, [revert_mathnumberingname]],
2306            [541, [revert_mathnumberpos]],
2307            [540, [revert_allowbreak]],
2308            [539, [revert_rotfloat]],
2309            [538, [revert_baselineskip]],
2310            [537, [revert_mathindent]],
2311            [536, [revert_xout]],
2312            [535, [revert_noto]],
2313            [534, [revert_dashligatures]],
2314            [533, [revert_chapterbib]],
2315            [532, [revert_multibib]],
2316            [531, [revert_literalparam]],
2317            [530, [revert_qualicites]],
2318            [529, [revert_bibpackopts]],
2319            [528, [revert_citekeyonly]],
2320            [527, [revert_biblatex]],
2321            [526, [revert_noprefix]],
2322            [525, [revert_plural_refs]],
2323            [524, [revert_labelonly]],
2324            [523, [revert_crimson, revert_cochinealmath]],
2325            [522, [revert_cjkquotes]],
2326            [521, [revert_dynamicquotes]],
2327            [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
2328            [519, [revert_plainquote]],
2329            [518, [revert_quotestyle]],
2330            [517, [revert_iopart]],
2331            [516, [revert_quotes]],
2332            [515, []],
2333            [514, [revert_urdu, revert_syriac]],
2334            [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
2335            [512, [revert_bosnian, revert_friulan, revert_macedonian, revert_piedmontese, revert_romansh]],
2336            [511, [revert_beamer_article_styles]],
2337            [510, [revert_ibranches]],
2338            [509, []],
2339            [508, [revert_microtype]]
2340           ]
2341
2342
2343 if __name__ == "__main__":
2344     pass