]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/layout2layout.py
Remove profiling.py
[lyx.git] / lib / scripts / layout2layout.py
index e718017cbeda8346cb138416578be10a8c94ae31..a610da47616e8246042e5ee638f1f075a40a0b64 100644 (file)
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
 # file layout2layout.py
 # This file is part of LyX, the document processor.
 # Licence details can be found in the file COPYING.
@@ -11,7 +9,7 @@
 # This script will update a .layout file to current format
 
 # The latest layout format is also defined in src/TextClass.cpp
-currentFormat = 100
+currentFormat = 105
 
 
 # Incremented to format 4, 6 April 2007, lasgouttes
@@ -337,6 +335,23 @@ currentFormat = 100
 # Incremented to format 100, 9 May 2023 by forenr
 # Add inset label color
 
+# Incremented to format 101, 22 July 2023 by lasgouttes
+# add InsetLayout tag InheritFont
+
+# Incremented to format 102, 25 July 2023 by spitz
+# add InsetLayout tags AllowedInInsets, EndAllowedInInsets,
+# AllowedInLayouts, EndAllowedInLayouts, AllowedOccurrences,
+# AllowedOccurrencesPerItem
+
+# Incremented to format 103, 27 July 2023 by rikiheck
+# Allow e.g. \roman{section} in PrettyFormat
+
+# Incremented to format 104, 28 July 2023 by rikiheck
+# RefFormat for counters and PrettyFormat for floats
+
+# Incremented to format 105, 2 June 2024 by spitz
+# ParskipFull and ParskipHalf class options
+
 # Do not forget to document format change in Customization
 # Manual (section "Declaring a new text class").
 
@@ -348,19 +363,6 @@ currentFormat = 100
 import os, re, 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):
     sys.stderr.write(message + '\n')
@@ -475,8 +477,9 @@ def convert(lines, end_format):
     re_trimLabelStringAppendix  = re.compile(b'^(\\s*LabelStringAppendix\\s+)"\\s*(.*?)\\s*"\\s*$')
     re_trimEndLabelString = re.compile(b'^(\\s*EndLabelString\\s+)"\\s*(.*?)\\s*"\\s*$')
     re_trimLabelCounter = re.compile(b'^(\\s*LabelCounter\\s+)"\\s*(.*?)\\s*"\\s*$')
-
-
+    # for format 100
+    re_InsetLayout100 = re.compile(b'^\\s*InsetLayout\\s+\\"?(Box|Float|Foot|Marginal|Listings|Note:Comment|Note:Greyedout|Tabular)(:\\S*)?\\"?\\s*$', re.IGNORECASE)
+    re_InheritFont = re.compile(b'^(\\s*)InheritFont(\\s+)(\\S+)$', re.IGNORECASE)
     # counters for sectioning styles (hardcoded in 1.3)
     counters = {b"part"          : b"\\Roman{part}",
                 b"chapter"       : b"\\arabic{chapter}",
@@ -552,15 +555,15 @@ def convert(lines, end_format):
             match = re_Format.match(lines[i])
             if match:
                 formatline = i
-                format = int(match.group(4))
-                if 1 < format < end_format:
-                    lines[i] = b"Format %d" % (format + 1)
+                format_ = int(match.group(4))
+                if 1 < format_ < end_format:
+                    lines[i] = b"Format %d" % (format_ + 1)
                     only_comment = 0
-                elif format == end_format:
+                elif format_ == end_format:
                     # nothing to do
-                    return format
+                    return format_
                 else:
-                    error('Cannot convert file format %s to %s' % (format, end_format))
+                    error(f'Cannot convert file format {format_} to {end_format}')
             else:
                 lines.insert(i, b"Format 2")
                 only_comment = 0
@@ -583,7 +586,41 @@ def convert(lines, end_format):
                 i += 1
             continue
 
-        if 87 <= format <= 100:
+        if 101 <= format <= 104:
+            # nothing to do.
+            i += 1
+            continue
+
+        if format == 100:
+            # InheritFont has been introduced and defaults to true. Some insets had
+            # an hardcoded inheritFont') method returning true. We removed them, so
+            # we want to introduce the correct tag if it is not already there.
+            match = re_InsetLayout100.match(lines[i])
+            if not match:
+                i += 1
+                continue
+
+            inheritfont_found = False
+            inherited = False
+            while i < len(lines):
+                match = re_InheritFont.match(lines[i])
+                if match:
+                    inheritfont_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 inheritfont_found and not inherited:
+                lines.insert(i, b"\tInheritFont false")
+
+            continue
+
+        if 87 <= format <= 99:
             # nothing to do.
             i += 1
             continue
@@ -1352,9 +1389,9 @@ def main(argv):
 
     parser.add_argument("-t", "--to", type=int, dest="format", default= currentFormat,
                         help=("destination layout format, default %i (latest)") % currentFormat)
-    parser.add_argument("input_file", nargs='?', type=cmd_arg, default=None,
+    parser.add_argument("input_file", nargs='?', type=str, default=None,
                         help="input file (default stdin)")
-    parser.add_argument("output_file", nargs='?', type=cmd_arg, default=None,
+    parser.add_argument("output_file", nargs='?', type=str, default=None,
                         help="output file (default stdout)")
 
     options = parser.parse_args(argv[1:])
@@ -1362,15 +1399,11 @@ def main(argv):
     # Open files
     if options.input_file:
         source = open(options.input_file, 'rb')
-    elif PY2:
-        source = sys.stdin
     else:
         source = sys.stdin.buffer
 
     if options.output_file:
         output = open(options.output_file, 'wb')
-    elif PY2:
-        output = sys.stdout
     else:
         output = sys.stdout.buffer