]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyx_2_3.py
New format for changes to iopart.layout made at 91f980cf31.
[features.git] / lib / lyx2lyx / lyx_2_3.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2016 The LyX team
5 #
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.
10 #
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.
15 #
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.
19
20 """ Convert files to the file format generated by lyx 2.3"""
21
22 import re, string
23 import unicodedata
24 import sys, os
25
26 # Uncomment only what you need to import, please.
27
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
33
34 from parser_tools import find_token, find_end_of_inset, get_value, \
35      get_bool_value, get_containing_layout
36
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
42
43 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
44
45 ####################################################################
46 # Private helper functions
47
48
49
50 ###############################################################################
51 ###
52 ### Conversion and reversion routines
53 ###
54 ###############################################################################
55
56 def convert_microtype(document):
57     " Add microtype settings. "
58     i = find_token(document.header, "\\font_tt_scale" , 0)
59     if i == -1:
60         document.warning("Malformed LyX document: Can't find \\font_tt_scale.")
61         i = len(document.header) - 1
62
63     j = find_token(document.preamble, "\\usepackage{microtype}", 0)
64     if j == -1:
65         document.header.insert(i + 1, "\\use_microtype false")
66     else:
67         document.header.insert(i + 1, "\\use_microtype true")
68         del document.preamble[j]
69
70
71 def revert_microtype(document):
72     " Remove microtype settings. "
73     i = find_token(document.header, "\\use_microtype", 0)
74     if i == -1:
75         return
76     use_microtype = get_bool_value(document.header, "\\use_microtype" , i)
77     del document.header[i]
78     if use_microtype:
79         add_to_preamble(document, ["\\usepackage{microtype}"])
80
81
82 def convert_dateinset(document):
83     ' Convert date external inset to ERT '
84     i = 0
85     while True:
86         i = find_token(document.body, "\\begin_inset External", i)
87         if i == -1:
88             return
89         j = find_end_of_inset(document.body, i)
90         if j == -1:
91             document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
92             i += 1
93             continue
94         if get_value(document.body, 'template', i, j) == "Date":
95             document.body[i : j + 1] = put_cmd_in_ert("\\today ")
96         i += 1
97         continue
98
99
100 def convert_inputenc(document):
101     " Replace no longer supported input encoding settings. "
102     i = find_token(document.header, "\\inputenc", 0)
103     if i == -1:
104         return
105     if get_value(document.header, "\\inputencoding", i) == "pt254":
106         document.header[i] = "\\inputencoding pt154"
107     
108
109 def convert_ibranches(document):
110     ' Add "inverted 0" to branch insets'
111     i = 0
112     while True:
113         i = find_token(document.body, "\\begin_inset Branch", i)
114         if i == -1:
115             return
116         document.body.insert(i + 1, "inverted 0")
117         i += 1
118
119
120 def revert_ibranches(document):
121     ' Convert inverted branches to explicit anti-branches'
122     # Get list of branches
123     ourbranches = {}
124     i = 0
125     while True:
126         i = find_token(document.header, "\\branch", i)
127         if i == -1:
128             break
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])
134         else:
135             document.warning("Malformed LyX document: No selection indicator for branch " + branch)
136             selected = 1
137             
138         # the value tells us whether the branch is selected
139         ourbranches[document.header[i][8:].strip()] = selected
140         i += 1
141
142     # Figure out what inverted branches, if any, have been used
143     # and convert them to "Anti-OldBranch"
144     ibranches = {}
145     i = 0
146     while True:
147         i = find_token(document.body, "\\begin_inset Branch", i)
148         if i == -1:
149             break
150         if not document.body[i+1].startswith("inverted "):
151             document.warning("Malformed LyX document: Missing 'inverted' tag!")
152             i += 1
153             continue
154         inverted = document.body[i+1][9]
155         #document.warning(document.body[i+1])
156
157         if inverted == "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
165             else:
166                 antibranch = ibranches[branch]
167             #document.warning(antibranch)
168             document.body[i] = "\\begin_inset Branch " + antibranch
169
170         # remove "inverted" key
171         del document.body[i+1]
172         i += 1
173
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)
177         if i == -1:
178             document.warning("Can't find branch %s even though we found it before!" % (old))
179             continue
180         j = find_token(document.header, "\\end_branch", i)
181         if j == -1:
182             document.warning("Malformed LyX document! Can't find end of branch " + old)
183             continue
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
190
191
192 def revert_beamer_article_styles(document):
193     " Include (scr)article styles in beamer article "
194
195     beamer_articles = ["article-beamer", "scrarticle-beamer"]
196     if document.textclass not in beamer_articles:
197         return
198
199     inclusion = "article.layout"
200     if document.textclass == "scrarticle-beamer":
201         inclusion = "scrartcl.layout"
202
203     while True:
204         i = find_token(document.header, "\\begin_local_layout", 0)
205         if i == -1:
206             k = find_token(document.header, "\\language", 0)
207             if k == -1:
208                 # this should not happen
209                 document.warning("Malformed LyX document! No \\language header found!")
210                 break
211             document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
212             i = find_token(document.header, "\\begin_local_layout", 0)
213         if i != -1:
214             j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
215             if j == -1:
216                 # this should not happen
217                 break
218
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",
224                                           "DefaultFont",
225                                           "     Family                Roman",
226                                           "     Series                Medium",
227                                           "     Shape                 Up",
228                                           "     Size                  Normal",
229                                           "     Color                 None",
230                                           "EndFont",
231                                           "Preamble",
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}",
239                                           "     }",
240                                           "EndPreamble",
241                                           "### End of insertion by lyx2lyx (more [scr]article styles) ###"]
242         return
243
244
245 def convert_beamer_article_styles(document):
246     " Remove included (scr)article styles in beamer article "
247
248     beamer_articles = ["article-beamer", "scrarticle-beamer"]
249     if document.textclass not in beamer_articles:
250         return
251
252     while True:
253         i = find_token(document.header, "\\begin_local_layout", 0)
254         if i == -1:
255             return
256
257         j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
258         if j == -1:
259             # this should not happen
260             break
261
262         k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
263         if k != -1:
264             l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
265             if l == -1:
266                 # this should not happen
267                 document.warning("End of lyx2lyx local layout insertion not found!")
268                 break
269
270             document.header[k : l + 1] = []
271
272         return
273
274
275 def revert_bosnian(document):
276     "Set the document language to English but assure Bosnian output"
277
278     if document.language == "bosnian":
279         document.language = "english"
280         i = find_token(document.header, "\\language bosnian", 0)
281         if i != -1:
282             document.header[i] = "\\language english"
283         j = find_token(document.header, "\\language_package default", 0)
284         if j != -1:
285             document.header[j] = "\\language_package babel"
286         k = find_token(document.header, "\\options", 0)
287         if k != -1:
288             document.header[k] = document.header[k].replace("\\options", "\\options bosnian,")
289         else:
290             l = find_token(document.header, "\\use_default_options", 0)
291             document.header.insert(l + 1, "\\options bosnian")
292
293
294 def revert_friulan(document):
295     "Set the document language to English but assure Friulan output"
296
297     if document.language == "friulan":
298         document.language = "english"
299         i = find_token(document.header, "\\language friulan", 0)
300         if i != -1:
301             document.header[i] = "\\language english"
302         j = find_token(document.header, "\\language_package default", 0)
303         if j != -1:
304             document.header[j] = "\\language_package babel"
305         k = find_token(document.header, "\\options", 0)
306         if k != -1:
307             document.header[k] = document.header[k].replace("\\options", "\\options friulan,")
308         else:
309             l = find_token(document.header, "\\use_default_options", 0)
310             document.header.insert(l + 1, "\\options friulan")
311
312
313 def revert_macedonian(document):
314     "Set the document language to English but assure Macedonian output"
315
316     if document.language == "macedonian":
317         document.language = "english"
318         i = find_token(document.header, "\\language macedonian", 0)
319         if i != -1:
320             document.header[i] = "\\language english"
321         j = find_token(document.header, "\\language_package default", 0)
322         if j != -1:
323             document.header[j] = "\\language_package babel"
324         k = find_token(document.header, "\\options", 0)
325         if k != -1:
326             document.header[k] = document.header[k].replace("\\options", "\\options macedonian,")
327         else:
328             l = find_token(document.header, "\\use_default_options", 0)
329             document.header.insert(l + 1, "\\options macedonian")
330
331
332 def revert_piedmontese(document):
333     "Set the document language to English but assure Piedmontese output"
334
335     if document.language == "piedmontese":
336         document.language = "english"
337         i = find_token(document.header, "\\language piedmontese", 0)
338         if i != -1:
339             document.header[i] = "\\language english"
340         j = find_token(document.header, "\\language_package default", 0)
341         if j != -1:
342             document.header[j] = "\\language_package babel"
343         k = find_token(document.header, "\\options", 0)
344         if k != -1:
345             document.header[k] = document.header[k].replace("\\options", "\\options piedmontese,")
346         else:
347             l = find_token(document.header, "\\use_default_options", 0)
348             document.header.insert(l + 1, "\\options piedmontese")
349
350
351 def revert_romansh(document):
352     "Set the document language to English but assure Romansh output"
353
354     if document.language == "romansh":
355         document.language = "english"
356         i = find_token(document.header, "\\language romansh", 0)
357         if i != -1:
358             document.header[i] = "\\language english"
359         j = find_token(document.header, "\\language_package default", 0)
360         if j != -1:
361             document.header[j] = "\\language_package babel"
362         k = find_token(document.header, "\\options", 0)
363         if k != -1:
364             document.header[k] = document.header[k].replace("\\options", "\\options romansh,")
365         else:
366             l = find_token(document.header, "\\use_default_options", 0)
367             document.header.insert(l + 1, "\\options romansh")
368
369
370 def revert_amharic(document):
371     "Set the document language to English but assure Amharic output"
372
373     if document.language == "amharic":
374         document.language = "english"
375         i = find_token(document.header, "\\language amharic", 0)
376         if i != -1:
377             document.header[i] = "\\language english"
378         j = find_token(document.header, "\\language_package default", 0)
379         if j != -1:
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", "", "",
385                                 "\\backslash",
386                                 "resetdefaultlanguage{amharic}",
387                                 "\\end_layout", "", "\\end_inset", "", "",
388                                 "\\end_layout", ""]
389
390
391 def revert_asturian(document):
392     "Set the document language to English but assure Asturian output"
393
394     if document.language == "asturian":
395         document.language = "english"
396         i = find_token(document.header, "\\language asturian", 0)
397         if i != -1:
398             document.header[i] = "\\language english"
399         j = find_token(document.header, "\\language_package default", 0)
400         if j != -1:
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", "", "",
406                                 "\\backslash",
407                                 "resetdefaultlanguage{asturian}",
408                                 "\\end_layout", "", "\\end_inset", "", "",
409                                 "\\end_layout", ""]
410
411
412 def revert_kannada(document):
413     "Set the document language to English but assure Kannada output"
414
415     if document.language == "kannada":
416         document.language = "english"
417         i = find_token(document.header, "\\language kannada", 0)
418         if i != -1:
419             document.header[i] = "\\language english"
420         j = find_token(document.header, "\\language_package default", 0)
421         if j != -1:
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", "", "",
427                                 "\\backslash",
428                                 "resetdefaultlanguage{kannada}",
429                                 "\\end_layout", "", "\\end_inset", "", "",
430                                 "\\end_layout", ""]
431
432
433 def revert_khmer(document):
434     "Set the document language to English but assure Khmer output"
435
436     if document.language == "khmer":
437         document.language = "english"
438         i = find_token(document.header, "\\language khmer", 0)
439         if i != -1:
440             document.header[i] = "\\language english"
441         j = find_token(document.header, "\\language_package default", 0)
442         if j != -1:
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", "", "",
448                                 "\\backslash",
449                                 "resetdefaultlanguage{khmer}",
450                                 "\\end_layout", "", "\\end_inset", "", "",
451                                 "\\end_layout", ""]
452
453
454 def revert_urdu(document):
455     "Set the document language to English but assure Urdu output"
456
457     if document.language == "urdu":
458         document.language = "english"
459         i = find_token(document.header, "\\language urdu", 0)
460         if i != -1:
461             document.header[i] = "\\language english"
462         j = find_token(document.header, "\\language_package default", 0)
463         if j != -1:
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", "", "",
469                                 "\\backslash",
470                                 "resetdefaultlanguage{urdu}",
471                                 "\\end_layout", "", "\\end_inset", "", "",
472                                 "\\end_layout", ""]
473
474
475 def revert_syriac(document):
476     "Set the document language to English but assure Syriac output"
477
478     if document.language == "syriac":
479         document.language = "english"
480         i = find_token(document.header, "\\language syriac", 0)
481         if i != -1:
482             document.header[i] = "\\language english"
483         j = find_token(document.header, "\\language_package default", 0)
484         if j != -1:
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", "", "",
490                                 "\\backslash",
491                                 "resetdefaultlanguage{syriac}",
492                                 "\\end_layout", "", "\\end_inset", "", "",
493                                 "\\end_layout", ""]
494
495
496 def revert_quotes(document):
497     " Revert Quote Insets in verbatim or Hebrew context to plain quotes "
498
499     # First handle verbatim insets
500     i = 0
501     j = 0
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)
507             if j == -1:
508                 document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
509                 i += 1
510                 continue
511             while True:
512                 k = find_token(document.body, '\\begin_inset Quotes', i, j)
513                 if k == -1:
514                     i += 1
515                     break
516                 l = find_end_of_inset(document.body, k)
517                 if l == -1:
518                     document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
519                     i = k
520                     continue
521                 replace = "\""
522                 if document.body[k].endswith("s"):
523                     replace = "'"
524                 document.body[k:l+1] = [replace]
525         else:
526             i += 1
527             continue
528
529     # Now verbatim layouts
530     i = 0
531     j = 0
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)
537             if j == -1:
538                 document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
539                 i += 1
540                 continue
541             while True:
542                 k = find_token(document.body, '\\begin_inset Quotes', i, j)
543                 if k == -1:
544                     i += 1
545                     break
546                 l = find_end_of_inset(document.body, k)
547                 if l == -1:
548                     document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
549                     i = k
550                     continue
551                 replace = "\""
552                 if document.body[k].endswith("s"):
553                     replace = "'"
554                 document.body[k:l+1] = [replace]
555         else:
556             i += 1
557             continue
558
559     # Now handle Hebrew
560     if not document.language == "hebrew" and find_token(document.body, '\\lang hebrew', 0) == -1:
561         return
562
563     i = 0
564     j = 0
565     while True:
566         k = find_token(document.body, '\\begin_inset Quotes', i)
567         if k == -1:
568             return
569         l = find_end_of_inset(document.body, k)
570         if l == -1:
571             document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
572             i = k
573             continue
574         hebrew = False
575         parent = get_containing_layout(document.body, k)
576         ql = find_token_backwards(document.body, "\\lang", k)
577         if ql == -1 or ql < parent[1]:
578             hebrew = document.language == "hebrew"
579         elif document.body[ql] == "\\lang hebrew":
580             hebrew = True
581         if hebrew:
582             replace = "\""
583             if document.body[k].endswith("s"):
584                 replace = "'"
585             document.body[k:l+1] = [replace]
586         i = l
587     
588
589 def revert_iopart(document):
590     " Input new styles via local layout "
591     if document.textclass != "iopart":
592         return
593
594     i = find_token(document.header, "\\begin_local_layout", 0)
595     if i == -1:
596         k = find_token(document.header, "\\language", 0)
597         if k == -1:
598             # this should not happen
599             document.warning("Malformed LyX document! No \\language header found!")
600             return
601         document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
602         i = k-1
603
604     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
605     if j == -1:
606         # this should not happen
607         document.warning("Malformed LyX document! Can't find end of local layout!")
608         return
609
610     document.header[i+1 : i+1] = [
611         "### Inserted by lyx2lyx (stdlayouts) ###",
612         "Input stdlayouts.inc",
613         "### End of insertion by lyx2lyx (stdlayouts) ###"
614     ]
615     return
616
617
618 def convert_iopart(document):
619     " Remove local layout we added, if it is there "
620     if document.textclass != "iopart":
621         return
622
623     i = find_token(document.header, "\\begin_local_layout", 0)
624     if i == -1:
625         return
626
627     j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
628     if j == -1:
629         # this should not happen
630         document.warning("Malformed LyX document! Can't find end of local layout!")
631         return
632
633     k = find_token(document.header, "### Inserted by lyx2lyx (stdlayouts) ###", i, j)
634     if k != -1:
635         l = find_token(document.header, "### End of insertion by lyx2lyx (stdlayouts) ###", i, j)
636         if l == -1:
637             # this should not happen
638             document.warning("End of lyx2lyx local layout insertion not found!")
639             return
640         if k == i + 1 and l == j - 1:
641             # that was all the local layout there was
642             document.header[i : j + 1] = []
643         else:
644             document.header[k : l + 1] = []
645
646     return
647
648
649 ##
650 # Conversion hub
651 #
652
653 supported_versions = ["2.3.0", "2.3"]
654 convert = [
655            [509, [convert_microtype]],
656            [510, [convert_dateinset]],
657            [511, [convert_ibranches]],
658            [512, [convert_beamer_article_styles]],
659            [513, []],
660            [514, []],
661            [515, []],
662            [516, [convert_inputenc]],
663            [517, []],
664            [518, [convert_iopart]]
665           ]
666
667 revert =  [
668            [517, [revert_iopart]],
669            [516, [revert_quotes]],
670            [515, []],
671            [514, [revert_urdu, revert_syriac]],
672            [513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
673            [512, [revert_bosnian, revert_friulan, revert_macedonian, revert_piedmontese, revert_romansh]],
674            [511, [revert_beamer_article_styles]],
675            [510, [revert_ibranches]],
676            [509, []],
677            [508, [revert_microtype]]
678           ]
679
680
681 if __name__ == "__main__":
682     pass