2006-07-17 19:41:32 +00:00
|
|
|
#! /usr/bin/env python
|
2006-08-08 10:42:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2005-10-21 16:11:36 +00:00
|
|
|
|
|
|
|
# file layout2layout.py
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
|
|
|
# author Georg Baum
|
|
|
|
|
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
2009-02-04 20:06:50 +00:00
|
|
|
# This script will update a .layout file to current format
|
2005-10-21 16:11:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
import os, re, string, sys
|
|
|
|
|
2008-03-24 13:21:26 +00:00
|
|
|
# Incremented to format 4, 6 April 2007, lasgouttes
|
|
|
|
# Introduction of generic "Provides" declaration
|
2005-10-21 16:11:36 +00:00
|
|
|
|
2008-03-24 13:21:26 +00:00
|
|
|
# Incremented to format 5, 22 August 2007 by vermeer
|
|
|
|
# InsetLayout material
|
|
|
|
|
|
|
|
# Incremented to format 6, 7 January 2008 by spitz
|
|
|
|
# Requires tag added to layout files
|
|
|
|
|
|
|
|
# Incremented to format 7, 24 March 2008 by rgh
|
2008-03-24 13:11:16 +00:00
|
|
|
# AddToPreamble tag added to layout files
|
2008-07-25 15:51:27 +00:00
|
|
|
|
|
|
|
# Incremented to format 8, 25 July 2008 by rgh
|
|
|
|
# UseModule tag added to layout files
|
2008-07-28 15:14:37 +00:00
|
|
|
# CopyStyle added to InsetLayout
|
2008-10-05 19:39:09 +00:00
|
|
|
|
|
|
|
# Incremented to format 9, 5 October 2008 by rgh
|
|
|
|
# ForcePlain and CustomPars tags added to InsetLayout
|
|
|
|
|
2008-10-06 14:37:28 +00:00
|
|
|
# Incremented to format 10, 6 October 2008 by rgh
|
|
|
|
# Change format of counters
|
|
|
|
|
2008-10-23 00:27:03 +00:00
|
|
|
# Incremented to format 11, 14 October 2008 by rgh
|
|
|
|
# Add ProvidesModule, ExcludesModule tags
|
|
|
|
|
2009-02-04 20:06:50 +00:00
|
|
|
# Incremented to format 12, 10 January 2009 by gb
|
|
|
|
# Add I18NPreamble tag
|
|
|
|
|
2009-02-06 17:54:33 +00:00
|
|
|
# Incremented to format 13, 5 February 2009 by rgh
|
|
|
|
# Add InToc tag for InsetLayout
|
|
|
|
|
2009-02-17 20:25:56 +00:00
|
|
|
# Incremented to format 14, 14 February 2009 by gb
|
|
|
|
# Rename I18NPreamble to BabelPreamble and add LangPreamble
|
|
|
|
|
2009-05-28 08:59:25 +00:00
|
|
|
# Incremented to format 15, 28 May 2009 by lasgouttes
|
2009-10-27 19:03:30 +00:00
|
|
|
# Add new tag OutputFormat; modules can be conditioned on feature
|
2009-05-28 08:59:25 +00:00
|
|
|
# "from->to".
|
|
|
|
|
2009-06-05 20:07:16 +00:00
|
|
|
# Incremented to format 16, 5 June 2009 by rgh
|
|
|
|
# Add new tags for Text Class:
|
2009-06-19 13:11:06 +00:00
|
|
|
# HTMLPreamble, HTMLAddToPreamble
|
2009-06-05 20:07:16 +00:00
|
|
|
# For Layout:
|
|
|
|
# HTMLTag, HTMLAttr, HTMLLabel, HTMLLabelAttr, HTMLItem, HTMLItemAttr
|
|
|
|
# HTMLStyle, and HTMLPreamble
|
|
|
|
# For InsetLayout:
|
|
|
|
# HTMLTag, HTMLAttr, HTMLStyle, and HTMLPreamble
|
2009-06-19 13:10:36 +00:00
|
|
|
# For Floats:
|
|
|
|
# HTMLType, HTMLClass, HTMLStyle
|
2009-06-05 20:07:16 +00:00
|
|
|
|
2009-10-27 19:03:30 +00:00
|
|
|
# Incremented to format 17, 12 August 2009 by rgh
|
|
|
|
# Add IfStyle and IfCounter tags for layout.
|
|
|
|
|
|
|
|
# Incremented to format 18, 27 October 2009 by rgh
|
2010-01-07 17:32:42 +00:00
|
|
|
# Added some new tags for HTML output.
|
2009-10-27 19:03:30 +00:00
|
|
|
|
2009-12-01 14:34:05 +00:00
|
|
|
# Incremented to format 19, 17 November 2009 by rgh
|
|
|
|
# Added InPreamble tag.
|
|
|
|
|
2009-12-18 00:29:22 +00:00
|
|
|
# Incremented to format 20, 17 December 2009 by rgh
|
|
|
|
# Added ContentAsLabel tag.
|
|
|
|
|
2010-01-12 19:38:31 +00:00
|
|
|
# Incremented to format 21, 12 January 2010 by rgh
|
2010-01-19 19:43:15 +00:00
|
|
|
# Added HTMLTocLayout and HTMLTitle tags.
|
2010-02-13 15:44:17 +00:00
|
|
|
|
2010-01-20 20:33:36 +00:00
|
|
|
# Incremented to format 22, 20 January 2010 by rgh
|
|
|
|
# Added HTMLFormat tag to Counters.
|
2010-01-12 19:38:31 +00:00
|
|
|
|
2010-02-13 15:44:17 +00:00
|
|
|
# Incremented to format 23, 13 February 2010 by spitz
|
|
|
|
# Added Spellcheck tag.
|
|
|
|
|
2010-03-05 17:24:38 +00:00
|
|
|
# Incremented to format 24, 5 March 2010 by rgh
|
2010-03-05 22:58:29 +00:00
|
|
|
# Changed LaTeXBuiltin tag to NeedsFloatPkg and
|
|
|
|
# added new tag ListCommand.
|
2010-03-05 17:24:38 +00:00
|
|
|
|
2010-03-17 12:23:24 +00:00
|
|
|
# Incremented to format 25, 12 March 2010 by rgh
|
|
|
|
# Added RefPrefix tag for layouts and floats.
|
|
|
|
|
2010-03-29 18:37:25 +00:00
|
|
|
# Incremented to format 26, 29 March 2010 by rgh
|
|
|
|
# Added CiteFormat.
|
|
|
|
|
2009-05-19 23:34:12 +00:00
|
|
|
# Do not forget to document format change in Customization
|
2009-05-28 02:00:04 +00:00
|
|
|
# Manual (section "Declaring a new text class").
|
2009-05-19 23:34:12 +00:00
|
|
|
|
2010-01-14 14:05:50 +00:00
|
|
|
# You might also want to consider running the
|
|
|
|
# development/tools/updatelayouts.sh script to update all
|
|
|
|
# layout files to the new format.
|
|
|
|
|
2010-03-29 18:37:25 +00:00
|
|
|
currentFormat = 26
|
2008-01-10 03:59:39 +00:00
|
|
|
|
|
|
|
|
2005-10-21 16:11:36 +00:00
|
|
|
def usage(prog_name):
|
|
|
|
return ("Usage: %s inputfile outputfile\n" % prog_name +
|
|
|
|
"or %s <inputfile >outputfile" % prog_name)
|
|
|
|
|
|
|
|
|
|
|
|
def error(message):
|
|
|
|
sys.stderr.write(message + '\n')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2009-06-21 18:23:10 +00:00
|
|
|
def trim_bom(line):
|
|
|
|
" Remove byte order mark."
|
|
|
|
if line[0:3] == "\357\273\277":
|
|
|
|
return line[3:]
|
|
|
|
else:
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
2009-11-13 16:12:04 +00:00
|
|
|
def read(source):
|
2005-10-21 16:11:36 +00:00
|
|
|
" Read input file and strip lineendings."
|
2009-11-14 19:16:53 +00:00
|
|
|
lines = source.read().splitlines()
|
|
|
|
lines[0] = trim_bom(lines[0])
|
2005-10-21 16:11:36 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
def write(output, lines):
|
|
|
|
" Write output file with native lineendings."
|
2009-11-14 19:16:53 +00:00
|
|
|
output.write(os.linesep.join(lines) + os.linesep)
|
2005-10-21 16:11:36 +00:00
|
|
|
|
|
|
|
|
2006-03-29 18:49:27 +00:00
|
|
|
# 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 + '"'
|
|
|
|
|
2008-10-07 15:45:08 +00:00
|
|
|
# appends a string to a list unless it's already there
|
|
|
|
def addstring(s, l):
|
|
|
|
if l.count(s) > 0:
|
|
|
|
return
|
|
|
|
l.append(s)
|
|
|
|
|
2006-03-29 18:49:27 +00:00
|
|
|
|
2005-10-21 16:11:36 +00:00
|
|
|
def convert(lines):
|
|
|
|
" Convert to new format."
|
|
|
|
re_Comment = re.compile(r'^(\s*)#')
|
2008-10-07 15:45:08 +00:00
|
|
|
re_Counter = re.compile(r'\s*Counter\s*', re.IGNORECASE)
|
|
|
|
re_Name = re.compile(r'\s*Name\s+(\S+)\s*', re.IGNORECASE)
|
2008-10-23 00:27:03 +00:00
|
|
|
re_UseMod = re.compile(r'^\s*UseModule\s+(.*)', re.IGNORECASE)
|
2005-10-21 16:11:36 +00:00
|
|
|
re_Empty = re.compile(r'^(\s*)$')
|
|
|
|
re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
|
|
|
|
re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
|
|
|
|
re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
|
2009-02-17 20:25:56 +00:00
|
|
|
re_LangPreamble = re.compile(r'^(\s*)LangPreamble', re.IGNORECASE)
|
|
|
|
re_EndLangPreamble = re.compile(r'^(\s*)EndLangPreamble', re.IGNORECASE)
|
|
|
|
re_BabelPreamble = re.compile(r'^(\s*)BabelPreamble', re.IGNORECASE)
|
|
|
|
re_EndBabelPreamble = re.compile(r'^(\s*)EndBabelPreamble', re.IGNORECASE)
|
2006-03-29 18:49:27 +00:00
|
|
|
re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
|
2005-10-21 16:11:36 +00:00
|
|
|
re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
|
2006-03-29 18:49:27 +00:00
|
|
|
re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
|
|
|
|
re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
|
2006-01-08 15:35:38 +00:00
|
|
|
re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
|
|
|
|
re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
|
2007-01-30 13:23:21 +00:00
|
|
|
re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
|
|
|
|
re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
|
2006-01-08 15:35:38 +00:00
|
|
|
re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
|
2007-04-06 09:02:23 +00:00
|
|
|
re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
|
2007-08-22 14:36:07 +00:00
|
|
|
re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
|
2009-08-13 19:17:41 +00:00
|
|
|
re_AMSMaths = re.compile(r'^\s*Input ams(?:math|def)s.inc\s*')
|
2008-10-07 15:45:08 +00:00
|
|
|
re_AMSMathsPlain = re.compile(r'^\s*Input amsmaths-plain.inc\s*')
|
|
|
|
re_AMSMathsSeq = re.compile(r'^\s*Input amsmaths-seq.inc\s*')
|
2009-02-04 20:06:50 +00:00
|
|
|
re_TocLevel = re.compile(r'^(\s*)(TocLevel)(\s+)(\S+)', re.IGNORECASE)
|
2009-02-17 20:25:56 +00:00
|
|
|
re_I18nPreamble = re.compile(r'^(\s*)I18nPreamble', re.IGNORECASE)
|
|
|
|
re_EndI18nPreamble = re.compile(r'^(\s*)EndI18nPreamble', re.IGNORECASE)
|
2010-03-05 22:58:29 +00:00
|
|
|
re_Float = re.compile(r'^\s*Float\s*$', re.IGNORECASE)
|
|
|
|
re_Type = re.compile(r'\s*Type\s+(\w+)', re.IGNORECASE)
|
2010-03-05 17:24:38 +00:00
|
|
|
re_Builtin = re.compile(r'^(\s*)LaTeXBuiltin\s+(\w*)', re.IGNORECASE)
|
|
|
|
re_True = re.compile(r'^\s*(?:true|1)\s*$', re.IGNORECASE)
|
2005-10-21 16:11:36 +00:00
|
|
|
|
2006-03-29 18:49:27 +00:00
|
|
|
# counters for sectioning styles (hardcoded in 1.3)
|
|
|
|
counters = {"part" : "\\Roman{part}",
|
|
|
|
"chapter" : "\\arabic{chapter}",
|
|
|
|
"section" : "\\arabic{section}",
|
2006-04-07 15:25:34 +00:00
|
|
|
"subsection" : "\\arabic{section}.\\arabic{subsection}",
|
|
|
|
"subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
|
|
|
|
"paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
|
|
|
|
"subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
|
2006-03-29 18:49:27 +00:00
|
|
|
|
|
|
|
# counters for sectioning styles in appendix (hardcoded in 1.3)
|
|
|
|
appendixcounters = {"chapter" : "\\Alph{chapter}",
|
|
|
|
"section" : "\\Alph{section}",
|
2006-04-07 15:25:34 +00:00
|
|
|
"subsection" : "\\arabic{section}.\\arabic{subsection}",
|
|
|
|
"subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
|
|
|
|
"paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
|
|
|
|
"subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
|
2006-03-29 18:49:27 +00:00
|
|
|
|
|
|
|
# Value of TocLevel for sectioning styles
|
|
|
|
toclevels = {"part" : 0,
|
|
|
|
"chapter" : 0,
|
|
|
|
"section" : 1,
|
|
|
|
"subsection" : 2,
|
|
|
|
"subsubsection" : 3,
|
|
|
|
"paragraph" : 4,
|
|
|
|
"subparagraph" : 5}
|
|
|
|
|
2005-10-21 16:11:36 +00:00
|
|
|
i = 0
|
|
|
|
only_comment = 1
|
2006-03-29 18:49:27 +00:00
|
|
|
counter = ""
|
2009-02-04 20:06:50 +00:00
|
|
|
toclevel = ""
|
2006-01-08 15:35:38 +00:00
|
|
|
label = ""
|
2006-03-29 18:49:27 +00:00
|
|
|
labelstring = ""
|
|
|
|
labelstringappendix = ""
|
2006-01-08 15:35:38 +00:00
|
|
|
space1 = ""
|
2006-03-29 18:49:27 +00:00
|
|
|
labelstring_line = -1
|
|
|
|
labelstringappendix_line = -1
|
|
|
|
labeltype_line = -1
|
2006-04-13 08:54:50 +00:00
|
|
|
latextype = ""
|
2006-01-19 08:40:10 +00:00
|
|
|
latextype_line = -1
|
2006-01-08 15:35:38 +00:00
|
|
|
style = ""
|
2006-03-29 18:49:27 +00:00
|
|
|
maxcounter = 0
|
2007-01-30 13:23:21 +00:00
|
|
|
format = 1
|
2008-10-07 15:45:08 +00:00
|
|
|
formatline = 0
|
|
|
|
usemodules = []
|
|
|
|
|
2005-10-21 16:11:36 +00:00
|
|
|
while i < len(lines):
|
|
|
|
# Skip comments and empty lines
|
|
|
|
if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2005-10-21 16:11:36 +00:00
|
|
|
continue
|
|
|
|
|
2006-01-19 08:40:10 +00:00
|
|
|
# insert file format if not already there
|
2005-10-21 16:11:36 +00:00
|
|
|
if (only_comment):
|
2008-10-06 12:16:17 +00:00
|
|
|
match = re_Format.match(lines[i])
|
|
|
|
if match:
|
2008-10-07 15:45:08 +00:00
|
|
|
formatline = i
|
2008-10-06 12:16:17 +00:00
|
|
|
format = int(match.group(4))
|
|
|
|
if format > 1 and format < currentFormat:
|
|
|
|
lines[i] = "Format %d" % (format + 1)
|
|
|
|
only_comment = 0
|
|
|
|
elif format == currentFormat:
|
|
|
|
# nothing to do
|
|
|
|
return format
|
2005-10-21 16:11:36 +00:00
|
|
|
else:
|
2008-10-06 12:16:17 +00:00
|
|
|
error('Cannot convert file format %s' % format)
|
|
|
|
else:
|
|
|
|
lines.insert(i, "Format 2")
|
|
|
|
only_comment = 0
|
|
|
|
continue
|
2005-10-21 16:11:36 +00:00
|
|
|
|
|
|
|
# Don't get confused by LaTeX code
|
|
|
|
if re_Preamble.match(lines[i]):
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2005-10-21 16:11:36 +00:00
|
|
|
while i < len(lines) and not re_EndPreamble.match(lines[i]):
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
|
|
|
continue
|
2009-02-17 20:25:56 +00:00
|
|
|
if re_LangPreamble.match(lines[i]):
|
|
|
|
i += 1
|
|
|
|
while i < len(lines) and not re_EndLangPreamble.match(lines[i]):
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
if re_BabelPreamble.match(lines[i]):
|
|
|
|
i += 1
|
|
|
|
while i < len(lines) and not re_EndBabelPreamble.match(lines[i]):
|
|
|
|
i += 1
|
|
|
|
continue
|
2010-03-17 12:23:24 +00:00
|
|
|
|
|
|
|
# Only new features
|
2010-03-29 18:37:25 +00:00
|
|
|
if format >= 24 and format <= 25:
|
2010-03-17 12:23:24 +00:00
|
|
|
i += 1
|
|
|
|
continue
|
2009-02-17 20:25:56 +00:00
|
|
|
|
2010-03-05 17:24:38 +00:00
|
|
|
if format == 23:
|
2010-03-05 22:58:29 +00:00
|
|
|
match = re_Float.match(lines[i])
|
|
|
|
i += 1
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
# we need to do two things:
|
|
|
|
# (i) Convert Builtin to NeedsFloatPkg
|
|
|
|
# (ii) Write ListCommand lines for the builtin floats table and figure
|
|
|
|
builtin = False
|
|
|
|
cmd = ""
|
|
|
|
while True and i < len(lines):
|
|
|
|
m1 = re_End.match(lines[i])
|
|
|
|
if m1:
|
|
|
|
if builtin and cmd:
|
|
|
|
line = " ListCommand " + cmd
|
|
|
|
lines.insert(i, line)
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
m2 = re_Builtin.match(lines[i])
|
|
|
|
if m2:
|
|
|
|
builtin = True
|
|
|
|
ws1 = m2.group(1)
|
|
|
|
arg = m2.group(2)
|
|
|
|
newarg = ""
|
|
|
|
if re_True.match(arg):
|
|
|
|
newarg = "false"
|
|
|
|
else:
|
|
|
|
newarg = "true"
|
|
|
|
lines[i] = ws1 + "NeedsFloatPkg " + newarg
|
|
|
|
m3 = re_Type.match(lines[i])
|
|
|
|
if m3:
|
|
|
|
fltype = m3.group(1)
|
|
|
|
fltype = fltype.lower()
|
|
|
|
if fltype == "table":
|
|
|
|
cmd = "listoftables"
|
|
|
|
elif fltype == "figure":
|
|
|
|
cmd = "listoffigures"
|
|
|
|
# else unknown, which is why we're doing this
|
|
|
|
i += 1
|
|
|
|
continue
|
2010-03-05 17:24:38 +00:00
|
|
|
|
2009-05-28 08:59:25 +00:00
|
|
|
# This just involved new features, not any changes to old ones
|
2010-02-13 15:44:17 +00:00
|
|
|
if format >= 14 and format <= 22:
|
2009-05-28 08:59:25 +00:00
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
2009-02-17 20:25:56 +00:00
|
|
|
# Rename I18NPreamble to BabelPreamble
|
|
|
|
if format == 13:
|
|
|
|
match = re_I18nPreamble.match(lines[i])
|
|
|
|
if match:
|
|
|
|
lines[i] = match.group(1) + "BabelPreamble"
|
|
|
|
i += 1
|
|
|
|
match = re_EndI18nPreamble.match(lines[i])
|
|
|
|
while i < len(lines) and not match:
|
|
|
|
i += 1
|
|
|
|
match = re_EndI18nPreamble.match(lines[i])
|
|
|
|
lines[i] = match.group(1) + "EndBabelPreamble"
|
|
|
|
i += 1
|
|
|
|
continue
|
2007-08-22 14:36:07 +00:00
|
|
|
|
2009-02-06 17:54:33 +00:00
|
|
|
# These just involved new features, not any changes to old ones
|
|
|
|
if format == 11 or format == 12:
|
2009-02-04 20:06:50 +00:00
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
2008-10-23 00:27:03 +00:00
|
|
|
if format == 10:
|
|
|
|
match = re_UseMod.match(lines[i])
|
|
|
|
if match:
|
|
|
|
module = match.group(1)
|
|
|
|
lines[i] = "DefaultModule " + module
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
2008-10-06 14:37:28 +00:00
|
|
|
if format == 9:
|
|
|
|
match = re_Counter.match(lines[i])
|
|
|
|
if match:
|
|
|
|
counterline = i
|
|
|
|
i += 1
|
|
|
|
while i < len(lines):
|
|
|
|
namem = re_Name.match(lines[i])
|
|
|
|
if namem:
|
|
|
|
name = namem.group(1)
|
|
|
|
lines.pop(i)
|
|
|
|
lines[counterline] = "Counter %s" % name
|
|
|
|
# we don't need to increment i
|
|
|
|
continue
|
|
|
|
endem = re_End.match(lines[i])
|
|
|
|
if endem:
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
2008-10-07 15:45:08 +00:00
|
|
|
if format == 8:
|
|
|
|
# We want to scan for ams-type includes and, if we find them,
|
|
|
|
# add corresponding UseModule tags to the layout.
|
|
|
|
match = re_AMSMaths.match(lines[i])
|
|
|
|
if match:
|
|
|
|
addstring("theorems-ams", usemodules)
|
|
|
|
addstring("theorems-ams-extended", usemodules)
|
|
|
|
addstring("theorems-sec", usemodules)
|
|
|
|
lines.pop(i)
|
|
|
|
continue
|
|
|
|
match = re_AMSMathsPlain.match(lines[i])
|
|
|
|
if match:
|
|
|
|
addstring("theorems-starred", usemodules)
|
|
|
|
lines.pop(i)
|
|
|
|
continue
|
|
|
|
match = re_AMSMathsSeq.match(lines[i])
|
|
|
|
if match:
|
|
|
|
addstring("theorems-ams", usemodules)
|
|
|
|
addstring("theorems-ams-extended", usemodules)
|
|
|
|
lines.pop(i)
|
|
|
|
continue
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
2008-10-05 19:39:09 +00:00
|
|
|
# These just involved new features, not any changes to old ones
|
2008-10-07 15:45:08 +00:00
|
|
|
if format >= 5 and format <= 7:
|
2008-01-10 03:59:39 +00:00
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
2007-08-22 14:36:07 +00:00
|
|
|
if format == 4:
|
|
|
|
# Handle conversion to long CharStyle names
|
|
|
|
match = re_CharStyle.match(lines[i])
|
|
|
|
if match:
|
|
|
|
lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
|
|
|
|
i += 1
|
|
|
|
lines.insert(i, "\tLyXType charstyle")
|
|
|
|
i += 1
|
|
|
|
lines.insert(i, "")
|
|
|
|
lines[i] = "\tLabelString %s" % (match.group(3))
|
|
|
|
i += 1
|
2005-10-21 16:11:36 +00:00
|
|
|
continue
|
|
|
|
|
2007-04-06 09:02:23 +00:00
|
|
|
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))
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2007-04-06 09:02:23 +00:00
|
|
|
continue
|
|
|
|
|
2007-01-30 13:23:21 +00:00
|
|
|
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']
|
|
|
|
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2007-01-30 13:23:21 +00:00
|
|
|
continue
|
|
|
|
|
2006-03-29 18:49:27 +00:00
|
|
|
# Delete MaxCounter and remember the value of it
|
|
|
|
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
|
2005-10-21 16:11:36 +00:00
|
|
|
del lines[i]
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Replace line
|
|
|
|
#
|
|
|
|
# LabelType Counter_EnumI
|
|
|
|
#
|
|
|
|
# with two lines
|
|
|
|
#
|
|
|
|
# LabelType Counter
|
|
|
|
# LabelCounter EnumI
|
|
|
|
#
|
|
|
|
match = re_LabelType.match(lines[i])
|
|
|
|
if match:
|
|
|
|
label = match.group(4)
|
2006-03-29 18:49:27 +00:00
|
|
|
# Remember indenting space for later reuse in added lines
|
2006-01-08 15:35:38 +00:00
|
|
|
space1 = match.group(1)
|
2006-03-29 18:49:27 +00:00
|
|
|
# Remember the line for adding the LabelCounter later.
|
|
|
|
# We can't do it here because it could shift latextype_line etc.
|
|
|
|
labeltype_line = i
|
2005-10-21 16:11:36 +00:00
|
|
|
if string.lower(label[:8]) == "counter_":
|
2006-03-29 18:49:27 +00:00
|
|
|
counter = string.lower(label[8:])
|
2005-10-21 16:11:36 +00:00
|
|
|
lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
|
|
|
|
|
2006-03-29 18:49:27 +00:00
|
|
|
# Remember the LabelString line
|
|
|
|
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
|
2006-01-08 15:35:38 +00:00
|
|
|
match = re_LatexType.match(lines[i])
|
|
|
|
if match:
|
2006-04-13 08:54:50 +00:00
|
|
|
latextype = string.lower(match.group(4))
|
2006-01-19 08:40:10 +00:00
|
|
|
latextype_line = i
|
2006-03-29 18:49:27 +00:00
|
|
|
|
2009-02-04 20:06:50 +00:00
|
|
|
# Remember the TocLevel line
|
|
|
|
match = re_TocLevel.match(lines[i])
|
|
|
|
if match:
|
|
|
|
toclevel = string.lower(match.group(4))
|
|
|
|
|
2006-03-29 18:49:27 +00:00
|
|
|
# Reset variables at the beginning of a style definition
|
2006-01-08 15:35:38 +00:00
|
|
|
match = re_Style.match(lines[i])
|
|
|
|
if match:
|
2006-03-29 18:49:27 +00:00
|
|
|
style = string.lower(match.group(4))
|
|
|
|
counter = ""
|
2009-02-04 20:06:50 +00:00
|
|
|
toclevel = ""
|
2006-01-08 15:35:38 +00:00
|
|
|
label = ""
|
|
|
|
space1 = ""
|
2006-03-29 18:49:27 +00:00
|
|
|
labelstring = ""
|
|
|
|
labelstringappendix = ""
|
|
|
|
labelstring_line = -1
|
|
|
|
labelstringappendix_line = -1
|
|
|
|
labeltype_line = -1
|
2006-04-13 08:54:50 +00:00
|
|
|
latextype = ""
|
2006-01-19 08:40:10 +00:00
|
|
|
latextype_line = -1
|
2006-03-29 18:49:27 +00:00
|
|
|
|
|
|
|
if re_End.match(lines[i]):
|
|
|
|
|
|
|
|
# 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)
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2006-03-29 18:49:27 +00:00
|
|
|
else:
|
|
|
|
lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
|
|
|
|
|
2006-04-13 08:54:50 +00:00
|
|
|
# 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 = ""
|
|
|
|
|
2006-03-29 18:49:27 +00:00
|
|
|
# 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]))
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2006-03-29 18:49:27 +00:00
|
|
|
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]))
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2006-03-29 18:49:27 +00:00
|
|
|
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))
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2006-03-29 18:49:27 +00:00
|
|
|
|
|
|
|
# Add the TocLevel setting for sectioning styles
|
2009-02-04 20:06:50 +00:00
|
|
|
if toclevel == "" and toclevels.has_key(style) and maxcounter <= toclevels[style]:
|
2009-12-05 22:56:16 +00:00
|
|
|
lines.insert(i, '%s\tTocLevel %d' % (space1, toclevels[style]))
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2006-01-08 15:35:38 +00:00
|
|
|
|
2007-08-22 14:36:07 +00:00
|
|
|
i += 1
|
2005-10-21 16:11:36 +00:00
|
|
|
|
2008-10-07 15:45:08 +00:00
|
|
|
if usemodules:
|
|
|
|
i = formatline + 1
|
|
|
|
for mod in usemodules:
|
|
|
|
lines.insert(i, "UseModule " + mod)
|
|
|
|
i += 1
|
|
|
|
|
2007-03-24 12:39:45 +00:00
|
|
|
return format + 1
|
|
|
|
|
2005-10-21 16:11:36 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
|
|
|
|
# Open files
|
|
|
|
if len(argv) == 1:
|
2009-11-13 16:12:04 +00:00
|
|
|
source = sys.stdin
|
2005-10-21 16:11:36 +00:00
|
|
|
output = sys.stdout
|
|
|
|
elif len(argv) == 3:
|
2009-11-13 16:12:04 +00:00
|
|
|
source = open(argv[1], 'rb')
|
2005-10-21 16:11:36 +00:00
|
|
|
output = open(argv[2], 'wb')
|
|
|
|
else:
|
|
|
|
error(usage(argv[0]))
|
|
|
|
|
|
|
|
# Do the real work
|
2009-11-13 16:12:04 +00:00
|
|
|
lines = read(source)
|
2007-03-24 12:39:45 +00:00
|
|
|
format = 1
|
2008-01-10 03:59:39 +00:00
|
|
|
while (format < currentFormat):
|
2007-03-24 12:39:45 +00:00
|
|
|
format = convert(lines)
|
2005-10-21 16:11:36 +00:00
|
|
|
write(output, lines)
|
|
|
|
|
|
|
|
# Close files
|
|
|
|
if len(argv) == 3:
|
2009-11-13 16:12:04 +00:00
|
|
|
source.close()
|
2005-10-21 16:11:36 +00:00
|
|
|
output.close()
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|