2 # -*- coding: utf-8 -*-
4 # file layout2layout.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
10 # Full author contact details are available in file CREDITS
12 # This script will update a .layout file to current format
15 import os, re, string, sys
17 # Incremented to format 4, 6 April 2007, lasgouttes
18 # Introduction of generic "Provides" declaration
20 # Incremented to format 5, 22 August 2007 by vermeer
21 # InsetLayout material
23 # Incremented to format 6, 7 January 2008 by spitz
24 # Requires tag added to layout files
26 # Incremented to format 7, 24 March 2008 by rgh
27 # AddToPreamble tag added to layout files
29 # Incremented to format 8, 25 July 2008 by rgh
30 # UseModule tag added to layout files
31 # CopyStyle added to InsetLayout
33 # Incremented to format 9, 5 October 2008 by rgh
34 # ForcePlain and CustomPars tags added to InsetLayout
36 # Incremented to format 10, 6 October 2008 by rgh
37 # Change format of counters
39 # Incremented to format 11, 14 October 2008 by rgh
40 # Add ProvidesModule, ExcludesModule tags
42 # Incremented to format 12, 10 January 2009 by gb
43 # Add I18NPreamble tag
45 # Incremented to format 13, 5 February 2009 by rgh
46 # Add InToc tag for InsetLayout
48 # Incremented to format 14, 14 February 2009 by gb
49 # Rename I18NPreamble to BabelPreamble and add LangPreamble
51 # Incremented to format 15, 28 May 2009 by lasgouttes
52 # Add new tag OutputFormat; modules can be conditionned on feature
55 # Incremented to format 16, 5 June 2009 by rgh
56 # Add new tags for Text Class:
57 # HTMLPreamble, HTMLAddToPreamble
59 # HTMLTag, HTMLAttr, HTMLLabel, HTMLLabelAttr, HTMLItem, HTMLItemAttr
60 # HTMLStyle, and HTMLPreamble
62 # HTMLTag, HTMLAttr, HTMLStyle, and HTMLPreamble
64 # HTMLType, HTMLClass, HTMLStyle
65 # These are still to be documented, once everything stabilizes.
67 # Do not forget to document format change in Customization
68 # Manual (section "Declaring a new text class").
74 return ("Usage: %s inputfile outputfile\n" % prog_name +
75 "or %s <inputfile >outputfile" % prog_name)
79 sys.stderr.write(message + '\n')
84 " Remove end of line char(s)."
85 if line[-2:-1] == '\r':
87 elif line[-1:] == '\r' or line[-1:] == '\n':
90 # file with no EOL in last line
95 " Remove byte order mark."
96 if line[0:3] == "\357\273\277":
103 " Read input file and strip lineendings."
107 line = input.readline()
111 line = trim_bom(line)
113 lines.append(trim_eol(line))
117 def write(output, lines):
118 " Write output file with native lineendings."
120 output.write(line + os.linesep)
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
128 return old[0:-1] + new + '"'
130 return '"' + old + new + '"'
132 # appends a string to a list unless it's already there
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)
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}"}
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}"}
188 # Value of TocLevel for sectioning styles
189 toclevels = {"part" : 0,
203 labelstringappendix = ""
205 labelstring_line = -1
206 labelstringappendix_line = -1
216 while i < len(lines):
217 # Skip comments and empty lines
218 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
222 # insert file format if not already there
224 match = re_Format.match(lines[i])
227 format = int(match.group(4))
228 if format > 1 and format < currentFormat:
229 lines[i] = "Format %d" % (format + 1)
231 elif format == currentFormat:
235 error('Cannot convert file format %s' % format)
237 lines.insert(i, "Format 2")
241 # Don't get confused by LaTeX code
242 if re_Preamble.match(lines[i]):
244 while i < len(lines) and not re_EndPreamble.match(lines[i]):
247 if re_LangPreamble.match(lines[i]):
249 while i < len(lines) and not re_EndLangPreamble.match(lines[i]):
252 if re_BabelPreamble.match(lines[i]):
254 while i < len(lines) and not re_EndBabelPreamble.match(lines[i]):
258 # This just involved new features, not any changes to old ones
259 if format == 14 or format == 15:
263 # Rename I18NPreamble to BabelPreamble
265 match = re_I18nPreamble.match(lines[i])
267 lines[i] = match.group(1) + "BabelPreamble"
269 match = re_EndI18nPreamble.match(lines[i])
270 while i < len(lines) and not match:
272 match = re_EndI18nPreamble.match(lines[i])
273 lines[i] = match.group(1) + "EndBabelPreamble"
277 # These just involved new features, not any changes to old ones
278 if format == 11 or format == 12:
283 match = re_UseMod.match(lines[i])
285 module = match.group(1)
286 lines[i] = "DefaultModule " + module
291 match = re_Counter.match(lines[i])
295 while i < len(lines):
296 namem = re_Name.match(lines[i])
298 name = namem.group(1)
300 lines[counterline] = "Counter %s" % name
301 # we don't need to increment i
303 endem = re_End.match(lines[i])
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])
316 addstring("theorems-ams", usemodules)
317 addstring("theorems-ams-extended", usemodules)
318 addstring("theorems-sec", usemodules)
321 match = re_AMSMathsPlain.match(lines[i])
323 addstring("theorems-starred", usemodules)
326 match = re_AMSMathsSeq.match(lines[i])
328 addstring("theorems-ams", usemodules)
329 addstring("theorems-ams-extended", usemodules)
335 # These just involved new features, not any changes to old ones
336 if format >= 5 and format <= 7:
341 # Handle conversion to long CharStyle names
342 match = re_CharStyle.match(lines[i])
344 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
346 lines.insert(i, "\tLyXType charstyle")
349 lines[i] = "\tLabelString %s" % (match.group(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'
357 match = re_Provides.match(lines[i])
359 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
360 match.group(3), match.group(4))
367 # delete caption styles
368 match = re_Style.match(lines[i])
370 style = string.lower(match.group(4))
371 if style == "caption":
373 while i < len(lines) and not re_End.match(lines[i]):
374 caption.append(lines[i])
377 error('Incomplete caption style.')
382 # delete undefinition of caption styles
383 match = re_NoStyle.match(lines[i])
385 style = string.lower(match.group(4))
386 if style == "caption":
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])
395 style = string.lower(match.group(4))
396 if style == "caption":
398 lines[i:i+1] = caption
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',
410 ' AlignPossible Center',
411 ' LabelType Sensitive',
412 ' LabelString "Senseless!"',
421 # Delete MaxCounter and remember the value of it
422 match = re_MaxCounter.match(lines[i])
424 level = match.group(4)
425 if string.lower(level) == "counter_chapter":
427 elif string.lower(level) == "counter_section":
429 elif string.lower(level) == "counter_subsection":
431 elif string.lower(level) == "counter_subsubsection":
433 elif string.lower(level) == "counter_paragraph":
435 elif string.lower(level) == "counter_subparagraph":
437 elif string.lower(level) == "counter_enumi":
439 elif string.lower(level) == "counter_enumii":
441 elif string.lower(level) == "counter_enumiii":
448 # LabelType Counter_EnumI
455 match = re_LabelType.match(lines[i])
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.
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])
467 # Remember the LabelString line
468 match = re_LabelString.match(lines[i])
470 labelstring = match.group(4)
473 # Remember the LabelStringAppendix line
474 match = re_LabelStringAppendix.match(lines[i])
476 labelstringappendix = match.group(4)
477 labelstringappendix_line = i
479 # Remember the LatexType line
480 match = re_LatexType.match(lines[i])
482 latextype = string.lower(match.group(4))
485 # Remember the TocLevel line
486 match = re_TocLevel.match(lines[i])
488 toclevel = string.lower(match.group(4))
490 # Reset variables at the beginning of a style definition
491 match = re_Style.match(lines[i])
493 style = string.lower(match.group(4))
499 labelstringappendix = ""
500 labelstring_line = -1
501 labelstringappendix_line = -1
506 if re_End.match(lines[i]):
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)
515 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
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])
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
529 # LabelString "Chapter"
533 # LabelString "Chapter \arabic{chapter}"
535 # if this style has a counter. Ditto for LabelStringAppendix.
536 # This emulates the hardcoded article style numbering of 1.3
539 if counters.has_key(style):
540 if labelstring_line < 0:
541 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
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]))
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])
558 # Now we can safely add the LabelCounter line
559 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
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]))
571 for mod in usemodules:
572 lines.insert(i, "UseModule " + mod)
585 input = open(argv[1], 'rb')
586 output = open(argv[2], 'wb')
588 error(usage(argv[0]))
593 while (format < currentFormat):
594 format = convert(lines)
605 if __name__ == "__main__":