]> git.lyx.org Git - lyx.git/blob - lib/scripts/layout2layout.py
Forgot these.
[lyx.git] / lib / scripts / layout2layout.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file layout2layout.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Georg Baum
9
10 # Full author contact details are available in file CREDITS
11
12 # This script will update a .layout file to current format
13
14
15 import os, re, string, sys
16
17 # Incremented to format 4, 6 April 2007, lasgouttes
18 # Introduction of generic "Provides" declaration
19
20 # Incremented to format 5, 22 August 2007 by vermeer
21 # InsetLayout material
22
23 # Incremented to format 6, 7 January 2008 by spitz
24 # Requires tag added to layout files
25
26 # Incremented to format 7, 24 March 2008 by rgh
27 # AddToPreamble tag added to layout files
28
29 # Incremented to format 8, 25 July 2008 by rgh
30 # UseModule tag added to layout files
31 # CopyStyle added to InsetLayout
32
33 # Incremented to format 9, 5 October 2008 by rgh
34 # ForcePlain and CustomPars tags added to InsetLayout
35
36 # Incremented to format 10, 6 October 2008 by rgh
37 # Change format of counters
38
39 # Incremented to format 11, 14 October 2008 by rgh
40 # Add ProvidesModule, ExcludesModule tags
41
42 # Incremented to format 12, 10 January 2009 by gb
43 # Add I18NPreamble tag
44
45 # Incremented to format 13, 5 February 2009 by rgh
46 # Add InToc tag for InsetLayout
47
48 # Incremented to format 14, 14 February 2009 by gb
49 # Rename I18NPreamble to BabelPreamble and add LangPreamble
50
51 # Incremented to format 15, 28 May 2009 by lasgouttes
52 # Add new tag OutputFormat; modules can be conditionned on feature 
53 # "from->to".
54
55 # Incremented to format 16, 5 June 2009 by rgh
56 # Add new tags for Text Class:
57 #   HTML Preamble
58 # For Layout:
59 #   HTMLTag, HTMLAttr, HTMLLabel, HTMLLabelAttr, HTMLItem, HTMLItemAttr
60 #   HTMLStyle, and HTMLPreamble
61 # For InsetLayout:
62 #   HTMLTag, HTMLAttr, HTMLStyle, and HTMLPreamble
63 # For Floats:
64 #   HTMLType, HTMLClass, HTMLStyle
65 # These are still to be documented, once everything stabilizes.
66
67 # Do not forget to document format change in Customization
68 # Manual (section "Declaring a new text class").
69
70 currentFormat = 16
71
72
73 def usage(prog_name):
74     return ("Usage: %s inputfile outputfile\n" % prog_name +
75             "or     %s <inputfile >outputfile" % prog_name)
76
77
78 def error(message):
79     sys.stderr.write(message + '\n')
80     sys.exit(1)
81
82
83 def trim_eol(line):
84     " Remove end of line char(s)."
85     if line[-2:-1] == '\r':
86         return line[:-2]
87     elif line[-1:] == '\r' or line[-1:] == '\n':
88         return line[:-1]
89     else:
90         # file with no EOL in last line
91         return line
92
93
94 def read(input):
95     " Read input file and strip lineendings."
96     lines = list()
97     while 1:
98         line = input.readline()
99         if not line:
100             break
101         lines.append(trim_eol(line))
102     return lines
103
104
105 def write(output, lines):
106     " Write output file with native lineendings."
107     for line in lines:
108         output.write(line + os.linesep)
109
110
111 # Concatenates old and new in an intelligent way:
112 # If old is wrapped in ", they are stripped. The result is wrapped in ".
113 def concatenate_label(old, new):
114     # Don't use strip as long as we support python 1.5.2
115     if old[0] == '"':
116         return old[0:-1] + new + '"'
117     else:
118         return '"' + old + new + '"'
119
120 # appends a string to a list unless it's already there
121 def addstring(s, l):
122     if l.count(s) > 0:
123         return
124     l.append(s)
125
126
127 def convert(lines):
128     " Convert to new format."
129     re_Comment = re.compile(r'^(\s*)#')
130     re_Counter = re.compile(r'\s*Counter\s*', re.IGNORECASE)
131     re_Name = re.compile(r'\s*Name\s+(\S+)\s*', re.IGNORECASE)
132     re_UseMod = re.compile(r'^\s*UseModule\s+(.*)', re.IGNORECASE)
133     re_Empty = re.compile(r'^(\s*)$')
134     re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
135     re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
136     re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
137     re_LangPreamble = re.compile(r'^(\s*)LangPreamble', re.IGNORECASE)
138     re_EndLangPreamble = re.compile(r'^(\s*)EndLangPreamble', re.IGNORECASE)
139     re_BabelPreamble = re.compile(r'^(\s*)BabelPreamble', re.IGNORECASE)
140     re_EndBabelPreamble = re.compile(r'^(\s*)EndBabelPreamble', re.IGNORECASE)
141     re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
142     re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
143     re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
144     re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
145     re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
146     re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
147     re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
148     re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
149     re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
150     re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
151     re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
152     re_AMSMaths = re.compile(r'^\s*Input amsmaths.inc\s*')
153     re_AMSMathsPlain = re.compile(r'^\s*Input amsmaths-plain.inc\s*')
154     re_AMSMathsSeq = re.compile(r'^\s*Input amsmaths-seq.inc\s*')
155     re_TocLevel = re.compile(r'^(\s*)(TocLevel)(\s+)(\S+)', re.IGNORECASE)
156     re_I18nPreamble = re.compile(r'^(\s*)I18nPreamble', re.IGNORECASE)
157     re_EndI18nPreamble = re.compile(r'^(\s*)EndI18nPreamble', re.IGNORECASE)
158
159     # counters for sectioning styles (hardcoded in 1.3)
160     counters = {"part"          : "\\Roman{part}",
161                 "chapter"       : "\\arabic{chapter}",
162                 "section"       : "\\arabic{section}",
163                 "subsection"    : "\\arabic{section}.\\arabic{subsection}",
164                 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
165                 "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
166                 "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
167
168     # counters for sectioning styles in appendix (hardcoded in 1.3)
169     appendixcounters = {"chapter"       : "\\Alph{chapter}",
170                         "section"       : "\\Alph{section}",
171                         "subsection"    : "\\arabic{section}.\\arabic{subsection}",
172                         "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
173                         "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
174                         "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
175
176     # Value of TocLevel for sectioning styles
177     toclevels = {"part"          : 0,
178                  "chapter"       : 0,
179                  "section"       : 1,
180                  "subsection"    : 2,
181                  "subsubsection" : 3,
182                  "paragraph"     : 4,
183                  "subparagraph"  : 5}
184
185     i = 0
186     only_comment = 1
187     counter = ""
188     toclevel = ""
189     label = ""
190     labelstring = ""
191     labelstringappendix = ""
192     space1 = ""
193     labelstring_line = -1
194     labelstringappendix_line = -1
195     labeltype_line = -1
196     latextype = ""
197     latextype_line = -1
198     style = ""
199     maxcounter = 0
200     format = 1
201     formatline = 0
202     usemodules = []
203
204     while i < len(lines):
205         # Skip comments and empty lines
206         if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
207             i += 1
208             continue
209
210         # insert file format if not already there
211         if (only_comment):
212             match = re_Format.match(lines[i])
213             if match:
214                 formatline = i
215                 format = int(match.group(4))
216                 if format > 1 and format < currentFormat:
217                     lines[i] = "Format %d" % (format + 1)
218                     only_comment = 0
219                 elif format == currentFormat:
220                     # nothing to do
221                     return format
222                 else:
223                     error('Cannot convert file format %s' % format)
224             else:
225                 lines.insert(i, "Format 2")
226                 only_comment = 0
227                 continue
228
229         # Don't get confused by LaTeX code
230         if re_Preamble.match(lines[i]):
231             i += 1
232             while i < len(lines) and not re_EndPreamble.match(lines[i]):
233                 i += 1
234             continue
235         if re_LangPreamble.match(lines[i]):
236             i += 1
237             while i < len(lines) and not re_EndLangPreamble.match(lines[i]):
238                 i += 1
239             continue
240         if re_BabelPreamble.match(lines[i]):
241             i += 1
242             while i < len(lines) and not re_EndBabelPreamble.match(lines[i]):
243                 i += 1
244             continue
245
246         # This just involved new features, not any changes to old ones
247         if format == 14 or format == 15:
248           i += 1
249           continue
250
251         # Rename I18NPreamble to BabelPreamble
252         if format == 13:
253             match = re_I18nPreamble.match(lines[i])
254             if match:
255                 lines[i] = match.group(1) + "BabelPreamble"
256                 i += 1
257                 match = re_EndI18nPreamble.match(lines[i])
258                 while i < len(lines) and not match:
259                     i += 1
260                     match = re_EndI18nPreamble.match(lines[i])
261                 lines[i] = match.group(1) + "EndBabelPreamble"
262                 i += 1
263                 continue
264
265         # These just involved new features, not any changes to old ones
266         if format == 11 or format == 12:
267           i += 1
268           continue
269
270         if format == 10:
271             match = re_UseMod.match(lines[i])
272             if match:
273                 module = match.group(1)
274                 lines[i] = "DefaultModule " + module
275             i += 1
276             continue
277
278         if format == 9:
279             match = re_Counter.match(lines[i])
280             if match:
281                 counterline = i
282                 i += 1
283                 while i < len(lines):
284                     namem = re_Name.match(lines[i])
285                     if namem:
286                         name = namem.group(1)
287                         lines.pop(i)
288                         lines[counterline] = "Counter %s" % name
289                         # we don't need to increment i
290                         continue
291                     endem = re_End.match(lines[i])
292                     if endem:
293                         i += 1
294                         break
295                     i += 1
296             i += 1
297             continue
298
299         if format == 8:
300             # We want to scan for ams-type includes and, if we find them,
301             # add corresponding UseModule tags to the layout.
302             match = re_AMSMaths.match(lines[i])
303             if match:
304                 addstring("theorems-ams", usemodules)
305                 addstring("theorems-ams-extended", usemodules)
306                 addstring("theorems-sec", usemodules)
307                 lines.pop(i)
308                 continue
309             match = re_AMSMathsPlain.match(lines[i])
310             if match:
311                 addstring("theorems-starred", usemodules)
312                 lines.pop(i)
313                 continue
314             match = re_AMSMathsSeq.match(lines[i])
315             if match:
316                 addstring("theorems-ams", usemodules)
317                 addstring("theorems-ams-extended", usemodules)
318                 lines.pop(i)
319                 continue
320             i += 1
321             continue
322
323         # These just involved new features, not any changes to old ones
324         if format >= 5 and format <= 7:
325           i += 1
326           continue
327
328         if format == 4:
329             # Handle conversion to long CharStyle names
330             match = re_CharStyle.match(lines[i])
331             if match:
332                 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
333                 i += 1
334                 lines.insert(i, "\tLyXType charstyle")
335                 i += 1
336                 lines.insert(i, "")
337                 lines[i] = "\tLabelString %s" % (match.group(3))
338             i += 1
339             continue
340
341         if format == 3:
342             # convert 'providesamsmath x',  'providesmakeidx x',  'providesnatbib x',  'providesurl x' to
343             #         'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
344             # x is either 0 or 1
345             match = re_Provides.match(lines[i])
346             if match:
347                 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
348                                                   match.group(3), match.group(4))
349             i += 1
350             continue
351
352         if format == 2:
353             caption = []
354
355             # delete caption styles
356             match = re_Style.match(lines[i])
357             if match:
358                 style = string.lower(match.group(4))
359                 if style == "caption":
360                     del lines[i]
361                     while i < len(lines) and not re_End.match(lines[i]):
362                         caption.append(lines[i])
363                         del lines[i]
364                     if i == len(lines):
365                         error('Incomplete caption style.')
366                     else:
367                         del lines[i]
368                         continue
369
370             # delete undefinition of caption styles
371             match = re_NoStyle.match(lines[i])
372             if match:
373                 style = string.lower(match.group(4))
374                 if style == "caption":
375                     del lines[i]
376                     continue
377
378             # replace the CopyStyle statement with the definition of the real
379             # style. This may result in duplicate statements, but that is OK
380             # since the second one will overwrite the first one.
381             match = re_CopyStyle.match(lines[i])
382             if match:
383                 style = string.lower(match.group(4))
384                 if style == "caption":
385                     if len(caption) > 0:
386                         lines[i:i+1] = caption
387                     else:
388                         # FIXME: This style comes from an include file, we
389                         # should replace the real style and not this default.
390                         lines[i:i+1] = ['       Margin                First_Dynamic',
391                                         '       LatexType             Command',
392                                         '       LatexName             caption',
393                                         '       NeedProtect           1',
394                                         '       LabelSep              xx',
395                                         '       ParSkip               0.4',
396                                         '       TopSep                0.5',
397                                         '       Align                 Center',
398                                         '       AlignPossible         Center',
399                                         '       LabelType             Sensitive',
400                                         '       LabelString           "Senseless!"',
401                                         '       OptionalArgs          1',
402                                         '       LabelFont',
403                                         '         Series              Bold',
404                                         '       EndFont']
405
406             i += 1
407             continue
408
409         # Delete MaxCounter and remember the value of it
410         match = re_MaxCounter.match(lines[i])
411         if match:
412             level = match.group(4)
413             if string.lower(level) == "counter_chapter":
414                 maxcounter = 0
415             elif string.lower(level) == "counter_section":
416                 maxcounter = 1
417             elif string.lower(level) == "counter_subsection":
418                 maxcounter = 2
419             elif string.lower(level) == "counter_subsubsection":
420                 maxcounter = 3
421             elif string.lower(level) == "counter_paragraph":
422                 maxcounter = 4
423             elif string.lower(level) == "counter_subparagraph":
424                 maxcounter = 5
425             elif string.lower(level) == "counter_enumi":
426                 maxcounter = 6
427             elif string.lower(level) == "counter_enumii":
428                 maxcounter = 7
429             elif string.lower(level) == "counter_enumiii":
430                 maxcounter = 8
431             del lines[i]
432             continue
433
434         # Replace line
435         #
436         # LabelType Counter_EnumI
437         #
438         # with two lines
439         #
440         # LabelType Counter
441         # LabelCounter EnumI
442         #
443         match = re_LabelType.match(lines[i])
444         if match:
445             label = match.group(4)
446             # Remember indenting space for later reuse in added lines
447             space1 = match.group(1)
448             # Remember the line for adding the LabelCounter later.
449             # We can't do it here because it could shift latextype_line etc.
450             labeltype_line = i
451             if string.lower(label[:8]) == "counter_":
452                 counter = string.lower(label[8:])
453                 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
454
455         # Remember the LabelString line
456         match = re_LabelString.match(lines[i])
457         if match:
458             labelstring = match.group(4)
459             labelstring_line = i
460
461         # Remember the LabelStringAppendix line
462         match = re_LabelStringAppendix.match(lines[i])
463         if match:
464             labelstringappendix = match.group(4)
465             labelstringappendix_line = i
466
467         # Remember the LatexType line
468         match = re_LatexType.match(lines[i])
469         if match:
470             latextype = string.lower(match.group(4))
471             latextype_line = i
472
473         # Remember the TocLevel line
474         match = re_TocLevel.match(lines[i])
475         if match:
476             toclevel = string.lower(match.group(4))
477
478         # Reset variables at the beginning of a style definition
479         match = re_Style.match(lines[i])
480         if match:
481             style = string.lower(match.group(4))
482             counter = ""
483             toclevel = ""
484             label = ""
485             space1 = ""
486             labelstring = ""
487             labelstringappendix = ""
488             labelstring_line = -1
489             labelstringappendix_line = -1
490             labeltype_line = -1
491             latextype = ""
492             latextype_line = -1
493
494         if re_End.match(lines[i]):
495
496             # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
497             # (or change the existing LatexType)
498             if string.lower(label) == "bibliography":
499                 if (latextype_line < 0):
500                     lines.insert(i, "%sLatexType Bib_Environment" % space1)
501                     i += 1
502                 else:
503                     lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
504
505             # Change "LabelType Static" to "LabelType Itemize" for itemize environments
506             if latextype == "item_environment" and string.lower(label) == "static":
507                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
508
509             # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
510             if latextype == "item_environment" and string.lower(label) == "counter_enumi":
511                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
512                 # Don't add the LabelCounter line later
513                 counter = ""
514
515             # Replace
516             #
517             # LabelString "Chapter"
518             #
519             # with
520             #
521             # LabelString "Chapter \arabic{chapter}"
522             #
523             # if this style has a counter. Ditto for LabelStringAppendix.
524             # This emulates the hardcoded article style numbering of 1.3
525             #
526             if counter != "":
527                 if counters.has_key(style):
528                     if labelstring_line < 0:
529                         lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
530                         i += 1
531                     else:
532                         new_labelstring = concatenate_label(labelstring, counters[style])
533                         lines[labelstring_line] = re_LabelString.sub(
534                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
535                                 lines[labelstring_line])
536                 if appendixcounters.has_key(style):
537                     if labelstringappendix_line < 0:
538                         lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
539                         i += 1
540                     else:
541                         new_labelstring = concatenate_label(labelstring, appendixcounters[style])
542                         lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
543                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
544                                 lines[labelstringappendix_line])
545
546                 # Now we can safely add the LabelCounter line
547                 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
548                 i += 1
549
550             # Add the TocLevel setting for sectioning styles
551             if toclevel == "" and toclevels.has_key(style) and maxcounter <= toclevels[style]:
552                 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
553                 i += 1
554
555         i += 1
556
557     if usemodules:
558         i = formatline + 1
559         for mod in usemodules:
560             lines.insert(i, "UseModule " + mod)
561             i += 1
562
563     return format + 1
564
565
566 def main(argv):
567
568     # Open files
569     if len(argv) == 1:
570         input = sys.stdin
571         output = sys.stdout
572     elif len(argv) == 3:
573         input = open(argv[1], 'rb')
574         output = open(argv[2], 'wb')
575     else:
576         error(usage(argv[0]))
577
578     # Do the real work
579     lines = read(input)
580     format = 1
581     while (format < currentFormat):
582         format = convert(lines)
583     write(output, lines)
584
585     # Close files
586     if len(argv) == 3:
587         input.close()
588         output.close()
589
590     return 0
591
592
593 if __name__ == "__main__":
594     main(sys.argv)