1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2016 The LyX team
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 """ Convert files to the file format generated by lyx 2.3"""
26 # Uncomment only what you need to import, please.
28 from parser_tools import find_end_of, find_token_backwards, find_end_of_layout#,
29 # find_token, find_tokens, \
30 # find_token_exact, find_end_of_inset, \
31 # is_in_inset, get_value, get_quoted_value, \
32 # del_token, check_token, get_option_value, get_bool_value
34 from parser_tools import find_token, find_end_of_inset, get_value, \
35 get_bool_value, get_containing_layout
37 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
38 # get_ert, lyx2latex, \
39 # lyx2verbatim, length_in_bp, convert_info_insets
40 # insert_to_preamble, latex_length, revert_flex_inset, \
41 # revert_font_attrs, hex2ratio, str2bool
43 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
45 ####################################################################
46 # Private helper functions
50 ###############################################################################
52 ### Conversion and reversion routines
54 ###############################################################################
56 def convert_microtype(document):
57 " Add microtype settings. "
58 i = find_token(document.header, "\\font_tt_scale" , 0)
60 document.warning("Malformed LyX document: Can't find \\font_tt_scale.")
61 i = len(document.header) - 1
63 j = find_token(document.preamble, "\\usepackage{microtype}", 0)
65 document.header.insert(i + 1, "\\use_microtype false")
67 document.header.insert(i + 1, "\\use_microtype true")
68 del document.preamble[j]
71 def revert_microtype(document):
72 " Remove microtype settings. "
73 i = find_token(document.header, "\\use_microtype", 0)
76 use_microtype = get_bool_value(document.header, "\\use_microtype" , i)
77 del document.header[i]
79 add_to_preamble(document, ["\\usepackage{microtype}"])
82 def convert_dateinset(document):
83 ' Convert date external inset to ERT '
86 i = find_token(document.body, "\\begin_inset External", i)
89 j = find_end_of_inset(document.body, i)
91 document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
94 if get_value(document.body, 'template', i, j) == "Date":
95 document.body[i : j + 1] = put_cmd_in_ert("\\today ")
100 def convert_inputenc(document):
101 " Replace no longer supported input encoding settings. "
102 i = find_token(document.header, "\\inputenc", 0)
105 if get_value(document.header, "\\inputencoding", i) == "pt254":
106 document.header[i] = "\\inputencoding pt154"
109 def convert_ibranches(document):
110 ' Add "inverted 0" to branch insets'
113 i = find_token(document.body, "\\begin_inset Branch", i)
116 document.body.insert(i + 1, "inverted 0")
120 def revert_ibranches(document):
121 ' Convert inverted branches to explicit anti-branches'
122 # Get list of branches
126 i = find_token(document.header, "\\branch", i)
129 branch = document.header[i][8:].strip()
130 if document.header[i+1].startswith("\\selected "):
131 #document.warning(document.header[i+1])
132 #document.warning(document.header[i+1][10])
133 selected = int(document.header[i+1][10])
135 document.warning("Malformed LyX document: No selection indicator for branch " + branch)
138 # the value tells us whether the branch is selected
139 ourbranches[document.header[i][8:].strip()] = selected
142 # Figure out what inverted branches, if any, have been used
143 # and convert them to "Anti-OldBranch"
147 i = find_token(document.body, "\\begin_inset Branch", i)
150 if not document.body[i+1].startswith("inverted "):
151 document.warning("Malformed LyX document: Missing 'inverted' tag!")
154 inverted = document.body[i+1][9]
155 #document.warning(document.body[i+1])
158 branch = document.body[i][20:].strip()
159 #document.warning(branch)
160 if not branch in ibranches:
161 antibranch = "Anti-" + branch
162 while antibranch in ibranches:
163 antibranch = "x" + antibranch
164 ibranches[branch] = antibranch
166 antibranch = ibranches[branch]
167 #document.warning(antibranch)
168 document.body[i] = "\\begin_inset Branch " + antibranch
170 # remove "inverted" key
171 del document.body[i+1]
174 # now we need to add the new branches to the header
175 for old, new in ibranches.iteritems():
176 i = find_token(document.header, "\\branch " + old, 0)
178 document.warning("Can't find branch %s even though we found it before!" % (old))
180 j = find_token(document.header, "\\end_branch", i)
182 document.warning("Malformed LyX document! Can't find end of branch " + old)
184 # ourbranches[old] - 1 inverts the selection status of the old branch
185 lines = ["\\branch " + new,
186 "\\selected " + str(ourbranches[old] - 1)]
187 # these are the old lines telling us color, etc.
188 lines += document.header[i+2 : j+1]
189 document.header[i:i] = lines
192 def revert_beamer_article_styles(document):
193 " Include (scr)article styles in beamer article "
195 beamer_articles = ["article-beamer", "scrarticle-beamer"]
196 if document.textclass not in beamer_articles:
199 inclusion = "article.layout"
200 if document.textclass == "scrarticle-beamer":
201 inclusion = "scrartcl.layout"
203 i = find_token(document.header, "\\begin_local_layout", 0)
205 k = find_token(document.header, "\\language", 0)
207 # this should not happen
208 document.warning("Malformed LyX document! No \\language header found!")
210 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
213 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
215 # this should not happen
216 document.warning("Malformed LyX document: Can't find end of local layout!")
219 document.header[i+1 : i+1] = [
220 "### Inserted by lyx2lyx (more [scr]article styles) ###",
221 "Input " + inclusion,
222 "Input beamer.layout",
223 "Provides geometry 0",
224 "Provides hyperref 0",
233 " \\usepackage{beamerarticle,pgf}",
234 " % this default might be overridden by plain title style",
235 " \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
236 " \\AtBeginDocument{",
237 " \\let\\origtableofcontents=\\tableofcontents",
238 " \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
239 " \\def\\gobbletableofcontents#1{\\origtableofcontents}",
242 "### End of insertion by lyx2lyx (more [scr]article styles) ###"
246 def convert_beamer_article_styles(document):
247 " Remove included (scr)article styles in beamer article "
249 beamer_articles = ["article-beamer", "scrarticle-beamer"]
250 if document.textclass not in beamer_articles:
253 i = find_token(document.header, "\\begin_local_layout", 0)
257 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
259 # this should not happen
260 document.warning("Malformed LyX document: Can't find end of local layout!")
263 k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
265 l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
267 # this should not happen
268 document.warning("End of lyx2lyx local layout insertion not found!")
271 if k == i + 1 and l == j - 1:
272 # that was all the local layout there was
273 document.header[i : j + 1] = []
275 document.header[k : l + 1] = []
278 def revert_bosnian(document):
279 "Set the document language to English but assure Bosnian output"
281 if document.language == "bosnian":
282 document.language = "english"
283 i = find_token(document.header, "\\language bosnian", 0)
285 document.header[i] = "\\language english"
286 j = find_token(document.header, "\\language_package default", 0)
288 document.header[j] = "\\language_package babel"
289 k = find_token(document.header, "\\options", 0)
291 document.header[k] = document.header[k].replace("\\options", "\\options bosnian,")
293 l = find_token(document.header, "\\use_default_options", 0)
294 document.header.insert(l + 1, "\\options bosnian")
297 def revert_friulan(document):
298 "Set the document language to English but assure Friulan output"
300 if document.language == "friulan":
301 document.language = "english"
302 i = find_token(document.header, "\\language friulan", 0)
304 document.header[i] = "\\language english"
305 j = find_token(document.header, "\\language_package default", 0)
307 document.header[j] = "\\language_package babel"
308 k = find_token(document.header, "\\options", 0)
310 document.header[k] = document.header[k].replace("\\options", "\\options friulan,")
312 l = find_token(document.header, "\\use_default_options", 0)
313 document.header.insert(l + 1, "\\options friulan")
316 def revert_macedonian(document):
317 "Set the document language to English but assure Macedonian output"
319 if document.language == "macedonian":
320 document.language = "english"
321 i = find_token(document.header, "\\language macedonian", 0)
323 document.header[i] = "\\language english"
324 j = find_token(document.header, "\\language_package default", 0)
326 document.header[j] = "\\language_package babel"
327 k = find_token(document.header, "\\options", 0)
329 document.header[k] = document.header[k].replace("\\options", "\\options macedonian,")
331 l = find_token(document.header, "\\use_default_options", 0)
332 document.header.insert(l + 1, "\\options macedonian")
335 def revert_piedmontese(document):
336 "Set the document language to English but assure Piedmontese output"
338 if document.language == "piedmontese":
339 document.language = "english"
340 i = find_token(document.header, "\\language piedmontese", 0)
342 document.header[i] = "\\language english"
343 j = find_token(document.header, "\\language_package default", 0)
345 document.header[j] = "\\language_package babel"
346 k = find_token(document.header, "\\options", 0)
348 document.header[k] = document.header[k].replace("\\options", "\\options piedmontese,")
350 l = find_token(document.header, "\\use_default_options", 0)
351 document.header.insert(l + 1, "\\options piedmontese")
354 def revert_romansh(document):
355 "Set the document language to English but assure Romansh output"
357 if document.language == "romansh":
358 document.language = "english"
359 i = find_token(document.header, "\\language romansh", 0)
361 document.header[i] = "\\language english"
362 j = find_token(document.header, "\\language_package default", 0)
364 document.header[j] = "\\language_package babel"
365 k = find_token(document.header, "\\options", 0)
367 document.header[k] = document.header[k].replace("\\options", "\\options romansh,")
369 l = find_token(document.header, "\\use_default_options", 0)
370 document.header.insert(l + 1, "\\options romansh")
373 def revert_amharic(document):
374 "Set the document language to English but assure Amharic output"
376 if document.language == "amharic":
377 document.language = "english"
378 i = find_token(document.header, "\\language amharic", 0)
380 document.header[i] = "\\language english"
381 j = find_token(document.header, "\\language_package default", 0)
383 document.header[j] = "\\language_package default"
384 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{amharic}}"])
385 document.body[2 : 2] = ["\\begin_layout Standard",
386 "\\begin_inset ERT", "status open", "",
387 "\\begin_layout Plain Layout", "", "",
389 "resetdefaultlanguage{amharic}",
390 "\\end_layout", "", "\\end_inset", "", "",
394 def revert_asturian(document):
395 "Set the document language to English but assure Asturian output"
397 if document.language == "asturian":
398 document.language = "english"
399 i = find_token(document.header, "\\language asturian", 0)
401 document.header[i] = "\\language english"
402 j = find_token(document.header, "\\language_package default", 0)
404 document.header[j] = "\\language_package default"
405 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{asturian}}"])
406 document.body[2 : 2] = ["\\begin_layout Standard",
407 "\\begin_inset ERT", "status open", "",
408 "\\begin_layout Plain Layout", "", "",
410 "resetdefaultlanguage{asturian}",
411 "\\end_layout", "", "\\end_inset", "", "",
415 def revert_kannada(document):
416 "Set the document language to English but assure Kannada output"
418 if document.language == "kannada":
419 document.language = "english"
420 i = find_token(document.header, "\\language kannada", 0)
422 document.header[i] = "\\language english"
423 j = find_token(document.header, "\\language_package default", 0)
425 document.header[j] = "\\language_package default"
426 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{kannada}}"])
427 document.body[2 : 2] = ["\\begin_layout Standard",
428 "\\begin_inset ERT", "status open", "",
429 "\\begin_layout Plain Layout", "", "",
431 "resetdefaultlanguage{kannada}",
432 "\\end_layout", "", "\\end_inset", "", "",
436 def revert_khmer(document):
437 "Set the document language to English but assure Khmer output"
439 if document.language == "khmer":
440 document.language = "english"
441 i = find_token(document.header, "\\language khmer", 0)
443 document.header[i] = "\\language english"
444 j = find_token(document.header, "\\language_package default", 0)
446 document.header[j] = "\\language_package default"
447 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{khmer}}"])
448 document.body[2 : 2] = ["\\begin_layout Standard",
449 "\\begin_inset ERT", "status open", "",
450 "\\begin_layout Plain Layout", "", "",
452 "resetdefaultlanguage{khmer}",
453 "\\end_layout", "", "\\end_inset", "", "",
457 def revert_urdu(document):
458 "Set the document language to English but assure Urdu output"
460 if document.language == "urdu":
461 document.language = "english"
462 i = find_token(document.header, "\\language urdu", 0)
464 document.header[i] = "\\language english"
465 j = find_token(document.header, "\\language_package default", 0)
467 document.header[j] = "\\language_package default"
468 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{urdu}}"])
469 document.body[2 : 2] = ["\\begin_layout Standard",
470 "\\begin_inset ERT", "status open", "",
471 "\\begin_layout Plain Layout", "", "",
473 "resetdefaultlanguage{urdu}",
474 "\\end_layout", "", "\\end_inset", "", "",
478 def revert_syriac(document):
479 "Set the document language to English but assure Syriac output"
481 if document.language == "syriac":
482 document.language = "english"
483 i = find_token(document.header, "\\language syriac", 0)
485 document.header[i] = "\\language english"
486 j = find_token(document.header, "\\language_package default", 0)
488 document.header[j] = "\\language_package default"
489 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{syriac}}"])
490 document.body[2 : 2] = ["\\begin_layout Standard",
491 "\\begin_inset ERT", "status open", "",
492 "\\begin_layout Plain Layout", "", "",
494 "resetdefaultlanguage{syriac}",
495 "\\end_layout", "", "\\end_inset", "", "",
499 def revert_quotes(document):
500 " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
502 # First handle verbatim insets
505 while i < len(document.body):
506 words = document.body[i].split()
507 if len(words) > 1 and words[0] == "\\begin_inset" and \
508 ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
509 j = find_end_of_inset(document.body, i)
511 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
515 k = find_token(document.body, '\\begin_inset Quotes', i, j)
519 l = find_end_of_inset(document.body, k)
521 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
525 if document.body[k].endswith("s"):
527 document.body[k:l+1] = [replace]
532 # Now verbatim layouts
535 while i < len(document.body):
536 words = document.body[i].split()
537 if len(words) > 1 and words[0] == "\\begin_layout" and \
538 words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
539 j = find_end_of_layout(document.body, i)
541 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
545 k = find_token(document.body, '\\begin_inset Quotes', i, j)
549 l = find_end_of_inset(document.body, k)
551 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
555 if document.body[k].endswith("s"):
557 document.body[k:l+1] = [replace]
563 if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
569 k = find_token(document.body, '\\begin_inset Quotes', i)
572 l = find_end_of_inset(document.body, k)
574 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
578 parent = get_containing_layout(document.body, k)
579 ql = find_token_backwards(document.body, "\\lang", k)
580 if ql == -1 or ql < parent[1]:
581 hebrew = document.language == "hebrew"
582 elif document.body[ql] == "\\lang hebrew":
586 if document.body[k].endswith("s"):
588 document.body[k:l+1] = [replace]
592 def revert_iopart(document):
593 " Input new styles via local layout "
594 if document.textclass != "iopart":
597 i = find_token(document.header, "\\begin_local_layout", 0)
599 k = find_token(document.header, "\\language", 0)
601 # this should not happen
602 document.warning("Malformed LyX document! No \\language header found!")
604 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
607 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
609 # this should not happen
610 document.warning("Malformed LyX document! Can't find end of local layout!")
613 document.header[i+1 : i+1] = [
614 "### Inserted by lyx2lyx (stdlayouts) ###",
615 "Input stdlayouts.inc",
616 "### End of insertion by lyx2lyx (stdlayouts) ###"
620 def convert_iopart(document):
621 " Remove local layout we added, if it is there "
622 if document.textclass != "iopart":
625 i = find_token(document.header, "\\begin_local_layout", 0)
629 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
631 # this should not happen
632 document.warning("Malformed LyX document! Can't find end of local layout!")
635 k = find_token(document.header, "### Inserted by lyx2lyx (stdlayouts) ###", i, j)
637 l = find_token(document.header, "### End of insertion by lyx2lyx (stdlayouts) ###", i, j)
639 # this should not happen
640 document.warning("End of lyx2lyx local layout insertion not found!")
642 if k == i + 1 and l == j - 1:
643 # that was all the local layout there was
644 document.header[i : j + 1] = []
646 document.header[k : l + 1] = []
649 def convert_quotestyle(document):
650 " Convert \\quotes_language to \\quotes_style "
651 i = find_token(document.header, "\\quotes_language", 0)
653 document.warning("Malformed LyX document! Can't find \\quotes_language!")
655 val = get_value(document.header, "\\quotes_language", i)
656 document.header[i] = "\\quotes_style " + val
659 def revert_quotestyle(document):
660 " Revert \\quotes_style to \\quotes_language "
661 i = find_token(document.header, "\\quotes_style", 0)
663 document.warning("Malformed LyX document! Can't find \\quotes_style!")
665 val = get_value(document.header, "\\quotes_style", i)
666 document.header[i] = "\\quotes_language " + val
669 def revert_plainquote(document):
670 " Revert plain quote insets "
672 # First, revert style setting
673 i = find_token(document.header, "\\quotes_style plain", 0)
675 document.header[i] = "\\quotes_style english"
681 k = find_token(document.body, '\\begin_inset Quotes q', i)
684 l = find_end_of_inset(document.body, k)
686 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
690 if document.body[k].endswith("s"):
692 document.body[k:l+1] = [replace]
696 def convert_frenchquotes(document):
697 " Convert french quote insets to swiss "
699 # First, revert style setting
700 i = find_token(document.header, "\\quotes_style french", 0)
702 document.header[i] = "\\quotes_style swiss"
707 i = find_token(document.body, '\\begin_inset Quotes f', i)
710 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
711 newval = val.replace("f", "c", 1)
712 document.body[i] = document.body[i].replace(val, newval)
716 def revert_swissquotes(document):
717 " Revert swiss quote insets to french "
719 # First, revert style setting
720 i = find_token(document.header, "\\quotes_style swiss", 0)
722 document.header[i] = "\\quotes_style french"
727 i = find_token(document.body, '\\begin_inset Quotes c', i)
730 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
731 newval = val.replace("c", "f", 1)
732 document.body[i] = document.body[i].replace(val, newval)
736 def revert_britishquotes(document):
737 " Revert british quote insets to english "
739 # First, revert style setting
740 i = find_token(document.header, "\\quotes_style british", 0)
742 document.header[i] = "\\quotes_style english"
747 i = find_token(document.body, '\\begin_inset Quotes b', i)
750 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
751 newval = val.replace("b", "e", 1)
754 newval = newval.replace("d", "s")
757 newval = newval.replace("s", "d")
758 document.body[i] = document.body[i].replace(val, newval)
762 def revert_swedishgquotes(document):
763 " Revert swedish quote insets "
765 # First, revert style setting
766 i = find_token(document.header, "\\quotes_style swedishg", 0)
768 document.header[i] = "\\quotes_style danish"
773 i = find_token(document.body, '\\begin_inset Quotes w', i)
776 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
779 newval = val.replace("w", "a", 1).replace("r", "l")
782 newval = val.replace("w", "s", 1)
783 document.body[i] = document.body[i].replace(val, newval)
787 def revert_frenchquotes(document):
788 " Revert french inner quote insets "
792 i = find_token(document.body, '\\begin_inset Quotes f', i)
795 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
798 newval = val.replace("f", "e", 1).replace("s", "d")
799 document.body[i] = document.body[i].replace(val, newval)
803 def revert_frenchinquotes(document):
804 " Revert inner frenchin quote insets "
806 # First, revert style setting
807 i = find_token(document.header, "\\quotes_style frenchin", 0)
809 document.header[i] = "\\quotes_style french"
814 i = find_token(document.body, '\\begin_inset Quotes i', i)
817 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
818 newval = val.replace("i", "f", 1)
821 newval = newval.replace("s", "d")
822 document.body[i] = document.body[i].replace(val, newval)
826 def revert_russianquotes(document):
827 " Revert russian quote insets "
829 # First, revert style setting
830 i = find_token(document.header, "\\quotes_style russian", 0)
832 document.header[i] = "\\quotes_style french"
837 i = find_token(document.body, '\\begin_inset Quotes r', i)
840 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
844 newval = val.replace("r", "g", 1).replace("s", "d")
847 newval = val.replace("r", "f", 1)
848 document.body[i] = document.body[i].replace(val, newval)
852 def revert_dynamicquotes(document):
853 " Revert dynamic quote insets "
855 # First, revert header
856 i = find_token(document.header, "\\dynamic_quotes", 0)
858 del document.header[i]
862 i = find_token(document.header, "\\quotes_style", 0)
864 document.warning("Malformed document! Missing \\quotes_style")
866 style = get_value(document.header, "\\quotes_style", i)
869 if style == "english":
871 elif style == "swedish":
873 elif style == "german":
875 elif style == "polish":
877 elif style == "swiss":
879 elif style == "danish":
881 elif style == "plain":
883 elif style == "british":
885 elif style == "swedishg":
887 elif style == "french":
889 elif style == "frenchin":
891 elif style == "russian":
894 # now transform the insets
897 i = find_token(document.body, '\\begin_inset Quotes x', i)
900 document.body[i] = document.body[i].replace("x", s)
904 def revert_cjkquotes(document):
905 " Revert cjk quote insets "
909 i = find_token(document.header, "\\quotes_style", 0)
911 document.warning("Malformed document! Missing \\quotes_style")
913 style = get_value(document.header, "\\quotes_style", i)
915 global_cjk = style.find("cjk") != -1
918 document.header[i] = "\\quotes_style english"
919 # transform dynamic insets
921 if style == "cjkangle":
925 i = find_token(document.body, '\\begin_inset Quotes x', i)
928 document.body[i] = document.body[i].replace("x", s)
931 cjk_langs = ["chinese-simplified", "chinese-traditional", "japanese", "japanese-cjk", "korean"]
936 k = find_token(document.body, '\\begin_inset Quotes j', i)
939 l = find_end_of_inset(document.body, k)
941 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
945 parent = get_containing_layout(document.body, k)
946 ql = find_token_backwards(document.body, "\\lang", k)
947 if ql == -1 or ql < parent[1]:
948 cjk = document.language in cjk_langs
949 elif document.body[ql].split()[1] in cjk_langs:
951 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
958 replace = [u"\u300E"]
960 replace = ["\\begin_inset Formula $\\llceil$", "\\end_inset"]
964 replace = [u"\u300F"]
966 replace = ["\\begin_inset Formula $\\rrfloor$", "\\end_inset"]
972 replace = [u"\u300C"]
974 replace = ["\\begin_inset Formula $\\lceil$", "\\end_inset"]
978 replace = [u"\u300D"]
980 replace = ["\\begin_inset Formula $\\rfloor$", "\\end_inset"]
982 document.body[k:l+1] = replace
988 k = find_token(document.body, '\\begin_inset Quotes k', i)
991 l = find_end_of_inset(document.body, k)
993 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
997 parent = get_containing_layout(document.body, k)
998 ql = find_token_backwards(document.body, "\\lang", k)
999 if ql == -1 or ql < parent[1]:
1000 cjk = document.language in cjk_langs
1001 elif document.body[ql].split()[1] in cjk_langs:
1003 val = get_value(document.body, "\\begin_inset Quotes", i)[7:]
1008 # inner opening mark
1010 replace = [u"\u3008"]
1012 replace = ["\\begin_inset Formula $\\langle$", "\\end_inset"]
1014 # inner closing mark
1016 replace = [u"\u3009"]
1018 replace = ["\\begin_inset Formula $\\rangle$", "\\end_inset"]
1022 # outer opening mark
1024 replace = [u"\u300A"]
1026 replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$$", "\\end_inset"]
1028 # outer closing mark
1030 replace = [u"\u300B"]
1032 replace = ["\\begin_inset Formula $\\rangle\\kern -2.5pt\\rangle$", "\\end_inset"]
1034 document.body[k:l+1] = replace
1038 def revert_crimson(document):
1039 " Revert native Cochineal/Crimson font definition to LaTeX "
1041 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1043 i = find_token(document.header, "\\font_roman \"cochineal\"", 0)
1046 j = find_token(document.header, "\\font_osf true", 0)
1049 preamble = "\\usepackage"
1051 document.header[j] = "\\font_osf false"
1052 preamble += "[proportional,osf]"
1053 preamble += "{cochineal}"
1054 add_to_preamble(document, [preamble])
1055 document.header[i] = document.header[i].replace("cochineal", "default")
1058 def revert_cochinealmath(document):
1059 " Revert cochineal newtxmath definitions to LaTeX "
1061 if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
1062 i = find_token(document.header, "\\font_math \"cochineal-ntxm\"", 0)
1064 add_to_preamble(document, "\\usepackage[cochineal]{newtxmath}")
1065 document.header[i] = document.header[i].replace("cochineal-ntxm", "auto")
1072 supported_versions = ["2.3.0", "2.3"]
1074 [509, [convert_microtype]],
1075 [510, [convert_dateinset]],
1076 [511, [convert_ibranches]],
1077 [512, [convert_beamer_article_styles]],
1081 [516, [convert_inputenc]],
1083 [518, [convert_iopart]],
1084 [519, [convert_quotestyle]],
1086 [521, [convert_frenchquotes]],
1093 [523, [revert_crimson, revert_cochinealmath]],
1094 [522, [revert_cjkquotes]],
1095 [521, [revert_dynamicquotes]],
1096 [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],
1097 [519, [revert_plainquote]],
1098 [518, [revert_quotestyle]],
1099 [517, [revert_iopart]],
1100 [516, [revert_quotes]],
1102 [514, [revert_urdu, revert_syriac]],
1103 [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
1104 [512, [revert_bosnian, revert_friulan, revert_macedonian, revert_piedmontese, revert_romansh]],
1105 [511, [revert_beamer_article_styles]],
1106 [510, [revert_ibranches]],
1108 [508, [revert_microtype]]
1112 if __name__ == "__main__":