Reorganised lyx2lyx, transfering it knowleadge of the file format to LyX.py,

and using it as library. The lyx2lyx script now becomes a simple cliente.
Simple fix in the UserGuide.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8941 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
José Matox 2004-08-15 16:29:04 +00:00
parent 93430a89da
commit 6c79118290
17 changed files with 536 additions and 510 deletions

View File

@ -836,6 +836,7 @@ dist_lyx2lyx_SCRIPTS = lyx2lyx/lyx2lyx
dist_lyx2lyx_DATA = \ dist_lyx2lyx_DATA = \
lyx2lyx/lyx2lyx \ lyx2lyx/lyx2lyx \
lyx2lyx/parser_tools.py \ lyx2lyx/parser_tools.py \
LyX.py \
lyx2lyx/lyx_0_12.py \ lyx2lyx/lyx_0_12.py \
lyx2lyx/lyx_1_0_0.py \ lyx2lyx/lyx_1_0_0.py \
lyx2lyx/lyx_1_0_1.py \ lyx2lyx/lyx_1_0_1.py \

View File

@ -1,3 +1,8 @@
2004.08-15 José Matos <jamatos@lyx.org>
* UserGuide.lyx: replaced ERT (\newline) with the hyphenisation
inset.
2004-08-13 Jean-Marc Lasgouttes <lasgouttes@lyx.org> 2004-08-13 Jean-Marc Lasgouttes <lasgouttes@lyx.org>
* Makefile.am (docfiles): add Customization.lyx * Makefile.am (docfiles): add Customization.lyx

View File

@ -1661,17 +1661,7 @@ T
ools\SpecialChar \menuseparator ools\SpecialChar \menuseparator
\bar under \bar under
-P
\begin_inset ERT
status Collapsed
\layout Standard
\backslash
linebreak
\end_inset
P
\bar default \bar default
references references
\family default \family default

View File

@ -1,4 +1,19 @@
2004-08-15 José Matos <jamatos@lyx.org>
* LyX.py: new file that gathers all the knowleadge of the LyX file
format. This can be used as library.
* parser_tools.py: moved all the functions that are not related
with the parser to LyX.py.
* lyx2lyx: is just a client of the LyX.py library.
* lyx_.py (convert, revert): renamed previous opt to file. Change
the arguments since now file contains both the header and the
body.
2004-08-14 José Matos <jamatos@lyx.org> 2004-08-14 José Matos <jamatos@lyx.org>
* lyx_1_4.py: * lyx_1_4.py:
* parser_tools.py: up the format to 236. * parser_tools.py: up the format to 236.
@ -52,10 +67,12 @@
* lyx2lyx: up the format to 233. * lyx2lyx: up the format to 233.
2004-04-19 José Matos <jamatos@lyx.orrg> 2004-04-19 José Matos <jamatos@lyx.orrg>
* parser_tools.py (chain): fix the detection of the last format for * parser_tools.py (chain): fix the detection of the last format for
revertions. revertions.
2004-04-19 Martin Vermeer <martin.vermeer@hut.fi> 2004-04-19 Martin Vermeer <martin.vermeer@hut.fi>
* lyx_1_4.py (convert_frameless_box): Replace instead of adding * lyx_1_4.py (convert_frameless_box): Replace instead of adding
new code. new code.
. .

276
lib/lyx2lyx/LyX.py Normal file
View File

