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"
204 i = find_token(document.header, "\\begin_local_layout", 0)
206 k = find_token(document.header, "\\language", 0)
208 # this should not happen
209 document.warning("Malformed LyX document! No \\language header found!")
211 document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
212 i = find_token(document.header, "\\begin_local_layout", 0)
214 j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
216 # this should not happen
219 document.header[i+1 : i+1] = ["### Inserted by lyx2lyx (more [scr]article styles) ###",
220 "Input " + inclusion,
221 "Input beamer.layout",
222 "Provides geometry 0",
223 "Provides hyperref 0",
232 " \\usepackage{beamerarticle,pgf}",
233 " % this default might be overridden by plain title style",
234 " \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
235 " \\AtBeginDocument{",
236 " \\let\\origtableofcontents=\\tableofcontents",
237 " \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
238 " \\def\\gobbletableofcontents#1{\\origtableofcontents}",
241 "### End of insertion by lyx2lyx (more [scr]article styles) ###"]
245 def convert_beamer_article_styles(document):
246 " Remove included (scr)article styles in beamer article "
248 beamer_articles = ["article-beamer", "scrarticle-beamer"]
249 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
262 k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
264 l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
266 # this should not happen
267 document.warning("End of lyx2lyx local layout insertion not found!")
270 document.header[k : l + 1] = []
275 def revert_bosnian(document):
276 "Set the document language to English but assure Bosnian output"
278 if document.language == "bosnian":
279 document.language = "english"
280 i = find_token(document.header, "\\language bosnian", 0)
282 document.header[i] = "\\language english"
283 j = find_token(document.header, "\\language_package default", 0)
285 document.header[j] = "\\language_package babel"
286 k = find_token(document.header, "\\options", 0)
288 document.header[k] = document.header[k].replace("\\options", "\\options bosnian,")
290 l = find_token(document.header, "\\use_default_options", 0)
291 document.header.insert(l + 1, "\\options bosnian")
294 def revert_friulan(document):
295 "Set the document language to English but assure Friulan output"
297 if document.language == "friulan":
298 document.language = "english"
299 i = find_token(document.header, "\\language friulan", 0)
301 document.header[i] = "\\language english"
302 j = find_token(document.header, "\\language_package default", 0)
304 document.header[j] = "\\language_package babel"
305 k = find_token(document.header, "\\options", 0)
307 document.header[k] = document.header[k].replace("\\options", "\\options friulan,")
309 l = find_token(document.header, "\\use_default_options", 0)
310 document.header.insert(l + 1, "\\options friulan")
313 def revert_macedonian(document):
314 "Set the document language to English but assure Macedonian output"
316 if document.language == "macedonian":
317 document.language = "english"
318 i = find_token(document.header, "\\language macedonian", 0)
320 document.header[i] = "\\language english"
321 j = find_token(document.header, "\\language_package default", 0)
323 document.header[j] = "\\language_package babel"
324 k = find_token(document.header, "\\options", 0)
326 document.header[k] = document.header[k].replace("\\options", "\\options macedonian,")
328 l = find_token(document.header, "\\use_default_options", 0)
329 document.header.insert(l + 1, "\\options macedonian")
332 def revert_piedmontese(document):
333 "Set the document language to English but assure Piedmontese output"
335 if document.language == "piedmontese":
336 document.language = "english"
337 i = find_token(document.header, "\\language piedmontese", 0)
339 document.header[i] = "\\language english"
340 j = find_token(document.header, "\\language_package default", 0)
342 document.header[j] = "\\language_package babel"
343 k = find_token(document.header, "\\options", 0)
345 document.header[k] = document.header[k].replace("\\options", "\\options piedmontese,")
347 l = find_token(document.header, "\\use_default_options", 0)
348 document.header.insert(l + 1, "\\options piedmontese")
351 def revert_romansh(document):
352 "Set the document language to English but assure Romansh output"
354 if document.language == "romansh":
355 document.language = "english"
356 i = find_token(document.header, "\\language romansh", 0)
358 document.header[i] = "\\language english"
359 j = find_token(document.header, "\\language_package default", 0)
361 document.header[j] = "\\language_package babel"
362 k = find_token(document.header, "\\options", 0)
364 document.header[k] = document.header[k].replace("\\options", "\\options romansh,")
366 l = find_token(document.header, "\\use_default_options", 0)
367 document.header.insert(l + 1, "\\options romansh")
370 def revert_amharic(document):
371 "Set the document language to English but assure Amharic output"
373 if document.language == "amharic":
374 document.language = "english"
375 i = find_token(document.header, "\\language amharic", 0)
377 document.header[i] = "\\language english"
378 j = find_token(document.header, "\\language_package default", 0)
380 document.header[j] = "\\language_package default"
381 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{amharic}}"])
382 document.body[2 : 2] = ["\\begin_layout Standard",
383 "\\begin_inset ERT", "status open", "",
384 "\\begin_layout Plain Layout", "", "",
386 "resetdefaultlanguage{amharic}",
387 "\\end_layout", "", "\\end_inset", "", "",
391 def revert_asturian(document):
392 "Set the document language to English but assure Asturian output"
394 if document.language == "asturian":
395 document.language = "english"
396 i = find_token(document.header, "\\language asturian", 0)
398 document.header[i] = "\\language english"
399 j = find_token(document.header, "\\language_package default", 0)
401 document.header[j] = "\\language_package default"
402 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{asturian}}"])
403 document.body[2 : 2] = ["\\begin_layout Standard",
404 "\\begin_inset ERT", "status open", "",
405 "\\begin_layout Plain Layout", "", "",
407 "resetdefaultlanguage{asturian}",
408 "\\end_layout", "", "\\end_inset", "", "",
412 def revert_kannada(document):
413 "Set the document language to English but assure Kannada output"
415 if document.language == "kannada":
416 document.language = "english"
417 i = find_token(document.header, "\\language kannada", 0)
419 document.header[i] = "\\language english"
420 j = find_token(document.header, "\\language_package default", 0)
422 document.header[j] = "\\language_package default"
423 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{kannada}}"])
424 document.body[2 : 2] = ["\\begin_layout Standard",
425 "\\begin_inset ERT", "status open", "",
426 "\\begin_layout Plain Layout", "", "",
428 "resetdefaultlanguage{kannada}",
429 "\\end_layout", "", "\\end_inset", "", "",
433 def revert_khmer(document):
434 "Set the document language to English but assure Khmer output"
436 if document.language == "khmer":
437 document.language = "english"
438 i = find_token(document.header, "\\language khmer", 0)
440 document.header[i] = "\\language english"
441 j = find_token(document.header, "\\language_package default", 0)
443 document.header[j] = "\\language_package default"
444 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{khmer}}"])
445 document.body[2 : 2] = ["\\begin_layout Standard",
446 "\\begin_inset ERT", "status open", "",
447 "\\begin_layout Plain Layout", "", "",
449 "resetdefaultlanguage{khmer}",
450 "\\end_layout", "", "\\end_inset", "", "",
454 def revert_urdu(document):
455 "Set the document language to English but assure Urdu output"
457 if document.language == "urdu":
458 document.language = "english"
459 i = find_token(document.header, "\\language urdu", 0)
461 document.header[i] = "\\language english"
462 j = find_token(document.header, "\\language_package default", 0)
464 document.header[j] = "\\language_package default"
465 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{urdu}}"])
466 document.body[2 : 2] = ["\\begin_layout Standard",
467 "\\begin_inset ERT", "status open", "",
468 "\\begin_layout Plain Layout", "", "",
470 "resetdefaultlanguage{urdu}",
471 "\\end_layout", "", "\\end_inset", "", "",
475 def revert_syriac(document):
476 "Set the document language to English but assure Syriac output"
478 if document.language == "syriac":
479 document.language = "english"
480 i = find_token(document.header, "\\language syriac", 0)
482 document.header[i] = "\\language english"
483 j = find_token(document.header, "\\language_package default", 0)
485 document.header[j] = "\\language_package default"
486 add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{syriac}}"])
487 document.body[2 : 2] = ["\\begin_layout Standard",
488 "\\begin_inset ERT", "status open", "",
489 "\\begin_layout Plain Layout", "", "",
491 "resetdefaultlanguage{syriac}",
492 "\\end_layout", "", "\\end_inset", "", "",
496 def revert_quotes(document):
497 " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
499 # First handle verbatim insets
502 while i < len(document.body):
503 words = document.body[i].split()
504 if len(words) > 1 and words[0] == "\\begin_inset" and \
505 ( words[1] in ["ERT", "listings"] or ( len(words) > 2 and words[2] in ["URL", "Chunk", "Sweave", "S/R"]) ):
506 j = find_end_of_inset(document.body, i)
508 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
512 k = find_token(document.body, '\\begin_inset Quotes', i, j)
516 l = find_end_of_inset(document.body, k)
518 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
522 if document.body[k].endswith("s"):
524 document.body[k:l+1] = [replace]
529 # Now verbatim layouts
532 while i < len(document.body):
533 words = document.body[i].split()
534 if len(words) > 1 and words[0] == "\\begin_layout" and \
535 words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
536 j = find_end_of_layout(document.body, i)
538 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
542 k = find_token(document.body, '\\begin_inset Quotes', i, j)
546 l = find_end_of_inset(document.body, k)
548 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
552 if document.body[k].endswith("s"):
554 document.body[k:l+1] = [replace]
563 k = find_token(document.body, '\\begin_inset Quotes', i)
566 l = find_end_of_inset(document.body, k)
568 document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
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":
580 if document.body[k].endswith("s"):
582 document.body[k:l+1] = [replace]
591 supported_versions = ["2.3.0", "2.3"]
593 [509, [convert_microtype]],
594 [510, [convert_dateinset]],
595 [511, [convert_ibranches]],
596 [512, [convert_beamer_article_styles]],
600 [516, [convert_inputenc]],
605 [516, [revert_quotes]],
607 [514, [revert_urdu, revert_syriac]],
608 [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
609 [512, [revert_bosnian, revert_friulan, revert_macedonian, revert_piedmontese, revert_romansh]],
610 [511, [revert_beamer_article_styles]],
611 [510, [revert_ibranches]],
613 [508, [revert_microtype]]
617 if __name__ == "__main__":