Convert all python files to utf-8.

Add a documentation string to each module.

Add a documentation string to every function in the modules.

file is a python reserved word => s/file/document/g as variable

add a new property to modules, supported_versions that holds
 the versions supported by that module

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@14534 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
José Matox 2006-08-02 14:19:22 +00:00
parent 2987ccdd9e
commit 695bfd88ed
16 changed files with 1105 additions and 1005 deletions

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>, José Matos <jamatos@lyx.org> # Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>, José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -34,6 +34,7 @@ default_debug_level = 2
# Private helper functions # Private helper functions
def find_end_of_inset(lines, i): def find_end_of_inset(lines, i):
" Find beginning of inset, where lines[i] is included."
return find_end_of(lines, i, "\\begin_inset", "\\end_inset") return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
# End of helper functions # End of helper functions
@ -49,16 +50,16 @@ original_version = re.compile(r"\#LyX (\S*)")
# file format information: # file format information:
# file, supported formats, stable release versions # file, supported formats, stable release versions
format_relation = [("0_08", [210], ["0.8.%d" % i for i in range(7)] + ["0.8"]), format_relation = [("0_08", [210], ["0.8.%d" % i for i in range(7)] + ["0.8"]),
("0_10", [210], ["0.10.7","0.10"]), ("0_10", [210], ["0.10.%d" % i for i in range(8)] + ["0.10"]),
("0_12", [215], ["0.12","0.12.1","0.12"]), ("0_12", [215], ["0.12.0","0.12.1","0.12"]),
("1_0_0", [215], ["1.0.0","1.0"]), ("1_0_0", [215], ["1.0.0","1.0"]),
("1_0_1", [215], ["1.0.1","1.0.2","1.0.3","1.0.4", "1.1.2","1.1"]), ("1_0_1", [215], ["1.0.1","1.0.2","1.0.3","1.0.4", "1.1.2","1.1"]),
("1_1_4", [215], ["1.1.4","1.1"]), ("1_1_4", [215], ["1.1.4","1.1"]),
("1_1_5", [216], ["1.1.5","1.1.5fix1","1.1.5fix2","1.1"]), ("1_1_5", [216], ["1.1.5","1.1.5fix1","1.1.5fix2","1.1"]),
("1_1_6_0", [217], ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]), ("1_1_6_0", [217], ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]),
("1_1_6_3", [218], ["1.1.6fix3","1.1.6fix4","1.1"]), ("1_1_6_3", [218], ["1.1.6fix3","1.1.6fix4","1.1"]),
("1_2", [220], ["1.2.0","1.2.1","1.2.3","1.2.4","1.2"]), ("1_2", [220], ["1.2.%d" % i for i in range(5)] + ["1.2"]),
("1_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3.5","1.3.6","1.3.7","1.3"]), ("1_3", [221], ["1.3.%d" % i for i in range(8)] + ["1.3"]),
("1_4", range(222,246), ["1.4.0", "1.4.1", "1.4.2","1.4.3svn"]), ("1_4", range(222,246), ["1.4.0", "1.4.1", "1.4.2","1.4.3svn"]),
("1_5", range(246,249), ["1.5.0svn","1.5"])] ("1_5", range(246,249), ["1.5.0svn","1.5"])]

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx -*- python -*- # This file is part of lyx2lyx -*- python -*-
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2006 José Matos <jamatos@lyx.org> # Copyright (C) 2006 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License

View File