@ -0,0 +1,276 @@
# This file is part of lyx2lyx
# -*- coding: iso-8859-1 -*-
# 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
# 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.
from parser_tools import get_value, check_token, find_token
import os.path
import gzip
import sys
import re
import string
##
# file format version
#
version = "1.4.0cvs"
default_debug_level = 2
format_re = re.compile(r"(\d)[\.,]?(\d\d)")
fileformat = re.compile(r"\\lyxformat\s*(\S*)")
original_version = re.compile(r"\#LyX (\S*)")
lst_ft = [210, 215, 216, 217, 218, 220, 221, 223, 224, 225, 226, 227, 228, 229,
230, 231, 232, 233, 234, 235, 236]
format_relation = [("0_10", [210], ["0.10.7","0.10"]),
("0_12", [215], ["0.12","0.12.1","0.12"]),
("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_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_6", [217], ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]),
("1_1_6fix3", [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_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3"]),
("1_4", [223,224,225,226,227,228,229,230,231,232,233,234,235, 236], ["1.4.0cvs","1.4"])]
def get_end_format():
return format_relation[-1:][0][1][-1:][0]
def get_backend(textclass):
if textclass == "linuxdoc" or textclass == "manpage":
return "linuxdoc"
if textclass[:7] == "docbook":
return "docbook"
return "latex"
##
# Class
#
class FileInfo:
"""This class carries all the information of the LyX file."""
def __init__(self, end_format = 0, input = "", output = "", error = "", debug = default_debug_level):
if input:
self.input = self.open(input)
else:
self.input = sys.stdin
if output:
self.output = open(output, "w")
else:
self.output = sys.stdout
if error:
self.err = open(error, "w")
else:
self.err = sys.stderr
self.debug = debug
if end_format:
self.end_format = self.lyxformat(end_format)
else:
self.end_format = get_end_format()
self.backend = "latex"
self.textclass = "article"
self.header = []
self.body = []
self.read()
def warning(self, message, debug_level= default_debug_level):
if debug_level <= self.debug:
self.err.write(message + "\n")
def error(self, message):
self.warning(message)
self.warning("Quiting.")
sys.exit(1)
def read(self):
"""Reads a file into the self.header and self.body parts"""
preamble = 0
while 1:
line = self.input.readline()
if not line:
self.error("Invalid LyX file.")
line = line[:-1]
# remove '\r' from line's end, if present
if line[-1:] == '\r':
line = line[:-1]
if check_token(line, '\\begin_preamble'):
preamble = 1
if check_token(line, '\\end_preamble'):
preamble = 0
if not preamble:
line = string.strip(line)
if not line and not preamble:
break
self.header.append(line)
while 1:
line = self.input.readline()
if not line:
break
# remove '\r' from line's end, if present
if line[-2:-1] == '\r':
self.body.append(line[:-2])
else:
self.body.append(line[:-1])
self.textclass = get_value(self.header, "\\textclass", 0)
self.backend = get_backend(self.textclass)
self.format = self.read_format()
self.language = get_value(self.header, "\\language", 0)
if self.language == "":
self.language = "english"
self.initial_version = self.read_version()
def write(self):
self.set_version()
self.set_format()
for line in self.header:
self.output.write(line+"\n")
self.output.write("\n")
for line in self.body:
self.output.write(line+"\n")
def open(self, file):
"""Transparently deals with compressed files."""
self.dir = os.path.dirname(os.path.abspath(file))
try:
gzip.open(file).readline()
self.output = gzip.GzipFile("","wb",6,self.output)
return gzip.open(file)
except:
return open(file)
def lyxformat(self, format):
result = format_re.match(format)
if result:
format = int(result.group(1) + result.group(2))
else:
self.error(str(format) + ": " + "Invalid LyX file.")
if format in lst_ft:
return format
self.error(str(format) + ": " + "Format not supported.")
return None
def read_version(self):
for line in self.header:
if line[0] != "#":
return None
result = original_version.match(line)
if result:
return result.group(1)
return None
def set_version(self):
self.header[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
if self.header[1][0] == '#':
del self.header[1]
def read_format(self):
for line in self.header:
result = fileformat.match(line)
if result:
return self.lyxformat(result.group(1))
else:
self.error("Invalid LyX File.")
return None
def set_format(self):
if int(self.format) <= 217:
format = str(float(format)/100)
else:
format = self.format
i = find_token(self.header, "\\lyxformat", 0)
self.header[i] = "\\lyxformat %s" % format
def chain(self):
""" This is where all the decisions related with the convertion are taken"""
self.start = self.format
format = self.format
correct_version = 0
for rel in format_relation:
if self.initial_version in rel[2]:
if format in rel[1]:
initial_step = rel[0]
correct_version = 1
break
if not correct_version:
if format <= 215:
self.warning("Version does not match file format, discarding it.")
for rel in format_relation:
if format in rel[1]:
initial_step = rel[0]
break
else:
# This should not happen, really.
self.error("Format not supported.")
# Find the final step
for rel in format_relation:
if self.end_format in rel[1]:
final_step = rel[0]
break
else:
self.error("Format not supported.")
# Convertion mode, back or forth
steps = []
if (initial_step, self.start) < (final_step, self.end_format):
mode = "convert"
first_step = 1
for step in format_relation:
if initial_step <= step[0] <= final_step:
if first_step and len(step[1]) == 1:
first_step = 0
continue
steps.append(step[0])
else:
mode = "revert"
relation_format = format_relation
relation_format.reverse()
last_step = None
for step in relation_format:
if final_step <= step[0] <= initial_step:
steps.append(step[0])
last_step = step
if last_step[1][-1] == self.end_format:
steps.pop()
return mode, steps

View File

@ -17,36 +17,8 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import getopt import getopt
import gzip
import sys import sys
import os.path import LyX
from parser_tools import read_file, write_file, read_version, set_version, \
read_format, set_format, chain, lyxformat, get_value, get_backend
# Allow the dummy object to be able to carry related data
# like a C struct
class struct:
def __init__(self):
self.output = sys.stdout
self.input = sys.stdin
self.err = sys.stderr
self.debug = 1
self.dir = ""
self.start = None
self.end = None
self.backend = "latex"
self.textclass = "article"
def warning(self, message, debug_level= 1):
if debug_level <= self.debug:
self.err.write(message + "\n")
def error(self, message):
self.warning(message)
self.warning("Quiting.")
sys.exit(1)
def usage(): def usage():
print """Usage: lyx2lyx [options] [file] print """Usage: lyx2lyx [options] [file]
@ -60,13 +32,12 @@ Options:
-d, --debug level level=0..2 (O_ no debug information, 2_verbose) -d, --debug level level=0..2 (O_ no debug information, 2_verbose)
default: level=1 default: level=1
-e, --err error_file name of the error file or else goes to stderr -e, --err error_file name of the error file or else goes to stderr
-f, --from version initial version (optional)
-t, --to version final version (optional) -t, --to version final version (optional)
-o, --output name name of the output file or else goes to stdout -o, --output name name of the output file or else goes to stdout
-q, --quiet same as --debug=0""" -q, --quiet same as --debug=0"""
def parse_options(argv, version, opt): def parse_options(argv):
_options = ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "quiet"] _options = ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "quiet"]
try: try:
opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options) opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options)
@ -74,73 +45,43 @@ def parse_options(argv, version, opt):
usage() usage()
sys.exit(2) sys.exit(2)
end_format, input, output, error, debug = 0, "", "", "", LyX.default_debug_level
for o, a in opts: for o, a in opts:
if o in ("-h", "--help"): if o in ("-h", "--help"):
usage() usage()
sys.exit() sys.exit()
if o in ("-v", "--version"): if o in ("-v", "--version"):
print "lyx2lyx, version %s" %(version) print "lyx2lyx, version %s" %(LyX.version)
print "Copyright (C) 2002-2004 José Matos and Dekel Tsur" print "Copyright (C) 2002-2004 José Matos and Dekel Tsur"
sys.exit() sys.exit()
if o in ("-d", "--debug"): if o in ("-d", "--debug"):
opt.debug = int(a) debug = int(a)
if o in ("-q", "--quiet"): if o in ("-q", "--quiet"):
opt.debug = 0 debug = 0
if o in ("-l", "--list"): if o in ("-l", "--list"):
# list available formats # list available formats
sys.exit() sys.exit()
if o in ("-o", "--output"): if o in ("-o", "--output"):
opt.output = open(a, "w") output = a
if o in ("-f", "--from"):
opt.start = lyxformat(a, opt)
if o in ("-t", "--to"): if o in ("-t", "--to"):
opt.end = lyxformat(a, opt) end_format = a
if o in ("-e","--err"): if o in ("-e","--err"):
opt.err = open(a, "w") error = a
if args: if args:
file = args[0] input = args[0]
opt.dir = os.path.dirname(os.path.abspath(file))
try:
gzip.open(file).readline()
opt.output = gzip.GzipFile("","wb",6,opt.output)
opt.input = gzip.open(file)
except:
opt.input = open(file)
return end_format, input, output, error, debug
def main(argv):
version = "1.4.0cvs"
# options object, with default values
opt = struct()
parse_options(argv, version, opt)
header, body = [], []
read_file(header, body, opt)
initial_version = read_version(header)
opt.textclass = get_value(header, "\\textclass", 0)
opt.backend = get_backend( opt.textclass)
opt.format = read_format(header, opt)
opt.language = get_value(header, "\\language", 0)
if opt.language == "":
opt.language = "english"
mode, convertion_chain = chain(opt, initial_version)
opt.warning("convertion chain: " + str(convertion_chain), 3)
for step in convertion_chain:
convert = getattr(__import__("lyx_" + step), mode)
convert(header,body, opt)
set_version(header, version)
set_format(header, opt.format)
write_file(header, body, opt)
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) end_format, input, output, error, debug = parse_options(sys.argv)
file = LyX.FileInfo(end_format, input, output, error, debug)
mode, convertion_chain = file.chain()
file.warning("convertion chain: " + str(convertion_chain), 3)
for step in convertion_chain:
convert = getattr(__import__("lyx_" + step), mode)
convert(file)
file.write()

