X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Fscripts%2Flayout2layout.py;h=d932700bb4771025c286e3497067209e4595085f;hb=ed233663008608e5e32a3c675ade790ea63f0a24;hp=bbecf80cea7084e0c9c81b3875b2771ae252e44f;hpb=dc18fcea6fe2b0ad244228165fe59a7e9c749536;p=lyx.git diff --git a/lib/scripts/layout2layout.py b/lib/scripts/layout2layout.py index bbecf80cea..d932700bb4 100644 --- a/lib/scripts/layout2layout.py +++ b/lib/scripts/layout2layout.py @@ -1,4 +1,3 @@ -#! /usr/bin/env python # -*- coding: utf-8 -*- # file layout2layout.py @@ -11,8 +10,9 @@ # This script will update a .layout file to current format +# The latest layout format is also defined in src/TextClass.cpp +currentFormat = 63 -import os, re, string, sys # Incremented to format 4, 6 April 2007, lasgouttes # Introduction of generic "Provides" declaration @@ -119,19 +119,122 @@ import os, re, string, sys # Incremented to format 34, 28 March 2011 by rgh # Remove obsolete Fill_(Top|Bottom) tags +# Incremented to format 35, 28 March 2011 by rgh +# Try to add "Flex:" to any flex insets that don't have it. + +# Incremented to format 36, 7 December 2011, by rgh +# Added HTMLStyles and AddToHTMLStyles tags. + +# Incremented to format 37, 29 February 2012 by jrioux +# Implement the citation engine machinery in layouts. +# Change CiteFormat to CiteFormat (default|authoryear|numerical). + +# Incremented to format 38, 08 April 2012 by gb +# Introduce LangPreamble and BabelPreamble for InsetLayout. + +# Incremented to format 39, 15 April 2012 by sanda +# Introduce styling of branches via "InsetLayout Branch:". + +# Incremented to format 40, 10 October 2012 by rgh +# Re-do layout names for layout categories + +# Incremented to format 41, 20 November 2012 by spitz +# New Argument syntax + +# Incremented to format 42, 22 December 2012 by spitz +# New Style tag "ItemCommand" + +# Incremented to format 43, 30 December 2012 by spitz +# Extended InsetCaption format + +# Incremented to format 44, 9 February 2013 by rgh +# Remove COUNTER label style; rename as STATIC +# Rename TOP_ENVIRONMENT to ABOVE and CENTERED_TOP_ENVIRONMENT to CENTERED + +# Incremented to format 45, 12 February 2013 by rgh +# New Tag "NoInsetLayout" + +# Incremented to format 46, 15 May 2013 by gb +# New Tag "ForceLocal" + +# Incremented to format 47, 23 May 2013 by rgh +# Add PackageOptions tag + +# Incremented to format 48, 31 May 2013 by rgh +# Add InitialValue tag for counters + +# Incremented to format 49, 10 Feb 2014 by gb +# Change default of "ResetsFont" tag to false + +# Incremented to format 50, 9 May 2014 by forenr +# Removal of "Separator" layouts + +# Incremented to format 51, 29 May 2014 by spitz +# New Style tag "ToggleIndent" + +# Incremented to format 52, 1 December 2014 by spitz +# New InsetLayout tag "ForceOwnlines" + +# Incremented to format 53, 7 December 2014 by spitz +# New InsetLayout tag "ObsoletedBy" + +# Incremented to format 54, 11 Jan 2014 by gb +# New InsetLayout tag "FixedWidthPreambleEncoding" + +# Incremented to format 55, 20 April 2015 by spitz +# New InsetLayout and Layout tags "PassThruChars" + +# Incremented to format 56, 20 May 2015 by spitz +# New Float tags "AllowedPlacement", "AllowsWide", "AllowsSideways" + +# Incremented to format 57, 30 May 2015 by spitz +# New Layout tag "ParagraphGroup" + +# Incremented to format 58, 5 December 2015, by rgh +# New Layout tag "ProvideStyle" +# Change "IfStyle" to "ModifyStyle" + +# Incremented to format 59, 22 November 2015 by gm +# New Tag "OutlinerName" +# New Layout tags "AddToToc", "IsTocCaption" +# New Layout argument tag "IsTocCaption" + +# Incremented to format 60, 25 March 2016 by lasgouttes +# Rename caption subtype LongTableNoNumber to Unnumbered + +# Incremented to format 61, 14 October 2016 by spitz +# New Layout tags "ResumeCounter", "StepMasterCounter" + +# Incremented to format 62, 21 October 2016 by spitz +# New Layout argument tag "PassThru" + +# Incremented to format 63, 7 January 2017 by spitz +# - New textclass tags CiteFramework, MaxCiteNames (for cite engines) +# - Extended InsetCite syntax. + # Do not forget to document format change in Customization # Manual (section "Declaring a new text class"). # You might also want to consider running the -# development/tools/updatelayouts.sh script to update all +# development/tools/updatelayouts.py script to update all # layout files to the new format. -currentFormat = 34 - -def usage(prog_name): - return ("Usage: %s inputfile outputfile\n" % prog_name + - "or %s outputfile" % prog_name) +import os, re, string, sys +import argparse + +# Provide support for both python 2 and 3 +# (copied from lyx2lyx) +PY2 = sys.version_info[0] == 2 +if PY2: + # argparse returns strings in the commandline encoding, we need to convert. + # sys.getdefaultencoding() would not always be correct, see + # http://legacy.python.org/dev/peps/pep-0383/ + def cmd_arg(arg): + return arg.decode(sys.getfilesystemencoding()) +else: + cmd_arg = str +# End of code to support for both python 2 and 3 def error(message): @@ -149,7 +252,7 @@ def trim_bom(line): def read(source): " Read input file and strip lineendings." - lines = source.read().splitlines() + lines = source.read().splitlines() or [''] lines[0] = trim_bom(lines[0]) return lines @@ -175,7 +278,7 @@ def addstring(s, l): l.append(s) -def convert(lines): +def convert(lines, end_format): " Convert to new format." re_Comment = re.compile(r'^(\s*)#') re_Counter = re.compile(r'\s*Counter\s*', re.IGNORECASE) @@ -195,11 +298,13 @@ def convert(lines): 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_IfStyle = re.compile(r'^(\s*)IfStyle(\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) re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE) + re_CiteFormat = re.compile(r'^(\s*)(CiteFormat)(?:(\s*)()|(\s+)(default|authoryear|numerical))', re.IGNORECASE) re_AMSMaths = re.compile(r'^\s*Input ams(?:math|def)s.inc\s*') re_AMSMathsPlain = re.compile(r'^\s*Input amsmaths-plain.inc\s*') re_AMSMathsSeq = re.compile(r'^\s*Input amsmaths-seq.inc\s*') @@ -211,12 +316,35 @@ def convert(lines): re_Builtin = re.compile(r'^(\s*)LaTeXBuiltin\s+(\w*)', re.IGNORECASE) re_True = re.compile(r'^\s*(?:true|1)\s*$', re.IGNORECASE) re_InsetLayout = re.compile(r'^\s*InsetLayout\s+(?:Custom|CharStyle|Element):(\S+)\s*$', re.IGNORECASE) + re_ResetsFont = re.compile(r'^(\s*)ResetsFont(\s+)(\S+)$', re.IGNORECASE) # with quotes re_QInsetLayout = re.compile(r'^\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_QInsetLayout_CopyStyle = re.compile(r'^\s*CopyStyle\s+"(?:Custom|CharStyle|Element):([^"]+)"\s*$', re.IGNORECASE) re_NeedsFloatPkg = re.compile(r'^(\s*)NeedsFloatPkg\s+(\w+)\s*$', re.IGNORECASE) re_Fill = re.compile(r'^\s*Fill_(?:Top|Bottom).*$', re.IGNORECASE) + re_InsetLayout2 = re.compile(r'^\s*InsetLayout\s+(\S+)\s*$', re.IGNORECASE) + # with quotes + re_QInsetLayout2 = re.compile(r'^\s*InsetLayout\s+"([^"]+)"\s*$', re.IGNORECASE) + re_IsFlex = re.compile(r'\s*LyXType.*$', re.IGNORECASE) + re_CopyStyle2 = re.compile(r'(\s*CopyStyle\s+)"?([^"]+)"?\s*$') + re_Separator = re.compile(r'^(?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE) + # for categories + re_Declaration = re.compile(r'^#\s*\\Declare\w+Class.*$') + re_ExtractCategory = re.compile(r'^(#\s*\\Declare\w+Class(?:\[[^]]*?\])?){([^(]+?)\s+\(([^)]+?)\)\s*}\s*$') + ConvDict = {"article": "Articles", "book" : "Books", "letter" : "Letters", "report": "Reports", \ + "presentation" : "Presentations", "curriculum vitae" : "Curricula Vitae", "handout" : "Handouts"} + # Arguments + re_OptArgs = re.compile(r'^(\s*)OptionalArgs(\s+)(\d+)\D*$', re.IGNORECASE) + re_ReqArgs = re.compile(r'^(\s*)RequiredArgs(\s+)(\d+)\D*$', re.IGNORECASE) + + # various changes associated with changing how chapters are handled + re_LabelTypeIsCounter = re.compile(r'^(\s*)LabelType(\s*)Counter\s*$', re.IGNORECASE) + re_TopEnvironment = re.compile(r'^(\s*)LabelType(\s+)Top_Environment\s*$', re.IGNORECASE) + re_CenteredEnvironment = re.compile(r'^(\s*)LabelType(\s+)Centered_Top_Environment\s*$', re.IGNORECASE) + re_ChapterStyle = re.compile(r'^\s*Style\s+Chapter\s*$', re.IGNORECASE) + re_InsetLayout_CaptionLTNN = re.compile(r'^(\s*InsetLayout\s+)(Caption:LongTableNonumber)', re.IGNORECASE) + # counters for sectioning styles (hardcoded in 1.3) counters = {"part" : "\\Roman{part}", @@ -236,7 +364,7 @@ def convert(lines): "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"} # Value of TocLevel for sectioning styles - toclevels = {"part" : 0, + toclevels = {"part" : -1, "chapter" : 0, "section" : 1, "subsection" : 2, @@ -262,12 +390,34 @@ def convert(lines): format = 1 formatline = 0 usemodules = [] + flexstyles = [] + opts = 0 + reqs = 0 + inchapter = False + isflexlayout = False # only used for 48 -> 49 + # Whether a style is inherited (works only for CopyStyle currently, + # not for true inherited styles, see bug 8920 + inherited = False # only used for 48 -> 49 + resetsfont_found = False # only used for 48 -> 49 while i < len(lines): # Skip comments and empty lines - if re_Comment.match(lines[i]) or re_Empty.match(lines[i]): - i += 1 - continue + if (re_Comment.match(lines[i]) or re_Empty.match(lines[i])): + # We need to deal with this conversion here, because it happens + # inside the initial comment block. + if only_comment and format == 39: + match = re_ExtractCategory.match(lines[i]) + if match: + lpre = match.group(1) + lcat = match.group(2) + lnam = match.group(3) + if lcat in ConvDict: + lcat = ConvDict[lcat] + lines[i] = lpre + "{" + lnam + "}" + lines.insert(i+1, "# \\DeclareCategory{" + lcat + "}") + i += 1 + i += 1 + continue # insert file format if not already there if (only_comment): @@ -275,14 +425,14 @@ def convert(lines): if match: formatline = i format = int(match.group(4)) - if format > 1 and format < currentFormat: + if format > 1 and format < end_format: lines[i] = "Format %d" % (format + 1) only_comment = 0 - elif format == currentFormat: + elif format == end_format: # nothing to do return format else: - error('Cannot convert file format %s' % format) + error('Cannot convert file format %s to %s' % (format, end_format)) else: lines.insert(i, "Format 2") only_comment = 0 @@ -305,6 +455,303 @@ def convert(lines): i += 1 continue + if format >= 60 and format <= 62: + # nothing to do. + i += 1 + continue + + if format == 59: + match = re_InsetLayout_CaptionLTNN.match(lines[i]) + if not match: + i += 1 + continue + # '^(\s*InsetLayout\s+)(Caption:LongTableNonumber)' + lead = match.group(1) + lines[i] = lead + "Caption:Unnumbered" + i += 1 + continue + + if format == 58: + # nothing to do. + i += 1 + continue + + if format == 57: + match = re_IfStyle.match(lines[i]) + if not match: + i += 1 + continue + # r'^(\s*)IfStyle(\s+\S+) + lead = match.group(1) + trail = match.group(2) + lines[i] = lead + "ModifyStyle" + trail + i += 1 + continue + + if format >= 50 and format <= 56: + # nothing to do. + i += 1 + continue + + if format == 49: + separator = [] + + # delete separator styles + match = re_Style.match(lines[i]) + if match: + style = string.lower(match.group(4)) + if re_Separator.match(style): + del lines[i] + while i < len(lines) and not re_End.match(lines[i]): + separator.append(lines[i]) + del lines[i] + if i == len(lines): + error('Incomplete separator style.') + else: + del lines[i] + continue + + # delete undefinition of separator styles + match = re_NoStyle.match(lines[i]) + if match: + style = string.lower(match.group(4)) + if re_Separator.match(style): + 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 re_Separator.match(style): + if len(separator) > 0: + lines[i:i+1] = separator + else: + # FIXME: If this style was redefined in an include file, + # we should replace the real style and not this default. + lines[i:i+1] = [' Category MainText', + ' KeepEmpty 1', + ' Margin Dynamic', + ' LatexType Paragraph', + ' LatexName dummy', + ' ParIndent MM', + ' Align Block', + ' LabelType Static', + ' LabelString "--- Separate Environment ---"', + ' LabelFont', + ' Family Roman', + ' Series Medium', + ' Size Normal', + ' Color Blue', + ' EndFont', + ' HTMLLabel NONE'] + i += 1 + continue + + if format == 48: + # The default of ResetsFont in LyX changed from true to false, + # because it is now used for all InsetLayouts, not only flex ones. + # Therefore we need to set it to true for all flex insets which do + # do not already have a ResetsFont. + match = re_InsetLayout2.match(lines[i]) + if not match: + i += 1 + continue + + name = string.lower(match.group(1)) + if name != "flex" and name != "\"flex\"" and name[0:5] != "flex:" and name [0:6] != "\"flex:": + i += 1 + continue + + resetsfont_found = False + inherited = False + notdone = True + while i < len(lines): + match = re_ResetsFont.match(lines[i]) + if match: + resetsfont_found = True + else: + match = re_CopyStyle.match(lines[i]) + if match: + inherited = True + else: + match = re_End.match(lines[i]) + if match: + break + i += 1 + if not resetsfont_found and not inherited: + lines.insert(i, "\tResetsFont true") + + continue + + if format >= 44 and format <= 47: + # nothing to do. + i += 1 + continue + + if format == 43: + match = re_LabelTypeIsCounter.match(lines[i]) + if match: + if inchapter: + lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above" + else: + lines[i] = match.group(1) + "LabelType" + match.group(2) + "Static" + + match = re_TopEnvironment.match(lines[i]) + if match: + lines[i] = match.group(1) + "LabelType" + match.group(2) + "Above" + + match = re_CenteredEnvironment.match(lines[i]) + if match: + lines[i] = match.group(1) + "LabelType" + match.group(2) + "Centered" + + if inchapter: + match = re_Style.match(lines[i]) + if match: + inchapter = False + else: + match = re_ChapterStyle.match(lines[i]) + if match: + inchapter = True + + i += 1 + continue + + if format == 42: + if lines[i] == "InsetLayout Caption": + lines[i] = "InsetLayout Caption:Standard" + i += 1 + continue + + if format == 41: + # nothing to do. + i += 1 + continue + + if format == 40: + # reset counters on Style beginning + match = re_Style.match(lines[i]) + if match: + opts = 0 + reqs = 0 + i += 1 + continue + match = re_OptArgs.match(lines[i]) + if match: + # Save number of optional arguments + space1 = match.group(1) + opts = int(match.group(3)) + # OptionalArgs 0 > ResetArgs 1 + if opts == 0: + lines[i] = space1 + "ResetArgs\t1" + i += 1 + else: + del lines[i] + continue + match = re_ReqArgs.match(lines[i]) + if match: + # Save number of required arguments + space1 = match.group(1) + reqs = int(match.group(3)) + del lines[i] + continue + # Insert the required number of arguments at the end of the style definition + match = re_End.match(lines[i]) + if match: + newarg = [''] + # First the optionals (this is the required order pre 2.1) + if opts > 0: + if opts == 1: + newarg = [ '%sArgument 1' % (space1), + '%s\tLabelString\t\"Optional Layout Argument\"' % (space1), + '%sEndArgument' % (space1)] + elif opts > 1: + actopt = 1 + while actopt < (opts + 1): + newarg += [ '%sArgument %d' % (space1, actopt), + '%s\tLabelString\t\"Optional Layout Argument %d\"' % (space1, actopt), + '%sEndArgument' % (space1)] + actopt += 1 + # Now the mandatories + if reqs > 0: + actopt = opts + 1 + while actopt < (opts + reqs + 1): + newarg += [ '%sArgument %d' % (space1, actopt), + '%s\tLabelString\t"Required Layout Argument %d"' % (space1, actopt - opts), + '%s\tMandatory\t1' % (space1), + '%sEndArgument' % (space1)] + actopt += 1 + # Since we replace the "End" line, re-add this line + if len(newarg) > 1: + newarg += ['End'] + lines[i:i+1] = newarg + i += len(newarg) + # Reset the counters + opts = 0 + reqs = 0 + i += 1 + continue + + if format == 39: + # There is a conversion with format 40, but it is done within the + # initial comment block and so is above. + i += 1 + continue + + if format == 37 or format == 38: + i += 1 + continue + + if format == 36: + match = re_CiteFormat.match(lines[i]); + if match and match.group(4) == "": + lines[i] = match.group(0) + " default" + i += 1 + continue + + if format == 35: + i += 1 + continue + + if format == 34: + match = re_QInsetLayout2.match(lines[i]) + if not match: + match = re_InsetLayout2.match(lines[i]) + if not match: + match = re_CopyStyle2.match(lines[i]) + if not match: + i += 1 + continue + style = match.group(2) + + if flexstyles.count(style): + lines[i] = match.group(1) + "\"Flex:" + style + "\"" + i += 1 + continue + + name = match.group(1) + names = name.split(":", 1) + if len(names) > 1 and names[0] == "Flex": + i += 1 + continue + + isflex = False + for j in range(i + 1, len(lines)): + if re_IsFlex.match(lines[j]): + isflex = True + break + if re_End.match(lines[j]): + break + + if isflex: + flexstyles.append(name) + lines[i] = "InsetLayout \"Flex:" + name + "\"" + + i += 1 + continue + if format == 33: m = re_Fill.match(lines[i]) if m: @@ -707,6 +1154,8 @@ def convert(lines): i += 1 + if only_comment: + lines.insert(i, "Format 2") if usemodules: i = formatline + 1 for mod in usemodules: @@ -717,27 +1166,50 @@ def convert(lines): def main(argv): + args = {} + args["description"] = "Convert layout file to a newer format." + + parser = argparse.ArgumentParser(**args) + + parser.add_argument("-t", "--to", type=int, dest="format", + help=("destination layout format, default %i (latest)") % currentFormat) + parser.add_argument("input_file", nargs='?', type=cmd_arg, default=None, + help="input file (default stdin)") + parser.add_argument("output_file", nargs='?', type=cmd_arg, default=None, + help="output file (default stdout)") + + options = parser.parse_args() # Open files - if len(argv) == 1: + if options.input_file: + source = open(options.input_file, 'rb') + else: source = sys.stdin + + if options.output_file: + output = open(options.output_file, 'wb') + else: output = sys.stdout - elif len(argv) == 3: - source = open(argv[1], 'rb') - output = open(argv[2], 'wb') + + if options.format: + end_format = options.format else: - error(usage(argv[0])) + end_format = currentFormat + + if end_format > currentFormat: + error("Format %i does not exist" % end_format); # Do the real work lines = read(source) format = 1 - while (format < currentFormat): - format = convert(lines) + while (format < end_format): + format = convert(lines, end_format) write(output, lines) # Close files - if len(argv) == 3: + if options.input_file: source.close() + if options.output_file: output.close() return 0