#! /usr/bin/env python
-# -*- coding: iso-8859-1 -*-
+# -*- coding: utf-8 -*-
# file layout2layout.py
# This file is part of LyX, the document processor.
# Full author contact details are available in file CREDITS
-# This script will update a .layout file to format 2
+# This script will update a .layout file to format 3
import os, re, string, sys
re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
+ re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
+ re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
+ re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
# counters for sectioning styles (hardcoded in 1.3)
counters = {"part" : "\\Roman{part}",
labelstring_line = -1
labelstringappendix_line = -1
labeltype_line = -1
+ latextype = ""
latextype_line = -1
style = ""
maxcounter = 0
+ format = 1
while i < len(lines):
# Skip comments and empty lines
if (only_comment):
match = re_Format.match(lines[i])
if match:
- format = match.group(4)
- if format == '2':
+ format = int(match.group(4))
+ if format > 1 and format < 4:
+ lines[i] = "Format %d" % (format + 1)
+ only_comment = 0
+ elif format == 4:
# nothing to do
- return
- error('Cannot convert file format %s' % format)
+ return format
+ else:
+ error('Cannot convert file format %s' % format)
else:
- lines.insert(i, "Format 2")
+ lines.insert(i, "Format 2")
only_comment = 0
continue
i = i + 1
continue
+ if format == 3:
+ # convert 'providesamsmath x', 'providesmakeidx x', 'providesnatbib x', 'providesurl x' to
+ # 'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
+ # x is either 0 or 1
+ match = re_Provides.match(lines[i])
+ if match:
+ lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
+ match.group(3), match.group(4))
+ i = i + 1
+ continue
+
+ if format == 2:
+ caption = []
+
+ # delete caption styles
+ match = re_Style.match(lines[i])
+ if match:
+ style = string.lower(match.group(4))
+ if style == "caption":
+ del lines[i]
+ while i < len(lines) and not re_End.match(lines[i]):
+ caption.append(lines[i])
+ del lines[i]
+ if i == len(lines):
+ error('Incomplete caption style.')
+ else:
+ del lines[i]
+ continue
+
+ # delete undefinition of caption styles
+ match = re_NoStyle.match(lines[i])
+ if match:
+ style = string.lower(match.group(4))
+ if style == "caption":
+ del lines[i]
+ continue
+
+ # replace the CopyStyle statement with the definition of the real
+ # style. This may result in duplicate statements, but that is OK
+ # since the second one will overwrite the first one.
+ match = re_CopyStyle.match(lines[i])
+ if match:
+ style = string.lower(match.group(4))
+ if style == "caption":
+ if len(caption) > 0:
+ lines[i:i+1] = caption
+ else:
+ # FIXME: This style comes from an include file, we
+ # should replace the real style and not this default.
+ lines[i:i+1] = [' Margin First_Dynamic',
+ ' LatexType Command',
+ ' LatexName caption',
+ ' NeedProtect 1',
+ ' LabelSep xx',
+ ' ParSkip 0.4',
+ ' TopSep 0.5',
+ ' Align Center',
+ ' AlignPossible Center',
+ ' LabelType Sensitive',
+ ' LabelString "Senseless!"',
+ ' OptionalArgs 1',
+ ' LabelFont',
+ ' Series Bold',
+ ' EndFont']
+
+ i = i + 1
+ continue
+
# Delete MaxCounter and remember the value of it
match = re_MaxCounter.match(lines[i])
if match:
# Remember the LatexType line
match = re_LatexType.match(lines[i])
if match:
+ latextype = string.lower(match.group(4))
latextype_line = i
# Reset variables at the beginning of a style definition
labelstring_line = -1
labelstringappendix_line = -1
labeltype_line = -1
+ latextype = ""
latextype_line = -1
if re_End.match(lines[i]):
else:
lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
+ # Change "LabelType Static" to "LabelType Itemize" for itemize environments
+ if latextype == "item_environment" and string.lower(label) == "static":
+ lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
+
+ # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
+ if latextype == "item_environment" and string.lower(label) == "counter_enumi":
+ lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
+ # Don't add the LabelCounter line later
+ counter = ""
+
# Replace
#
# LabelString "Chapter"
i = i + 1
# Add the TocLevel setting for sectioning styles
- if toclevels.has_key(style) and maxcounter <= toclevels[style]:
+ if toclevels.has_key(style) and maxcounter <= toclevels[style]:
lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
i = i + 1
i = i + 1
+ return format + 1
+
def main(argv):
# Do the real work
lines = read(input)
- convert(lines)
+ format = 1
+ while (format < 4):
+ format = convert(lines)
write(output, lines)
# Close files