layout2layout.py: Python 3 compatibility

The layout2layout.py script already reads and writes files in
binary format. However, all regexes and comparisons assume strings,
so that python3 chokes on them. This commit converts all involved
strings into bytes-like objects. In this way both python 2 and 3
behave the same.
This commit is contained in:
Enrico Forestieri 2017-04-13 16:20:39 +02:00
parent 70b0f8d953
commit 1a26f9b26c

View File

@ -220,7 +220,7 @@ currentFormat = 63
# layout files to the new format. # layout files to the new format.
import os, re, string, sys import os, re, sys
import argparse import argparse
# Provide support for both python 2 and 3 # Provide support for both python 2 and 3
@ -259,17 +259,18 @@ def read(source):
def write(output, lines): def write(output, lines):
" Write output file with native lineendings." " Write output file with native lineendings."
output.write(os.linesep.join(lines) + os.linesep) output.write(os.linesep.encode('ascii').join(lines)
+ os.linesep.encode('ascii'))
# Concatenates old and new in an intelligent way: # Concatenates old and new in an intelligent way:
# If old is wrapped in ", they are stripped. The result is wrapped in ". # If old is wrapped in ", they are stripped. The result is wrapped in ".
def concatenate_label(old, new): def concatenate_label(old, new):
# Don't use strip as long as we support python 1.5.2 # Don't use strip as long as we support python 1.5.2
if old[0] == '"': if old[0] == b'"':
return old[0:-1] + new + '"' return old[0:-1] + new + b'"'
else: else:
return '"' + old + new + '"' return b'"' + old + new + b'"'
# appends a string to a list unless it's already there # appends a string to a list unless it's already there
def addstring(s, l): def addstring(s, l):
@ -280,112 +281,112 @@ def addstring(s, l):
def convert(lines, end_format): def convert(lines, end_format):
" Convert to new format." " Convert to new format."
re_Comment = re.compile(r'^(\s*)#') re_Comment = re.compile(b'^(\\s*)#')
re_Counter = re.compile(r'\s*Counter\s*', re.IGNORECASE) re_Counter = re.compile(b'\\s*Counter\\s*', re.IGNORECASE)
re_Name = re.compile(r'\s*Name\s+(\S+)\s*', re.IGNORECASE) re_Name = re.compile(b'\\s*Name\\s+(\\S+)\\s*', re.IGNORECASE)
re_UseMod = re.compile(r'^\s*UseModule\s+(.*)', re.IGNORECASE) re_UseMod = re.compile(b'^\\s*UseModule\\s+(.*)', re.IGNORECASE)
re_Empty = re.compile(r'^(\s*)$') re_Empty = re.compile(b'^(\\s*)$')
re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE) re_Format = re.compile(b'^(\\s*)(Format)(\\s+)(\\S+)', re.IGNORECASE)
re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE) re_Preamble = re.compile(b'^(\\s*)Preamble', re.IGNORECASE)
re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE) re_EndPreamble = re.compile(b'^(\\s*)EndPreamble', re.IGNORECASE)
re_LangPreamble = re.compile(r'^(\s*)LangPreamble', re.IGNORECASE) re_LangPreamble = re.compile(b'^(\\s*)LangPreamble', re.IGNORECASE)
re_EndLangPreamble = re.compile(r'^(\s*)EndLangPreamble', re.IGNORECASE) re_EndLangPreamble = re.compile(b'^(\\s*)EndLangPreamble', re.IGNORECASE)
re_BabelPreamble = re.compile(r'^(\s*)BabelPreamble', re.IGNORECASE) re_BabelPreamble = re.compile(b'^(\\s*)BabelPreamble', re.IGNORECASE)
re_EndBabelPreamble = re.compile(r'^(\s*)EndBabelPreamble', re.IGNORECASE) re_EndBabelPreamble = re.compile(b'^(\\s*)EndBabelPreamble', re.IGNORECASE)
re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE) re_MaxCounter = re.compile(b'^(\\s*)(MaxCounter)(\\s+)(\\S+)', re.IGNORECASE)
re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE) re_LabelType = re.compile(b'^(\\s*)(LabelType)(\\s+)(\\S+)', re.IGNORECASE)
re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE) re_LabelString = re.compile(b'^(\\s*)(LabelString)(\\s+)(("[^"]+")|(\\S+))', re.IGNORECASE)
re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE) re_LabelStringAppendix = re.compile(b'^(\\s*)(LabelStringAppendix)(\\s+)(("[^"]+")|(\\S+))', re.IGNORECASE)
re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE) re_LatexType = re.compile(b'^(\\s*)(LatexType)(\\s+)(\\S+)', re.IGNORECASE)
re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE) re_Style = re.compile(b'^(\\s*)(Style)(\\s+)(\\S+)', re.IGNORECASE)
re_IfStyle = re.compile(r'^(\s*)IfStyle(\s+\S+)', re.IGNORECASE) re_IfStyle = re.compile(b'^(\\s*)IfStyle(\\s+\\S+)', re.IGNORECASE)
re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE) re_CopyStyle = re.compile(b'^(\\s*)(CopyStyle)(\\s+)(\\S+)', re.IGNORECASE)
re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE) re_NoStyle = re.compile(b'^(\\s*)(NoStyle)(\\s+)(\\S+)', re.IGNORECASE)
re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE) re_End = re.compile(b'^(\\s*)(End)(\\s*)$', re.IGNORECASE)
re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE) re_Provides = re.compile(b'^(\\s*)Provides(\\S+)(\\s+)(\\S+)', re.IGNORECASE)
re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE) re_CharStyle = re.compile(b'^(\\s*)CharStyle(\\s+)(\\S+)$', re.IGNORECASE)
re_CiteFormat = re.compile(r'^(\s*)(CiteFormat)(?:(\s*)()|(\s+)(default|authoryear|numerical))', re.IGNORECASE) re_CiteFormat = re.compile(b'^(\\s*)(CiteFormat)(?:(\\s*)()|(\\s+)(default|authoryear|numerical))', re.IGNORECASE)
re_AMSMaths = re.compile(r'^\s*Input ams(?:math|def)s.inc\s*') re_AMSMaths = re.compile(b'^\\s*Input ams(?:math|def)s.inc\\s*')
re_AMSMathsPlain = re.compile(r'^\s*Input amsmaths-plain.inc\s*') re_AMSMathsPlain = re.compile(b'^\\s*Input amsmaths-plain.inc\\s*')
re_AMSMathsSeq = re.compile(r'^\s*Input amsmaths-seq.inc\s*') re_AMSMathsSeq = re.compile(b'^\\s*Input amsmaths-seq.inc\\s*')
re_TocLevel = re.compile(r'^(\s*)(TocLevel)(\s+)(\S+)', re.IGNORECASE) re_TocLevel = re.compile(b'^(\\s*)(TocLevel)(\\s+)(\\S+)', re.IGNORECASE)
re_I18nPreamble = re.compile(r'^(\s*)I18nPreamble', re.IGNORECASE) re_I18nPreamble = re.compile(b'^(\\s*)I18nPreamble', re.IGNORECASE)
re_EndI18nPreamble = re.compile(r'^(\s*)EndI18nPreamble', re.IGNORECASE) re_EndI18nPreamble = re.compile(b'^(\\s*)EndI18nPreamble', re.IGNORECASE)
re_Float = re.compile(r'^\s*Float\s*$', re.IGNORECASE) re_Float = re.compile(b'^\\s*Float\\s*$', re.IGNORECASE)
re_Type = re.compile(r'\s*Type\s+(\w+)', re.IGNORECASE) re_Type = re.compile(b'\\s*Type\\s+(\\w+)', re.IGNORECASE)
re_Builtin = re.compile(r'^(\s*)LaTeXBuiltin\s+(\w*)', re.IGNORECASE) re_Builtin = re.compile(b'^(\\s*)LaTeXBuiltin\\s+(\\w*)', re.IGNORECASE)
re_True = re.compile(r'^\s*(?:true|1)\s*$', re.IGNORECASE) re_True = re.compile(b'^\\s*(?:true|1)\\s*$', re.IGNORECASE)
re_InsetLayout = re.compile(r'^\s*InsetLayout\s+(?:Custom|CharStyle|Element):(\S+)\s*$', re.IGNORECASE) re_InsetLayout = re.compile(b'^\\s*InsetLayout\\s+(?:Custom|CharStyle|Element):(\\S+)\\s*$', re.IGNORECASE)
re_ResetsFont = re.compile(r'^(\s*)ResetsFont(\s+)(\S+)$', re.IGNORECASE) re_ResetsFont = re.compile(b'^(\\s*)ResetsFont(\\s+)(\\S+)$', re.IGNORECASE)
# with quotes # with quotes
re_QInsetLayout = re.compile(r'^\s*InsetLayout\s+"(?:Custom|CharStyle|Element):([^"]+)"\s*$', re.IGNORECASE) re_QInsetLayout = re.compile(b'^\\s*InsetLayout\\s+"(?:Custom|CharStyle|Element):([^"]+)"\\s*$', re.IGNORECASE)
re_InsetLayout_CopyStyle = re.compile(r'^\s*CopyStyle\s+(?:Custom|CharStyle|Element):(\S+)\s*$', re.IGNORECASE) re_InsetLayout_CopyStyle = re.compile(b'^\\s*CopyStyle\\s+(?:Custom|CharStyle|Element):(\\S+)\\s*$', re.IGNORECASE)
re_QInsetLayout_CopyStyle = re.compile(r'^\s*CopyStyle\s+"(?:Custom|CharStyle|Element):([^"]+)"\s*$', re.IGNORECASE) re_QInsetLayout_CopyStyle = re.compile(b'^\\s*CopyStyle\\s+"(?:Custom|CharStyle|Element):([^"]+)"\\s*$', re.IGNORECASE)
re_NeedsFloatPkg = re.compile(r'^(\s*)NeedsFloatPkg\s+(\w+)\s*$', re.IGNORECASE) re_NeedsFloatPkg = re.compile(b'^(\\s*)NeedsFloatPkg\\s+(\\w+)\\s*$', re.IGNORECASE)
re_Fill = re.compile(r'^\s*Fill_(?:Top|Bottom).*$', re.IGNORECASE) re_Fill = re.compile(b'^\\s*Fill_(?:Top|Bottom).*$', re.IGNORECASE)
re_InsetLayout2 = re.compile(r'^\s*InsetLayout\s+(\S+)\s*$', re.IGNORECASE) re_InsetLayout2 = re.compile(b'^\\s*InsetLayout\\s+(\\S+)\\s*$', re.IGNORECASE)
# with quotes # with quotes
re_QInsetLayout2 = re.compile(r'^\s*InsetLayout\s+"([^"]+)"\s*$', re.IGNORECASE) re_QInsetLayout2 = re.compile(b'^\\s*InsetLayout\\s+"([^"]+)"\\s*$', re.IGNORECASE)
re_IsFlex = re.compile(r'\s*LyXType.*$', re.IGNORECASE) re_IsFlex = re.compile(b'\\s*LyXType.*$', re.IGNORECASE)
re_CopyStyle2 = re.compile(r'(\s*CopyStyle\s+)"?([^"]+)"?\s*$') re_CopyStyle2 = re.compile(b'(\\s*CopyStyle\\s+)"?([^"]+)"?\\s*$')
re_Separator = re.compile(r'^(?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE) re_Separator = re.compile(b'^(?:(-*)|(\\s*))(Separator|EndOfSlide)(?:(-*)|(\\s*))$', re.IGNORECASE)
# for categories # for categories
re_Declaration = re.compile(r'^#\s*\\Declare\w+Class.*$') re_Declaration = re.compile(b'^#\\s*\\Declare\\w+Class.*$')
re_ExtractCategory = re.compile(r'^(#\s*\\Declare\w+Class(?:\[[^]]*?\])?){([^(]+?)\s+\(([^)]+?)\)\s*}\s*$') re_ExtractCategory = re.compile(b'^(#\\s*\\Declare\\w+Class(?:\\[[^]]*?\\])?){([^(]+?)\\s+\\(([^)]+?)\\)\\s*}\\s*$')
ConvDict = {"article": "Articles", "book" : "Books", "letter" : "Letters", "report": "Reports", \ ConvDict = {"article": "Articles", "book" : "Books", "letter" : "Letters", "report": "Reports", \
"presentation" : "Presentations", "curriculum vitae" : "Curricula Vitae", "handout" : "Handouts"} "presentation" : "Presentations", "curriculum vitae" : "Curricula Vitae", "handout" : "Handouts"}
# Arguments # Arguments
re_OptArgs = re.compile(r'^(\s*)OptionalArgs(\s+)(\d+)\D*$', re.IGNORECASE) re_OptArgs = re.compile(b'^(\\s*)OptionalArgs(\\s+)(\\d+)\\D*$', re.IGNORECASE)
re_ReqArgs = re.compile(r'^(\s*)RequiredArgs(\s+)(\d+)\D*$', re.IGNORECASE) re_ReqArgs = re.compile(b'^(\\s*)RequiredArgs(\\s+)(\\d+)\\D*$', re.IGNORECASE)
# various changes associated with changing how chapters are handled # various changes associated with changing how chapters are handled
re_LabelTypeIsCounter = re.compile(r'^(\s*)LabelType(\s*)Counter\s*$', re.IGNORECASE) re_LabelTypeIsCounter = re.compile(b'^(\\s*)LabelType(\\s*)Counter\\s*$', re.IGNORECASE)
re_TopEnvironment = re.compile(r'^(\s*)LabelType(\s+)Top_Environment\s*$', re.IGNORECASE) re_TopEnvironment = re.compile(b'^(\\s*)LabelType(\\s+)Top_Environment\\s*$', re.IGNORECASE)
re_CenteredEnvironment = re.compile(r'^(\s*)LabelType(\s+)Centered_Top_Environment\s*$', re.IGNORECASE) re_CenteredEnvironment = re.compile(b'^(\\s*)LabelType(\\s+)Centered_Top_Environment\\s*$', re.IGNORECASE)
re_ChapterStyle = re.compile(r'^\s*Style\s+Chapter\s*$', re.IGNORECASE) re_ChapterStyle = re.compile(b'^\\s*Style\\s+Chapter\\s*$', re.IGNORECASE)
re_InsetLayout_CaptionLTNN = re.compile(r'^(\s*InsetLayout\s+)(Caption:LongTableNonumber)', re.IGNORECASE) re_InsetLayout_CaptionLTNN = re.compile(b'^(\\s*InsetLayout\\s+)(Caption:LongTableNonumber)', re.IGNORECASE)
# counters for sectioning styles (hardcoded in 1.3) # counters for sectioning styles (hardcoded in 1.3)
counters = {"part" : "\\Roman{part}", counters = {b"part" : b"\\Roman{part}",
"chapter" : "\\arabic{chapter}", b"chapter" : b"\\arabic{chapter}",
"section" : "\\arabic{section}", b"section" : b"\\arabic{section}",
"subsection" : "\\arabic{section}.\\arabic{subsection}", b"subsection" : b"\\arabic{section}.\\arabic{subsection}",
"subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}", b"subsubsection" : b"\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
"paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}", b"paragraph" : b"\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
"subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"} b"subparagraph" : b"\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
# counters for sectioning styles in appendix (hardcoded in 1.3) # counters for sectioning styles in appendix (hardcoded in 1.3)
appendixcounters = {"chapter" : "\\Alph{chapter}", appendixcounters = {b"chapter" : b"\\Alph{chapter}",
"section" : "\\Alph{section}", b"section" : b"\\Alph{section}",
"subsection" : "\\arabic{section}.\\arabic{subsection}", b"subsection" : b"\\arabic{section}.\\arabic{subsection}",
"subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}", b"subsubsection" : b"\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
"paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}", b"paragraph" : b"\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
"subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"} b"subparagraph" : b"\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
# Value of TocLevel for sectioning styles # Value of TocLevel for sectioning styles
toclevels = {"part" : -1, toclevels = {b"part" : -1,
"chapter" : 0, b"chapter" : 0,
"section" : 1, b"section" : 1,
"subsection" : 2, b"subsection" : 2,
"subsubsection" : 3, b"subsubsection" : 3,
"paragraph" : 4, b"paragraph" : 4,
"subparagraph" : 5} b"subparagraph" : 5}
i = 0 i = 0
only_comment = 1 only_comment = 1
counter = "" counter = b""
toclevel = "" toclevel = b""
label = "" label = b""
labelstring = "" labelstring = b""
labelstringappendix = "" labelstringappendix = b""
space1 = "" space1 = b""
labelstring_line = -1 labelstring_line = -1
labelstringappendix_line = -1 labelstringappendix_line = -1
labeltype_line = -1 labeltype_line = -1
latextype = "" latextype = b""
latextype_line = -1 latextype_line = -1
style = "" style = b""
maxcounter = 0 maxcounter = 0
format = 1 format = 1
formatline = 0 formatline = 0
@ -413,8 +414,8 @@ def convert(lines, end_format):
lnam = match.group(3) lnam = match.group(3)
if lcat in ConvDict: if lcat in ConvDict:
lcat = ConvDict[lcat] lcat = ConvDict[lcat]
lines[i] = lpre + "{" + lnam + "}" lines[i] = lpre + b"{" + lnam + b"}"
lines.insert(i+1, "# \\DeclareCategory{" + lcat + "}") lines.insert(i+1, b"# \\DeclareCategory{" + lcat + b"}")
i += 1 i += 1
i += 1 i += 1
continue continue
@ -426,7 +427,7 @@ def convert(lines, end_format):
formatline = i formatline = i
format = int(match.group(4)) format = int(match.group(4))
if format > 1 and format < end_format: if format > 1 and format < end_format:
lines[i] = "Format %d" % (format + 1) lines[i] = b"Format %d" % (format + 1)
only_comment = 0 only_comment = 0
elif format == end_format: elif format == end_format:
# nothing to do # nothing to do
@ -434,7 +435,7 @@ def convert(lines, end_format):
else: else:
error('Cannot convert file format %s to %s' % (format, end_format)) error('Cannot convert file format %s to %s' % (format, end_format))
else: else:
lines.insert(i, "Format 2") lines.insert(i, b"Format 2")
only_comment = 0 only_comment = 0
continue continue
@ -467,7 +468,7 @@ def convert(lines, end_format):
continue continue
# '^(\s*InsetLayout\s+)(Caption:LongTableNonumber)' # '^(\s*InsetLayout\s+)(Caption:LongTableNonumber)'
lead = match.group(1) lead = match.group(1)
lines[i] = lead + "Caption:Unnumbered" lines[i] = lead + b"Caption:Unnumbered"
i += 1 i += 1
continue continue
@ -481,10 +482,10 @@ def convert(lines, end_format):
if not match: if not match:
i += 1 i += 1
continue continue
# r'^(\s*)IfStyle(\s+\S+) # b'^(\\s*)IfStyle(\\s+\\S+)
lead = match.group(1) lead = match.group(1)
trail = match.group(2) trail = match.group(2)
lines[i] = lead + "ModifyStyle" + trail lines[i] = lead + b"ModifyStyle" + trail
i += 1 i += 1
continue continue
@ -499,7 +500,7 @@ def convert(lines, end_format):
# delete separator styles # delete separator styles
match = re_Style.match(lines[i]) match = re_Style.match(lines[i])
if match: if match:
style = string.lower(match.group(4)) style = match.group(4).lower()
if re_Separator.match(style): if re_Separator.match(style):
del lines[i] del lines[i]
while i < len(lines) and not re_End.match(lines[i]): while i < len(lines) and not re_End.match(lines[i]):
@ -514,7 +515,7 @@ def convert(lines, end_format):
# delete undefinition of separator styles # delete undefinition of separator styles
match = re_NoStyle.match(lines[i]) match = re_NoStyle.match(lines[i])
if match: if match:
style = string.lower(match.group(4)) style = match.group(4).lower()
if re_Separator.match(style): if re_Separator.match(style):
del lines[i] del lines[i]
continue continue
@ -524,29 +525,29 @@ def convert(lines, end_format):
# since the second one will overwrite the first one. # since the second one will overwrite the first one.
match = re_CopyStyle.match(lines[i]) match = re_CopyStyle.match(lines[i])
if match: if match:
style = string.lower(match.group(4)) style = match.group(4).lower()
if re_Separator.match(style): if re_Separator.match(style):
if len(separator) > 0: if len(separator) > 0:
lines[i:i+1] = separator lines[i:i+1] = separator
else: else:
# FIXME: If this style was redefined in an include file, # FIXME: If this style was redefined in an include file,
# we should replace the real style and not this default. # we should replace the real style and not this default.
lines[i:i+1] = [' Category MainText', lines[i:i+1] = [b' Category MainText',
' KeepEmpty 1', b' KeepEmpty 1',
' Margin Dynamic', b' Margin Dynamic',
' LatexType Paragraph', b' LatexType Paragraph',
' LatexName dummy', b' LatexName dummy',
' ParIndent MM', b' ParIndent MM',
' Align Block', b' Align Block',
' LabelType Static', b' LabelType Static',
' LabelString "--- Separate Environment ---"', b' LabelString "--- Separate Environment ---"',
' LabelFont', b' LabelFont',
' Family Roman', b' Family Roman',
' Series Medium', b' Series Medium',
' Size Normal', b' Size Normal',
' Color Blue', b' Color Blue',
' EndFont', b' EndFont',
' HTMLLabel NONE'] b' HTMLLabel NONE']
i += 1 i += 1
continue continue
@ -560,8 +561,8 @@ def convert(lines, end_format):
i += 1 i += 1
continue continue
name = string.lower(match.group(1)) name = match.group(1).lower()
if name != "flex" and name != "\"flex\"" and name[0:5] != "flex:" and name [0:6] != "\"flex:": if name != b"flex" and name != b"\"flex\"" and name[0:5] != b"flex:" and name [0:6] != b"\"flex:":
i += 1 i += 1
continue continue
@ -582,7 +583,7 @@ def convert(lines, end_format):
break break
i += 1 i += 1
if not resetsfont_found and not inherited: if not resetsfont_found and not inherited:
lines.insert(i, "\tResetsFont true") lines.insert(i, b"\tResetsFont true")
continue continue
@ -595,17 +596,17 @@ def convert(lines, end_format):
match = re_LabelTypeIsCounter.match(lines[i]) match = re_LabelTypeIsCounter.match(lines[i])
if match: if match:
if inchapter: if inchapter:
lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above" lines[i] = match.group(1) + b"LabelType" + match.group(2) + b"Above"
else: else:
lines[i] = match.group(1) + "LabelType" + match.group(2) + "Static" lines[i] = match.group(1) + b"LabelType" + match.group(2) + b"Static"
match = re_TopEnvironment.match(lines[i]) match = re_TopEnvironment.match(lines[i])
if match: if match:
lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above" lines[i] = match.group(1) + b"LabelType" + match.group(2) + b"Above"
match = re_CenteredEnvironment.match(lines[i]) match = re_CenteredEnvironment.match(lines[i])
if match: if match:
lines[i] = match.group(1) + "LabelType" + match.group(2) + "Centered" lines[i] = match.group(1) + b"LabelType" + match.group(2) + b"Centered"
if inchapter: if inchapter:
match = re_Style.match(lines[i]) match = re_Style.match(lines[i])
@ -620,8 +621,8 @@ def convert(lines, end_format):
continue continue
if format == 42: if format == 42:
if lines[i] == "InsetLayout Caption": if lines[i] == b"InsetLayout Caption":
lines[i] = "InsetLayout Caption:Standard" lines[i] = b"InsetLayout Caption:Standard"
i += 1 i += 1
continue continue
@ -645,7 +646,7 @@ def convert(lines, end_format):
opts = int(match.group(3)) opts = int(match.group(3))
# OptionalArgs 0 > ResetArgs 1 # OptionalArgs 0 > ResetArgs 1
if opts == 0: if opts == 0:
lines[i] = space1 + "ResetArgs\t1" lines[i] = space1 + b"ResetArgs\t1"
i += 1 i += 1
else: else:
del lines[i] del lines[i]
@ -664,28 +665,28 @@ def convert(lines, end_format):
# First the optionals (this is the required order pre 2.1) # First the optionals (this is the required order pre 2.1)
if opts > 0: if opts > 0:
if opts == 1: if opts == 1:
newarg = [ '%sArgument 1' % (space1), newarg = [ b'%sArgument 1' % (space1),
'%s\tLabelString\t\"Optional Layout Argument\"' % (space1), b'%s\tLabelString\t\"Optional Layout Argument\"' % (space1),
'%sEndArgument' % (space1)] b'%sEndArgument' % (space1)]
elif opts > 1: elif opts > 1:
actopt = 1 actopt = 1
while actopt < (opts + 1): while actopt < (opts + 1):
newarg += [ '%sArgument %d' % (space1, actopt), newarg += [ b'%sArgument %d' % (space1, actopt),
'%s\tLabelString\t\"Optional Layout Argument %d\"' % (space1, actopt), b'%s\tLabelString\t\"Optional Layout Argument %d\"' % (space1, actopt),
'%sEndArgument' % (space1)] b'%sEndArgument' % (space1)]
actopt += 1 actopt += 1
# Now the mandatories # Now the mandatories
if reqs > 0: if reqs > 0:
actopt = opts + 1 actopt = opts + 1
while actopt < (opts + reqs + 1): while actopt < (opts + reqs + 1):
newarg += [ '%sArgument %d' % (space1, actopt), newarg += [ b'%sArgument %d' % (space1, actopt),
'%s\tLabelString\t"Required Layout Argument %d"' % (space1, actopt - opts), b'%s\tLabelString\t"Required Layout Argument %d"' % (space1, actopt - opts),
'%s\tMandatory\t1' % (space1), b'%s\tMandatory\t1' % (space1),
'%sEndArgument' % (space1)] b'%sEndArgument' % (space1)]
actopt += 1 actopt += 1
# Since we replace the "End" line, re-add this line # Since we replace the "End" line, re-add this line
if len(newarg) > 1: if len(newarg) > 1:
newarg += ['End'] newarg += [b'End']
lines[i:i+1] = newarg lines[i:i+1] = newarg
i += len(newarg) i += len(newarg)
# Reset the counters # Reset the counters
@ -706,8 +707,8 @@ def convert(lines, end_format):
if format == 36: if format == 36:
match = re_CiteFormat.match(lines[i]); match = re_CiteFormat.match(lines[i]);
if match and match.group(4) == "": if match and match.group(4) == b"":
lines[i] = match.group(0) + " default" lines[i] = match.group(0) + b" default"
i += 1 i += 1
continue continue
@ -727,13 +728,13 @@ def convert(lines, end_format):
style = match.group(2) style = match.group(2)
if flexstyles.count(style): if flexstyles.count(style):
lines[i] = match.group(1) + "\"Flex:" + style + "\"" lines[i] = match.group(1) + b"\"Flex:" + style + b"\""
i += 1 i += 1
continue continue
name = match.group(1) name = match.group(1)
names = name.split(":", 1) names = name.split(b":", 1)
if len(names) > 1 and names[0] == "Flex": if len(names) > 1 and names[0] == b"Flex":
i += 1 i += 1
continue continue
@ -747,7 +748,7 @@ def convert(lines, end_format):
if isflex: if isflex:
flexstyles.append(name) flexstyles.append(name)
lines[i] = "InsetLayout \"Flex:" + name + "\"" lines[i] = b"InsetLayout \"Flex:" + name + b"\""
i += 1 i += 1
continue continue
@ -755,7 +756,7 @@ def convert(lines, end_format):
if format == 33: if format == 33:
m = re_Fill.match(lines[i]) m = re_Fill.match(lines[i])
if m: if m:
lines[i] = "" lines[i] = b""
i += 1 i += 1
continue continue
@ -764,11 +765,11 @@ def convert(lines, end_format):
if match: if match:
space = match.group(1) space = match.group(1)
val = match.group(2) val = match.group(2)
lines[i] = space + "UsesFloatPkg " + val lines[i] = space + b"UsesFloatPkg " + val
newval = 'true' newval = b'true'
if val == '1' or val.lower() == 'true': if val == b'1' or val.lower() == b'true':
newval = 'false' newval = b'false'
lines.insert(i, space + "IsPredefined " + newval) lines.insert(i, space + b"IsPredefined " + newval)
i += 1 i += 1
i += 1 i += 1
continue continue
@ -781,19 +782,19 @@ def convert(lines, end_format):
if format == 28: if format == 28:
match = re_InsetLayout.match(lines[i]) match = re_InsetLayout.match(lines[i])
if match: if match:
lines[i] = "InsetLayout Flex:" + match.group(1) lines[i] = b"InsetLayout Flex:" + match.group(1)
else: else:
match = re_QInsetLayout.match(lines[i]) match = re_QInsetLayout.match(lines[i])
if match: if match:
lines[i] = "InsetLayout \"Flex:" + match.group(1) + "\"" lines[i] = b"InsetLayout \"Flex:" + match.group(1) + b"\""
else: else:
match = re_InsetLayout_CopyStyle.match(lines[i]) match = re_InsetLayout_CopyStyle.match(lines[i])
if match: if match:
lines[i] = "\tCopyStyle Flex:" + match.group(1) lines[i] = b"\tCopyStyle Flex:" + match.group(1)
else: else:
match = re_QInsetLayout_CopyStyle.match(lines[i]) match = re_QInsetLayout_CopyStyle.match(lines[i])
if match: if match:
lines[i] = "\tCopyStyle \"Flex:" + match.group(1) + "\"" lines[i] = b"\tCopyStyle \"Flex:" + match.group(1) + b"\""
i += 1 i += 1
continue continue
@ -811,12 +812,12 @@ def convert(lines, end_format):
# (i) Convert Builtin to NeedsFloatPkg # (i) Convert Builtin to NeedsFloatPkg
# (ii) Write ListCommand lines for the builtin floats table and figure # (ii) Write ListCommand lines for the builtin floats table and figure
builtin = False builtin = False
cmd = "" cmd = b""
while True and i < len(lines): while True and i < len(lines):
m1 = re_End.match(lines[i]) m1 = re_End.match(lines[i])
if m1: if m1:
if builtin and cmd: if builtin and cmd:
line = " ListCommand " + cmd line = b" ListCommand " + cmd
lines.insert(i, line) lines.insert(i, line)
i += 1 i += 1
break break
@ -825,20 +826,20 @@ def convert(lines, end_format):
builtin = True builtin = True
ws1 = m2.group(1) ws1 = m2.group(1)
arg = m2.group(2) arg = m2.group(2)
newarg = "" newarg = b""
if re_True.match(arg): if re_True.match(arg):
newarg = "false" newarg = b"false"
else: else:
newarg = "true" newarg = b"true"
lines[i] = ws1 + "NeedsFloatPkg " + newarg lines[i] = ws1 + b"NeedsFloatPkg " + newarg
m3 = re_Type.match(lines[i]) m3 = re_Type.match(lines[i])
if m3: if m3:
fltype = m3.group(1) fltype = m3.group(1)
fltype = fltype.lower() fltype = fltype.lower()
if fltype == "table": if fltype == b"table":
cmd = "listoftables" cmd = b"listoftables"
elif fltype == "figure": elif fltype == b"figure":
cmd = "listoffigures" cmd = b"listoffigures"
# else unknown, which is why we're doing this # else unknown, which is why we're doing this
i += 1 i += 1
continue continue
@ -852,13 +853,13 @@ def convert(lines, end_format):
if format == 13: if format == 13:
match = re_I18nPreamble.match(lines[i]) match = re_I18nPreamble.match(lines[i])
if match: if match:
lines[i] = match.group(1) + "BabelPreamble" lines[i] = match.group(1) + b"BabelPreamble"
i += 1 i += 1
match = re_EndI18nPreamble.match(lines[i]) match = re_EndI18nPreamble.match(lines[i])
while i < len(lines) and not match: while i < len(lines) and not match:
i += 1 i += 1
match = re_EndI18nPreamble.match(lines[i]) match = re_EndI18nPreamble.match(lines[i])
lines[i] = match.group(1) + "EndBabelPreamble" lines[i] = match.group(1) + b"EndBabelPreamble"
i += 1 i += 1
continue continue
@ -871,7 +872,7 @@ def convert(lines, end_format):
match = re_UseMod.match(lines[i]) match = re_UseMod.match(lines[i])
if match: if match:
module = match.group(1) module = match.group(1)
lines[i] = "DefaultModule " + module lines[i] = b"DefaultModule " + module
i += 1 i += 1
continue continue
@ -885,7 +886,7 @@ def convert(lines, end_format):
if namem: if namem:
name = namem.group(1) name = namem.group(1)
lines.pop(i) lines.pop(i)
lines[counterline] = "Counter %s" % name lines[counterline] = b"Counter %s" % name
# we don't need to increment i # we don't need to increment i
continue continue
endem = re_End.match(lines[i]) endem = re_End.match(lines[i])
@ -901,20 +902,20 @@ def convert(lines, end_format):
# add corresponding UseModule tags to the layout. # add corresponding UseModule tags to the layout.
match = re_AMSMaths.match(lines[i]) match = re_AMSMaths.match(lines[i])
if match: if match:
addstring("theorems-ams", usemodules) addstring(b"theorems-ams", usemodules)
addstring("theorems-ams-extended", usemodules) addstring(b"theorems-ams-extended", usemodules)
addstring("theorems-sec", usemodules) addstring(b"theorems-sec", usemodules)
lines.pop(i) lines.pop(i)
continue continue
match = re_AMSMathsPlain.match(lines[i]) match = re_AMSMathsPlain.match(lines[i])
if match: if match:
addstring("theorems-starred", usemodules) addstring(b"theorems-starred", usemodules)
lines.pop(i) lines.pop(i)
continue continue
match = re_AMSMathsSeq.match(lines[i]) match = re_AMSMathsSeq.match(lines[i])
if match: if match:
addstring("theorems-ams", usemodules) addstring(b"theorems-ams", usemodules)
addstring("theorems-ams-extended", usemodules) addstring(b"theorems-ams-extended", usemodules)
lines.pop(i) lines.pop(i)
continue continue
i += 1 i += 1
@ -929,12 +930,12 @@ def convert(lines, end_format):
# Handle conversion to long CharStyle names # Handle conversion to long CharStyle names
match = re_CharStyle.match(lines[i]) match = re_CharStyle.match(lines[i])
if match: if match:
lines[i] = "InsetLayout CharStyle:%s" % (match.group(3)) lines[i] = b"InsetLayout CharStyle:%s" % (match.group(3))
i += 1 i += 1
lines.insert(i, "\tLyXType charstyle") lines.insert(i, b"\tLyXType charstyle")
i += 1 i += 1
lines.insert(i, "") lines.insert(i, b"")
lines[i] = "\tLabelString %s" % (match.group(3)) lines[i] = b"\tLabelString %s" % (match.group(3))
i += 1 i += 1
continue continue
@ -944,7 +945,7 @@ def convert(lines, end_format):
# x is either 0 or 1 # x is either 0 or 1
match = re_Provides.match(lines[i]) match = re_Provides.match(lines[i])
if match: if match:
lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(), lines[i] = b"%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
match.group(3), match.group(4)) match.group(3), match.group(4))
i += 1 i += 1
continue continue
@ -955,8 +956,8 @@ def convert(lines, end_format):
# delete caption styles # delete caption styles
match = re_Style.match(lines[i]) match = re_Style.match(lines[i])
if match: if match:
style = string.lower(match.group(4)) style = match.group(4).lower()
if style == "caption": if style == b"caption":
del lines[i] del lines[i]
while i < len(lines) and not re_End.match(lines[i]): while i < len(lines) and not re_End.match(lines[i]):
caption.append(lines[i]) caption.append(lines[i])
@ -970,8 +971,8 @@ def convert(lines, end_format):
# delete undefinition of caption styles # delete undefinition of caption styles
match = re_NoStyle.match(lines[i]) match = re_NoStyle.match(lines[i])
if match: if match:
style = string.lower(match.group(4)) style = match.group(4).lower()
if style == "caption": if style == b"caption":
del lines[i] del lines[i]
continue continue
@ -980,28 +981,28 @@ def convert(lines, end_format):
# since the second one will overwrite the first one. # since the second one will overwrite the first one.
match = re_CopyStyle.match(lines[i]) match = re_CopyStyle.match(lines[i])
if match: if match:
style = string.lower(match.group(4)) style = match.group(4).lower()
if style == "caption": if style == b"caption":
if len(caption) > 0: if len(caption) > 0:
lines[i:i+1] = caption lines[i:i+1] = caption
else: else:
# FIXME: This style comes from an include file, we # FIXME: This style comes from an include file, we
# should replace the real style and not this default. # should replace the real style and not this default.
lines[i:i+1] = [' Margin First_Dynamic', lines[i:i+1] = [b' Margin First_Dynamic',
' LatexType Command', b' LatexType Command',
' LatexName caption', b' LatexName caption',
' NeedProtect 1', b' NeedProtect 1',
' LabelSep xx', b' LabelSep xx',
' ParSkip 0.4', b' ParSkip 0.4',
' TopSep 0.5', b' TopSep 0.5',
' Align Center', b' Align Center',
' AlignPossible Center', b' AlignPossible Center',
' LabelType Sensitive', b' LabelType Sensitive',
' LabelString "Senseless!"', b' LabelString "Senseless!"',
' OptionalArgs 1', b' OptionalArgs 1',
' LabelFont', b' LabelFont',
' Series Bold', b' Series Bold',
' EndFont'] b' EndFont']
i += 1 i += 1
continue continue
@ -1009,24 +1010,24 @@ def convert(lines, end_format):
# Delete MaxCounter and remember the value of it # Delete MaxCounter and remember the value of it
match = re_MaxCounter.match(lines[i]) match = re_MaxCounter.match(lines[i])
if match: if match:
level = match.group(4) level = match.group(4).lower()
if string.lower(level) == "counter_chapter": if level == b"counter_chapter":
maxcounter = 0 maxcounter = 0
elif string.lower(level) == "counter_section": elif level == b"counter_section":
maxcounter = 1 maxcounter = 1
elif string.lower(level) == "counter_subsection": elif level == b"counter_subsection":
maxcounter = 2 maxcounter = 2
elif string.lower(level) == "counter_subsubsection": elif level == b"counter_subsubsection":
maxcounter = 3 maxcounter = 3
elif string.lower(level) == "counter_paragraph": elif level == b"counter_paragraph":
maxcounter = 4 maxcounter = 4
elif string.lower(level) == "counter_subparagraph": elif level == b"counter_subparagraph":
maxcounter = 5 maxcounter = 5
elif string.lower(level) == "counter_enumi": elif level == b"counter_enumi":
maxcounter = 6 maxcounter = 6
elif string.lower(level) == "counter_enumii": elif level == b"counter_enumii":
maxcounter = 7 maxcounter = 7
elif string.lower(level) == "counter_enumiii": elif level == b"counter_enumiii":
maxcounter = 8 maxcounter = 8
del lines[i] del lines[i]
continue continue
@ -1048,9 +1049,9 @@ def convert(lines, end_format):
# Remember the line for adding the LabelCounter later. # Remember the line for adding the LabelCounter later.
# We can't do it here because it could shift latextype_line etc. # We can't do it here because it could shift latextype_line etc.
labeltype_line = i labeltype_line = i
if string.lower(label[:8]) == "counter_": if label[:8].lower() == b"counter_":
counter = string.lower(label[8:]) counter = label[8:].lower()
lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i]) lines[i] = re_LabelType.sub(b'\\1\\2\\3Counter', lines[i])
# Remember the LabelString line # Remember the LabelString line
match = re_LabelString.match(lines[i]) match = re_LabelString.match(lines[i])
@ -1067,48 +1068,48 @@ def convert(lines, end_format):
# Remember the LatexType line # Remember the LatexType line
match = re_LatexType.match(lines[i]) match = re_LatexType.match(lines[i])
if match: if match:
latextype = string.lower(match.group(4)) latextype = match.group(4).lower()
latextype_line = i latextype_line = i
# Remember the TocLevel line # Remember the TocLevel line
match = re_TocLevel.match(lines[i]) match = re_TocLevel.match(lines[i])
if match: if match:
toclevel = string.lower(match.group(4)) toclevel = match.group(4).lower()
# Reset variables at the beginning of a style definition # 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 = string.lower(match.group(4)) style = match.group(4).lower()
counter = "" counter = b""
toclevel = "" toclevel = b""
label = "" label = b""
space1 = "" space1 = b""
labelstring = "" labelstring = b""
labelstringappendix = "" labelstringappendix = b""
labelstring_line = -1 labelstring_line = -1
labelstringappendix_line = -1 labelstringappendix_line = -1
labeltype_line = -1 labeltype_line = -1
latextype = "" latextype = b""
latextype_line = -1 latextype_line = -1
if re_End.match(lines[i]): if re_End.match(lines[i]):
# Add a line "LatexType Bib_Environment" if LabelType is Bibliography # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
# (or change the existing LatexType) # (or change the existing LatexType)
if string.lower(label) == "bibliography": if label.lower() == b"bibliography":
if (latextype_line < 0): if (latextype_line < 0):
lines.insert(i, "%sLatexType Bib_Environment" % space1) lines.insert(i, b"%sLatexType Bib_Environment" % space1)
i += 1 i += 1
else: else:
lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line]) lines[latextype_line] = re_LatexType.sub(b'\\1\\2\\3Bib_Environment', lines[latextype_line])
# Change "LabelType Static" to "LabelType Itemize" for itemize environments # Change "LabelType Static" to "LabelType Itemize" for itemize environments
if latextype == "item_environment" and string.lower(label) == "static": if latextype == b"item_environment" and label.lower() == b"static":
lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line]) lines[labeltype_line] = re_LabelType.sub(b'\\1\\2\\3Itemize', lines[labeltype_line])
# Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
if latextype == "item_environment" and string.lower(label) == "counter_enumi": if latextype == b"item_environment" and label.lower() == b"counter_enumi":
lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line]) lines[labeltype_line] = re_LabelType.sub(b'\\1\\2\\3Enumerate', lines[labeltype_line])
# Don't add the LabelCounter line later # Don't add the LabelCounter line later
counter = "" counter = ""
@ -1123,43 +1124,43 @@ def convert(lines, end_format):
# if this style has a counter. Ditto for LabelStringAppendix. # if this style has a counter. Ditto for LabelStringAppendix.
# This emulates the hardcoded article style numbering of 1.3 # This emulates the hardcoded article style numbering of 1.3
# #
if counter != "": if counter != b"":
if style in counters: if style in counters:
if labelstring_line < 0: if labelstring_line < 0:
lines.insert(i, '%sLabelString "%s"' % (space1, counters[style])) lines.insert(i, b'%sLabelString "%s"' % (space1, counters[style]))
i += 1 i += 1
else: else:
new_labelstring = concatenate_label(labelstring, counters[style]) new_labelstring = concatenate_label(labelstring, counters[style])
lines[labelstring_line] = re_LabelString.sub( lines[labelstring_line] = re_LabelString.sub(
r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"), b'\\1\\2\\3%s' % new_labelstring.replace(b"\\", b"\\\\"),
lines[labelstring_line]) lines[labelstring_line])
if style in appendixcounters: if style in appendixcounters:
if labelstringappendix_line < 0: if labelstringappendix_line < 0:
lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style])) lines.insert(i, b'%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
i += 1 i += 1
else: else:
new_labelstring = concatenate_label(labelstring, appendixcounters[style]) new_labelstring = concatenate_label(labelstring, appendixcounters[style])
lines[labelstringappendix_line] = re_LabelStringAppendix.sub( lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"), b'\\1\\2\\3%s' % new_labelstring.replace(b"\\", b"\\\\"),
lines[labelstringappendix_line]) lines[labelstringappendix_line])
# Now we can safely add the LabelCounter line # Now we can safely add the LabelCounter line
lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter)) lines.insert(labeltype_line + 1, b"%sLabelCounter %s" % (space1, counter))
i += 1 i += 1
# Add the TocLevel setting for sectioning styles # Add the TocLevel setting for sectioning styles
if toclevel == "" and style in toclevels and maxcounter <= toclevels[style]: if toclevel == b"" and style in toclevels and maxcounter <= toclevels[style]:
lines.insert(i, '%s\tTocLevel %d' % (space1, toclevels[style])) lines.insert(i, b'%s\tTocLevel %d' % (space1, toclevels[style]))
i += 1 i += 1
i += 1 i += 1
if only_comment: if only_comment:
lines.insert(i, "Format 2") lines.insert(i, b"Format 2")
if usemodules: if usemodules:
i = formatline + 1 i = formatline + 1
for mod in usemodules: for mod in usemodules:
lines.insert(i, "UseModule " + mod) lines.insert(i, b"UseModule " + mod)
i += 1 i += 1
return format + 1 return format + 1