]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_1_4.py
clean up french language handling
[lyx.git] / lib / lyx2lyx / lyx_1_4.py
index 034f0b726ea0fe3f3a5fb30b76a4d6611cb1da8d..ca253df85c80b0d8c4fec84f63c5a1bb4db4c8e0 100644 (file)
@@ -2,7 +2,7 @@
 # -*- coding: iso-8859-1 -*-
 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
-# Copyright (C) 2004-2005 Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+# Copyright (C) 2004-2005 Georg Baum <Georg.Baum@post.rwth-aachen.de>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -23,7 +23,7 @@ from os import access, F_OK
 import os.path
 from parser_tools import find_token, find_end_of_inset, get_next_paragraph, \
                          get_paragraph, get_value, del_token, is_nonempty_line,\
-                        find_tokens, find_end_of, find_token2
+                        find_tokens, find_end_of, find_token2, find_re
 from sys import stdin
 from string import replace, split, find, strip, join
 
@@ -69,6 +69,41 @@ def revert_spaces(file):
         file.body[i] = replace(file.body[i],"\\InsetSpace ~", "\\SpecialChar ~")
 
 
+##
+# equivalent to lyx::support::escape()
+#
+def lyx_support_escape(lab):
+    hexdigit = ['0', '1', '2', '3', '4', '5', '6', '7',
+                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
+    enc = ""
+    for c in lab:
+        o = ord(c)
+        if o >= 128 or c == '=' or c == '%':
+            enc = enc + '='
+            enc = enc + hexdigit[o >> 4]
+            enc = enc + hexdigit[o & 15]
+        else:
+            enc = enc + c
+    return enc;
+
+
+##
+# \begin_inset LatexCommand \eqref -> ERT
+#
+def revert_eqref(file):
+    regexp = re.compile(r'^\\begin_inset\s+LatexCommand\s+\\eqref')
+    i = 0
+    while 1:
+        i = find_re(file.body, regexp, i)
+        if i == -1:
+            break
+        eqref = lyx_support_escape(regexp.sub("", file.body[i]))
+        file.body[i:i+1] = ["\\begin_inset ERT", "status Collapsed", "",
+                            "\\layout Standard", "", "\\backslash ",
+                            "eqref" + eqref]
+        i = i + 7
+
+
 ##
 # BibTeX changes
 #
@@ -269,7 +304,13 @@ def add_end_layout(file):
         i = find_tokens(file.body, ["\\begin_inset", "\\end_inset", "\\layout",
                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
 
-        token = split(file.body[i])[0]
+        if i != -1:
+            token = split(file.body[i])[0]
+        else:
+            file.warning("Truncated file.")
+            i = len(file.body)
+            file.body.insert(i, '\\the_end')
+            token = ""
 
         if token == "\\begin_inset":
             struct_stack.append(token)
@@ -396,9 +437,10 @@ def convert_valignment_middle(body, start, end):
 
 
 def convert_table_valignment_middle(file):
+    regexp = re.compile(r'^\\begin_inset\s+Tabular')
     i = 0
     while 1:
-        i = find_token(file.body, '\\begin_inset  Tabular', i)
+        i = find_re(file.body, regexp, i)
         if i == -1:
             return
         j = find_end_of_inset(file.body, i + 1)
@@ -417,9 +459,10 @@ def revert_table_valignment_middle(body, start, end):
 
 
 def revert_valignment_middle(file):
+    regexp = re.compile(r'^\\begin_inset\s+Tabular')
     i = 0
     while 1:
-        i = find_token(file.body, '\\begin_inset  Tabular', i)
+        i = find_re(file.body, regexp, i)
         if i == -1:
             return
         j = find_end_of_inset(file.body, i + 1)
@@ -481,12 +524,24 @@ def revert_end_document(file):
 #
 #\end_layout
 def convert_breaks(file):
+    par_params = ('added_space_bottom', 'added_space_top', 'align',
+                 'labelwidthstring', 'line_bottom', 'line_top', 'noindent',
+                 'pagebreak_bottom', 'pagebreak_top', 'paragraph_spacing',
+                 'start_of_appendix')
     i = 0
     while 1:
         i = find_token(file.body, "\\begin_layout", i)
         if i == -1:
             return
         i = i + 1
+
+        # Merge all paragraph parameters into a single line
+        # We cannot check for '\\' only because paragraphs may start e.g.
+        # with '\\backslash'
+        while file.body[i + 1][:1] == '\\' and split(file.body[i + 1][1:])[0] in par_params:
+            file.body[i] = file.body[i + 1] + ' ' + file.body[i]
+            del file.body[i+1]
+
         line_top   = find(file.body[i],"\\line_top")
         line_bot   = find(file.body[i],"\\line_bottom")
         pb_top     = find(file.body[i],"\\pagebreak_top")
@@ -521,7 +576,7 @@ def convert_breaks(file):
 
         #  Create an empty paragraph for line and page break that belong
         # above the paragraph
-        if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
+        if pb_top !=-1 or line_top != -1 or vspace_top != -1:
 
             paragraph_above = ['','\\begin_layout Standard','','']
 
@@ -546,23 +601,23 @@ def convert_breaks(file):
         if k == -1:
             return
 
-        if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
+        if pb_bot !=-1 or line_bot != -1 or vspace_bot != -1:
 
-            paragraph_bellow = ['','\\begin_layout Standard','','']
+            paragraph_below = ['','\\begin_layout Standard','','']
 
             if line_bot != -1:
-                paragraph_bellow.extend(['\\lyxline ',''])
+                paragraph_below.extend(['\\lyxline ',''])
 
             if vspace_bot != -1:
-                paragraph_bellow.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset','',''])
+                paragraph_below.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset','',''])
 
             if pb_bot != -1:
-                paragraph_bellow.extend(['\\newpage ',''])
+                paragraph_below.extend(['\\newpage ',''])
 
-            paragraph_bellow.extend(['\\end_layout',''])
+            paragraph_below.extend(['\\end_layout',''])
 
             #inset new paragraph above the current paragraph
-            file.body[k + 1: k + 1] = paragraph_bellow
+            file.body[k + 1: k + 1] = paragraph_below
 
 
 ##
@@ -1466,8 +1521,11 @@ def convert_paperpackage(file):
         file.warning("Malformed lyx file: Missing '\\paperpackage'.")
         return
 
-    packages = {'a4':'none', 'a4wide':'a4', 'widemarginsa4':'a4wide'}
-    paperpackage = split(file.header[i])[1]
+    packages = {'default':'none','a4':'none', 'a4wide':'a4', 'widemarginsa4':'a4wide'}
+    if len(split(file.header[i])) > 1:
+        paperpackage = split(file.header[i])[1]
+    else:
+        paperpackage = "default"
     file.header[i] = replace(file.header[i], paperpackage, packages[paperpackage])
 
 
@@ -1786,6 +1844,13 @@ def revert_ert_paragraphs(file):
         i = i + 1
 
 
+def convert_french(file):
+    regexp = re.compile(r'^\\language\s+frenchb')
+    i = find_re(file.header, regexp, 0)
+    if i != -1:
+        file.header[i] = "\\language french"
+
+
 ##
 # Convertion hub
 #
@@ -1811,9 +1876,11 @@ convert = [[223, [insert_tracking_changes, add_end_header, remove_color_default,
            [238, [update_latexaccents]],
            [239, [normalize_paragraph_params]],
            [240, [convert_output_changes]],
-           [241, [convert_ert_paragraphs]]]
+           [241, [convert_ert_paragraphs]],
+           [242, [convert_french]]]
 
-revert =  [[240, [revert_ert_paragraphs]],
+revert =  [[241, []],
+           [240, [revert_ert_paragraphs]],
            [239, [revert_output_changes]],
            [238, []],
            [237, []],
@@ -1832,7 +1899,7 @@ revert =  [[240, [revert_ert_paragraphs]],
            [225, [revert_note]],
            [224, [rm_end_layout, begin_layout2layout, revert_end_document,
                   revert_valignment_middle, convert_vspace, convert_frameless_box]],
-           [223, [revert_external_2, revert_comment]],
+           [223, [revert_external_2, revert_comment, revert_eqref]],
            [221, [rm_end_header, revert_spaces, revert_bibtex,
                   rm_tracking_changes, rm_body_changes]]]