mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-09 18:31:04 +00:00
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:
parent
93430a89da
commit
6c79118290
@ -836,6 +836,7 @@ dist_lyx2lyx_SCRIPTS = lyx2lyx/lyx2lyx
|
||||
dist_lyx2lyx_DATA = \
|
||||
lyx2lyx/lyx2lyx \
|
||||
lyx2lyx/parser_tools.py \
|
||||
LyX.py \
|
||||
lyx2lyx/lyx_0_12.py \
|
||||
lyx2lyx/lyx_1_0_0.py \
|
||||
lyx2lyx/lyx_1_0_1.py \
|
||||
|
@ -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>
|
||||
|
||||
* Makefile.am (docfiles): add Customization.lyx
|
||||
|
@ -1661,17 +1661,7 @@ T
|
||||
ools\SpecialChar \menuseparator
|
||||
|
||||
\bar under
|
||||
|
||||
\begin_inset ERT
|
||||
status Collapsed
|
||||
|
||||
\layout Standard
|
||||
|
||||
\backslash
|
||||
linebreak
|
||||
\end_inset
|
||||
|
||||
P
|
||||
-P
|
||||
\bar default
|
||||
references
|
||||
\family default
|
||||
|
@ -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>
|
||||
|
||||
* lyx_1_4.py:
|
||||
* parser_tools.py: up the format to 236.
|
||||
|
||||
@ -52,10 +67,12 @@
|
||||
* lyx2lyx: up the format to 233.
|
||||
|
||||
2004-04-19 José Matos <jamatos@lyx.orrg>
|
||||
|
||||
* parser_tools.py (chain): fix the detection of the last format for
|
||||
revertions.
|
||||
|
||||
2004-04-19 Martin Vermeer <martin.vermeer@hut.fi>
|
||||
|
||||
* lyx_1_4.py (convert_frameless_box): Replace instead of adding
|
||||
new code.
|
||||
.
|
||||
|
276
lib/lyx2lyx/LyX.py
Normal file
276
lib/lyx2lyx/LyX.py
Normal 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
|
@ -17,36 +17,8 @@
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
import getopt
|
||||
import gzip
|
||||
import sys
|
||||
import os.path
|
||||
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)
|
||||
|
||||
import LyX
|
||||
|
||||
def usage():
|
||||
print """Usage: lyx2lyx [options] [file]
|
||||
@ -60,13 +32,12 @@ Options:
|
||||
-d, --debug level level=0..2 (O_ no debug information, 2_verbose)
|
||||
default: level=1
|
||||
-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)
|
||||
-o, --output name name of the output file or else goes to stdout
|
||||
-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"]
|
||||
try:
|
||||
opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options)
|
||||
@ -74,73 +45,43 @@ def parse_options(argv, version, opt):
|
||||
usage()
|
||||
sys.exit(2)
|
||||
|
||||
end_format, input, output, error, debug = 0, "", "", "", LyX.default_debug_level
|
||||
for o, a in opts:
|
||||
if o in ("-h", "--help"):
|
||||
usage()
|
||||
sys.exit()
|
||||
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"
|
||||
sys.exit()
|
||||
if o in ("-d", "--debug"):
|
||||
opt.debug = int(a)
|
||||
debug = int(a)
|
||||
if o in ("-q", "--quiet"):
|
||||
opt.debug = 0
|
||||
debug = 0
|
||||
if o in ("-l", "--list"):
|
||||
# list available formats
|
||||
sys.exit()
|
||||
if o in ("-o", "--output"):
|
||||
opt.output = open(a, "w")
|
||||
if o in ("-f", "--from"):
|
||||
opt.start = lyxformat(a, opt)
|
||||
output = a
|
||||
if o in ("-t", "--to"):
|
||||
opt.end = lyxformat(a, opt)
|
||||
end_format = a
|
||||
if o in ("-e","--err"):
|
||||
opt.err = open(a, "w")
|
||||
|
||||
error = a
|
||||
if args:
|
||||
file = 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)
|
||||
input = args[0]
|
||||
|
||||
|
||||
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)
|
||||
return end_format, input, output, error, debug
|
||||
|
||||
|
||||
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()
|
||||
|
@ -177,14 +177,14 @@ def add_end_document(lines):
|
||||
lines.append('\\the_end')
|
||||
|
||||
|
||||
def header_update(lines, opt):
|
||||
def header_update(lines, file):
|
||||
i = 0
|
||||
l = len(lines)
|
||||
while i < l:
|
||||
if check_token(lines[i], '\\begin_preamble'):
|
||||
i = find_token(lines, '\\end_preamble', i)
|
||||
if i == -1:
|
||||
opt.error('Unfinished preamble')
|
||||
file.error('Unfinished preamble')
|
||||
i = i + 1
|
||||
continue
|
||||
|
||||
@ -236,26 +236,26 @@ def header_update(lines, opt):
|
||||
i = i + 1
|
||||
|
||||
|
||||
def convert(header,body, opt):
|
||||
header_update(header, opt)
|
||||
add_end_document(body)
|
||||
remove_cursor(body)
|
||||
final_dot(body)
|
||||
update_inset_label(body)
|
||||
update_latexdel(body)
|
||||
update_space_units(body)
|
||||
update_inset_accent(body)
|
||||
space_before_layout(body)
|
||||
formula_inset_space_eat(body)
|
||||
update_tabular(body)
|
||||
update_vfill(body)
|
||||
remove_empty_insets(body)
|
||||
remove_formula_latex(body)
|
||||
opt.format = 215
|
||||
def convert(file):
|
||||
header_update(file.header, file)
|
||||
add_end_document(file.body)
|
||||
remove_cursor(file.body)
|
||||
final_dot(file.body)
|
||||
update_inset_label(file.body)
|
||||
update_latexdel(file.body)
|
||||
update_space_units(file.body)
|
||||
update_inset_accent(file.body)
|
||||
space_before_layout(file.body)
|
||||
formula_inset_space_eat(file.body)
|
||||
update_tabular(file.body)
|
||||
update_vfill(file.body)
|
||||
remove_empty_insets(file.body)
|
||||
remove_formula_latex(file.body)
|
||||
file.format = 215
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -16,12 +16,12 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
def convert(header, body, opt):
|
||||
opt.format = 215
|
||||
def convert(file):
|
||||
file.format = 215
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file .format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -16,12 +16,12 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
def convert(header, body, opt):
|
||||
opt.format = 215
|
||||
def convert(file):
|
||||
file.format = 215
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -16,12 +16,12 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
def convert(header, body, opt):
|
||||
opt.format = 215
|
||||
def convert(file):
|
||||
file.format = 215
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -147,20 +147,20 @@ def remove_space_in_units(lines):
|
||||
i = i + 1
|
||||
|
||||
|
||||
def convert(header, body, opt):
|
||||
first_layout(body)
|
||||
remove_vcid(header)
|
||||
remove_cursor(body)
|
||||
update_toc(body)
|
||||
replace_protected_separator(body)
|
||||
merge_formula_inset(body)
|
||||
update_tabular(body)
|
||||
remove_space_in_units(header)
|
||||
opt.format = 216
|
||||
def convert(file):
|
||||
first_layout(file.body)
|
||||
remove_vcid(file.header)
|
||||
remove_cursor(file.body)
|
||||
update_toc(file.body)
|
||||
replace_protected_separator(file.body)
|
||||
merge_formula_inset(file.body)
|
||||
update_tabular(file.body)
|
||||
remove_space_in_units(file.header)
|
||||
file.format = 216
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
if __name__ == "__main__":
|
||||
pass
|
||||
|
@ -274,14 +274,14 @@ def update_language(header):
|
||||
return
|
||||
|
||||
|
||||
def convert(header, body, opt):
|
||||
update_tabular(body, opt)
|
||||
update_language(header)
|
||||
opt.format = 217
|
||||
def convert(file):
|
||||
update_tabular(file.body, file)
|
||||
update_language(file.header)
|
||||
file.format = 217
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -113,13 +113,13 @@ def table_update(lines):
|
||||
return lines[:2] + col_info + lines[2:]
|
||||
|
||||
|
||||
def convert(header, body, opt):
|
||||
update_tabular(body, opt)
|
||||
opt.format = 218
|
||||
def convert(file):
|
||||
update_tabular(file.body, file)
|
||||
file.format = 218
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -540,23 +540,23 @@ def change_preamble(lines):
|
||||
"\use_numerical_citations 0"]
|
||||
|
||||
|
||||
def convert(header, body, opt):
|
||||
change_preamble(header)
|
||||
change_listof(body)
|
||||
fix_oldfloatinset(body)
|
||||
update_tabular(body)
|
||||
remove_pextra(body)
|
||||
remove_oldfloat(body, opt)
|
||||
remove_figinset(body)
|
||||
remove_oldertinset(body)
|
||||
remove_oldert(body)
|
||||
combine_ert(body)
|
||||
change_infoinset(body)
|
||||
opt.format = 220
|
||||
def convert(file):
|
||||
change_preamble(file.header)
|
||||
change_listof(file.body)
|
||||
fix_oldfloatinset(file.body)
|
||||
update_tabular(file.body)
|
||||
remove_pextra(file.body)
|
||||
remove_oldfloat(file.body, opt)
|
||||
remove_figinset(file.body)
|
||||
remove_oldertinset(file.body)
|
||||
remove_oldert(file.body)
|
||||
combine_ert(file.body)
|
||||
change_infoinset(file.body)
|
||||
file.format = 220
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -90,14 +90,14 @@ def change_tabular(lines):
|
||||
i = i+1
|
||||
|
||||
|
||||
def convert(header, body, opt):
|
||||
change_insetgraphics(body)
|
||||
change_tabular(body)
|
||||
opt.format = 221
|
||||
def convert(file):
|
||||
change_insetgraphics(file.body)
|
||||
change_tabular(file.body)
|
||||
file.format = 221
|
||||
|
||||
|
||||
def revert(header, body, opt):
|
||||
opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
|
||||
def revert(file):
|
||||
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -1447,171 +1447,171 @@ def remove_begin_body(body, opt):
|
||||
##
|
||||
# Convertion hub
|
||||
#
|
||||
def convert(header, body, opt):
|
||||
if opt.format < 223:
|
||||
insert_tracking_changes(header)
|
||||
add_end_header(header)
|
||||
remove_color_default(body)
|
||||
convert_spaces(body)
|
||||
convert_bibtex(body)
|
||||
remove_insetparent(body)
|
||||
opt.format = 223
|
||||
if opt.end == opt.format: return
|
||||
def convert(file):
|
||||
if file.format < 223:
|
||||
insert_tracking_changes(file.header)
|
||||
add_end_header(file.header)
|
||||
remove_color_default(file.body)
|
||||
convert_spaces(file.body)
|
||||
convert_bibtex(file.body)
|
||||
remove_insetparent(file.body)
|
||||
file.format = 223
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 224:
|
||||
convert_external(body)
|
||||
convert_comment(body)
|
||||
opt.format = 224
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 224:
|
||||
convert_external(file.body)
|
||||
convert_comment(file.body)
|
||||
file.format = 224
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 225:
|
||||
add_end_layout(body)
|
||||
layout2begin_layout(body)
|
||||
convert_end_document(body)
|
||||
convert_table_valignment_middle(body)
|
||||
convert_breaks(body)
|
||||
opt.format = 225
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 225:
|
||||
add_end_layout(file.body)
|
||||
layout2begin_layout(file.body)
|
||||
convert_end_document(file.body)
|
||||
convert_table_valignment_middle(file.body)
|
||||
convert_breaks(file.body)
|
||||
file.format = 225
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 226:
|
||||
convert_note(body)
|
||||
opt.format = 226
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 226:
|
||||
convert_note(file.body)
|
||||
file.format = 226
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 227:
|
||||
convert_box(body)
|
||||
opt.format = 227
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 227:
|
||||
convert_box(file.body)
|
||||
file.format = 227
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 228:
|
||||
convert_collapsable(body, opt)
|
||||
convert_ert(body, opt)
|
||||
opt.format = 228
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 228:
|
||||
convert_collapsable(file.body, file)
|
||||
convert_ert(file.body, file)
|
||||
file.format = 228
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 229:
|
||||
convert_minipage(body)
|
||||
opt.format = 229
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 229:
|
||||
convert_minipage(file.body)
|
||||
file.format = 229
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 230:
|
||||
convert_jurabib(header, opt)
|
||||
opt.format = 230
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 230:
|
||||
convert_jurabib(file.header, file)
|
||||
file.format = 230
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 231:
|
||||
convert_float(body, opt)
|
||||
opt.format = 231
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 231:
|
||||
convert_float(file.body, file)
|
||||
file.format = 231
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 232:
|
||||
convert_bibtopic(header, opt)
|
||||
opt.format = 232
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 232:
|
||||
convert_bibtopic(file.header, file)
|
||||
file.format = 232
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 233:
|
||||
convert_graphics(body, opt)
|
||||
convert_names(body, opt)
|
||||
opt.format = 233
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 233:
|
||||
convert_graphics(file.body, file)
|
||||
convert_names(file.body, file)
|
||||
file.format = 233
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 234:
|
||||
convert_cite_engine(header, opt)
|
||||
opt.format = 234
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 234:
|
||||
convert_cite_engine(file.header, file)
|
||||
file.format = 234
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 235:
|
||||
convert_paperpackage(header, opt)
|
||||
opt.format = 235
|
||||
if opt.end == opt.format: return
|
||||
if file.format < 235:
|
||||
convert_paperpackage(file.header, file)
|
||||
file.format = 235
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format < 236:
|
||||
convert_bullets(header, opt)
|
||||
add_begin_header(header, opt)
|
||||
add_begin_body(body, opt)
|
||||
opt.format = 236
|
||||
if file.format < 236:
|
||||
convert_bullets(file.header, file)
|
||||
add_begin_header(file.header, file)
|
||||
add_begin_body(file.body, file)
|
||||
file.format = 236
|
||||
|
||||
def revert(header, body, opt):
|
||||
if opt.format > 235:
|
||||
remove_begin_body(body, opt)
|
||||
remove_begin_header(header, opt)
|
||||
revert_bullets(header, opt)
|
||||
opt.format = 235
|
||||
if opt.end == opt.format: return
|
||||
def revert(file):
|
||||
if file.format > 235:
|
||||
remove_begin_body(file.body, file)
|
||||
remove_begin_header(file.header, file)
|
||||
revert_bullets(file.header, file)
|
||||
file.format = 235
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 234:
|
||||
revert_paperpackage(header, opt)
|
||||
opt.format = 234
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 234:
|
||||
revert_paperpackage(file.header, file)
|
||||
file.format = 234
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 233:
|
||||
revert_cite_engine(header, opt)
|
||||
opt.format = 233
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 233:
|
||||
revert_cite_engine(file.header, file)
|
||||
file.format = 233
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 232:
|
||||
revert_names(body, opt)
|
||||
opt.format = 232
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 232:
|
||||
revert_names(file.body, file)
|
||||
file.format = 232
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 231:
|
||||
revert_bibtopic(header, opt)
|
||||
opt.format = 231
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 231:
|
||||
revert_bibtopic(file.header, file)
|
||||
file.format = 231
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 230:
|
||||
revert_float(body, opt)
|
||||
opt.format = 230
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 230:
|
||||
revert_float(file.body, file)
|
||||
file.format = 230
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 229:
|
||||
revert_jurabib(header, opt)
|
||||
opt.format = 229
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 229:
|
||||
revert_jurabib(file.header, file)
|
||||
file.format = 229
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 228:
|
||||
opt.format = 228
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 228:
|
||||
file.format = 228
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 227:
|
||||
revert_collapsable(body, opt)
|
||||
revert_ert(body, opt)
|
||||
opt.format = 227
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 227:
|
||||
revert_collapsable(file.body, file)
|
||||
revert_ert(file.body, file)
|
||||
file.format = 227
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 226:
|
||||
revert_box(body)
|
||||
revert_external_2(body)
|
||||
opt.format = 226
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 226:
|
||||
revert_box(file.body)
|
||||
revert_external_2(file.body)
|
||||
file.format = 226
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 225:
|
||||
revert_note(body)
|
||||
opt.format = 225
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 225:
|
||||
revert_note(file.body)
|
||||
file.format = 225
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 224:
|
||||
rm_end_layout(body)
|
||||
begin_layout2layout(body)
|
||||
revert_end_document(body)
|
||||
revert_valignment_middle(body)
|
||||
convert_vspace(header, body, opt)
|
||||
convert_frameless_box(body, opt)
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 224:
|
||||
rm_end_layout(file.body)
|
||||
begin_layout2layout(file.body)
|
||||
revert_end_document(file.body)
|
||||
revert_valignment_middle(file.body)
|
||||
convert_vspace(file.header, file.body, file)
|
||||
convert_frameless_box(file.body, file)
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 223:
|
||||
revert_external_2(body)
|
||||
revert_comment(body)
|
||||
opt.format = 223
|
||||
if opt.end == opt.format: return
|
||||
if file.format > 223:
|
||||
revert_external_2(file.body)
|
||||
revert_comment(file.body)
|
||||
file.format = 223
|
||||
if file.end_format == file.format: return
|
||||
|
||||
if opt.format > 221:
|
||||
rm_end_header(header)
|
||||
revert_spaces(body)
|
||||
revert_bibtex(body)
|
||||
rm_tracking_changes(header)
|
||||
rm_body_changes(body)
|
||||
opt.format = 221
|
||||
if file.format > 221:
|
||||
rm_end_header(file.header)
|
||||
revert_spaces(file.body)
|
||||
revert_bibtex(file.body)
|
||||
rm_tracking_changes(file.header)
|
||||
rm_file.body_changes(file.body)
|
||||
file.format = 221
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -19,7 +19,6 @@
|
||||
import string
|
||||
import re
|
||||
|
||||
|
||||
def check_token(line, token):
|
||||
if line[:len(token)] == token:
|
||||
return 1
|
||||
@ -195,206 +194,3 @@ def find_nonempty_line(lines, start, end = 0):
|
||||
if is_nonempty_line(lines[i]):
|
||||
return i
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user