]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/layout2layout.py
Remove profiling.py
[lyx.git] / lib / scripts / layout2layout.py
index cc609929ef7eab1e0f34193a7646a3570bb7eebc..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 = 98
+currentFormat = 105
 
 
 # Incremented to format 4, 6 April 2007, lasgouttes
@@ -331,6 +329,29 @@ currentFormat = 98
 # Incremented to format 98, 5 December 2022 by rikiheck
 # Add HTMLClass for InsetLayout
 
+# Incremented to format 99, 22 December 2022 by tcuvelier
+# Add DocBookGenerateTitle for Layout
+
+# 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").
 
@@ -342,19 +363,6 @@ currentFormat = 98
 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')
@@ -451,7 +459,6 @@ def convert(lines, end_format):
     re_CopyStyle2 = re.compile(b'(\\s*CopyStyle\\s+)"?([^"]+)"?\\s*$')
     re_Separator = re.compile(b'^(?:(-*)|(\\s*))(Separator|EndOfSlide)(?:(-*)|(\\s*))$', re.IGNORECASE)
     # for categories
-    re_Declaration = re.compile(b'^#\\s*\\Declare\\w+Class.*$')
     re_ExtractCategory = re.compile(b'^(#\\s*\\Declare\\w+Class(?:\\[[^]]*?\\])?){([^(]+?)\\s+\\(([^)]+?)\\)\\s*}\\s*$')
     ConvDict = {b"article": b"Articles", b"book": b"Books", b"letter": b"Letters", b"report": b"Reports",
                 b"presentation": b"Presentations", b"curriculum vitae": b"Curricula Vitae", b"handout": b"Handouts"}
@@ -470,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}",
@@ -547,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
@@ -578,7 +586,41 @@ def convert(lines, end_format):
                 i += 1
             continue
 
-        if 87 <= format <= 97:
+        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
@@ -1347,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:])
@@ -1357,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