]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx2lyx_tools.py
Consistent output of breakable/non-breakable dashes on all TeX engines.
[lyx.git] / lib / lyx2lyx / lyx2lyx_tools.py
index 3ac8391bb6563a58fe8230d82cc6cdbd21ab4568..f63f4026167a8f4b563beb01bc75f216d50863dc 100644 (file)
@@ -47,6 +47,13 @@ put_cmd_in_ert(arg):
     ert = put_cmd_in_ert(content)
     document.body[i:j+1] = ert
 
+get_ert(lines, i[, verbatim]):
+  Here, lines is a list of lines of LyX material containing an ERT inset,
+  whose content we want to convert to LaTeX. The ERT starts at index i.
+  If the optional (by default: False) bool verbatim is True, the content
+  of the ERT is returned verbatim, that is in LyX syntax (not LaTeX syntax)
+  for the use in verbatim insets.
+
 lyx2latex(document, lines):
   Here, lines is a list of lines of LyX material we want to convert
   to LaTeX. We do the best we can and return a string containing
@@ -62,6 +69,11 @@ latex_length(slen):
     (bool, length), where the bool tells us if it was a percentage, and
     the length is the LaTeX representation.
 
+convert_info_insets(document, type, func):
+    Applies func to the argument of all info insets matching certain types
+    type : the type to match. This can be a regular expression.
+    func : function from string to string to apply to the "arg" field of
+           the info insets.
 '''
 
 import re
@@ -161,8 +173,11 @@ def get_ert(lines, i, verbatim = False):
         elif lines[i] == "\\end_layout":
             while i + 1 < j and lines[i+1] == "":
                 i = i + 1
-        elif lines[i] == "\\backslash" and not verbatim:
-            ret = ret + "\\"
+        elif lines[i] == "\\backslash":
+            if verbatim:
+                ret = ret + "\n" + lines[i] + "\n"
+            else:
+                ret = ret + "\\"
         else:
             ret = ret + lines[i]
         i = i + 1
@@ -493,3 +508,21 @@ def str2bool(s):
   "'true' goes to True, case-insensitively, and we strip whitespace."
   s = s.strip().lower()
   return s == "true"
+
+
+def convert_info_insets(document, type, func):
+    "Convert info insets matching type using func."
+    i = 0
+    type_re = re.compile(r'^type\s+"(%s)"$' % type)
+    arg_re = re.compile(r'^arg\s+"(.*)"$')
+    while True:
+        i = find_token(document.body, "\\begin_inset Info", i)
+        if i == -1:
+            return
+        t = type_re.match(document.body[i + 1])
+        if t:
+            arg = arg_re.match(document.body[i + 2])
+            if arg:
+                new_arg = func(arg.group(1))
+                document.body[i + 2] = 'arg   "%s"' % new_arg
+        i += 3