@ -16,8 +16,9 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files generated by lyx 0.8""" """ Convert files to the file format generated by lyx 0.8"""
supported_versions = ["0.8.%d" % i for i in range(7)] + ["0.8"]
convert = [[210, []]] convert = [[210, []]]
revert = [] revert = []

View File

@ -16,10 +16,10 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files generated by lyx 0.10""" """ Convert files to the file format generated by lyx 0.10"""
def regularise_header(document): def regularise_header(document):
" Place tokens in their separate line. " " Put each entry in header into a separate line. "
i = 0 i = 0
while i < len(document.header): while i < len(document.header):
line = document.header[i] line = document.header[i]
@ -31,8 +31,9 @@ def regularise_header(document):
def find_next_space(line, j): def find_next_space(line, j):
""" Return position of next space, starting from position k, if """ Return position of next space or backslash, which one comes
not existing return last position in line.""" first, starting from position k, if not existing return last
position in line."""
l = line.find(' ', j) l = line.find(' ', j)
if l == -1: if l == -1:
l = len(line) l = len(line)
@ -46,6 +47,20 @@ def find_next_space(line, j):
def regularise_body(document): def regularise_body(document):
""" Place tokens starting with a backslash into a separate line. """
getline_tokens = ["added_space_bottom", "added_space_top",
"align", "layout", "fill_bottom", "fill_top",
"labelwidthstring", "pagebreak_top",
"pagebreak_bottom", "noindent"]
noargs_tokens = ["backslash", "begin_deeper", "end_deeper",
"end_float", "end_inset", "hfill", "newline",
"protected_separator"]
onearg_tokens = ["bar", "begin_float", "family", "latex", "shape",
"size", "series", "cursor"]
i = 0 i = 0
while i < len(document.body): while i < len(document.body):
line = document.body[i] line = document.body[i]
@ -66,18 +81,18 @@ def regularise_body(document):
# These tokens take the rest of the line # These tokens take the rest of the line
token = line[j+1:k] token = line[j+1:k]
if token in ["added_space_bottom", "added_space_top", "align", "layout", "fill_bottom", "fill_top", "labelwidthstring", "pagebreak_top", "pagebreak_bottom", "noindent"]: if token in getline_tokens:
tmp += [line[j:]] tmp += [line[j:]]
break break
# These tokens take no arguments # These tokens take no arguments
if token in ["backslash", "begin_deeper", "end_deeper", "end_float", "end_inset", "hfill", "newline", "protected_separator"]: if token in noargs_tokens:
tmp += [line[j:k]] tmp += [line[j:k]]
j = k j = k
continue continue
# These tokens take one argument # These tokens take one argument
if token in ["bar", "begin_float", "family", "latex", "shape", "size", "series", "cursor"]: if token in onearg_tokens:
k = find_next_space(line, k + 1) k = find_next_space(line, k + 1)
tmp += [line[j:k]] tmp += [line[j:k]]
j = k j = k
@ -113,10 +128,10 @@ def regularise_body(document):
i += len(tmp) i += len(tmp)
supported_versions = ["0.10.%d" % i for i in range(8)] + ["0.10"]
convert = [[210, [regularise_header, regularise_body]]] convert = [[210, [regularise_header, regularise_body]]]
revert = [] revert = []
if __name__ == "__main__": if __name__ == "__main__":
pass pass

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2003-2004 José Matos <jamatos@lyx.org> # Copyright (C) 2003-2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -16,39 +16,45 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 0.12"""
import re import re
import string import string
from parser_tools import find_token, find_re, check_token from parser_tools import find_token, find_re, check_token
def space_before_layout(file): def space_before_layout(document):
lines = file.body " Remove empty line before \\layout. "
lines = document.body
i = 2 # skip first layout i = 2 # skip first layout
while 1: while 1:
i = find_token(lines, '\\layout', i) i = find_token(lines, '\\layout', i)
if i == -1: if i == -1:
break break
if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1: prot_space = string.find(lines[i-2],'\\protected_separator')
if lines[i - 1] == '' and prot_space == -1:
del lines[i-1] del lines[i-1]
i = i + 1 i = i + 1
def formula_inset_space_eat(file): def formula_inset_space_eat(document):
lines = file.body " Remove space after inset formula."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset Formula", i) i = find_token(lines, "\\begin_inset Formula", i)
if i == -1: break if i == -1:
break
if len(lines[i]) > 22 and lines[i][21] == ' ': if len(lines[i]) > 22 and lines[i][21] == ' ':
lines[i] = lines[i][:20] + lines[i][21:] lines[i] = lines[i][:20] + lines[i][21:]
i = i + 1 i = i + 1
# Update from tabular format 1 or 2 to 4 def update_tabular(document):
def update_tabular(file): " Update from tabular format 1 or 2 to 4."
lines = file.body lines = document.body
lyxtable_re = re.compile(r".*\\LyXTable$") lyxtable_re = re.compile(r".*\\LyXTable$")
i = 0 i = 0
while 1: while 1:
@ -82,19 +88,26 @@ def update_tabular(file):
lines[i] = string.strip(lines[i]) lines[i] = string.strip(lines[i])
def final_dot(file):
lines = file.body def final_dot(document):
" Merge lines if the dot is the final character."
lines = document.body
i = 0 i = 0
while i < len(lines): while i < len(lines):
if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 and lines[i+1] != '':
if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and \
lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 \
and lines[i+1] != '':
lines[i] = lines[i] + lines[i+1] lines[i] = lines[i] + lines[i+1]
del lines[i+1] del lines[i+1]
else: else:
i = i + 1 i = i + 1
def update_inset_label(file): def update_inset_label(document):
lines = file.body " Update inset Label."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, '\\begin_inset Label', i) i = find_token(lines, '\\begin_inset Label', i)
@ -104,26 +117,35 @@ def update_inset_label(file):
i = i + 1 i = i + 1
def update_latexdel(file): def update_latexdel(document):
lines = file.body " Update inset LatexDel."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, '\\begin_inset LatexDel', i) i = find_token(lines, '\\begin_inset LatexDel', i)
if i == -1: if i == -1:
return return
lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand') lines[i] = string.replace(lines[i],
'\\begin_inset LatexDel',
'\\begin_inset LatexCommand')
i = i + 1 i = i + 1
def update_vfill(file): def update_vfill(document):
lines = file.body " Update fill_top and fill_bottom."
lines = document.body
for i in range(len(lines)): for i in range(len(lines)):
lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill') lines[i] = string.replace(lines[i],
lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill') '\\fill_top',
'\\added_space_top vfill')
lines[i] = string.replace(lines[i],
'\\fill_bottom',
'\\added_space_bottom vfill')
def update_space_units(file): def update_space_units(document):
lines = file.body " Update space units."
lines = document.body
added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)') added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
added_space_top = re.compile(r'\\added_space_top ([^ ]*)') added_space_top = re.compile(r'\\added_space_top ([^ ]*)')
for i in range(len(lines)): for i in range(len(lines)):
@ -140,8 +162,9 @@ def update_space_units(file):
lines[i] = string.replace(lines[i], old, new) lines[i] = string.replace(lines[i], old, new)
def remove_cursor(file): def remove_cursor(document):
lines = file.body " Remove cursor, it is not saved on the file anymore."
lines = document.body
i = 0 i = 0
cursor_re = re.compile(r'.*(\\cursor \d*)') cursor_re = re.compile(r'.*(\\cursor \d*)')
while 1: while 1:
@ -153,8 +176,9 @@ def remove_cursor(file):
i = i + 1 i = i + 1
def remove_empty_insets(file): def remove_empty_insets(document):
lines = file.body " Remove empty insets."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, '\\begin_inset ', i) i = find_token(lines, '\\begin_inset ', i)
@ -166,8 +190,9 @@ def remove_empty_insets(file):
i = i + 1 i = i + 1
def remove_formula_latex(file): def remove_formula_latex(document):
lines = file.body " Remove formula latex."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, '\\latex formula_latex ', i) i = find_token(lines, '\\latex formula_latex ', i)
@ -181,15 +206,17 @@ def remove_formula_latex(file):
del lines[i] del lines[i]
def add_end_document(file): def add_end_document(document):
lines = file.body " Add \\the_end to the end of the document."
lines = document.body
i = find_token(lines, '\\the_end', 0) i = find_token(lines, '\\the_end', 0)
if i == -1: if i == -1:
lines.append('\\the_end') lines.append('\\the_end')
def header_update(file): def header_update(document):
lines = file.header " Update document header."
lines = document.header
i = 0 i = 0
l = len(lines) l = len(lines)
while i < l: while i < l:
@ -241,8 +268,9 @@ def header_update(file):
i = i + 1 i = i + 1
def update_latexaccents(file): def update_latexaccents(document):
body = file.body " Update latex accent insets."
body = document.body
i = 1 i = 1
while 1: while 1:
i = find_token(body, '\\i ', i) i = find_token(body, '\\i ', i)
@ -269,8 +297,9 @@ def update_latexaccents(file):
i = i + 1 i = i + 1
def obsolete_latex_title(file): def obsolete_latex_title(document):
body = file.body " Replace layout Latex_Title with Title."
body = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(body, '\\layout', i) i = find_token(body, '\\layout', i)
@ -283,12 +312,14 @@ def obsolete_latex_title(file):
i = i + 1 i = i + 1
supported_versions = ["0.12.0","0.12.1","0.12"]
convert = [[215, [header_update, add_end_document, remove_cursor, convert = [[215, [header_update, add_end_document, remove_cursor,
final_dot, update_inset_label, update_latexdel, final_dot, update_inset_label, update_latexdel,
update_space_units, space_before_layout, update_space_units, space_before_layout,
formula_inset_space_eat, update_tabular, formula_inset_space_eat, update_tabular,
update_vfill, remove_empty_insets, update_vfill, remove_empty_insets,
remove_formula_latex, update_latexaccents, obsolete_latex_title]]] remove_formula_latex, update_latexaccents,
obsolete_latex_title]]]
revert = [] revert = []

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2004 José Matos <jamatos@lyx.org> # Copyright (C) 2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -16,12 +16,16 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.0"""
import re import re
import string import string
from parser_tools import find_token, find_re from parser_tools import find_token, find_re
def obsolete_latex_title(file): def obsolete_latex_title(document):
body = file.body " Replace LatexTitle layout with Title. "
body = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(body, '\\layout', i) i = find_token(body, '\\layout', i)
@ -34,9 +38,10 @@ def obsolete_latex_title(file):
i = i + 1 i = i + 1
# Update from tabular format 3 to 4 if necessary def update_tabular(document):
def update_tabular(file): " Update from tabular format 3 to 4 if necessary."
lines = file.body
lines = document.body
lyxtable_re = re.compile(r".*\\LyXTable$") lyxtable_re = re.compile(r".*\\LyXTable$")
i = 0 i = 0
while 1: while 1:
@ -72,6 +77,7 @@ def update_tabular(file):
lines[i] = string.strip(lines[i]) lines[i] = string.strip(lines[i])
supported_versions = ["1.0.0","1.0"]
convert = [[215, [obsolete_latex_title, update_tabular]]] convert = [[215, [obsolete_latex_title, update_tabular]]]
revert = [] revert = []

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2004 José Matos <jamatos@lyx.org> # Copyright (C) 2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -16,6 +16,9 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.0.1"""
supported_versions = ["1.0.1","1.0.2","1.0.3","1.0.4", "1.1.2","1.1"]
convert = [[215, []]] convert = [[215, []]]
revert = [] revert = []

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2004 José Matos <jamatos@lyx.org> # Copyright (C) 2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -16,6 +16,9 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.1.4"""
supported_versions = ["1.1.4","1.1"]
convert = [[215, []]] convert = [[215, []]]
revert = [] revert = []

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This document is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2002-2004 José Matos <jamatos@lyx.org> # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -16,6 +16,8 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.1.5"""
import re import re
import string import string
from parser_tools import find_token, find_token_backwards, find_re from parser_tools import find_token, find_token_backwards, find_re
@ -24,6 +26,7 @@ from parser_tools import find_token, find_token_backwards, find_re
# Private helper functions # Private helper functions
def get_layout(line, default_layout): def get_layout(line, default_layout):
" Get the line layout, beware of the empty layout."
tokens = string.split(line) tokens = string.split(line)
if len(tokens) > 1: if len(tokens) > 1:
return tokens[1] return tokens[1]
@ -34,8 +37,9 @@ def get_layout(line, default_layout):
math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"] math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
def replace_protected_separator(file): def replace_protected_separator(document):
lines = file.body " Replace protected separator. "
lines = document.body
i=0 i=0
while 1: while 1:
i = find_token(lines, "\\protected_separator", i) i = find_token(lines, "\\protected_separator", i)
@ -43,7 +47,7 @@ def replace_protected_separator(file):
break break
j = find_token_backwards(lines, "\\layout", i) j = find_token_backwards(lines, "\\layout", i)
#if j == -1: print error #if j == -1: print error
layout = get_layout(lines[j], file.default_layout) layout = get_layout(lines[j], document.default_layout)
if layout == "LyX-Code": if layout == "LyX-Code":
result = "" result = ""
@ -58,8 +62,9 @@ def replace_protected_separator(file):
del lines[i] del lines[i]
def merge_formula_inset(file): def merge_formula_inset(document):
lines = file.body " Merge formula insets. "
lines = document.body
i=0 i=0
while 1: while 1:
i = find_token(lines, "\\begin_inset Formula", i) i = find_token(lines, "\\begin_inset Formula", i)
@ -70,9 +75,9 @@ def merge_formula_inset(file):
i = i + 1 i = i + 1
# Update from tabular format 4 to 5 if necessary def update_tabular(document):
def update_tabular(file): " Update from tabular format 4 to 5 if necessary. "
lines = file.body lines = document.body
lyxtable_re = re.compile(r".*\\LyXTable$") lyxtable_re = re.compile(r".*\\LyXTable$")
i=0 i=0
while 1: while 1:
@ -103,26 +108,30 @@ def update_tabular(file):
i = i + 1 i = i + 1
def update_toc(file): def update_toc(document):
lines = file.body " Update table of contents. "
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i) i = find_token(lines,
'\\begin_inset LatexCommand \\tableofcontents', i)
if i == -1: if i == -1:
break break
lines[i] = lines[i] + '{}' lines[i] = lines[i] + '{}'
i = i + 1 i = i + 1
def remove_cursor(file): def remove_cursor(document):
lines = file.body " Remove cursor. "
lines = document.body
i = find_token(lines, '\\cursor', 0) i = find_token(lines, '\\cursor', 0)
if i != -1: if i != -1:
del lines[i] del lines[i]
def remove_vcid(file): def remove_vcid(document):
lines = file.header " Remove \\lyxvcid and \\lyxrcsid. "
lines = document.header
i = find_token(lines, '\\lyxvcid', 0) i = find_token(lines, '\\lyxvcid', 0)
if i != -1: if i != -1:
del lines[i] del lines[i]
@ -131,16 +140,18 @@ def remove_vcid(file):
del lines[i] del lines[i]
def first_layout(file): def first_layout(document):
lines = file.body " Fix first layout, if empty use the default layout."
lines = document.body
while (lines[0] == ""): while (lines[0] == ""):
del lines[0] del lines[0]
if lines[0][:7] != "\\layout": if lines[0][:7] != "\\layout":
lines[:0] = ['\\layout %s' % file.default_layout, ''] lines[:0] = ['\\layout %s' % document.default_layout, '']
def remove_space_in_units(file): def remove_space_in_units(document):
lines = file.header " Remove space in units. "
lines = document.header
margins = ["\\topmargin","\\rightmargin", margins = ["\\topmargin","\\rightmargin",
"\\leftmargin","\\bottommargin"] "\\leftmargin","\\bottommargin"]
@ -159,8 +170,9 @@ def remove_space_in_units(file):
i = i + 1 i = i + 1
def latexdel_getargs(file, i): def latexdel_getargs(document, i):
lines = file.body " Get arguments from latexdel insets. "
lines = document.body
# play safe, clean empty lines # play safe, clean empty lines
while 1: while 1:
@ -173,7 +185,7 @@ def latexdel_getargs(file, i):
if i == j: if i == j:
del lines[i] del lines[i]
else: else:
file.warning("Unexpected end of inset.") document.warning("Unexpected end of inset.")
j = find_token(lines, '\\begin_inset LatexDel }{', i) j = find_token(lines, '\\begin_inset LatexDel }{', i)
ref = string.join(lines[i:j]) ref = string.join(lines[i:j])
@ -189,7 +201,7 @@ def latexdel_getargs(file, i):
if i == j: if i == j:
del lines[i] del lines[i]
else: else:
file.warning("Unexpected end of inset.") document.warning("Unexpected end of inset.")
j = find_token(lines, '\\begin_inset LatexDel }', i) j = find_token(lines, '\\begin_inset LatexDel }', i)
label = string.join(lines[i:j]) label = string.join(lines[i:j])
del lines[i:j + 1] del lines[i:j + 1]
@ -197,8 +209,9 @@ def latexdel_getargs(file, i):
return ref, label return ref, label
def update_ref(file): def update_ref(document):
lines = file.body " Update reference inset. "
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, '\\begin_inset LatexCommand', i) i = find_token(lines, '\\begin_inset LatexCommand', i)
@ -207,21 +220,24 @@ def update_ref(file):
if string.split(lines[i])[-1] == "\\ref{": if string.split(lines[i])[-1] == "\\ref{":
i = i + 1 i = i + 1
ref, label = latexdel_getargs(file, i) ref, label = latexdel_getargs(document, i)
lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label) lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label)
i = i + 1 i = i + 1
def update_latexdel(file): def update_latexdel(document):
lines = file.body " Remove latexdel insets. "
lines = document.body
i = 0 i = 0
latexdel_re = re.compile(r".*\\begin_inset LatexDel") latexdel_re = re.compile(r".*\\begin_inset LatexDel")
while 1: while 1:
i = find_re(lines, latexdel_re, i) i = find_re(lines, latexdel_re, i)
if i == -1: if i == -1:
return return
lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand') lines[i] = string.replace(lines[i],
'\\begin_inset LatexDel',
'\\begin_inset LatexCommand')
j = string.find(lines[i],'\\begin_inset') j = string.find(lines[i],'\\begin_inset')
lines.insert(i+1, lines[i][j:]) lines.insert(i+1, lines[i][j:])
@ -231,15 +247,19 @@ def update_latexdel(file):
if string.split(lines[i])[-1] in ("\\url{", "\\htmlurl{"): if string.split(lines[i])[-1] in ("\\url{", "\\htmlurl{"):
i = i + 1 i = i + 1
ref, label = latexdel_getargs(file, i) ref, label = latexdel_getargs(document, i)
lines[i -1] = "%s[%s]{%s}" % (lines[i-1][:-1], label, ref) lines[i -1] = "%s[%s]{%s}" % (lines[i-1][:-1], label, ref)
i = i + 1 i = i + 1
convert = [[216, [first_layout, remove_vcid, remove_cursor, update_toc, supported_versions = ["1.1.5","1.1.5fix1","1.1.5fix2","1.1"]
replace_protected_separator, merge_formula_inset, convert = [[216, [first_layout, remove_vcid, remove_cursor,
update_tabular, remove_space_in_units, update_ref, update_latexdel]]] update_toc, replace_protected_separator,
merge_formula_inset, update_tabular,
remove_space_in_units, update_ref,
update_latexdel]]]
revert = [] revert = []
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2002-2004 José Matos <jamatos@lyx.org> # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -16,14 +16,16 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.1.6"""
import re import re
import string import string
from parser_tools import find_re, find_tokens, find_token, check_token from parser_tools import find_re, find_tokens, find_token, check_token
lyxtable_re = re.compile(r".*\\LyXTable$") lyxtable_re = re.compile(r".*\\LyXTable$")
def update_tabular(file): def update_tabular(document):
lines = file.body " Update tabular to version 1 (xml like syntax). "
lines = document.body
i=0 i=0
while 1: while 1:
i = find_re(lines, lyxtable_re, i) i = find_re(lines, lyxtable_re, i)
@ -100,13 +102,13 @@ def update_tabular(file):
continue continue
if l == ncells -1: if l == ncells -1:
# the end variable refers to cell end, not to file end. # the end variable refers to cell end, not to document end.
end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i) end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i)
else: else:
end = find_token(lines, '\\newline', i) end = find_token(lines, '\\newline', i)
if end == -1: if end == -1:
file.error("Malformed LyX file.") document.error("Malformed LyX file.")
end = end - i end = end - i
while end > 0: while end > 0:
@ -146,7 +148,7 @@ def update_tabular(file):
tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%d" rightline="%d" rotate="%s" usebox="%s" width=%s special=%s>' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],leftline,rightline,cell_info[m][5],cell_info[m][6],cell_info[m][7],cell_info[m][8])) tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%d" rightline="%d" rotate="%s" usebox="%s" width=%s special=%s>' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],leftline,rightline,cell_info[m][5],cell_info[m][6],cell_info[m][7],cell_info[m][8]))
tmp.append('\\begin_inset Text') tmp.append('\\begin_inset Text')
tmp.append('') tmp.append('')
tmp.append('\\layout %s' % file.default_layout) tmp.append('\\layout %s' % document.default_layout)
tmp.append('') tmp.append('')
if cell_info[m][0] != '2': if cell_info[m][0] != '2':
@ -179,8 +181,8 @@ def update_tabular(file):
prop_exp = re.compile(r"\\(\S*)\s*(\S*)") prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
def set_paragraph_properties(lines, prop_dict): def set_paragraph_properties(lines, prop_dict):
" Set paragraph properties."
# we need to preserve the order of options # we need to preserve the order of options
properties = ["family","series","shape","size", properties = ["family","series","shape","size",
"emph","bar","noun","latex","color"] "emph","bar","noun","latex","color"]
@ -263,8 +265,10 @@ def set_paragraph_properties(lines, prop_dict):
return result[:] return result[:]
def update_language(file): def update_language(document):
header = file.header " Update document language, if language is default convert it to
english."
header = document.header
i = find_token(header, "\\language", 0) i = find_token(header, "\\language", 0)
if i == -1: if i == -1:
# no language, should emit a warning # no language, should emit a warning
@ -276,6 +280,7 @@ def update_language(file):
return return
supported_versions = ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]
convert = [[217, [update_tabular, update_language]]] convert = [[217, [update_tabular, update_language]]]
revert = [] revert = []

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2002-2004 José Matos <jamatos@lyx.org> # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -16,11 +16,14 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.1.6fix3"""
import re import re
import string import string
from parser_tools import find_token, find_re from parser_tools import find_token, find_re
def bool_table(item): def bool_table(item):
" Convert 0, 1 to false, true."
if item == "0": if item == "0":
return "false" return "false"
# should emit a warning if item != "1" # should emit a warning if item != "1"
@ -32,9 +35,10 @@ align_table = {"0": "top", "2": "left", "4": "right", "8": "center"}
use_table = {"0": "none", "1": "parbox"} use_table = {"0": "none", "1": "parbox"}
table_meta_re = re.compile(r'<LyXTabular version="?1"? rows="?(\d*)"? columns="?(\d*)"?>') table_meta_re = re.compile(r'<LyXTabular version="?1"? rows="?(\d*)"? columns="?(\d*)"?>')
def update_tabular(file): def update_tabular(document):
" Update tabular format to version 2 (xml like syntax)."
regexp = re.compile(r'^\\begin_inset\s+Tabular') regexp = re.compile(r'^\\begin_inset\s+Tabular')
lines = file.body lines = document.body
i=0 i=0
while 1: while 1:
i = find_re(lines, regexp, i) i = find_re(lines, regexp, i)
@ -51,7 +55,7 @@ def update_tabular(file):
j = find_token(lines, '</LyXTabular>', i) + 1 j = find_token(lines, '</LyXTabular>', i) + 1
if j == 0: if j == 0:
file.warning( "Error: Bad lyx format i=%d j=%d" % (i,j)) document.warning( "Error: Bad lyx format i=%d j=%d" % (i,j))
break break
new_table = table_update(lines[i:j]) new_table = table_update(lines[i:j])
@ -65,6 +69,7 @@ features_re = re.compile(r'<features rotate="?(\d)"? islongtable="?(\d)"? endhea
row_re = re.compile(r'<row topline="?(\d)"? bottomline="?(\d)"? newpage="?(\d)"?>') row_re = re.compile(r'<row topline="?(\d)"? bottomline="?(\d)"? newpage="?(\d)"?>')
def table_update(lines): def table_update(lines):
" Update table's internal content to format 2."
lines[1] = string.replace(lines[1], '<Features', '<features') lines[1] = string.replace(lines[1], '<Features', '<features')
res = features_re.match( lines[1] ) res = features_re.match( lines[1] )
if res: if res:
@ -116,6 +121,7 @@ def table_update(lines):
return lines[:2] + col_info + lines[2:] return lines[:2] + col_info + lines[2:]
supported_versions = ["1.1.6fix3","1.1.6fix4","1.1"]
convert = [[218, [update_tabular]]] convert = [[218, [update_tabular]]]
revert = [] revert = []

View File

@ -1,7 +1,7 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2002 Dekel Tsur <dekel@lyx.org> # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
# Copyright (C) 2004 José Matos <jamatos@lyx.org> # Copyright (C) 2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -17,11 +17,14 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.2"""
import string import string
import re import re
from parser_tools import find_token, find_token_backwards, \ from parser_tools import find_token, find_token_backwards, \
find_tokens, find_tokens_backwards, find_beginning_of, find_end_of, find_re, \ find_tokens, find_tokens_backwards,
find_beginning_of, find_end_of, find_re, \
is_nonempty_line, find_nonempty_line, \ is_nonempty_line, find_nonempty_line, \
get_value, check_token get_value, check_token
@ -29,14 +32,15 @@ from parser_tools import find_token, find_token_backwards, \
# Private helper functions # Private helper functions
def get_layout(line, default_layout): def get_layout(line, default_layout):
" Get layout, if empty return the default layout."
tokens = string.split(line) tokens = string.split(line)
if len(tokens) > 1: if len(tokens) > 1:
return tokens[1] return tokens[1]
return default_layout return default_layout
# Finds the paragraph that contains line i.
def get_paragraph(lines, i, format): def get_paragraph(lines, i, format):
" Finds the paragraph that contains line i."
begin_layout = "\\layout" begin_layout = "\\layout"
while i != -1: while i != -1:
@ -48,8 +52,8 @@ def get_paragraph(lines, i, format):
return -1 return -1
# Finds the paragraph after the paragraph that contains line i.
def get_next_paragraph(lines, i, format): def get_next_paragraph(lines, i, format):
" Finds the paragraph after the paragraph that contains line i."
tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"] tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
while i != -1: while i != -1:
@ -61,19 +65,22 @@ def get_next_paragraph(lines, i, format):
def find_beginning_of_inset(lines, i): def find_beginning_of_inset(lines, i):
" Find beginning of inset, where lines[i] is included."
return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset") return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
# Finds the matching \end_inset
def find_end_of_inset(lines, i): def find_end_of_inset(lines, i):
" Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset") return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def find_end_of_tabular(lines, i): def find_end_of_tabular(lines, i):
" Finds the matching end of tabular."
return find_end_of(lines, i, "<lyxtabular", "</lyxtabular") return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
def get_tabular_lines(lines, i): def get_tabular_lines(lines, i):
" Returns a lists of tabular lines."
result = [] result = []
i = i+1 i = i+1
j = find_end_of_tabular(lines, i) j = find_end_of_tabular(lines, i)
@ -126,6 +133,7 @@ pextra_rexp = re.compile(r"\\pextra_type\s+(\S+)"+\
def get_width(mo): def get_width(mo):
" Get width from a regular expression. "
if mo.group(10): if mo.group(10):
if mo.group(9) == "\\pextra_widthp": if mo.group(9) == "\\pextra_widthp":
return mo.group(10)+"col%" return mo.group(10)+"col%"
@ -135,11 +143,9 @@ def get_width(mo):
return "100col%" return "100col%"
# def remove_oldfloat(document):
# Change \begin_float .. \end_float into \begin_inset Float .. \end_inset " Change \begin_float .. \end_float into \begin_inset Float .. \end_inset"
# lines = document.body
def remove_oldfloat(file):
lines = file.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_float", i) i = find_token(lines, "\\begin_float", i)
@ -150,7 +156,7 @@ def remove_oldfloat(file):
floattype = string.split(lines[i])[1] floattype = string.split(lines[i])[1]
if not floats.has_key(floattype): if not floats.has_key(floattype):
file.warning("Error! Unknown float type " + floattype) document.warning("Error! Unknown float type " + floattype)
floattype = "fig" floattype = "fig"
# skip \end_deeper tokens # skip \end_deeper tokens
@ -158,7 +164,7 @@ def remove_oldfloat(file):
while check_token(lines[i2], "\\end_deeper"): while check_token(lines[i2], "\\end_deeper"):
i2 = i2+1 i2 = i2+1
if i2 > i+1: if i2 > i+1:
j2 = get_next_paragraph(lines, j + 1, file.format + 1) j2 = get_next_paragraph(lines, j + 1, document.format + 1)
lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1)) lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
new = floats[floattype]+[""] new = floats[floattype]+[""]
@ -183,7 +189,7 @@ def remove_oldfloat(file):
# as extra '\foo default' commands are ignored. # as extra '\foo default' commands are ignored.
# In fact, it might be safer to output '\foo default' for all # In fact, it might be safer to output '\foo default' for all
# font attributes. # font attributes.
k = get_paragraph(lines, i, file.format + 1) k = get_paragraph(lines, i, document.format + 1)
flag = 0 flag = 0
for token in font_tokens: for token in font_tokens:
if find_token(lines, token, k, i) != -1: if find_token(lines, token, k, i) != -1:
@ -193,7 +199,7 @@ def remove_oldfloat(file):
flag = 1 flag = 1
new.append("") new.append("")
if token == "\\lang": if token == "\\lang":
new.append(token+" "+ file.language) new.append(token+" "+ document.language)
else: else:
new.append(token+" default ") new.append(token+" default ")
@ -205,8 +211,9 @@ pextra_type2_rexp = re.compile(r".*\\pextra_type\s+[12]")
pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)") pextra_type2_rexp2 = re.compile(r".*(\\layout|\\pextra_type\s+2)")
pextra_widthp = re.compile(r"\\pextra_widthp") pextra_widthp = re.compile(r"\\pextra_widthp")
def remove_pextra(file): def remove_pextra(document):
lines = file.body " Remove pextra token."
lines = document.body
i = 0 i = 0
flag = 0 flag = 0
while 1: while 1:
@ -246,10 +253,10 @@ def remove_pextra(file):
if hfill: if hfill:
start = ["","\hfill",""]+start start = ["","\hfill",""]+start
else: else:
start = ['\\layout %s' % file.default_layout,''] + start start = ['\\layout %s' % document.default_layout,''] + start
j0 = find_token_backwards(lines,"\\layout", i-1) j0 = find_token_backwards(lines,"\\layout", i-1)
j = get_next_paragraph(lines, i, file.format + 1) j = get_next_paragraph(lines, i, document.format + 1)
count = 0 count = 0
while 1: while 1:
@ -277,6 +284,7 @@ def remove_pextra(file):
def is_empty(lines): def is_empty(lines):
" Are all the lines empty?"
return filter(is_nonempty_line, lines) == [] return filter(is_nonempty_line, lines) == []
@ -285,13 +293,14 @@ ert_rexp = re.compile(r"\\begin_inset|\\hfill|.*\\SpecialChar")
spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)") spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
def remove_oldert(file): def remove_oldert(document):
" Remove old ERT inset."
ert_begin = ["\\begin_inset ERT", ert_begin = ["\\begin_inset ERT",
"status Collapsed", "status Collapsed",
"", "",
'\\layout %s' % file.default_layout, '\\layout %s' % document.default_layout,
""] ""]
lines = file.body lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i) i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
@ -316,7 +325,7 @@ def remove_oldert(file):
new = [] new = []
new2 = [] new2 = []
if check_token(lines[i], "\\layout LaTeX"): if check_token(lines[i], "\\layout LaTeX"):
new = ['\layout %s' % file.default_layout, "", ""] new = ['\layout %s' % document.default_layout, "", ""]
k = i+1 k = i+1
while 1: while 1:
@ -398,9 +407,9 @@ def remove_oldert(file):
del lines[i] del lines[i]
# ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future. def remove_oldertinset(document):
def remove_oldertinset(file): " ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future."
lines = file.body lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset ERT", i) i = find_token(lines, "\\begin_inset ERT", i)
@ -408,7 +417,7 @@ def remove_oldertinset(file):
break break
j = find_end_of_inset(lines, i) j = find_end_of_inset(lines, i)
k = find_token(lines, "\\layout", i+1) k = find_token(lines, "\\layout", i+1)
l = get_paragraph(lines, i, file.format + 1) l = get_paragraph(lines, i, document.format + 1)
if lines[k] == lines[l]: # same layout if lines[k] == lines[l]: # same layout
k = k+1 k = k+1
new = lines[k:j] new = lines[k:j]
@ -416,11 +425,12 @@ def remove_oldertinset(file):
i = i+1 i = i+1
def is_ert_paragraph(file, i): def is_ert_paragraph(document, i):
lines = file.body " Is this a ert paragraph? "
lines = document.body
if not check_token(lines[i], "\\layout"): if not check_token(lines[i], "\\layout"):
return 0 return 0
if not file.is_default_layout(get_layout(lines[i], file.default_layout)): if not document.is_default_layout(get_layout(lines[i], document.default_layout)):
return 0 return 0
i = find_nonempty_line(lines, i+1) i = find_nonempty_line(lines, i+1)
@ -432,17 +442,18 @@ def is_ert_paragraph(file, i):
return check_token(lines[k], "\\layout") return check_token(lines[k], "\\layout")
def combine_ert(file): def combine_ert(document):
lines = file.body " Combine ERT paragraphs."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset ERT", i) i = find_token(lines, "\\begin_inset ERT", i)
if i == -1: if i == -1:
break break
j = get_paragraph(lines, i, file.format + 1) j = get_paragraph(lines, i, document.format + 1)
count = 0 count = 0
text = [] text = []
while is_ert_paragraph(file, j): while is_ert_paragraph(document, j):
count = count+1 count = count+1
i2 = find_token(lines, "\\layout", j+1) i2 = find_token(lines, "\\layout", j+1)
@ -462,6 +473,7 @@ def combine_ert(file):
oldunits = ["pt", "cm", "in", "text%", "col%"] oldunits = ["pt", "cm", "in", "text%", "col%"]
def get_length(lines, name, start, end): def get_length(lines, name, start, end):
" Get lenght."
i = find_token(lines, name, start, end) i = find_token(lines, name, start, end)
if i == -1: if i == -1:
return "" return ""
@ -470,12 +482,14 @@ def get_length(lines, name, start, end):
def write_attribute(x, token, value): def write_attribute(x, token, value):
" Write attribute."
if value != "": if value != "":
x.append("\t"+token+" "+value) x.append("\t"+token+" "+value)
def remove_figinset(file): def remove_figinset(document):
lines = file.body " Remove figinset."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset Figure", i) i = find_token(lines, "\\begin_inset Figure", i)
@ -543,15 +557,13 @@ def remove_figinset(file):
lines[i:j+1] = new lines[i:j+1] = new
##
# Convert tabular format 2 to 3
#
attr_re = re.compile(r' \w*="(false|0|)"') attr_re = re.compile(r' \w*="(false|0|)"')
line_re = re.compile(r'<(features|column|row|cell)') line_re = re.compile(r'<(features|column|row|cell)')
def update_tabular(file): def update_tabular(document):
" Convert tabular format 2 to 3."
regexp = re.compile(r'^\\begin_inset\s+Tabular') regexp = re.compile(r'^\\begin_inset\s+Tabular')
lines = file.body lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_re(lines, regexp, i) i = find_re(lines, regexp, i)
@ -587,8 +599,8 @@ def update_tabular(file):
false = 0 false = 0
true = 1 true = 1
# simple data structure to deal with long table info
class row: class row:
" Simple data structure to deal with long table info."
def __init__(self): def __init__(self):
self.endhead = false # header row self.endhead = false # header row
self.endfirsthead = false # first header row self.endfirsthead = false # first header row
@ -597,6 +609,7 @@ class row:
def haveLTFoot(row_info): def haveLTFoot(row_info):
" Does row has LTFoot?"
for row_ in row_info: for row_ in row_info:
if row_.endfoot: if row_.endfoot:
return true return true
@ -604,6 +617,7 @@ def haveLTFoot(row_info):
def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info): def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info):
" Set Header/Footer rows."
endfirsthead_empty = false endfirsthead_empty = false
endlastfoot_empty = false endlastfoot_empty = false
# set header info # set header info
@ -670,6 +684,7 @@ def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info):
def insert_attribute(lines, i, attribute): def insert_attribute(lines, i, attribute):
" Insert attribute in lines[i]."
last = string.find(lines[i],'>') last = string.find(lines[i],'>')
lines[i] = lines[i][:last] + ' ' + attribute + lines[i][last:] lines[i] = lines[i][:last] + ' ' + attribute + lines[i][last:]
@ -678,9 +693,10 @@ rows_re = re.compile(r'rows="(\d*)"')
longtable_re = re.compile(r'islongtable="(\w)"') longtable_re = re.compile(r'islongtable="(\w)"')
ltvalues_re = re.compile(r'endhead="(-?\d*)" endfirsthead="(-?\d*)" endfoot="(-?\d*)" endlastfoot="(-?\d*)"') ltvalues_re = re.compile(r'endhead="(-?\d*)" endfirsthead="(-?\d*)" endfoot="(-?\d*)" endlastfoot="(-?\d*)"')
lt_features_re = re.compile(r'(endhead="-?\d*" endfirsthead="-?\d*" endfoot="-?\d*" endlastfoot="-?\d*")') lt_features_re = re.compile(r'(endhead="-?\d*" endfirsthead="-?\d*" endfoot="-?\d*" endlastfoot="-?\d*")')
def update_longtables(file): def update_longtables(document):
" Update longtables to new format."
regexp = re.compile(r'^\\begin_inset\s+Tabular') regexp = re.compile(r'^\\begin_inset\s+Tabular')
body = file.body body = document.body
i = 0 i = 0
while 1: while 1:
i = find_re(body, regexp, i) i = find_re(body, regexp, i)
@ -747,9 +763,9 @@ def update_longtables(file):
i = i + 1 i = i + 1
# Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future. def fix_oldfloatinset(document):
def fix_oldfloatinset(file): " Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future."
lines = file.body lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset Float ", i) i = find_token(lines, "\\begin_inset Float ", i)
@ -761,8 +777,9 @@ def fix_oldfloatinset(file):
i = i+1 i = i+1
def change_listof(file): def change_listof(document):
lines = file.body " Change listof insets."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset LatexCommand \\listof", i) i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
@ -773,8 +790,9 @@ def change_listof(file):
i = i+1 i = i+1
def change_infoinset(file): def change_infoinset(document):
lines = file.body " Change info inset."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset Info", i) i = find_token(lines, "\\begin_inset Info", i)
@ -791,7 +809,7 @@ def change_infoinset(file):
note_lines = [txt]+note_lines note_lines = [txt]+note_lines
for line in note_lines: for line in note_lines:
new = new + ['\layout %s' % file.default_layout, ""] new = new + ['\layout %s' % document.default_layout, ""]
tmp = string.split(line, '\\') tmp = string.split(line, '\\')
new = new + [tmp[0]] new = new + [tmp[0]]
for x in tmp[1:]: for x in tmp[1:]:
@ -800,8 +818,9 @@ def change_infoinset(file):
i = i+5 i = i+5
def change_header(file): def change_header(document):
lines = file.header " Update header."
lines = document.header
i = find_token(lines, "\\use_amsmath", 0) i = find_token(lines, "\\use_amsmath", 0)
if i == -1: if i == -1:
return return
@ -809,6 +828,7 @@ def change_header(file):
"\use_numerical_citations 0"] "\use_numerical_citations 0"]
supported_versions = ["1.2.%d" % i for i in range(5)] + ["1.2"]
convert = [[220, [change_header, change_listof, fix_oldfloatinset, convert = [[220, [change_header, change_listof, fix_oldfloatinset,
update_tabular, update_longtables, remove_pextra, update_tabular, update_longtables, remove_pextra,
remove_oldfloat, remove_figinset, remove_oldertinset, remove_oldfloat, remove_figinset, remove_oldertinset,

View File

@ -1,7 +1,7 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2002 Dekel Tsur <dekel@lyx.org> # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
# Copyright (C) 2004 José Matos <jamatos@lyx.org> # Copyright (C) 2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
@ -17,6 +17,8 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.3"""
import string import string
import re import re
from parser_tools import find_token, find_end_of, get_value,\ from parser_tools import find_token, find_end_of, get_value,\
@ -33,8 +35,9 @@ def find_end_of_inset(lines, i):
#################################################################### ####################################################################
def change_insetgraphics(file): def change_insetgraphics(document):
lines = file.body " Change inset Graphics."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "\\begin_inset Graphics", i) i = find_token(lines, "\\begin_inset Graphics", i)
@ -91,8 +94,9 @@ def change_insetgraphics(file):
i = i+1 i = i+1
def change_tabular(file): def change_tabular(document):
lines = file.body " Change tabular."
lines = document.body
i = 0 i = 0
while 1: while 1:
i = find_token(lines, "<column", i) i = find_token(lines, "<column", i)
@ -103,6 +107,7 @@ def change_tabular(file):
i = i+1 i = i+1
supported_versions = ["1.3.%d" % i for i in range(8)] + ["1.3"]
convert = [[221, [change_insetgraphics, change_tabular]]] convert = [[221, [change_insetgraphics, change_tabular]]]
revert = [] revert = []

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
# This file is part of lyx2lyx # This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2006 José Matos <jamatos@lyx.org> # Copyright (C) 2006 José Matos <jamatos@lyx.org>
# Copyright (C) 2004-2006 Georg Baum <Georg.Baum@post.rwth-aachen.de> # Copyright (C) 2004-2006 Georg Baum <Georg.Baum@post.rwth-aachen.de>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
@ -17,6 +17,8 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Convert files to the file format generated by lyx 1.5"""
import re import re
from parser_tools import find_token, find_token_exact, find_tokens, find_end_of, get_value from parser_tools import find_token, find_token_exact, find_tokens, find_end_of, get_value
from string import replace from string import replace
@ -26,6 +28,7 @@ from string import replace
# Private helper functions # Private helper functions
def find_end_of_inset(lines, i): def find_end_of_inset(lines, i):
" Find beginning of inset, where lines[i] is included."
return find_end_of(lines, i, "\\begin_inset", "\\end_inset") return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
# End of helper functions # End of helper functions
@ -36,14 +39,15 @@ def find_end_of_inset(lines, i):
# Notes: Framed/Shaded # Notes: Framed/Shaded
# #
def revert_framed(file): def revert_framed(document):
"Revert framed notes. "
i = 0 i = 0
while 1: while 1:
i = find_tokens(file.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i) i = find_tokens(document.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
if i == -1: if i == -1:
return return
file.body[i] = "\\begin_inset Note" document.body[i] = "\\begin_inset Note"
i = i + 1 i = i + 1
@ -67,20 +71,21 @@ typewriter_fonts = {'default' : 'default', 'ae' : 'default',
'newcent' : 'default', 'bookman' : 'default', 'newcent' : 'default', 'bookman' : 'default',
'pslatex' : 'courier'} 'pslatex' : 'courier'}
def convert_font_settings(file): def convert_font_settings(document):
" Convert font settings. "
i = 0 i = 0
i = find_token_exact(file.header, "\\fontscheme", i) i = find_token_exact(document.header, "\\fontscheme", i)
if i == -1: if i == -1:
file.warning("Malformed LyX file: Missing `\\fontscheme'.") document.warning("Malformed LyX document: Missing `\\fontscheme'.")
return return
font_scheme = get_value(file.header, "\\fontscheme", i, i + 1) font_scheme = get_value(document.header, "\\fontscheme", i, i + 1)
if font_scheme == '': if font_scheme == '':
file.warning("Malformed LyX file: Empty `\\fontscheme'.") document.warning("Malformed LyX document: Empty `\\fontscheme'.")
font_scheme = 'default' font_scheme = 'default'
if not font_scheme in roman_fonts.keys(): if not font_scheme in roman_fonts.keys():
file.warning("Malformed LyX file: Unknown `\\fontscheme' `%s'." % font_scheme) document.warning("Malformed LyX document: Unknown `\\fontscheme' `%s'." % font_scheme)
font_scheme = 'default' font_scheme = 'default'
file.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme], document.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
'\\font_sans %s' % sans_fonts[font_scheme], '\\font_sans %s' % sans_fonts[font_scheme],
'\\font_typewriter %s' % typewriter_fonts[font_scheme], '\\font_typewriter %s' % typewriter_fonts[font_scheme],
'\\font_default_family default', '\\font_default_family default',
@ -90,124 +95,125 @@ def convert_font_settings(file):
'\\font_tt_scale 100'] '\\font_tt_scale 100']
def revert_font_settings(file): def revert_font_settings(document):
" Revert font settings. "
i = 0 i = 0
insert_line = -1 insert_line = -1
fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'} fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
for family in 'roman', 'sans', 'typewriter': for family in 'roman', 'sans', 'typewriter':
name = '\\font_%s' % family name = '\\font_%s' % family
i = find_token_exact(file.header, name, i) i = find_token_exact(document.header, name, i)
if i == -1: if i == -1:
file.warning("Malformed LyX file: Missing `%s'." % name) document.warning("Malformed LyX document: Missing `%s'." % name)
i = 0 i = 0
else: else:
if (insert_line < 0): if (insert_line < 0):
insert_line = i insert_line = i
fonts[family] = get_value(file.header, name, i, i + 1) fonts[family] = get_value(document.header, name, i, i + 1)
del file.header[i] del document.header[i]
i = find_token_exact(file.header, '\\font_default_family', i) i = find_token_exact(document.header, '\\font_default_family', i)
if i == -1: if i == -1:
file.warning("Malformed LyX file: Missing `\\font_default_family'.") document.warning("Malformed LyX document: Missing `\\font_default_family'.")
font_default_family = 'default' font_default_family = 'default'
else: else:
font_default_family = get_value(file.header, "\\font_default_family", i, i + 1) font_default_family = get_value(document.header, "\\font_default_family", i, i + 1)
del file.header[i] del document.header[i]
i = find_token_exact(file.header, '\\font_sc', i) i = find_token_exact(document.header, '\\font_sc', i)
if i == -1: if i == -1:
file.warning("Malformed LyX file: Missing `\\font_sc'.") document.warning("Malformed LyX document: Missing `\\font_sc'.")
font_sc = 'false' font_sc = 'false'
else: else:
font_sc = get_value(file.header, '\\font_sc', i, i + 1) font_sc = get_value(document.header, '\\font_sc', i, i + 1)
del file.header[i] del document.header[i]
if font_sc != 'false': if font_sc != 'false':
file.warning("Conversion of '\\font_sc' not yet implemented.") document.warning("Conversion of '\\font_sc' not yet implemented.")
i = find_token_exact(file.header, '\\font_osf', i) i = find_token_exact(document.header, '\\font_osf', i)
if i == -1: if i == -1:
file.warning("Malformed LyX file: Missing `\\font_osf'.") document.warning("Malformed LyX document: Missing `\\font_osf'.")
font_osf = 'false' font_osf = 'false'
else: else:
font_osf = get_value(file.header, '\\font_osf', i, i + 1) font_osf = get_value(document.header, '\\font_osf', i, i + 1)
del file.header[i] del document.header[i]
i = find_token_exact(file.header, '\\font_sf_scale', i) i = find_token_exact(document.header, '\\font_sf_scale', i)
if i == -1: if i == -1:
file.warning("Malformed LyX file: Missing `\\font_sf_scale'.") document.warning("Malformed LyX document: Missing `\\font_sf_scale'.")
font_sf_scale = '100' font_sf_scale = '100'
else: else:
font_sf_scale = get_value(file.header, '\\font_sf_scale', i, i + 1) font_sf_scale = get_value(document.header, '\\font_sf_scale', i, i + 1)
del file.header[i] del document.header[i]
if font_sf_scale != '100': if font_sf_scale != '100':
file.warning("Conversion of '\\font_sf_scale' not yet implemented.") document.warning("Conversion of '\\font_sf_scale' not yet implemented.")
i = find_token_exact(file.header, '\\font_tt_scale', i) i = find_token_exact(document.header, '\\font_tt_scale', i)
if i == -1: if i == -1:
file.warning("Malformed LyX file: Missing `\\font_tt_scale'.") document.warning("Malformed LyX document: Missing `\\font_tt_scale'.")
font_tt_scale = '100' font_tt_scale = '100'
else: else:
font_tt_scale = get_value(file.header, '\\font_tt_scale', i, i + 1) font_tt_scale = get_value(document.header, '\\font_tt_scale', i, i + 1)
del file.header[i] del document.header[i]
if font_tt_scale != '100': if font_tt_scale != '100':
file.warning("Conversion of '\\font_tt_scale' not yet implemented.") document.warning("Conversion of '\\font_tt_scale' not yet implemented.")
for font_scheme in roman_fonts.keys(): for font_scheme in roman_fonts.keys():
if (roman_fonts[font_scheme] == fonts['roman'] and if (roman_fonts[font_scheme] == fonts['roman'] and
sans_fonts[font_scheme] == fonts['sans'] and sans_fonts[font_scheme] == fonts['sans'] and
typewriter_fonts[font_scheme] == fonts['typewriter']): typewriter_fonts[font_scheme] == fonts['typewriter']):
file.header.insert(insert_line, '\\fontscheme %s' % font_scheme) document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
if font_default_family != 'default': if font_default_family != 'default':
file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family) document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
if font_osf == 'true': if font_osf == 'true':
file.warning("Ignoring `\\font_osf = true'") document.warning("Ignoring `\\font_osf = true'")
return return
font_scheme = 'default' font_scheme = 'default'
file.header.insert(insert_line, '\\fontscheme %s' % font_scheme) document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
if fonts['roman'] == 'cmr': if fonts['roman'] == 'cmr':
file.preamble.append('\\renewcommand{\\rmdefault}{cmr}') document.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
if font_osf == 'true': if font_osf == 'true':
file.preamble.append('\\usepackage{eco}') document.preamble.append('\\usepackage{eco}')
font_osf = 'false' font_osf = 'false'
for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery': for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
if fonts['roman'] == font: if fonts['roman'] == font:
file.preamble.append('\\usepackage{%s}' % font) document.preamble.append('\\usepackage{%s}' % font)
for font in 'cmss', 'lmss', 'cmbr': for font in 'cmss', 'lmss', 'cmbr':
if fonts['sans'] == font: if fonts['sans'] == font:
file.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font) document.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
for font in 'berasans': for font in 'berasans':
if fonts['sans'] == font: if fonts['sans'] == font:
file.preamble.append('\\usepackage{%s}' % font) document.preamble.append('\\usepackage{%s}' % font)
for font in 'cmtt', 'lmtt', 'cmtl': for font in 'cmtt', 'lmtt', 'cmtl':
if fonts['typewriter'] == font: if fonts['typewriter'] == font:
file.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font) document.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
for font in 'courier', 'beramono', 'luximono': for font in 'courier', 'beramono', 'luximono':
if fonts['typewriter'] == font: if fonts['typewriter'] == font:
file.preamble.append('\\usepackage{%s}' % font) document.preamble.append('\\usepackage{%s}' % font)
if font_default_family != 'default': if font_default_family != 'default':
file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family) document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
if font_osf == 'true': if font_osf == 'true':
file.warning("Ignoring `\\font_osf = true'") document.warning("Ignoring `\\font_osf = true'")
def revert_booktabs(file): def revert_booktabs(document):
# we just remove the booktabs flag, everything else will become a mess. " We remove the booktabs flag or everything else will become a mess. "
re_row = re.compile(r'^<row.*space="[^"]+".*>$') re_row = re.compile(r'^<row.*space="[^"]+".*>$')
re_tspace = re.compile(r'\s+topspace="[^"]+"') re_tspace = re.compile(r'\s+topspace="[^"]+"')
re_bspace = re.compile(r'\s+bottomspace="[^"]+"') re_bspace = re.compile(r'\s+bottomspace="[^"]+"')
re_ispace = re.compile(r'\s+interlinespace="[^"]+"') re_ispace = re.compile(r'\s+interlinespace="[^"]+"')
i = 0 i = 0
while 1: while 1:
i = find_token(file.body, "\\begin_inset Tabular", i) i = find_token(document.body, "\\begin_inset Tabular", i)
if i == -1: if i == -1:
return return
j = find_end_of_inset(file.body, i + 1) j = find_end_of_inset(document.body, i + 1)
if j == -1: if j == -1:
file.warning("Malformed LyX file: Could not find end of tabular.") document.warning("Malformed LyX document: Could not find end of tabular.")
continue continue
for k in range(i, j): for k in range(i, j):
if re.search('^<features.* booktabs="true".*>$', file.body[k]): if re.search('^<features.* booktabs="true".*>$', document.body[k]):
file.warning("Converting 'booktabs' table to normal table.") document.warning("Converting 'booktabs' table to normal table.")
file.body[k] = replace(file.body[k], ' booktabs="true"', '') document.body[k] = replace(document.body[k], ' booktabs="true"', '')
if re.search(re_row, file.body[k]): if re.search(re_row, document.body[k]):
file.warning("Removing extra row space.") document.warning("Removing extra row space.")
file.body[k] = re_tspace.sub('', file.body[k]) document.body[k] = re_tspace.sub('', document.body[k])
file.body[k] = re_bspace.sub('', file.body[k]) document.body[k] = re_bspace.sub('', document.body[k])
file.body[k] = re_ispace.sub('', file.body[k]) document.body[k] = re_ispace.sub('', document.body[k])
i = i + 1 i = i + 1
@ -215,6 +221,7 @@ def revert_booktabs(file):
# Conversion hub # Conversion hub
# #
supported_versions = ["1.5.0svn","1.5"]
convert = [[246, []], convert = [[246, []],
[247, [convert_font_settings]], [247, [convert_font_settings]],
[248, []]] [248, []]]

View File

@ -1,6 +1,6 @@
#! /usr/bin/env python #! /usr/bin/env python
# -*- coding: iso-8859-1 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2004 José Matos <jamatos@lyx.org> # Copyright (C) 2004 José Matos <jamatos@lyx.org>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License