View File

@ -177,14 +177,14 @@ def add_end_document(lines):
lines.append('\\the_end') lines.append('\\the_end')
def header_update(lines, opt): def header_update(lines, file):
i = 0 i = 0
l = len(lines) l = len(lines)
while i < l: while i < l:
if check_token(lines[i], '\\begin_preamble'): if check_token(lines[i], '\\begin_preamble'):
i = find_token(lines, '\\end_preamble', i) i = find_token(lines, '\\end_preamble', i)
if i == -1: if i == -1:
opt.error('Unfinished preamble') file.error('Unfinished preamble')
i = i + 1 i = i + 1
continue continue
@ -236,26 +236,26 @@ def header_update(lines, opt):
i = i + 1 i = i + 1
def convert(header,body, opt): def convert(file):
header_update(header, opt) header_update(file.header, file)
add_end_document(body) add_end_document(file.body)
remove_cursor(body) remove_cursor(file.body)
final_dot(body) final_dot(file.body)
update_inset_label(body) update_inset_label(file.body)
update_latexdel(body) update_latexdel(file.body)
update_space_units(body) update_space_units(file.body)
update_inset_accent(body) update_inset_accent(file.body)
space_before_layout(body) space_before_layout(file.body)
formula_inset_space_eat(body) formula_inset_space_eat(file.body)
update_tabular(body) update_tabular(file.body)
update_vfill(body) update_vfill(file.body)
remove_empty_insets(body) remove_empty_insets(file.body)
remove_formula_latex(body) remove_formula_latex(file.body)
opt.format = 215 file.format = 215
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -16,12 +16,12 @@
# 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.
def convert(header, body, opt): def convert(file):
opt.format = 215 file.format = 215
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file .format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -16,12 +16,12 @@
# 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.
def convert(header, body, opt): def convert(file):
opt.format = 215 file.format = 215
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -16,12 +16,12 @@
# 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.
def convert(header, body, opt): def convert(file):
opt.format = 215 file.format = 215
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -147,20 +147,20 @@ def remove_space_in_units(lines):
i = i + 1 i = i + 1
def convert(header, body, opt): def convert(file):
first_layout(body) first_layout(file.body)
remove_vcid(header) remove_vcid(file.header)
remove_cursor(body) remove_cursor(file.body)
update_toc(body) update_toc(file.body)
replace_protected_separator(body) replace_protected_separator(file.body)
merge_formula_inset(body) merge_formula_inset(file.body)
update_tabular(body) update_tabular(file.body)
remove_space_in_units(header) remove_space_in_units(file.header)
opt.format = 216 file.format = 216
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":
pass pass

