commit 09e69f96dd5830643a6bfe224ca8ff956d715cc1
Author: Günter Milde <[email protected]>
Date: Fri May 24 13:49:32 2019 +0200
Complete lyx2lyx for new "lineno" settings.
---
autotests/export/lyx2lyx/lyx_2_4_test.lyx | 16 ++++++++-
lib/lyx2lyx/lyx_2_4.py | 48 +++++++++++++++++++++-------
lib/lyx2lyx/parser_tools.py | 3 ++
3 files changed, 53 insertions(+), 14 deletions(-)
diff --git a/autotests/export/lyx2lyx/lyx_2_4_test.lyx
b/autotests/export/lyx2lyx/lyx_2_4_test.lyx
index a707705..365ef09 100644
--- a/autotests/export/lyx2lyx/lyx_2_4_test.lyx
+++ b/autotests/export/lyx2lyx/lyx_2_4_test.lyx
@@ -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
diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py
index 866bbb2..2f0081a 100644
--- a/lib/lyx2lyx/lyx_2_4.py
+++ b/lib/lyx2lyx/lyx_2_4.py
@@ -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]],
diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py
index 12d6aa6..d6be0c2 100644
--- a/lib/lyx2lyx/parser_tools.py
+++ b/lib/lyx2lyx/parser_tools.py
@@ -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