* lib/scripts/layout2layout.py

(concatenate_label): new, concatenate two labels in an intelligent way
	(convert): add the TocLevel for sectioning styles
	(convert): convert LabelString and StringAppendix for sectioning
	styles (append counter)


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_4_X@13538 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-03-31 14:15:43 +00:00
parent 7fac128691
commit a3c2ea155d
2 changed files with 147 additions and 14 deletions

View File

@ -53,6 +53,16 @@ def write(output, lines):
output.write(line + os.linesep) output.write(line + os.linesep)
# Concatenates old and new in an intelligent way:
# If old is wrapped in ", they are stripped. The result is wrapped in ".
def concatenate_label(old, new):
# Don't use strip as long as we support python 1.5.2
if old[0] == '"':
return old[0:-1] + new + '"'
else:
return '"' + old + new + '"'
def convert(lines): def convert(lines):
" Convert to new format." " Convert to new format."
re_Comment = re.compile(r'^(\s*)#') re_Comment = re.compile(r'^(\s*)#')
@ -60,18 +70,53 @@ def convert(lines):
re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE) re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE) re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE) re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
re_MaxCounter = re.compile(r'^\s*MaxCounter', re.IGNORECASE) re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE) re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
re_LatexType = re.compile(r'^(\s*)(LatexType)(\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_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE) re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
# counters for sectioning styles (hardcoded in 1.3)
counters = {"part" : "\\Roman{part}",
"chapter" : "\\arabic{chapter}",
"section" : "\\arabic{section}",
"subsection" : "@Section@.\\arabic{subsection}",
"subsubsection" : "@Subsection@.\\arabic{subsubsection}",
"paragraph" : "@Subsubsection@.\\arabic{paragraph}",
"subparagraph" : "@Paragraph@.\\arabic{subparagraph}"}
# counters for sectioning styles in appendix (hardcoded in 1.3)
appendixcounters = {"chapter" : "\\Alph{chapter}",
"section" : "\\Alph{section}",
"subsection" : "@Section@.\\arabic{subsection}",
"subsubsection" : "@Subsection@.\\arabic{subsubsection}",
"paragraph" : "@Subsubsection@.\\arabic{paragraph}",
"subparagraph" : "@Paragraph@.\\arabic{subparagraph}"}
# Value of TocLevel for sectioning styles
toclevels = {"part" : 0,
"chapter" : 0,
"section" : 1,
"subsection" : 2,
"subsubsection" : 3,
"paragraph" : 4,
"subparagraph" : 5}
i = 0 i = 0
only_comment = 1 only_comment = 1
counter = ""
label = "" label = ""
labelstring = ""
labelstringappendix = ""
space1 = "" space1 = ""
labelstring_line = -1
labelstringappendix_line = -1
labeltype_line = -1
latextype_line = -1 latextype_line = -1
style = "" style = ""
maxcounter = 0
while i < len(lines): while i < len(lines):
# Skip comments and empty lines # Skip comments and empty lines
@ -100,8 +145,28 @@ def convert(lines):
i = i + 1 i = i + 1
continue continue
# Delete MaxCounter # Delete MaxCounter and remember the value of it
if re_MaxCounter.match(lines[i]): match = re_MaxCounter.match(lines[i])
if match:
level = match.group(4)
if string.lower(level) == "counter_chapter":
maxcounter = 0
elif string.lower(level) == "counter_section":
maxcounter = 1
elif string.lower(level) == "counter_subsection":
maxcounter = 2
elif string.lower(level) == "counter_subsubsection":
maxcounter = 3
elif string.lower(level) == "counter_paragraph":
maxcounter = 4
elif string.lower(level) == "counter_subparagraph":
maxcounter = 5
elif string.lower(level) == "counter_enumi":
maxcounter = 6
elif string.lower(level) == "counter_enumii":
maxcounter = 7
elif string.lower(level) == "counter_enumiii":
maxcounter = 8
del lines[i] del lines[i]
continue continue
@ -117,30 +182,96 @@ def convert(lines):
match = re_LabelType.match(lines[i]) match = re_LabelType.match(lines[i])
if match: if match:
label = match.group(4) label = match.group(4)
# Remember indenting space for later reuse in added lines
space1 = match.group(1) space1 = match.group(1)
# Remember the line for adding the LabelCounter later.
# We can't do it here because it could shift latextype_line etc.
labeltype_line = i
if string.lower(label[:8]) == "counter_": if string.lower(label[:8]) == "counter_":
counter = label[8:] counter = string.lower(label[8:])
lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i]) lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
# use the same indentation
lines.insert(i + 1, "%sLabelCounter %s" % (space1, counter))
# Add a line "LatexType Bib_Environment" if LabelType is Bibliography # Remember the LabelString line
# (or change the existing LatexType) match = re_LabelString.match(lines[i])
if match:
labelstring = match.group(4)
labelstring_line = i
# Remember the LabelStringAppendix line
match = re_LabelStringAppendix.match(lines[i])
if match:
labelstringappendix = match.group(4)
labelstringappendix_line = i
# Remember the LatexType line
match = re_LatexType.match(lines[i]) match = re_LatexType.match(lines[i])
if match: if match:
latextype_line = i latextype_line = i
# Reset variables at the beginning of a style definition
match = re_Style.match(lines[i]) match = re_Style.match(lines[i])
if match: if match:
style = match.group(4) style = string.lower(match.group(4))
counter = ""
label = "" label = ""
space1 = "" space1 = ""
labelstring = ""
labelstringappendix = ""
labelstring_line = -1
labelstringappendix_line = -1
labeltype_line = -1
latextype_line = -1 latextype_line = -1
if re_End.match(lines[i]) and string.lower(label) == "bibliography":
if (latextype_line < 0): if re_End.match(lines[i]):
lines.insert(i, "%sLatexType Bib_Environment" % space1)
# Add a line "LatexType Bib_Environment" if LabelType is Bibliography
# (or change the existing LatexType)
if string.lower(label) == "bibliography":
if (latextype_line < 0):
lines.insert(i, "%sLatexType Bib_Environment" % space1)
i = i + 1
else:
lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
# Replace
#
# LabelString "Chapter"
#
# with
#
# LabelString "Chapter \arabic{chapter}"
#
# if this style has a counter. Ditto for LabelStringAppendix.
# This emulates the hardcoded article style numbering of 1.3
#
if counter != "":
if counters.has_key(style):
if labelstring_line < 0:
lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
i = i + 1
else:
new_labelstring = concatenate_label(labelstring, counters[style])
lines[labelstring_line] = re_LabelString.sub(
r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
lines[labelstring_line])
if appendixcounters.has_key(style):
if labelstringappendix_line < 0:
lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
i = i + 1
else:
new_labelstring = concatenate_label(labelstring, appendixcounters[style])
lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
lines[labelstringappendix_line])
# Now we can safely add the LabelCounter line
lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
i = i + 1
# Add the TocLevel setting for sectioning styles
if toclevels.has_key(style) and maxcounter <= toclevels[style]:
lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
i = i + 1 i = i + 1
else:
lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
i = i + 1 i = i + 1

View File

@ -54,6 +54,8 @@ What's new
- Set search path for included files correctly on Cygwin (bug 2409). - Set search path for included files correctly on Cygwin (bug 2409).
- Better layout file conversion (bug 2355)
* User Interface: * User Interface:
- Convert line endings for external copy/paste on OS X (bug 1955). - Convert line endings for external copy/paste on OS X (bug 1955).