View File

@ -274,14 +274,14 @@ def update_language(header):
return return
def convert(header, body, opt): def convert(file):
update_tabular(body, opt) update_tabular(file.body, file)
update_language(header) update_language(file.header)
opt.format = 217 file.format = 217
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -113,13 +113,13 @@ def table_update(lines):
return lines[:2] + col_info + lines[2:] return lines[:2] + col_info + lines[2:]
def convert(header, body, opt): def convert(file):
update_tabular(body, opt) update_tabular(file.body, file)
opt.format = 218 file.format = 218
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -540,23 +540,23 @@ def change_preamble(lines):
"\use_numerical_citations 0"] "\use_numerical_citations 0"]
def convert(header, body, opt): def convert(file):
change_preamble(header) change_preamble(file.header)
change_listof(body) change_listof(file.body)
fix_oldfloatinset(body) fix_oldfloatinset(file.body)
update_tabular(body) update_tabular(file.body)
remove_pextra(body) remove_pextra(file.body)
remove_oldfloat(body, opt) remove_oldfloat(file.body, opt)
remove_figinset(body) remove_figinset(file.body)
remove_oldertinset(body) remove_oldertinset(file.body)
remove_oldert(body) remove_oldert(file.body)
combine_ert(body) combine_ert(file.body)
change_infoinset(body) change_infoinset(file.body)
opt.format = 220 file.format = 220
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -90,14 +90,14 @@ def change_tabular(lines):
i = i+1 i = i+1
def convert(header, body, opt): def convert(file):
change_insetgraphics(body) change_insetgraphics(file.body)
change_tabular(body) change_tabular(file.body)
opt.format = 221 file.format = 221
def revert(header, body, opt): def revert(file):
opt.error("The convertion to an older format (%s) is not implemented." % opt.format) file.error("The convertion to an older format (%s) is not implemented." % file.format)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1447,171 +1447,171 @@ def remove_begin_body(body, opt):
## ##
# Convertion hub # Convertion hub
# #
def convert(header, body, opt): def convert(file):
if opt.format < 223: if file.format < 223:
insert_tracking_changes(header) insert_tracking_changes(file.header)
add_end_header(header) add_end_header(file.header)
remove_color_default(body) remove_color_default(file.body)
convert_spaces(body) convert_spaces(file.body)
convert_bibtex(body) convert_bibtex(file.body)
remove_insetparent(body) remove_insetparent(file.body)
opt.format = 223 file.format = 223
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 224: if file.format < 224:
convert_external(body) convert_external(file.body)
convert_comment(body) convert_comment(file.body)
opt.format = 224 file.format = 224
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 225: if file.format < 225:
add_end_layout(body) add_end_layout(file.body)
layout2begin_layout(body) layout2begin_layout(file.body)
convert_end_document(body) convert_end_document(file.body)
convert_table_valignment_middle(body) convert_table_valignment_middle(file.body)
convert_breaks(body) convert_breaks(file.body)
opt.format = 225 file.format = 225
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 226: if file.format < 226:
convert_note(body) convert_note(file.body)
opt.format = 226 file.format = 226
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 227: if file.format < 227:
convert_box(body) convert_box(file.body)
opt.format = 227 file.format = 227
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 228: if file.format < 228:
convert_collapsable(body, opt) convert_collapsable(file.body, file)
convert_ert(body, opt) convert_ert(file.body, file)
opt.format = 228 file.format = 228
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 229: if file.format < 229:
convert_minipage(body) convert_minipage(file.body)
opt.format = 229 file.format = 229
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 230: if file.format < 230:
convert_jurabib(header, opt) convert_jurabib(file.header, file)
opt.format = 230 file.format = 230
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 231: if file.format < 231:
convert_float(body, opt) convert_float(file.body, file)
opt.format = 231 file.format = 231
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 232: if file.format < 232:
convert_bibtopic(header, opt) convert_bibtopic(file.header, file)
opt.format = 232 file.format = 232
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 233: if file.format < 233:
convert_graphics(body, opt) convert_graphics(file.body, file)
convert_names(body, opt) convert_names(file.body, file)
opt.format = 233 file.format = 233
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 234: if file.format < 234:
convert_cite_engine(header, opt) convert_cite_engine(file.header, file)
opt.format = 234 file.format = 234
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 235: if file.format < 235:
convert_paperpackage(header, opt) convert_paperpackage(file.header, file)
opt.format = 235 file.format = 235
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format < 236: if file.format < 236:
convert_bullets(header, opt) convert_bullets(file.header, file)
add_begin_header(header, opt) add_begin_header(file.header, file)
add_begin_body(body, opt) add_begin_body(file.body, file)
opt.format = 236 file.format = 236
def revert(header, body, opt): def revert(file):
if opt.format > 235: if file.format > 235:
remove_begin_body(body, opt) remove_begin_body(file.body, file)
remove_begin_header(header, opt) remove_begin_header(file.header, file)
revert_bullets(header, opt) revert_bullets(file.header, file)
opt.format = 235 file.format = 235
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 234: if file.format > 234:
revert_paperpackage(header, opt) revert_paperpackage(file.header, file)
opt.format = 234 file.format = 234
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 233: if file.format > 233:
revert_cite_engine(header, opt) revert_cite_engine(file.header, file)
opt.format = 233 file.format = 233
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 232: if file.format > 232:
revert_names(body, opt) revert_names(file.body, file)
opt.format = 232 file.format = 232
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 231: if file.format > 231:
revert_bibtopic(header, opt) revert_bibtopic(file.header, file)
opt.format = 231 file.format = 231
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 230: if file.format > 230:
revert_float(body, opt) revert_float(file.body, file)
opt.format = 230 file.format = 230
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 229: if file.format > 229:
revert_jurabib(header, opt) revert_jurabib(file.header, file)
opt.format = 229 file.format = 229
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 228: if file.format > 228:
opt.format = 228 file.format = 228
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 227: if file.format > 227:
revert_collapsable(body, opt) revert_collapsable(file.body, file)
revert_ert(body, opt) revert_ert(file.body, file)
opt.format = 227 file.format = 227
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 226: if file.format > 226:
revert_box(body) revert_box(file.body)
revert_external_2(body) revert_external_2(file.body)
opt.format = 226 file.format = 226
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 225: if file.format > 225:
revert_note(body) revert_note(file.body)
opt.format = 225 file.format = 225
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 224: if file.format > 224:
rm_end_layout(body) rm_end_layout(file.body)
begin_layout2layout(body) begin_layout2layout(file.body)
revert_end_document(body) revert_end_document(file.body)
revert_valignment_middle(body) revert_valignment_middle(file.body)
convert_vspace(header, body, opt) convert_vspace(file.header, file.body, file)
convert_frameless_box(body, opt) convert_frameless_box(file.body, file)
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 223: if file.format > 223:
revert_external_2(body) revert_external_2(file.body)
revert_comment(body) revert_comment(file.body)
opt.format = 223 file.format = 223
if opt.end == opt.format: return if file.end_format == file.format: return
if opt.format > 221: if file.format > 221:
rm_end_header(header) rm_end_header(file.header)
revert_spaces(body) revert_spaces(file.body)
revert_bibtex(body) revert_bibtex(file.body)
rm_tracking_changes(header) rm_tracking_changes(file.header)
rm_body_changes(body) rm_file.body_changes(file.body)
opt.format = 221 file.format = 221
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -19,7 +19,6 @@
import string import string
import re import re
def check_token(line, token): def check_token(line, token):
if line[:len(token)] == token: if line[:len(token)] == token:
return 1 return 1
@ -195,206 +194,3 @@ def find_nonempty_line(lines, start, end = 0):
if is_nonempty_line(lines[i]): if is_nonempty_line(lines[i]):
return i return i
return -1 return -1
##
# Tools for file reading
#
def read_file(header, body, opt):
"""Reads a file into the header and body parts"""
preamble = 0
while 1:
line = opt.input.readline()
if not line:
opt.error("Invalid LyX file.")
line = line[:-1]
# remove '\r' from line's end, if present
if line[-1:] == '\r':
line = line[:-1]
if check_token(line, '\\begin_preamble'):
preamble = 1
if check_token(line, '\\end_preamble'):
preamble = 0
if not preamble:
line = string.strip(line)
if not line and not preamble:
break
header.append(line)
while 1:
line = opt.input.readline()
if not line:
break
# remove '\r' from line's end, if present
if line[-2:-1] == '\r':
body.append(line[:-2])
else:
body.append(line[:-1])
def write_file(header, body, opt):
for line in header:
opt.output.write(line+"\n")
opt.output.write("\n")
for line in body:
opt.output.write(line+"\n")
##
# lyx version
#
original_version = re.compile(r"\#LyX (\S*)")
def read_version(header):
for line in header:
if line[0] != "#":
return None
result = original_version.match(line)
if result:
return result.group(1)
return None
def set_version(lines, version):
lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
if lines[1][0] == '#':
del lines[1]
##
# file format version
#
format_re = re.compile(r"(\d)[\.,]?(\d\d)")
fileformat = re.compile(r"\\lyxformat\s*(\S*)")
lst_ft = [210, 215, 216, 217, 218, 220, 221, 223, 224, 225, 226, 227, 228, 229,
230, 231, 232, 233, 234, 235, 236]
format_relation = [("0_10", [210], ["0.10.7","0.10"]),
("0_12", [215], ["0.12","0.12.1","0.12"]),
("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_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_6", [217], ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]),
("1_1_6fix3", [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_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3"]),
("1_4", [223,224,225,226,227,228,229,230,231,232,233,234,235, 236], ["1.4.0cvs","1.4"])]
def lyxformat(format, opt):
result = format_re.match(format)
if result:
format = int(result.group(1) + result.group(2))
else:
opt.error(str(format) + ": " + "Invalid LyX file.")
if format in lst_ft:
return format
opt.error(str(format) + ": " + "Format not supported.")
return None
def read_format(header, opt):
for line in header:
result = fileformat.match(line)
if result:
return lyxformat(result.group(1), opt)
else:
opt.error("Invalid LyX File.")
return None
def set_format(lines, number):
if int(number) <= 217:
number = float(number)/100
i = find_token(lines, "\\lyxformat", 0)
lines[i] = "\\lyxformat %s" % number
def get_end_format():
return format_relation[-1:][0][1][-1:][0]
def get_backend(textclass):
if textclass == "linuxdoc" or textclass == "manpage":
return "linuxdoc"
if textclass[:7] == "docbook":
return "docbook"
return "latex"
def chain(opt, initial_version):
""" This is where all the decisions related with the convertion are taken"""
format = opt.format
if opt.start:
if opt.start != format:
opt.warning("%s: %s %s" % ("Proposed file format and input file formats do not match:", opt.start, format))
else:
opt.start = format
if not opt.end:
opt.end = get_end_format()
correct_version = 0
for rel in format_relation:
if initial_version in rel[2]:
if format in rel[1]:
initial_step = rel[0]
correct_version = 1
break
if not correct_version:
if format <= 215:
opt.warning("Version does not match file format, discarding it.")
for rel in format_relation:
if format in rel[1]:
initial_step = rel[0]
break
else:
# This should not happen, really.
opt.error("Format not supported.")
# Find the final step
for rel in format_relation:
if opt.end in rel[1]:
final_step = rel[0]
break
else:
opt.error("Format not supported.")
# Convertion mode, back or forth
steps = []
if (initial_step, opt.start) < (final_step, opt.end):
mode = "convert"
first_step = 1
for step in format_relation:
if initial_step <= step[0] <= final_step:
if first_step and len(step[1]) == 1:
first_step = 0
continue
steps.append(step[0])
else:
mode = "revert"
relation_format = format_relation
relation_format.reverse()
last_step = None
for step in relation_format:
if final_step <= step[0] <= initial_step:
steps.append(step[0])
last_step = step
if last_step[1][-1] == opt.end:
steps.pop()
return mode, steps