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
14 import os, re, string, sys
16 # Incremented to format 4, 6 April 2007, lasgouttes
17 # Introduction of generic "Provides" declaration
19 # Incremented to format 5, 22 August 2007 by vermeer
20 # InsetLayout material
22 # Incremented to format 6, 7 January 2008 by spitz
23 # Requires tag added to layout files
25 # Incremented to format 7, 24 March 2008 by rgh
26 # AddToPreamble tag added to layout files
28 # Incremented to format 8, 25 July 2008 by rgh
29 # UseModule tag added to layout files
30 # CopyStyle added to InsetLayout
32 # Incremented to format 9, 5 October 2008 by rgh
33 # ForcePlain and CustomPars tags added to InsetLayout
35 # Incremented to format 10, 6 October 2008 by rgh
36 # Change format of counters
38 # Incremented to format 11, 14 October 2008 by rgh
39 # Add ProvidesModule, ExcludesModule tags
41 # Incremented to format 12, 10 January 2009 by gb
42 # Add I18NPreamble tag
44 # Incremented to format 13, 5 February 2009 by rgh
45 # Add InToc tag for InsetLayout
47 # Incremented to format 14, 14 February 2009 by gb
48 # Rename I18NPreamble to BabelPreamble and add LangPreamble
50 # Incremented to format 15, 28 May 2009 by lasgouttes
51 # Add new tag OutputFormat; modules can be conditioned on feature
54 # Incremented to format 16, 5 June 2009 by rgh
55 # Add new tags for Text Class:
56 # HTMLPreamble, HTMLAddToPreamble
58 # HTMLTag, HTMLAttr, HTMLLabel, HTMLLabelAttr, HTMLItem, HTMLItemAttr
59 # HTMLStyle, and HTMLPreamble
61 # HTMLTag, HTMLAttr, HTMLStyle, and HTMLPreamble
63 # HTMLType, HTMLClass, HTMLStyle
65 # Incremented to format 17, 12 August 2009 by rgh
66 # Add IfStyle and IfCounter tags for layout.
68 # Incremented to format 18, 27 October 2009 by rgh
69 # Added some new tags for HTML output.
71 # Incremented to format 19, 17 November 2009 by rgh
72 # Added InPreamble tag.
74 # Incremented to format 20, 17 December 2009 by rgh
75 # Added ContentAsLabel tag.
77 # Incremented to format 21, 12 January 2010 by rgh
78 # Added HTMLTocLayout and HTMLTitle tags.
80 # Incremented to format 22, 20 January 2010 by rgh
81 # Added HTMLFormat tag to Counters.
83 # Incremented to format 23, 13 February 2010 by spitz
84 # Added Spellcheck tag.
86 # Incremented to format 24, 5 March 2010 by rgh
87 # Changed LaTeXBuiltin tag to NeedsFloatPkg and
88 # added new tag ListCommand.
90 # Incremented to format 25, 12 March 2010 by rgh
91 # Added RefPrefix tag for layouts and floats.
93 # Incremented to format 26, 29 March 2010 by rgh
96 # Incremented to format 27, 4 June 2010 by rgh
97 # Added RequiredArgs tag.
99 # Incremented to format 28, 6 August 2010 by lasgouttes
100 # Added ParbreakIsNewline tag for Layout and InsetLayout.
102 # Incremented to format 29, 10 August 2010 by rgh
103 # Changed Custom:Style, CharStyle:Style, and Element:Style
104 # uniformly to Flex:Style.
106 # Incremented to format 30, 13 August 2010 by rgh
107 # Introduced ResetsFont tag for InsetLayout.
109 # Incremented to format 31, 12 January 2011 by rgh
110 # Introducted NoCounter tag.
112 # Incremented to format 32, 30 January 2011 by forenr
113 # Added Display tag for InsetLayout
115 # Incremented to format 33, 2 February 2011 by rgh
116 # Changed NeedsFloatPkg to UsesFloatPkg
118 # Incremented to format 34, 28 March 2011 by rgh
119 # Remove obsolete Fill_(Top|Bottom) tags
121 # Incremented to format 35, 28 March 2011 by rgh
122 # Try to add "Flex:" to any flex insets that don't have it.
124 # Incremented to format 36, 7 December 2011, by rgh
125 # Added HTMLStyles and AddToHTMLStyles tags.
127 # Incremented to format 37, 29 February 2012 by jrioux
128 # Implement the citation engine machinery in layouts.
129 # Change CiteFormat to CiteFormat (default|authoryear|numerical).
131 # Incremented to format 38, 08 April 2012 by gb
132 # Introduce LangPreamble and BabelPreamble for InsetLayout.
134 # Incremented to format 39, 15 April 2012 by sanda
135 # Introduce styling of branches via "InsetLayout Branch:".
137 # Incremented to format 40, 10 October 2012 by rgh
138 # Re-do layout names for layout categories
140 # Incremented to format 41, 20 November 2012 by spitz
141 # New Argument syntax
143 # Incremented to format 42, 22 December 2012 by spitz
144 # New Style tag "ItemCommand"
146 # Incremented to format 43, 30 December 2012 by spitz
147 # Extended InsetCaption format
149 # Incremented to format 44, 9 February 2013 by rgh
150 # Remove COUNTER label style; rename as STATIC
151 # Rename TOP_ENVIRONMENT to ABOVE and CENTERED_TOP_ENVIRONMENT to CENTERED
153 # Incremented to format 45, 12 February 2013 by rgh
154 # New Tag "NoInsetLayout"
156 # Incremented to format 46, 15 May 2013 by gb
157 # New Tag "ForceLocal"
159 # Incremented to format 47, 23 May 2013 by rgh
160 # Add PackageOptions tag
162 # Incremented to format 48, 31 May 2013 by rgh
163 # Add InitialValue tag for counters
165 # Incremented to format 49, 10 Feb 2014 by gb
166 # Change default of "ResetsFont" tag to false
168 # Incremented to format 50, 9 May 2014 by forenr
169 # Removal of "Separator" layouts
171 # Incremented to format 51, 29 May 2014 by spitz
172 # New Style tag "ToggleIndent"
174 # Do not forget to document format change in Customization
175 # Manual (section "Declaring a new text class").
177 # You might also want to consider running the
178 # development/tools/updatelayouts.py script to update all
179 # layout files to the new format.
184 def usage(prog_name):
185 return ("Usage: %s inputfile outputfile\n" % prog_name +
186 "or %s <inputfile >outputfile" % prog_name)
190 sys.stderr.write(message + '\n')
195 " Remove byte order mark."
196 if line[0:3] == "\357\273\277":
203 " Read input file and strip lineendings."
204 lines = source.read().splitlines() or ['']
205 lines[0] = trim_bom(lines[0])
209 def write(output, lines):
210 " Write output file with native lineendings."
211 output.write(os.linesep.join(lines) + os.linesep)
214 # Concatenates old and new in an intelligent way:
215 # If old is wrapped in ", they are stripped. The result is wrapped in ".
216 def concatenate_label(old, new):
217 # Don't use strip as long as we support python 1.5.2
219 return old[0:-1] + new + '"'
221 return '"' + old + new + '"'
223 # appends a string to a list unless it's already there
231 " Convert to new format."
232 re_Comment = re.compile(r'^(\s*)#')
233 re_Counter = re.compile(r'\s*Counter\s*', re.IGNORECASE)
234 re_Name = re.compile(r'\s*Name\s+(\S+)\s*', re.IGNORECASE)
235 re_UseMod = re.compile(r'^\s*UseModule\s+(.*)', re.IGNORECASE)
236 re_Empty = re.compile(r'^(\s*)$')
237 re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
238 re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
239 re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
240 re_LangPreamble = re.compile(r'^(\s*)LangPreamble', re.IGNORECASE)
241 re_EndLangPreamble = re.compile(r'^(\s*)EndLangPreamble', re.IGNORECASE)
242 re_BabelPreamble = re.compile(r'^(\s*)BabelPreamble', re.IGNORECASE)
243 re_EndBabelPreamble = re.compile(r'^(\s*)EndBabelPreamble', re.IGNORECASE)
244 re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
245 re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
246 re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
247 re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
248 re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
249 re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
250 re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
251 re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
252 re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
253 re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
254 re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
255 re_CiteFormat = re.compile(r'^(\s*)(CiteFormat)(?:(\s*)()|(\s+)(default|authoryear|numerical))', re.IGNORECASE)
256 re_AMSMaths = re.compile(r'^\s*Input ams(?:math|def)s.inc\s*')
257 re_AMSMathsPlain = re.compile(r'^\s*Input amsmaths-plain.inc\s*')
258 re_AMSMathsSeq = re.compile(r'^\s*Input amsmaths-seq.inc\s*')
259 re_TocLevel = re.compile(r'^(\s*)(TocLevel)(\s+)(\S+)', re.IGNORECASE)
260 re_I18nPreamble = re.compile(r'^(\s*)I18nPreamble', re.IGNORECASE)
261 re_EndI18nPreamble = re.compile(r'^(\s*)EndI18nPreamble', re.IGNORECASE)
262 re_Float = re.compile(r'^\s*Float\s*$', re.IGNORECASE)
263 re_Type = re.compile(r'\s*Type\s+(\w+)', re.IGNORECASE)
264 re_Builtin = re.compile(r'^(\s*)LaTeXBuiltin\s+(\w*)', re.IGNORECASE)
265 re_True = re.compile(r'^\s*(?:true|1)\s*$', re.IGNORECASE)
266 re_InsetLayout = re.compile(r'^\s*InsetLayout\s+(?:Custom|CharStyle|Element):(\S+)\s*$', re.IGNORECASE)
267 re_ResetsFont = re.compile(r'^(\s*)ResetsFont(\s+)(\S+)$', re.IGNORECASE)
269 re_QInsetLayout = re.compile(r'^\s*InsetLayout\s+"(?:Custom|CharStyle|Element):([^"]+)"\s*$', re.IGNORECASE)
270 re_InsetLayout_CopyStyle = re.compile(r'^\s*CopyStyle\s+(?:Custom|CharStyle|Element):(\S+)\s*$', re.IGNORECASE)
271 re_QInsetLayout_CopyStyle = re.compile(r'^\s*CopyStyle\s+"(?:Custom|CharStyle|Element):([^"]+)"\s*$', re.IGNORECASE)
272 re_NeedsFloatPkg = re.compile(r'^(\s*)NeedsFloatPkg\s+(\w+)\s*$', re.IGNORECASE)
273 re_Fill = re.compile(r'^\s*Fill_(?:Top|Bottom).*$', re.IGNORECASE)
274 re_InsetLayout2 = re.compile(r'^\s*InsetLayout\s+(\S+)\s*$', re.IGNORECASE)
276 re_QInsetLayout2 = re.compile(r'^\s*InsetLayout\s+"([^"]+)"\s*$', re.IGNORECASE)
277 re_IsFlex = re.compile(r'\s*LyXType.*$', re.IGNORECASE)
278 re_CopyStyle2 = re.compile(r'(\s*CopyStyle\s+)"?([^"]+)"?\s*$')
279 re_Separator = re.compile(r'^(?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE)
281 re_Declaration = re.compile(r'^#\s*\\Declare\w+Class.*$')
282 re_ExtractCategory = re.compile(r'^(#\s*\\Declare\w+Class(?:\[[^]]*?\])?){([^(]+?)\s+\(([^)]+?)\)\s*}\s*$')
283 ConvDict = {"article": "Articles", "book" : "Books", "letter" : "Letters", "report": "Reports", \
284 "presentation" : "Presentations", "curriculum vitae" : "Curricula Vitae", "handout" : "Handouts"}
286 re_OptArgs = re.compile(r'^(\s*)OptionalArgs(\s+)(\d+)\D*$', re.IGNORECASE)
287 re_ReqArgs = re.compile(r'^(\s*)RequiredArgs(\s+)(\d+)\D*$', re.IGNORECASE)
289 # various changes associated with changing how chapters are handled
290 re_LabelTypeIsCounter = re.compile(r'^(\s*)LabelType(\s*)Counter\s*$', re.IGNORECASE)
291 re_TopEnvironment = re.compile(r'^(\s*)LabelType(\s+)Top_Environment\s*$', re.IGNORECASE)
292 re_CenteredEnvironment = re.compile(r'^(\s*)LabelType(\s+)Centered_Top_Environment\s*$', re.IGNORECASE)
293 re_ChapterStyle = re.compile(r'^\s*Style\s+Chapter\s*$', re.IGNORECASE)
296 # counters for sectioning styles (hardcoded in 1.3)
297 counters = {"part" : "\\Roman{part}",
298 "chapter" : "\\arabic{chapter}",
299 "section" : "\\arabic{section}",
300 "subsection" : "\\arabic{section}.\\arabic{subsection}",
301 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
302 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
303 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
305 # counters for sectioning styles in appendix (hardcoded in 1.3)
306 appendixcounters = {"chapter" : "\\Alph{chapter}",
307 "section" : "\\Alph{section}",
308 "subsection" : "\\arabic{section}.\\arabic{subsection}",
309 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
310 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
311 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
313 # Value of TocLevel for sectioning styles
314 toclevels = {"part" : -1,
328 labelstringappendix = ""
330 labelstring_line = -1
331 labelstringappendix_line = -1
344 isflexlayout = False # only used for 48 -> 49
345 # Whether a style is inherited (works only for CopyStyle currently,
346 # not for true inherited styles, see bug 8920
347 inherited = False # only used for 48 -> 49
348 resetsfont_found = False # only used for 48 -> 49
350 while i < len(lines):
351 # Skip comments and empty lines
352 if (re_Comment.match(lines[i]) or re_Empty.match(lines[i])):
353 # We need to deal with this conversion here, because it happens
354 # inside the initial comment block.
355 if only_comment and format == 39:
356 match = re_ExtractCategory.match(lines[i])
358 lpre = match.group(1)
359 lcat = match.group(2)
360 lnam = match.group(3)
362 lcat = ConvDict[lcat]
363 lines[i] = lpre + "{" + lnam + "}"
364 lines.insert(i+1, "# \\DeclareCategory{" + lcat + "}")
369 # insert file format if not already there
371 match = re_Format.match(lines[i])
374 format = int(match.group(4))
375 if format > 1 and format < currentFormat:
376 lines[i] = "Format %d" % (format + 1)
378 elif format == currentFormat:
382 error('Cannot convert file format %s to %s' % (format, currentFormat))
384 lines.insert(i, "Format 2")
388 # Don't get confused by LaTeX code
389 if re_Preamble.match(lines[i]):
391 while i < len(lines) and not re_EndPreamble.match(lines[i]):
394 if re_LangPreamble.match(lines[i]):
396 while i < len(lines) and not re_EndLangPreamble.match(lines[i]):
399 if re_BabelPreamble.match(lines[i]):
401 while i < len(lines) and not re_EndBabelPreamble.match(lines[i]):
413 # delete separator styles
414 match = re_Style.match(lines[i])
416 style = string.lower(match.group(4))
417 if re_Separator.match(style):
419 while i < len(lines) and not re_End.match(lines[i]):
420 separator.append(lines[i])
423 error('Incomplete separator style.')
428 # delete undefinition of separator styles
429 match = re_NoStyle.match(lines[i])
431 style = string.lower(match.group(4))
432 if re_Separator.match(style):
436 # replace the CopyStyle statement with the definition of the real
437 # style. This may result in duplicate statements, but that is OK
438 # since the second one will overwrite the first one.
439 match = re_CopyStyle.match(lines[i])
441 style = string.lower(match.group(4))
442 if re_Separator.match(style):
443 if len(separator) > 0:
444 lines[i:i+1] = separator
446 # FIXME: If this style was redefined in an include file,
447 # we should replace the real style and not this default.
448 lines[i:i+1] = [' Category MainText',
451 ' LatexType Paragraph',
456 ' LabelString "--- Separate Environment ---"',
468 # The default of ResetsFont in LyX changed from true to false,
469 # because it is now used for all InsetLayouts, not only flex ones.
470 # Therefore we need to set it to true for all flex insets which do
471 # do not already have a ResetsFont.
472 match = re_InsetLayout2.match(lines[i])
477 name = string.lower(match.group(1))
478 if name != "flex" and name != "\"flex\"" and name[0:5] != "flex:" and name [0:6] != "\"flex:":
482 resetsfont_found = False
485 while i < len(lines):
486 match = re_ResetsFont.match(lines[i])
488 resetsfont_found = True
490 match = re_CopyStyle.match(lines[i])
494 match = re_End.match(lines[i])
498 if not resetsfont_found and not inherited:
499 lines.insert(i, "\tResetsFont true")
503 if format >= 44 and format <= 47:
509 match = re_LabelTypeIsCounter.match(lines[i])
512 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above"
514 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Static"
516 match = re_TopEnvironment.match(lines[i])
518 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above"
520 match = re_CenteredEnvironment.match(lines[i])
522 lines[i] = match.group(1) + "LabelType" + match.group(2) + "Centered"
525 match = re_Style.match(lines[i])
529 match = re_ChapterStyle.match(lines[i])
537 if lines[i] == "InsetLayout Caption":
538 lines[i] = "InsetLayout Caption:Standard"
548 # reset counters on Style beginning
549 match = re_Style.match(lines[i])
555 match = re_OptArgs.match(lines[i])
557 # Save number of optional arguments
558 space1 = match.group(1)
559 opts = int(match.group(3))
560 # OptionalArgs 0 > ResetArgs 1
562 lines[i] = space1 + "ResetArgs\t1"
567 match = re_ReqArgs.match(lines[i])
569 # Save number of required arguments
570 space1 = match.group(1)
571 reqs = int(match.group(3))
574 # Insert the required number of arguments at the end of the style definition
575 match = re_End.match(lines[i])
578 # First the optionals (this is the required order pre 2.1)
581 newarg = [ '%sArgument 1' % (space1),
582 '%s\tLabelString\t\"Optional Layout Argument\"' % (space1),
583 '%sEndArgument' % (space1)]
586 while actopt < (opts + 1):
587 newarg += [ '%sArgument %d' % (space1, actopt),
588 '%s\tLabelString\t\"Optional Layout Argument %d\"' % (space1, actopt),
589 '%sEndArgument' % (space1)]
591 # Now the mandatories
594 while actopt < (opts + reqs + 1):
595 newarg += [ '%sArgument %d' % (space1, actopt),
596 '%s\tLabelString\t"Required Layout Argument %d"' % (space1, actopt - opts),
597 '%s\tMandatory\t1' % (space1),
598 '%sEndArgument' % (space1)]
600 # Since we replace the "End" line, re-add this line
603 lines[i:i+1] = newarg
612 # There is a conversion with format 40, but it is done within the
613 # initial comment block and so is above.
617 if format == 37 or format == 38:
622 match = re_CiteFormat.match(lines[i]);
623 if match and match.group(4) == "":
624 lines[i] = match.group(0) + " default"
633 match = re_QInsetLayout2.match(lines[i])
635 match = re_InsetLayout2.match(lines[i])
637 match = re_CopyStyle2.match(lines[i])
641 style = match.group(2)
643 if flexstyles.count(style):
644 lines[i] = match.group(1) + "\"Flex:" + style + "\""
648 name = match.group(1)
649 names = name.split(":", 1)
650 if len(names) > 1 and names[0] == "Flex":
655 for j in range(i + 1, len(lines)):
656 if re_IsFlex.match(lines[j]):
659 if re_End.match(lines[j]):
663 flexstyles.append(name)
664 lines[i] = "InsetLayout \"Flex:" + name + "\""
670 m = re_Fill.match(lines[i])
677 match = re_NeedsFloatPkg.match(lines[i])
679 space = match.group(1)
681 lines[i] = space + "UsesFloatPkg " + val
683 if val == '1' or val.lower() == 'true':
685 lines.insert(i, space + "IsPredefined " + newval)
691 if format >= 29 and format <= 31:
696 match = re_InsetLayout.match(lines[i])
698 lines[i] = "InsetLayout Flex:" + match.group(1)
700 match = re_QInsetLayout.match(lines[i])
702 lines[i] = "InsetLayout \"Flex:" + match.group(1) + "\""
704 match = re_InsetLayout_CopyStyle.match(lines[i])
706 lines[i] = "\tCopyStyle Flex:" + match.group(1)
708 match = re_QInsetLayout_CopyStyle.match(lines[i])
710 lines[i] = "\tCopyStyle \"Flex:" + match.group(1) + "\""
715 if format >= 24 and format <= 27:
720 match = re_Float.match(lines[i])
724 # we need to do two things:
725 # (i) Convert Builtin to NeedsFloatPkg
726 # (ii) Write ListCommand lines for the builtin floats table and figure
729 while True and i < len(lines):
730 m1 = re_End.match(lines[i])
733 line = " ListCommand " + cmd
734 lines.insert(i, line)
737 m2 = re_Builtin.match(lines[i])
743 if re_True.match(arg):
747 lines[i] = ws1 + "NeedsFloatPkg " + newarg
748 m3 = re_Type.match(lines[i])
751 fltype = fltype.lower()
752 if fltype == "table":
754 elif fltype == "figure":
755 cmd = "listoffigures"
756 # else unknown, which is why we're doing this
760 # This just involved new features, not any changes to old ones
761 if format >= 14 and format <= 22:
765 # Rename I18NPreamble to BabelPreamble
767 match = re_I18nPreamble.match(lines[i])
769 lines[i] = match.group(1) + "BabelPreamble"
771 match = re_EndI18nPreamble.match(lines[i])
772 while i < len(lines) and not match:
774 match = re_EndI18nPreamble.match(lines[i])
775 lines[i] = match.group(1) + "EndBabelPreamble"
779 # These just involved new features, not any changes to old ones
780 if format == 11 or format == 12:
785 match = re_UseMod.match(lines[i])
787 module = match.group(1)
788 lines[i] = "DefaultModule " + module
793 match = re_Counter.match(lines[i])
797 while i < len(lines):
798 namem = re_Name.match(lines[i])
800 name = namem.group(1)
802 lines[counterline] = "Counter %s" % name
803 # we don't need to increment i
805 endem = re_End.match(lines[i])
814 # We want to scan for ams-type includes and, if we find them,
815 # add corresponding UseModule tags to the layout.
816 match = re_AMSMaths.match(lines[i])
818 addstring("theorems-ams", usemodules)
819 addstring("theorems-ams-extended", usemodules)
820 addstring("theorems-sec", usemodules)
823 match = re_AMSMathsPlain.match(lines[i])
825 addstring("theorems-starred", usemodules)
828 match = re_AMSMathsSeq.match(lines[i])
830 addstring("theorems-ams", usemodules)
831 addstring("theorems-ams-extended", usemodules)
837 # These just involved new features, not any changes to old ones
838 if format >= 5 and format <= 7:
843 # Handle conversion to long CharStyle names
844 match = re_CharStyle.match(lines[i])
846 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
848 lines.insert(i, "\tLyXType charstyle")
851 lines[i] = "\tLabelString %s" % (match.group(3))
856 # convert 'providesamsmath x', 'providesmakeidx x', 'providesnatbib x', 'providesurl x' to
857 # 'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
859 match = re_Provides.match(lines[i])
861 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
862 match.group(3), match.group(4))
869 # delete caption styles
870 match = re_Style.match(lines[i])
872 style = string.lower(match.group(4))
873 if style == "caption":
875 while i < len(lines) and not re_End.match(lines[i]):
876 caption.append(lines[i])
879 error('Incomplete caption style.')
884 # delete undefinition of caption styles
885 match = re_NoStyle.match(lines[i])
887 style = string.lower(match.group(4))
888 if style == "caption":
892 # replace the CopyStyle statement with the definition of the real
893 # style. This may result in duplicate statements, but that is OK
894 # since the second one will overwrite the first one.
895 match = re_CopyStyle.match(lines[i])
897 style = string.lower(match.group(4))
898 if style == "caption":
900 lines[i:i+1] = caption
902 # FIXME: This style comes from an include file, we
903 # should replace the real style and not this default.
904 lines[i:i+1] = [' Margin First_Dynamic',
905 ' LatexType Command',
906 ' LatexName caption',
912 ' AlignPossible Center',
913 ' LabelType Sensitive',
914 ' LabelString "Senseless!"',
923 # Delete MaxCounter and remember the value of it
924 match = re_MaxCounter.match(lines[i])
926 level = match.group(4)
927 if string.lower(level) == "counter_chapter":
929 elif string.lower(level) == "counter_section":
931 elif string.lower(level) == "counter_subsection":
933 elif string.lower(level) == "counter_subsubsection":
935 elif string.lower(level) == "counter_paragraph":
937 elif string.lower(level) == "counter_subparagraph":
939 elif string.lower(level) == "counter_enumi":
941 elif string.lower(level) == "counter_enumii":
943 elif string.lower(level) == "counter_enumiii":
950 # LabelType Counter_EnumI
957 match = re_LabelType.match(lines[i])
959 label = match.group(4)
960 # Remember indenting space for later reuse in added lines
961 space1 = match.group(1)
962 # Remember the line for adding the LabelCounter later.
963 # We can't do it here because it could shift latextype_line etc.
965 if string.lower(label[:8]) == "counter_":
966 counter = string.lower(label[8:])
967 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
969 # Remember the LabelString line
970 match = re_LabelString.match(lines[i])
972 labelstring = match.group(4)
975 # Remember the LabelStringAppendix line
976 match = re_LabelStringAppendix.match(lines[i])
978 labelstringappendix = match.group(4)
979 labelstringappendix_line = i
981 # Remember the LatexType line
982 match = re_LatexType.match(lines[i])
984 latextype = string.lower(match.group(4))
987 # Remember the TocLevel line
988 match = re_TocLevel.match(lines[i])
990 toclevel = string.lower(match.group(4))
992 # Reset variables at the beginning of a style definition
993 match = re_Style.match(lines[i])
995 style = string.lower(match.group(4))
1001 labelstringappendix = ""
1002 labelstring_line = -1
1003 labelstringappendix_line = -1
1008 if re_End.match(lines[i]):
1010 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
1011 # (or change the existing LatexType)
1012 if string.lower(label) == "bibliography":
1013 if (latextype_line < 0):
1014 lines.insert(i, "%sLatexType Bib_Environment" % space1)
1017 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
1019 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
1020 if latextype == "item_environment" and string.lower(label) == "static":
1021 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
1023 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
1024 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
1025 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
1026 # Don't add the LabelCounter line later
1031 # LabelString "Chapter"
1035 # LabelString "Chapter \arabic{chapter}"
1037 # if this style has a counter. Ditto for LabelStringAppendix.
1038 # This emulates the hardcoded article style numbering of 1.3
1041 if style in counters:
1042 if labelstring_line < 0:
1043 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
1046 new_labelstring = concatenate_label(labelstring, counters[style])
1047 lines[labelstring_line] = re_LabelString.sub(
1048 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
1049 lines[labelstring_line])
1050 if style in appendixcounters:
1051 if labelstringappendix_line < 0:
1052 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
1055 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
1056 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
1057 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
1058 lines[labelstringappendix_line])
1060 # Now we can safely add the LabelCounter line
1061 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
1064 # Add the TocLevel setting for sectioning styles
1065 if toclevel == "" and style in toclevels and maxcounter <= toclevels[style]:
1066 lines.insert(i, '%s\tTocLevel %d' % (space1, toclevels[style]))
1072 lines.insert(i, "Format 2")
1075 for mod in usemodules:
1076 lines.insert(i, "UseModule " + mod)
1088 elif len(argv) == 3:
1089 source = open(argv[1], 'rb')
1090 output = open(argv[2], 'wb')
1092 error(usage(argv[0]))
1095 lines = read(source)
1097 while (format < currentFormat):
1098 format = convert(lines)
1099 write(output, lines)
1109 if __name__ == "__main__":