]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/layout2layout.py
Add tableaux to outliner
[lyx.git] / lib / scripts / layout2layout.py
index 7de844b87fb66ed41f5aaf216c5f0caa4165c08c..d932700bb4771025c286e3497067209e4595085f 100644 (file)
@@ -10,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
@@ -201,6 +202,16 @@ import os, re, string, sys
 # 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").
 
@@ -208,12 +219,22 @@ import os, re, string, sys
 # development/tools/updatelayouts.py script to update all
 # layout files to the new format.
 
-currentFormat = 60
-
 
-def usage(prog_name):
-    return ("Usage: %s inputfile outputfile\n" % prog_name +
-            "or     %s <inputfile >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):
@@ -257,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)
@@ -404,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 to %s' % (format, currentFormat))
+                    error('Cannot convert file format %s to %s' % (format, end_format))
             else:
                 lines.insert(i, "Format 2")
                 only_comment = 0
@@ -434,6 +455,11 @@ 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:
@@ -1140,27 +1166,50 @@ def convert(lines):
 
 
 def main(argv):
+    args = {}
+    args["description"] = "Convert layout file <inputfile> 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