commit d3ec2b539533077189e61b031e17586214c158e0
Author: Juergen Spitzmueller <[email protected]>
Date:   Wed Aug 26 15:02:32 2026 +0200

    lyx2lyx: new parser_tools function has_token
    
    This checks whether a given token is found at the beginning of one
    of the passed lines.
    
    As opposed to find_token, it also queries child documents, so we
    can check whether a multi-part document uses a given layout (or inset)
    from the master.
---
 lib/lyx2lyx/parser_tools.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py
index 3e3de99125..8fd5616ec7 100644
--- a/lib/lyx2lyx/parser_tools.py
+++ b/lib/lyx2lyx/parser_tools.py
@@ -44,6 +44,12 @@ find_tokens(lines, tokens[, start[, end[, ignorews]]]):
 find_tokens_exact(lines, token[, start[, end]]):
   As find_tokens, but with ignorews True.
 
+has_token(lines, token[, directory]):
+  Returns True if the token exists in lines, else False.
+  The method also queries child documents.
+  The param directory holds the path in case of relative
+  paths in the include inset. It might be passed as document.dir.
+
 find_token_backwards(lines, token, start):
 find_tokens_backwards(lines, tokens, start):
   As before, but look backwards.
@@ -164,6 +170,7 @@ count_pars_in_inset(lines, i):
 
 """
 
+import os
 import re
 
 
@@ -253,6 +260,44 @@ def find_tokens_exact(lines, tokens, start=0, end=0):
     return find_tokens(lines, tokens, start, end, True)
 
 
+def has_token(lines, token, directory=""):
+    """has_tokens(lines, token[, directory]) -> bool
+
+    Checks whether one of the lines starts with token,
+    returns True if so, otherwise False.
+
+    This is meant to check document bodies.
+    The function also checks in child documents.
+    To this end, directory should be passed to determine
+    the child files in case of relative paths.
+    You can use document.dir for this purpose."""
+
+    if find_token(lines, token) != -1:
+        # found in main file
+        return True
+    
+    i = 0
+    # Now we check child documents
+    while True:
+        i = find_token(lines, "\\begin_inset CommandInset include", i)
+        if i == -1:
+            break
+        j = find_end_of_inset(lines, i)
+        if j == -1:
+            i += 1
+            continue
+        fn = get_quoted_value(lines, "filename", i, j)
+        if directory:
+            fn = os.path.join(directory, fn)
+        with open(fn) as f:
+            for line in f:
+                if line.startswith(token):
+                    return True
+        i += 1
+        
+    return False
+
+
 def find_substring(lines, sub, start=0, end=0):
     """find_substring(lines, sub[, start[, end]]) -> int
 
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to