]> git.lyx.org Git - features.git/blobdiff - lib/scripts/layout2layout.py
Bump layout format
[features.git] / lib / scripts / layout2layout.py
index a9b5ba35bc7dc94ee0d38e7608e04d7b9cdb1656..113f99bf04fe1127b52e028e532a00d589e1f6d0 100644 (file)
@@ -138,6 +138,12 @@ import os, re, string, sys
 # 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"
+
 # Do not forget to document format change in Customization
 # Manual (section "Declaring a new text class").
 
@@ -145,7 +151,7 @@ import os, re, string, sys
 # development/tools/updatelayouts.sh script to update all
 # layout files to the new format.
 
-currentFormat = 40
+currentFormat = 42
 
 
 def usage(prog_name):
@@ -242,6 +248,15 @@ def convert(lines):
     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*$')
+    # 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)
+
 
     # counters for sectioning styles (hardcoded in 1.3)
     counters = {"part"          : "\\Roman{part}",
@@ -288,12 +303,27 @@ def convert(lines):
     formatline = 0
     usemodules = []
     flexstyles = []
+    opts = 0
+    reqs = 0
 
     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):
@@ -331,14 +361,84 @@ def convert(lines):
                 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:
-          # something more substantil will be inserted here shortly
-          i += 1
-          continue
+            # 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
+            i += 1
+            continue
 
         if format == 36:
             match = re_CiteFormat.match(lines[i]);
@@ -352,9 +452,9 @@ def convert(lines):
           continue
 
         if format == 34:
-          match = re_InsetLayout2.match(lines[i])
+          match = re_QInsetLayout2.match(lines[i])
           if not match:
-            match = re_QInsetLayout2.match(lines[i])
+            match = re_InsetLayout2.match(lines[i])
           if not match:
             match = re_CopyStyle2.match(lines[i])
             if not match: