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