]> git.lyx.org Git - features.git/commitdiff
Complete lyx2lyx for new "lineno" settings.
authorGünter Milde <milde@lyx.org>
Fri, 24 May 2019 11:49:32 +0000 (13:49 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 13:48:30 +0000 (15:48 +0200)
autotests/export/lyx2lyx/lyx_2_4_test.lyx
lib/lyx2lyx/lyx_2_4.py
lib/lyx2lyx/parser_tools.py

index a70770568272491da7e9b7120dc308b5ac289788..365ef097b42c5c78f5351b4cee25b8788da3dd2c 100644 (file)
@@ -1,5 +1,5 @@
 #LyX 2.4 created this file. For more info see https://www.lyx.org/
-\lyxformat 566
+\lyxformat 575
 \begin_document
 \begin_header
 \save_transient_properties true
@@ -9,7 +9,7 @@
 \maintain_unincluded_children false
 \language english
 \language_package default
-\inputencoding auto
+\inputencoding auto-legacy
 \fontencoding auto
 \font_roman "IBMPlexSerif" "Century Schoolbook L"
 \font_sans "DejaVuSans" "DejaVu Sans"
@@ -55,6 +55,8 @@
 \justification true
 \use_refstyle 1
 \use_minted 0
+\use_lineno 1
+\lineno_options left
 \index Index
 \shortcut idx
 \color #008000
@@ -70,6 +72,7 @@
 \papercolumns 1
 \papersides 1
 \paperpagestyle default
+\tablestyle default
 \tracking_changes false
 \output_changes false
 \html_math_output 0
@@ -175,5 +178,14 @@ leading to a corrupt file.
 \end_layout
 
 \end_deeper
+\begin_layout Description
+Lineno
+\begin_inset space ~
+\end_inset
+
+support: this file uses the native line numbering support introduced in
+ 2.4.
+\end_layout
+
 \end_body
 \end_document
index 866bbb2b865851ea7040876810356cc5779adde1..2f0081a3444d6f863df5751885534b5a0fbbad8a 100644 (file)
@@ -26,10 +26,10 @@ from datetime import (datetime, date, time)
 
 # Uncomment only what you need to import, please.
 
-from parser_tools import (count_pars_in_inset, find_end_of_inset, find_end_of_layout,
-                          find_token, find_re, get_bool_value, get_containing_layout,
-                          get_option_value, get_value, get_quoted_value)
-#    del_token, del_value, del_complete_lines,
+from parser_tools import (count_pars_in_inset, del_token, find_end_of_inset,
+    find_end_of_layout, find_token, find_re, get_bool_value,
+    get_containing_layout, get_option_value, get_value, get_quoted_value)
+#    del_value, del_complete_lines,
 #    find_complete_lines, find_end_of,
 #    find_re, find_substring, find_token_backwards,
 #    get_containing_inset,
@@ -1694,14 +1694,38 @@ def revert_utf8_japanese(document):
     if lang == "japanese-cjk":
         document.set_parameter("inputencoding", "utf8-cjk")
 
+
 def revert_lineno(document):
-    " Remove lineno package use."
-    i = find_token(document.header, "\\use_lineno", 0)
-    if i != -1:
-        del document.header[i]
-    i = find_token(document.header, "\\lineno_options", 0)
-    if i != -1:
-        del document.header[i]
+    " Replace lineno setting with user-preamble code."
+
+    if not get_bool_value(document.header, "\\use_lineno", delete=True):
+        return
+    lineno_options = get_quoted_value(document.header, "\\lineno_options",
+                                      delete=True)
+    if lineno_options:
+        usepkg = "\\usepackage[%s]{lineno}"%lineno_options
+    else:
+        usepkg = "\\usepackage{lineno}"
+    add_to_preamble(document, [usepkg, "\\linenumbers"])
+
+def convert_lineno(document):
+    " Replace user-preamble code with native lineno support."
+    j = find_token(document.preamble, "\\linenumbers", 1)
+    if j == -1:
+        return
+    usepkg = re.match(r"\\usepackage(.*){lineno}", document.preamble[j-1])
+    if usepkg is None:
+        return
+    options = usepkg.group(1).strip("[]")
+    
+    del(document.preamble[j-1:j+1])
+    print (j, document.preamble[j-2])
+    del_token(document.preamble, "% Added by lyx2lyx", j-2, j-1)
+    
+    k = find_token(document.header, "\\index ")
+    document.header.insert(k, "\\use_lineno 1")
+    if options:
+        document.header.insert(k+1 or -1, '\\lineno_options %s'%options)
 
 
 ##
@@ -1740,7 +1764,7 @@ convert = [
            [572, [convert_notoFonts]],  # Added options thin, light, extralight for Noto
            [573, [convert_inputencoding_namechange]],
            [574, [convert_ruby_module, convert_utf8_japanese]],
-           [575, []],
+           [575, [convert_lineno]],
           ]
 
 revert =  [[574, [revert_lineno]],
index 12d6aa6be05cd5396c833c3d0ab442f84fddd504..d6be0c213d8d3b7da921da8bbc96d84a9c3c5375 100644 (file)
@@ -87,6 +87,9 @@ set_bool_value(lines, token, value[, start[, end]]):
 del_token(lines, token[, start[, end]]):
   Like find_token, but deletes the line if it finds one.
   Returns True if a line got deleted, otherwise False.
+  
+  Use get_* with the optional argument "delete=True", if you want to
+  get and delete a token.
 
 find_beginning_of(lines, i, start_token, end_token):
   Here, start_token and end_token are meant to be a matching