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