]> git.lyx.org Git - features.git/commitdiff
Initial inclusion of lyx2lyx files
authorJosé Matox <jamatos@lyx.org>
Thu, 1 Aug 2002 15:26:32 +0000 (15:26 +0000)
committerJosé Matox <jamatos@lyx.org>
Thu, 1 Aug 2002 15:26:32 +0000 (15:26 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4827 a592a061-630c-0410-9148-cb99ea01b6c8

lib/lyx2lyx/error.py [new file with mode: 0644]
lib/lyx2lyx/lyx2lyx [new file with mode: 0755]
lib/lyx2lyx/lyxconvert_215.py [new file with mode: 0644]
lib/lyx2lyx/lyxconvert_218.py [new file with mode: 0644]
lib/lyx2lyx/parser_tools.py [new file with mode: 0644]

diff --git a/lib/lyx2lyx/error.py b/lib/lyx2lyx/error.py
new file mode 100644 (file)
index 0000000..5f3c3fc
--- /dev/null
@@ -0,0 +1,29 @@
+# This file is part of lyx2lyx
+# Copyright (C) 2002 José Matos <jamatos@lyx.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+class Error:
+    invalid_file = "Invalid LyX file\n"
+    invalid_format = "Invalid LyX format\n"
+    format_not_supported = "Format not supported\n"
+    same_format = "No convertion because start and ending formats are the same\n"
+    newer_format = "Starting format is newer than end format\n"
+
+class Warning:
+    dont_match = "Proposed and input file formats do not match"
+
+error = Error()
+warning = Warning()
diff --git a/lib/lyx2lyx/lyx2lyx b/lib/lyx2lyx/lyx2lyx
new file mode 100755 (executable)
index 0000000..51e830b
--- /dev/null
@@ -0,0 +1,173 @@
+#! /usr/bin/env python
+# Copyright (C) 2002 José Matos <jamatos@lyx.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+import getopt, sys, string, re
+from error import error, warning
+from parser_tools import set_format
+
+version = "0.0.2"
+
+# Allow the dummy object to be able to carry related data
+# like a C struct
+class struct:
+    pass
+
+# options object, with default values
+opt = struct()
+
+opt.output = sys.stdout
+opt.input = sys.stdin
+opt.start = None
+opt.end = None
+opt.quiet = 0
+
+format = re.compile(r"(\d)[\.,]?(\d\d)")
+fileformat = re.compile(r"\\lyxformat\s*(\S*)")
+lst_ft = ["210", "215", "216", "217", "218", "220"]
+format_name = {"210" : "2.10", "215":"2.15", "216": "2.16",
+               "217" : "2.17", "218":"218" , "220":"220"}
+def usage():
+    print """Usage: lyx2lyx [options] file1
+Convert old lyx file <file1> to newer format.
+Options:
+    -h, --help                 this information
+    -v, --version              output version information and exit
+    -l, --list                 list all available formats
+    -d, --debug level          level=0..2 (O_ no debug information,2_verbose)
+                               default: level=1
+    -f, --from version         initial version (optional)
+    -t, --to version           final version (optional)
+    -o, --output name          name of the output file or else goes to stdout
+    -q, --quiet                        same as --debug=0"""
+
+
+def parse_options(argv):
+    _options =  ["help", "version", "list", "from=", "to=", "output=", "quiet"]
+    try:
+       opts, args = getopt.getopt(argv[1:], "f:hlo:qt:v", _options)
+    except getopt.error:
+        usage()
+        sys.exit(2)
+
+    for o, a in opts:
+        if o in ("-h", "--help"):
+            usage()
+            sys.exit()
+        if o in ("-v", "--version"):
+            print "lyxconvert, version %s" %(version)
+            print "Copyright (C) 2002 LyX Team"
+            sys.exit()
+        if o in ("-d", "--debug"):
+            opt.debug = int(a)
+        if o in ("-q", "--quiet"):
+            opt.debug = 0
+        if o in ("-l", "--list"):
+            print lst_ft
+            sys.exit()
+        if o in ("-o", "--output"):
+            opt.output = open(a, "w")
+        if o in ("-f", "--from"):
+            opt.start = lyxformat(a)
+        if o in ("-t", "--to"):
+            opt.end = lyxformat(a)
+
+    if not opt.end:
+        opt.end = lst_ft[len(lst_ft)-1]
+
+    if opt.start and opt.start == opt.end:
+        sys.stderr.write(error.same_format)
+        sys.exit()
+
+    if opt.start > opt.end:
+        sys.stderr.write(error.newer_format)
+        sys.exit(1)
+
+    if args:
+        opt.input = open(args[0])
+
+def lyxformat(fmt):
+    result = format.match(fmt)
+    if result:
+        fmt = result.group(1)+result.group(2)
+    else:
+        sys.stderr.write(fmt + ": " + error.invalid_format)
+        sys.exit(2)
+    if fmt not in lst_ft:
+        sys.stderr.write(fmt + ": " + error.format_not_supported)
+        sys.exit(1)
+    return fmt
+
+def read_file(file, header, body):
+    """Reads a file into the header and body parts"""
+    fmt = None
+    while 1:
+        line = file.readline()
+        if not line:
+            sys.stderr.write(error.invalid_file)
+            sys.exit(3)
+
+        line = line[:-1]
+        if not line:
+            break
+
+        header.append(line)
+        result = fileformat.match(line)
+        if result:
+            fmt = lyxformat(result.group(1))
+
+    while 1:
+        line = file.readline()
+        if not line:
+            break
+        body.append(line[:-1])
+
+    if not fmt:
+        sys.stderr.write(error.invalid_file)
+        sys.exit(3)
+    return fmt
+
+def write_file(file, header, body):
+    for line in header:
+        file.write(line+"\n")
+    file.write("\n")
+    for line in body:
+        file.write(line+"\n")
+
+def main(argv):
+    parse_options(argv)
+
+    header, body = [], []
+    fmt =  read_file(opt.input, header, body)
+
+    if opt.start:
+        if opt.start != fmt:
+            print warning.dont_match + ":", opt.start, fmt
+    else:
+        opt.start = fmt
+
+    # Convertion chain
+    start = lst_ft.index(opt.start)
+    end = lst_ft.index(opt.end)
+
+    for fmt in lst_ft[start:end]:
+        __import__("lyxconvert_" + fmt).convert(header,body)
+
+    set_format(header,format_name[opt.end])
+    write_file(opt.output, header, body)
+    
+if __name__ == "__main__":
+    main(sys.argv)
diff --git a/lib/lyx2lyx/lyxconvert_215.py b/lib/lyx2lyx/lyxconvert_215.py
new file mode 100644 (file)
index 0000000..c4627da
--- /dev/null
@@ -0,0 +1,67 @@
+# This file is part of lyx2lyx
+# Copyright (C) 2002 José Matos <jamatos@lyx.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+import re
+from parser_tools import *
+
+layout_exp = re.compile(r"\\layout (\S*)")
+
+math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
+
+def replace_protected_separator(lines):
+    i=0
+    while 1:
+        i = find_token(lines, "\\protected_separator", i)
+        if i == -1:
+            break
+        j = find_token_backwards(lines, "\\layout", i)
+        #if j == -1: print error
+        layout = layout_exp.match(lines[j]).group(1)
+
+        if layout == "LyX-Code":
+            result = ""
+            while lines[i] == "\\protected_separator ":
+                result = result + " "
+                del lines[i]
+
+            if lines[i-1] != "":
+                lines[i-1] = lines[i-1] + result + lines[i]
+                del lines[i]
+            else:
+                lines[i] = result + lines[i]
+                del lines[i-1]
+        else:
+            del lines[i]
+            lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
+
+def merge_formula_inset(lines):
+    i=0
+    while 1:
+        i = find_token(lines, "\\begin_inset Formula", i)
+        if i == -1: break
+        if lines[i+1] in math_env:
+            lines[i] = lines[i] + lines[i+1]
+            del lines[i+1]
+        i = i + 1
+
+def convert(header,body):
+    replace_protected_separator(body)
+    merge_formula_inset(body)
+
+if __name__ == "__main__":
+    pass
+
diff --git a/lib/lyx2lyx/lyxconvert_218.py b/lib/lyx2lyx/lyxconvert_218.py
new file mode 100644 (file)
index 0000000..4088761
--- /dev/null
@@ -0,0 +1,128 @@
+# This file is part of lyx2lyx
+# Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+
+import sys,string
+from parser_tools import *
+
+floats = {
+    "footnote": ["\\begin_inset Foot\n",
+                "collapsed true\n"],
+    "margin":   ["\\begin_inset Marginal\n",
+                "collapsed true\n"],
+    "fig":      ["\\begin_inset Float figure\n",
+                "placement htbp\n",
+                "wide false\n",
+                "collapsed false\n"],
+    "tab":      ["\\begin_inset Float table\n",
+                "placement htbp\n",
+                "wide false\n",
+                "collapsed false\n"],
+    "alg":      ["\\begin_inset Float algorithm\n",
+                "placement htbp\n",
+                "wide false\n",
+                "collapsed false\n"],
+    "wide-fig": ["\\begin_inset Float figure\n",
+                "placement htbp\n",
+                "wide true\n",
+                "collapsed false\n"],
+    "wide-tab": ["\\begin_inset Float table\n",
+                "placement htbp\n",
+                "wide true\n",
+                "collapsed false\n"]
+}
+
+def remove_oldfloat(lines):
+    i = 0
+    while 1:
+       i = find_token(lines, "\\begin_float", i)
+       if i == -1:
+           break
+       j = find_token(lines, "\\end_float", i+1)
+       floattype = string.split(lines[i])[1]
+       #print floattype
+       start = floats[floattype]+["\n"]
+       mid = lines[i+1:j]
+       lines[i:j+1]= start+mid+["\\end_inset \n"]
+       i = i+1
+
+def remove_oldminipage(lines):
+    i = 0
+    flag = 0
+    while 1:
+       i = find_token(lines, "\\pextra_type 2", i)
+       if i == -1:
+           break
+       hfill = 0
+       line = string.split(lines[i])
+       if line[4] == "\\pextra_hfill":
+           line[4:6] = []
+           hfill = 1
+       if line[4] == "\\pextra_start_minipage":
+           line[4:6] = []
+       width = line[5]
+       if line[4] == "\\pextra_widthp":
+           width = line[5]+"col%"
+
+       start = ["\\begin_inset Minipage\n",
+                "position %s\n" % line[3],
+                "inner_position 0\n",
+                'height "0pt"\n',
+                'width "%s"\n' % width,
+                "collapsed false\n"
+                ]
+       if flag:
+           flag = 0
+           if hfill:
+               start = ["\n","\hfill\n","\n"]+start
+       else:
+           start = ["\\layout Standard\n"] + start
+
+       j = find_token_backwards(lines,"\\layout", i-1)
+       j0 = j
+       mid = lines[j:i]
+
+       j = find_token2(lines,"\\layout", "\\end_float", i+1)
+       mid = mid+lines[i+1:j]
+
+       while 1:
+           i = find_token2(lines,"\\layout", "\\pextra_type", j+1)
+           if i == -1 or not check_token(lines,  "\\pextra_type", i):
+               break
+           line = string.split(lines[i])
+           if line[4] == "\\pextra_hfill":
+               line[4:6] = []
+           if line[4] == "\\pextra_start_minipage" and line[5] == "1":
+               flag = 1
+               break
+           j = find_token_backwards(lines,"\\layout", i-1)
+           mid = mid+lines[j:i]
+           j = find_token2(lines,"\\layout", "\\end_float", i+1)
+           mid = mid+lines[i+1:j]
+
+       end = ["\\end_inset \n"]
+
+       lines[j0:j] = start+mid+end
+       i = i+1
+
+def convert(header,body):
+    remove_oldminipage(body)
+    remove_oldfloat(body)
+
+if __name__ == "__main__":
+    pass
+
diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py
new file mode 100644 (file)
index 0000000..7a8b356
--- /dev/null
@@ -0,0 +1,59 @@
+# This file is part of lyx2lyx
+# Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>, José Matos <jamatos@lyx.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+def check_token(lines, token, i):
+    if lines[i][:len(token)] == token:
+       return 1
+    return 0
+
+def find_token(lines, token, start):
+    n = len(lines)
+    m = len(token)
+    i = start
+    while i < n:
+       line = lines[i]
+       if line[:m] == token:
+           return i
+       i = i+1
+    return -1
+
+def find_token2(lines, token1, token2, start):
+    n = len(lines)
+    m1 = len(token1)
+    m2 = len(token2)
+    i = start
+    while i < n:
+       line = lines[i]
+       if line[:m1] == token1 or line[:m2] == token2:
+           return i
+       i = i+1
+    return -1
+
+def find_token_backwards(lines, token, start):
+    n = len(lines)
+    m = len(token)
+    i = start
+    while i >= 0:
+       line = lines[i]
+       if line[:m] == token:
+           return i
+       i = i-1
+    return -1
+
+def set_format(lines, number):
+    i = find_token(lines, "\\lyxformat", 0)
+    lines[i] = "\\lyxformat %s" % number