1 # -*- coding: utf-8 -*-
3 # file layout2layout.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
9 # Full author contact details are available in file CREDITS
11 # This script will update a .layout file to current format
13 # The latest layout format is also defined in src/TextClass.cpp
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 conditioned 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
66 # Incremented to format 17, 12 August 2009 by rgh
67 # Add IfStyle and IfCounter tags for layout.
69 # Incremented to format 18, 27 October 2009 by rgh
70 # Added some new tags for HTML output.
72 # Incremented to format 19, 17 November 2009 by rgh
73 # Added InPreamble tag.
75 # Incremented to format 20, 17 December 2009 by rgh
76 # Added ContentAsLabel tag.
78 # Incremented to format 21, 12 January 2010 by rgh
79 # Added HTMLTocLayout and HTMLTitle tags.
81 # Incremented to format 22, 20 January 2010 by rgh
82 # Added HTMLFormat tag to Counters.
84 # Incremented to format 23, 13 February 2010 by spitz
85 # Added Spellcheck tag.
87 # Incremented to format 24, 5 March 2010 by rgh
88 # Changed LaTeXBuiltin tag to NeedsFloatPkg and
89 # added new tag ListCommand.
91 # Incremented to format 25, 12 March 2010 by rgh
92 # Added RefPrefix tag for layouts and floats.
94 # Incremented to format 26, 29 March 2010 by rgh
97 # Incremented to format 27, 4 June 2010 by rgh
98 # Added RequiredArgs tag.
100 # Incremented to format 28, 6 August 2010 by lasgouttes
101 # Added ParbreakIsNewline tag for Layout and InsetLayout.
103 # Incremented to format 29, 10 August 2010 by rgh
104 # Changed Custom:Style, CharStyle:Style, and Element:Style
105 # uniformly to Flex:Style.
107 # Incremented to format 30, 13 August 2010 by rgh
108 # Introduced ResetsFont tag for InsetLayout.
110 # Incremented to format 31, 12 January 2011 by rgh
111 # Introducted NoCounter tag.
113 # Incremented to format 32, 30 January 2011 by forenr
114 # Added Display tag for InsetLayout
116 # Incremented to format 33, 2 February 2011 by rgh
117 # Changed NeedsFloatPkg to UsesFloatPkg
119 # Incremented to format 34, 28 March 2011 by rgh
120 # Remove obsolete Fill_(Top|Bottom) tags
122 # Incremented to format 35, 28 March 2011 by rgh
123 # Try to add "Flex:" to any flex insets that don't have it.
125 # Incremented to format 36, 7 December 2011, by rgh
126 # Added HTMLStyles and AddToHTMLStyles tags.
128 # Incremented to format 37, 29 February 2012 by jrioux
129 # Implement the citation engine machinery in layouts.
130 # Change CiteFormat to CiteFormat (default|authoryear|numerical).
132 # Incremented to format 38, 08 April 2012 by gb
133 # Introduce LangPreamble and BabelPreamble for InsetLayout.
135 # Incremented to format 39, 15 April 2012 by sanda
136 # Introduce styling of branches via "InsetLayout Branch:".
138 # Incremented to format 40, 10 October 2012 by rgh
139 # Re-do layout names for layout categories
141 # Incremented to format 41, 20 November 2012 by spitz
142 # New Argument syntax
144 # Incremented to format 42, 22 December 2012 by spitz
145 # New Style tag "ItemCommand"
147 # Incremented to format 43, 30 December 2012 by spitz
148 # Extended InsetCaption format
150 # Incremented to format 44, 9 February 2013 by rgh
151 # Remove COUNTER label style; rename as STATIC
152 # Rename TOP_ENVIRONMENT to ABOVE and CENTERED_TOP_ENVIRONMENT to CENTERED
154 # Incremented to format 45, 12 February 2013 by rgh
155 # New Tag "NoInsetLayout"
157 # Incremented to format 46, 15 May 2013 by gb
158 # New Tag "ForceLocal"
160 # Incremented to format 47, 23 May 2013 by rgh
161 # Add PackageOptions tag
163 # Incremented to format 48, 31 May 2013 by rgh
164 # Add InitialValue tag for counters
166 # Incremented to format 49, 10 Feb 2014 by gb
167 # Change default of "ResetsFont" tag to false
169 # Incremented to format 50, 9 May 2014 by forenr
170 # Removal of "Separator" layouts
172 # Incremented to format 51, 29 May 2014 by spitz
173 # New Style tag "ToggleIndent"
175 # Incremented to format 52, 1 December 2014 by spitz
176 # New InsetLayout tag "ForceOwnlines"
178 # Incremented to format 53, 7 December 2014 by spitz
179 # New InsetLayout tag "ObsoletedBy"
181 # Incremented to format 54, 11 Jan 2014 by gb
182 # New InsetLayout tag "FixedWidthPreambleEncoding"
184 # Incremented to format 55, 20 April 2015 by spitz
185 # New InsetLayout and Layout tags "PassThruChars"
187 # Incremented to format 56, 20 May 2015 by spitz
188 # New Float tags "AllowedPlacement", "AllowsWide", "AllowsSideways"
190 # Incremented to format 57, 30 May 2015 by spitz
191 # New Layout tag "ParagraphGroup"
193 # Incremented to format 58, 5 December 2015, by rgh
194 # New Layout tag "ProvideStyle"
195 # Change "IfStyle" to "ModifyStyle"
197 # Incremented to format 59, 22 November 2015 by gm
198 # New Tag "OutlinerName"
199 # New Layout tags "AddToToc", "IsTocCaption"
200 # New Layout argument tag "IsTocCaption"
202 # Incremented to format 60, 25 March 2016 by lasgouttes
203 # Rename caption subtype LongTableNoNumber to Unnumbered
205 # Incremented to format 61, 14 October 2016 by spitz
206 # New Layout tags "ResumeCounter", "StepMasterCounter"
208 # Do not forget to document format change in Customization
209 # Manual (section "Declaring a new text class").
211 # You might also want to consider running the
212 # development/tools/updatelayouts.py script to update all
213 # layout files to the new format.
216 import os, re, string, sys
219 # Provide support for both python 2 and 3
220 # (copied from lyx2lyx)
221 PY2 = sys.version_info[0] == 2
223 # argparse returns strings in the commandline encoding, we need to convert.
224 # sys.getdefaultencoding() would not always be correct, see
225 # http://legacy.python.org/dev/peps/pep-0383/
227 return arg.decode(sys.getfilesystemencoding())
230 # End of code to support for both python 2 and 3
234 sys.stderr.write(message + '\n')
239 " Remove byte order mark."
240 if line[0:3] == "\357\273\277":
247 " Read input file and strip lineendings."
248 lines = source.read().splitlines() or ['']
249 lines[0] = trim_bom(lines[0])
253 def write(output, lines):
254 " Write output file with native lineendings."
255 output.write(os.linesep.join(lines) + os.linesep)
258 # Concatenates old and new in an intelligent way:
259 # If old is wrapped in ", they are stripped. The result is wrapped in ".
260 def concatenate_label(old, new):
261 # Don't use strip as long as we support python 1.5.2
263 return old[0:-1] + new + '"'
265 return '"' + old + new + '"'
267 # appends a string to a list unless it's already there
274 def convert(lines, end_format):
275 " Convert to new format."
276 re_Comment = re.compile(r'^(\s*)#')
277 re_Counter = re.compile(r'\s*Counter\s*', re.IGNORECASE)
278 re_Name = re.compile(r'\s*Name\s+(\S+)\s*', re.IGNORECASE)
279 re_UseMod = re.compile(r'^\s*UseModule\s+(.*)', re.IGNORECASE)
280 re_Empty = re.compile(r'^(\s*)$')
281 re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
282 re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
283 re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
284 re_LangPreamble = re.compile(r'^(\s*)LangPreamble', re.IGNORECASE)
285 re_EndLangPreamble = re.compile(r'^(\s*)EndLangPreamble', re.IGNORECASE)
286 re_BabelPreamble = re.compile(r'^(\s*)BabelPreamble', re.IGNORECASE)
287 re_EndBabelPreamble = re.compile(r'^(\s*)EndBabelPreamble', re.IGNORECASE)
288 re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
289 re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
290 re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
291 re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
292 re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
293 re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
294 re_IfStyle = re.compile(r'^(\s*)IfStyle(\s+\S+)', re.IGNORECASE)
295 re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
296 re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
297 re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
298 re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
299 re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
300 re_CiteFormat = re.compile(r'^(\s*)(CiteFormat)(?:(\s*)()|(\s+)(default|authoryear|numerical))', re.IGNORECASE)
301 re_AMSMaths = re.compile(r'^\s*Input ams(?:math|def)s.inc\s*')
302 re_AMSMathsPlain = re.compile(r'^\s*Input amsmaths-plain.inc\s*')
303 re_AMSMathsSeq = re.compile(r'^\s*Input amsmaths-seq.inc\s*')
304 re_TocLevel = re.compile(r'^(\s*)(TocLevel)(\s+)(\S+)', re.IGNORECASE)
305 re_I18nPreamble = re.compile(r'^(\s*)I18nPreamble', re.IGNORECASE)
306 re_EndI18nPreamble = re.compile(r'^(\s*)EndI18nPreamble', re.IGNORECASE)
307 re_Float = re.compile(r'^\s*Float\s*$', re.IGNORECASE)
308 re_Type = re.compile(r'\s*Type\s+(\w+)', re.IGNORECASE)
309 re_Builtin = re.compile(r'^(\s*)LaTeXBuiltin\s+(\w*)', re.IGNORECASE)
310 re_True = re.compile(r'^\s*(?:true|1)\s*$', re.IGNORECASE)
311 re_InsetLayout = re.compile(r'^\s*InsetLayout\s+(?:Custom|CharStyle|Element):(\S+)\s*$', re.IGNORECASE)
312 re_ResetsFont = re.compile(r'^(\s*)ResetsFont(\s+)(\S+)$', re.IGNORECASE)
314 re_QInsetLayout = re.compile(r'^\s*InsetLayout\s+"(?:Custom|CharStyle|Element):([^"]+)"\s*$', re.IGNORECASE)
315 re_InsetLayout_CopyStyle = re.compile(r'^\s*CopyStyle\s+(?:Custom|CharStyle|Element):(\S+)\s*$', re.IGNORECASE)
316 re_QInsetLayout_CopyStyle = re.compile(r'^\s*CopyStyle\s+"(?:Custom|CharStyle|Element):([^"]+)"\s*$', re.IGNORECASE)
317 re_NeedsFloatPkg = re.compile(r'^(\s*)NeedsFloatPkg\s+(\w+)\s*$', re.IGNORECASE)
318 re_Fill = re.compile(r'^\s*Fill_(?:Top|Bottom).*$', re.IGNORECASE)
319 re_InsetLayout2 = re.compile(r'^\s*InsetLayout\s+(\S+)\s*$', re.IGNORECASE)
321 re_QInsetLayout2 = re.compile(r'^\s*InsetLayout\s+"([^"]+)"\s*$', re.IGNORECASE)
322 re_IsFlex = re.compile(r'\s*LyXType.*$', re.IGNORECASE)
323 re_CopyStyle2 = re.compile(r'(\s*CopyStyle\s+)"?([^"]+)"?\s*$')
324 re_Separator = re.compile(r'^(?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE)
326 re_Declaration = re.compile(r'^#\s*\\Declare\w+Class.*$')
327 re_ExtractCategory = re.compile(r'^(#\s*\\Declare\w+Class(?:\[[^]]*?\])?){([^(]+?)\s+\(([^)]+?)\)\s*}\s*$')
328 ConvDict = {"article": "Articles", "book" : "Books", "letter" : "Letters", "report": "Reports", \
329 "presentation" : "Presentations", "curriculum vitae" : "Curricula Vitae", "handout" : "Handouts"}
331 re_OptArgs = re.compile(r'^(\s*)OptionalArgs(\s+)(\d+)\D*$', re.IGNORECASE)
332 re_ReqArgs = re.compile(r'^(\s*)RequiredArgs(\s+)(\d+)\D*$', re.IGNORECASE)
334 # various changes associated with changing how chapters are handled
335 re_LabelTypeIsCounter = re.compile(r'^(\s*)LabelType(\s*)Counter\s*$', re.IGNORECASE)
336 re_TopEnvironment = re.compile(r'^(\s*)LabelType(\s+)Top_Environment\s*$', re.IGNORECASE)
337 re_CenteredEnvironment = re.compile(r'^(\s*)LabelType(\s+)Centered_Top_Environment\s*$', re.IGNORECASE)
338 re_ChapterStyle = re.compile(r'^\s*Style\s+Chapter\s*$', re.IGNORECASE)
339 re_InsetLayout_CaptionLTNN = re.compile(r'^(\s*InsetLayout\s+)(Caption:LongTableNonumber)', re.IGNORECASE)
342 # counters for sectioning styles (hardcoded in 1.3)
343 counters = {"part" : "\\Roman{part}",
344 "chapter" : "\\arabic{chapter}",
345 "section" : "\\arabic{section}",
346 "subsection" : "\\arabic{section}.\\arabic{subsection}",
347 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
348 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
349 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
351 # counters for sectioning styles in appendix (hardcoded in 1.3)
352 appendixcounters = {"chapter" : "\\Alph{chapter}",
353 "section" : "\\Alph{section}",
354 "subsection" : "\\arabic{section}.\\arabic{subsection}",
355 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
356 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
357 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
359 # Value of TocLevel for sectioning styles
360 toclevels = {"part" : -1,
374 labelstringappendix = ""
376 labelstring_line = -1
377 labelstringappendix_line = -1
390 isflexlayout = False # only used for 48 -> 49
391 # Whether a style is inherited (works only for CopyStyle currently,
392 # not for true inherited styles, see bug 8920
393 inherited = False # only used for 48 -> 49
394 resetsfont_found = False # only used for 48 -> 49
396 while i < len(lines):
397 # Skip comments and empty lines
398 if (re_Comment.match(lines[i]) or re_Empty.match(lines[i])):
399 # We need to deal with this conversion here, because it happens
400 # inside the initial comment block.
401 if only_comment and format == 39:
402 match = re_ExtractCategory.match(lines[i])
404 lpre = match.group(1)
405 lcat = match.group(2)
406 lnam = match.group(3)
408 lcat = ConvDict[lcat]
409 lines[i] = lpre + "{" + lnam + "}"
410 lines.insert(i+1, "# \\DeclareCategory{" + lcat + "}")
415 # insert file format if not already there
417 match = re_Format.match(lines[i])
420 format = int(match.group(4))
421 if format > 1 and format < end_format:
422 lines[i] = "Format %d" % (format + 1)
424 elif format == end_format:
428 error('Cannot convert file format %s to %s' % (format, end_format))
430 lines.insert(i, "Format 2")
434 # Don't get confused by LaTeX code
435 if re_Preamble.match(lines[i]):
437 while i < len(lines) and not re_EndPreamble.match(lines[i]):
440 if re_LangPreamble.match(lines[i]):
442 while i < len(lines) and not re_EndLangPreamble.match(lines[i]):
445 if re_BabelPreamble.match(lines[i]):
447 while i < len(lines) and not re_EndBabelPreamble.match(lines[i]):
457 match = re_InsetLayout_CaptionLTNN.match(lines[i])
461 # '^(\s*InsetLayout\s+)(Caption:LongTableNonumber)'
462 lead = match.group(1)
463 lines[i] = lead + "Caption:Unnumbered"
473 match = re_IfStyle.match(lines[i])
477 # r'^(\s*)IfStyle(\s+\S+)
478 lead = match.group(1)
479 trail = match.group(2)
480 lines[i] = lead + "ModifyStyle" + trail
484 if format >= 50 and format <= 56:
492 # delete separator styles
493 match = re_Style.match(lines[i])
495 style = string.lower(match.group(4))
496 if re_Separator.match(style):
498 while i < len(lines) and not re_End.match(lines[i]):
499 separator.append(lines[i])
502 error('Incomplete separator style.')
507 # delete undefinition of separator styles
508 match = re_NoStyle.match(lines[i])
510 style = string.lower(match.group(4))
511 if re_Separator.match(style):
515 # replace the CopyStyle statement with the definition of the real
516 # style. This may result in duplicate statements, but that is OK
517 # since the second one will overwrite the first one.
518 match = re_CopyStyle.match(lines[i])
520 style = string.lower(match.group(4))
521 if re_Separator.match(style):
522 if len(separator) > 0:
523 lines[i:i+1] = separator
525 # FIXME: If this style was redefined in an include file,
526 # we should replace the real style and not this default.
527 lines[i:i+1] = [' Category MainText',
530 ' LatexType Paragraph',
535 ' LabelString "--- Separate Environment ---"',
547 # The default of ResetsFont in LyX changed from true to false,
548 # because it is now used for all InsetLayouts, not only flex ones.
549 # Therefore we need to set it to true for all flex insets which do
550 # do not already have a ResetsFont.
551 match = re_InsetLayout2.match(lines[i])
556 name = string.lower(match.group(1))
557 if name != "flex" and name != "\"flex\"" and name[0:5] != "flex:" and name [0:6] != "\"flex:":
561 resetsfont_found = False
564 while i < len(lines):
565 match = re_ResetsFont.match(lines[i])
567 resetsfont_found = True
569 match = re_CopyStyle.match(lines[i])
573 match = re_End.match(lines[i])
577 if not resetsfont_found and not inherited:
578 lines.insert(i, "\tResetsFont true")
582 if format >= 44 and format <= 47:
588 match = re_LabelTypeIsCounter.match(lines[i])
591 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above"
593 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Static"
595 match = re_TopEnvironment.match(lines[i])
597 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above"
599 match = re_CenteredEnvironment.match(lines[i])
601 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Centered"
604 match = re_Style.match(lines[i])
608 match = re_ChapterStyle.match(lines[i])
616 if lines[i] == "InsetLayout Caption":
617 lines[i] = "InsetLayout Caption:Standard"
627 # reset counters on Style beginning
628 match = re_Style.match(lines[i])
634 match = re_OptArgs.match(lines[i])
636 # Save number of optional arguments
637 space1 = match.group(1)
638 opts = int(match.group(3))
639 # OptionalArgs 0 > ResetArgs 1
641 lines[i] = space1 + "ResetArgs\t1"
646 match = re_ReqArgs.match(lines[i])
648 # Save number of required arguments
649 space1 = match.group(1)
650 reqs = int(match.group(3))
653 # Insert the required number of arguments at the end of the style definition
654 match = re_End.match(lines[i])
657 # First the optionals (this is the required order pre 2.1)
660 newarg = [ '%sArgument 1' % (space1),
661 '%s\tLabelString\t\"Optional Layout Argument\"' % (space1),
662 '%sEndArgument' % (space1)]
665 while actopt < (opts + 1):
666 newarg += [ '%sArgument %d' % (space1, actopt),
667 '%s\tLabelString\t\"Optional Layout Argument %d\"' % (space1, actopt),
668 '%sEndArgument' % (space1)]
670 # Now the mandatories
673 while actopt < (opts + reqs + 1):
674 newarg += [ '%sArgument %d' % (space1, actopt),
675 '%s\tLabelString\t"Required Layout Argument %d"' % (space1, actopt - opts),
676 '%s\tMandatory\t1' % (space1),
677 '%sEndArgument' % (space1)]
679 # Since we replace the "End" line, re-add this line
682 lines[i:i+1] = newarg
691 # There is a conversion with format 40, but it is done within the
692 # initial comment block and so is above.
696 if format == 37 or format == 38:
701 match = re_CiteFormat.match(lines[i]);
702 if match and match.group(4) == "":
703 lines[i] = match.group(0) + " default"
712 match = re_QInsetLayout2.match(lines[i])
714 match = re_InsetLayout2.match(lines[i])
716 match = re_CopyStyle2.match(lines[i])
720 style = match.group(2)
722 if flexstyles.count(style):
723 lines[i] = match.group(1) + "\"Flex:" + style + "\""
727 name = match.group(1)
728 names = name.split(":", 1)
729 if len(names) > 1 and names[0] == "Flex":
734 for j in range(i + 1, len(lines)):
735 if re_IsFlex.match(lines[j]):
738 if re_End.match(lines[j]):
742 flexstyles.append(name)
743 lines[i] = "InsetLayout \"Flex:" + name + "\""
749 m = re_Fill.match(lines[i])
756 match = re_NeedsFloatPkg.match(lines[i])
758 space = match.group(1)
760 lines[i] = space + "UsesFloatPkg " + val
762 if val == '1' or val.lower() == 'true':
764 lines.insert(i, space + "IsPredefined " + newval)
770 if format >= 29 and format <= 31:
775 match = re_InsetLayout.match(lines[i])
777 lines[i] = "InsetLayout Flex:" + match.group(1)
779 match = re_QInsetLayout.match(lines[i])
781 lines[i] = "InsetLayout \"Flex:" + match.group(1) + "\""
783 match = re_InsetLayout_CopyStyle.match(lines[i])
785 lines[i] = "\tCopyStyle Flex:" + match.group(1)
787 match = re_QInsetLayout_CopyStyle.match(lines[i])
789 lines[i] = "\tCopyStyle \"Flex:" + match.group(1) + "\""
794 if format >= 24 and format <= 27:
799 match = re_Float.match(lines[i])
803 # we need to do two things:
804 # (i) Convert Builtin to NeedsFloatPkg
805 # (ii) Write ListCommand lines for the builtin floats table and figure
808 while True and i < len(lines):
809 m1 = re_End.match(lines[i])
812 line = " ListCommand " + cmd
813 lines.insert(i, line)
816 m2 = re_Builtin.match(lines[i])
822 if re_True.match(arg):
826 lines[i] = ws1 + "NeedsFloatPkg " + newarg
827 m3 = re_Type.match(lines[i])
830 fltype = fltype.lower()
831 if fltype == "table":
833 elif fltype == "figure":
834 cmd = "listoffigures"
835 # else unknown, which is why we're doing this
839 # This just involved new features, not any changes to old ones
840 if format >= 14 and format <= 22:
844 # Rename I18NPreamble to BabelPreamble
846 match = re_I18nPreamble.match(lines[i])
848 lines[i] = match.group(1) + "BabelPreamble"
850 match = re_EndI18nPreamble.match(lines[i])
851 while i < len(lines) and not match:
853 match = re_EndI18nPreamble.match(lines[i])
854 lines[i] = match.group(1) + "EndBabelPreamble"
858 # These just involved new features, not any changes to old ones
859 if format == 11 or format == 12:
864 match = re_UseMod.match(lines[i])
866 module = match.group(1)
867 lines[i] = "DefaultModule " + module
872 match = re_Counter.match(lines[i])
876 while i < len(lines):
877 namem = re_Name.match(lines[i])
879 name = namem.group(1)
881 lines[counterline] = "Counter %s" % name
882 # we don't need to increment i
884 endem = re_End.match(lines[i])
893 # We want to scan for ams-type includes and, if we find them,
894 # add corresponding UseModule tags to the layout.
895 match = re_AMSMaths.match(lines[i])
897 addstring("theorems-ams", usemodules)
898 addstring("theorems-ams-extended", usemodules)
899 addstring("theorems-sec", usemodules)
902 match = re_AMSMathsPlain.match(lines[i])
904 addstring("theorems-starred", usemodules)
907 match = re_AMSMathsSeq.match(lines[i])
909 addstring("theorems-ams", usemodules)
910 addstring("theorems-ams-extended", usemodules)
916 # These just involved new features, not any changes to old ones
917 if format >= 5 and format <= 7:
922 # Handle conversion to long CharStyle names
923 match = re_CharStyle.match(lines[i])
925 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
927 lines.insert(i, "\tLyXType charstyle")
930 lines[i] = "\tLabelString %s" % (match.group(3))
935 # convert 'providesamsmath x', 'providesmakeidx x', 'providesnatbib x', 'providesurl x' to
936 # 'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
938 match = re_Provides.match(lines[i])
940 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
941 match.group(3), match.group(4))
948 # delete caption styles
949 match = re_Style.match(lines[i])
951 style = string.lower(match.group(4))
952 if style == "caption":
954 while i < len(lines) and not re_End.match(lines[i]):
955 caption.append(lines[i])
958 error('Incomplete caption style.')
963 # delete undefinition of caption styles
964 match = re_NoStyle.match(lines[i])
966 style = string.lower(match.group(4))
967 if style == "caption":
971 # replace the CopyStyle statement with the definition of the real
972 # style. This may result in duplicate statements, but that is OK
973 # since the second one will overwrite the first one.
974 match = re_CopyStyle.match(lines[i])
976 style = string.lower(match.group(4))
977 if style == "caption":
979 lines[i:i+1] = caption
981 # FIXME: This style comes from an include file, we
982 # should replace the real style and not this default.
983 lines[i:i+1] = [' Margin First_Dynamic',
984 ' LatexType Command',
985 ' LatexName caption',
991 ' AlignPossible Center',
992 ' LabelType Sensitive',
993 ' LabelString "Senseless!"',
1002 # Delete MaxCounter and remember the value of it
1003 match = re_MaxCounter.match(lines[i])
1005 level = match.group(4)
1006 if string.lower(level) == "counter_chapter":
1008 elif string.lower(level) == "counter_section":
1010 elif string.lower(level) == "counter_subsection":
1012 elif string.lower(level) == "counter_subsubsection":
1014 elif string.lower(level) == "counter_paragraph":
1016 elif string.lower(level) == "counter_subparagraph":
1018 elif string.lower(level) == "counter_enumi":
1020 elif string.lower(level) == "counter_enumii":
1022 elif string.lower(level) == "counter_enumiii":
1029 # LabelType Counter_EnumI
1034 # LabelCounter EnumI
1036 match = re_LabelType.match(lines[i])
1038 label = match.group(4)
1039 # Remember indenting space for later reuse in added lines
1040 space1 = match.group(1)
1041 # Remember the line for adding the LabelCounter later.
1042 # We can't do it here because it could shift latextype_line etc.
1044 if string.lower(label[:8]) == "counter_":
1045 counter = string.lower(label[8:])
1046 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
1048 # Remember the LabelString line
1049 match = re_LabelString.match(lines[i])
1051 labelstring = match.group(4)
1052 labelstring_line = i
1054 # Remember the LabelStringAppendix line
1055 match = re_LabelStringAppendix.match(lines[i])
1057 labelstringappendix = match.group(4)
1058 labelstringappendix_line = i
1060 # Remember the LatexType line
1061 match = re_LatexType.match(lines[i])
1063 latextype = string.lower(match.group(4))
1066 # Remember the TocLevel line
1067 match = re_TocLevel.match(lines[i])
1069 toclevel = string.lower(match.group(4))
1071 # Reset variables at the beginning of a style definition
1072 match = re_Style.match(lines[i])
1074 style = string.lower(match.group(4))
1080 labelstringappendix = ""
1081 labelstring_line = -1
1082 labelstringappendix_line = -1
1087 if re_End.match(lines[i]):
1089 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
1090 # (or change the existing LatexType)
1091 if string.lower(label) == "bibliography":
1092 if (latextype_line < 0):
1093 lines.insert(i, "%sLatexType Bib_Environment" % space1)
1096 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
1098 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
1099 if latextype == "item_environment" and string.lower(label) == "static":
1100 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
1102 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
1103 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
1104 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
1105 # Don't add the LabelCounter line later
1110 # LabelString "Chapter"
1114 # LabelString "Chapter \arabic{chapter}"
1116 # if this style has a counter. Ditto for LabelStringAppendix.
1117 # This emulates the hardcoded article style numbering of 1.3
1120 if style in counters:
1121 if labelstring_line < 0:
1122 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
1125 new_labelstring = concatenate_label(labelstring, counters[style])
1126 lines[labelstring_line] = re_LabelString.sub(
1127 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
1128 lines[labelstring_line])
1129 if style in appendixcounters:
1130 if labelstringappendix_line < 0:
1131 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
1134 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
1135 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
1136 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
1137 lines[labelstringappendix_line])
1139 # Now we can safely add the LabelCounter line
1140 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
1143 # Add the TocLevel setting for sectioning styles
1144 if toclevel == "" and style in toclevels and maxcounter <= toclevels[style]:
1145 lines.insert(i, '%s\tTocLevel %d' % (space1, toclevels[style]))
1151 lines.insert(i, "Format 2")
1154 for mod in usemodules:
1155 lines.insert(i, "UseModule " + mod)
1163 args["description"] = "Convert layout file <inputfile> to a newer format."
1165 parser = argparse.ArgumentParser(**args)
1167 parser.add_argument("-t", "--to", type=int, dest="format",
1168 help=("destination layout format, default %i (latest)") % currentFormat)
1169 parser.add_argument("input_file", nargs='?', type=cmd_arg, default=None,
1170 help="input file (default stdin)")
1171 parser.add_argument("output_file", nargs='?', type=cmd_arg, default=None,
1172 help="output file (default stdout)")
1174 options = parser.parse_args()
1177 if options.input_file:
1178 source = open(options.input_file, 'rb')
1182 if options.output_file:
1183 output = open(options.output_file, 'wb')
1188 end_format = options.format
1190 end_format = currentFormat
1192 if end_format > currentFormat:
1193 error("Format %i does not exist" % end_format);
1196 lines = read(source)
1198 while (format < end_format):
1199 format = convert(lines, end_format)
1200 write(output, lines)
1203 if options.input_file:
1205 if options.output_file:
1211 if __name__ == "__main__":