2004-08-15 16:29:04 +00:00
|
|
|
# This file is part of lyx2lyx
|
2006-08-02 14:19:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-08-07 14:10:41 +00:00
|
|
|
# Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>
|
|
|
|
# Copyright (C) 2002-2006 José Matos <jamatos@lyx.org>
|
2004-08-15 16:29:04 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
" The LyX module has all the rules related with different lyx file formats."
|
|
|
|
|
|
|
|
from parser_tools import get_value, check_token, find_token, \
|
2006-07-27 18:30:13 +00:00
|
|
|
find_tokens, find_end_of
|
2004-08-15 16:29:04 +00:00
|
|
|
import os.path
|
|
|
|
import gzip
|
2007-02-13 16:57:48 +00:00
|
|
|
import locale
|
2004-08-15 16:29:04 +00:00
|
|
|
import sys
|
|
|
|
import re
|
2005-01-06 16:52:08 +00:00
|
|
|
import time
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2007-05-31 12:02:47 +00:00
|
|
|
try:
|
|
|
|
import lyx2lyx_version
|
2007-09-14 14:37:14 +00:00
|
|
|
version__ = lyx2lyx_version.version
|
2007-05-31 12:02:47 +00:00
|
|
|
except: # we are running from build directory so assume the last version
|
2007-09-14 14:37:14 +00:00
|
|
|
version__ = '1.6.0svn'
|
2006-04-10 16:18:31 +00:00
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
default_debug__ = 2
|
2004-10-28 11:21:27 +00:00
|
|
|
|
2006-07-27 18:30:13 +00:00
|
|
|
####################################################################
|
|
|
|
# Private helper functions
|
|
|
|
|
|
|
|
def find_end_of_inset(lines, i):
|
2006-08-02 14:19:22 +00:00
|
|
|
" Find beginning of inset, where lines[i] is included."
|
2006-07-27 18:30:13 +00:00
|
|
|
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
|
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
def minor_versions(major, last_minor_version):
|
2006-08-07 14:10:41 +00:00
|
|
|
""" Generate minor versions, using major as prefix and minor
|
|
|
|
versions from 0 until last_minor_version, plus the generic version.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
minor_versions("1.2", 4) ->
|
2006-08-07 14:10:41 +00:00
|
|
|
[ "1.2", "1.2.0", "1.2.1", "1.2.2", "1.2.3"]
|
|
|
|
"""
|
|
|
|
return [major] + [major + ".%d" % i for i in range(last_minor_version + 1)]
|
|
|
|
|
|
|
|
|
2006-07-27 18:30:13 +00:00
|
|
|
# End of helper functions
|
|
|
|
####################################################################
|
|
|
|
|
|
|
|
|
2004-10-28 11:21:27 +00:00
|
|
|
# Regular expressions used
|
2004-08-15 16:29:04 +00:00
|
|
|
format_re = re.compile(r"(\d)[\.,]?(\d\d)")
|
|
|
|
fileformat = re.compile(r"\\lyxformat\s*(\S*)")
|
2006-08-07 14:10:41 +00:00
|
|
|
original_version = re.compile(r".*?LyX ([\d.]*)")
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2004-10-28 11:21:27 +00:00
|
|
|
##
|
|
|
|
# file format information:
|
|
|
|
# file, supported formats, stable release versions
|
2007-09-14 14:37:14 +00:00
|
|
|
format_relation = [("0_06", [200], minor_versions("0.6" , 4)),
|
|
|
|
("0_08", [210], minor_versions("0.8" , 6) + ["0.7"]),
|
|
|
|
("0_10", [210], minor_versions("0.10", 7) + ["0.9"]),
|
|
|
|
("0_12", [215], minor_versions("0.12", 1) + ["0.11"]),
|
|
|
|
("1_0", [215], minor_versions("1.0" , 4)),
|
|
|
|
("1_1", [215], minor_versions("1.1" , 4)),
|
2006-08-07 14:10:41 +00:00
|
|
|
("1_1_5", [216], ["1.1.5","1.1.5.1","1.1.5.2","1.1"]),
|
|
|
|
("1_1_6_0", [217], ["1.1.6","1.1.6.1","1.1.6.2","1.1"]),
|
|
|
|
("1_1_6_3", [218], ["1.1.6.3","1.1.6.4","1.1"]),
|
2007-09-14 14:37:14 +00:00
|
|
|
("1_2", [220], minor_versions("1.2" , 4)),
|
|
|
|
("1_3", [221], minor_versions("1.3" , 7)),
|
|
|
|
("1_4", range(222,246), minor_versions("1.4" , 5)),
|
2007-10-12 23:51:56 +00:00
|
|
|
("1_5", range(246,277), minor_versions("1.5" , 2)),
|
2007-11-01 17:37:43 +00:00
|
|
|
("1_6", range(277,300), minor_versions("1.6" , 0))] # Uwe: Hyperlink types
|
2004-10-09 21:32:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def formats_list():
|
2004-10-28 11:21:27 +00:00
|
|
|
" Returns a list with supported file formats."
|
2004-10-09 21:32:56 +00:00
|
|
|
formats = []
|
|
|
|
for version in format_relation:
|
|
|
|
for format in version[1]:
|
|
|
|
if format not in formats:
|
|
|
|
formats.append(format)
|
|
|
|
return formats
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_end_format():
|
2004-10-28 11:21:27 +00:00
|
|
|
" Returns the more recent file format available."
|
2004-09-29 16:34:27 +00:00
|
|
|
return format_relation[-1][1][-1]
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_backend(textclass):
|
2004-10-28 11:21:27 +00:00
|
|
|
" For _textclass_ returns its backend."
|
2004-08-15 16:29:04 +00:00
|
|
|
if textclass == "linuxdoc" or textclass == "manpage":
|
|
|
|
return "linuxdoc"
|
|
|
|
if textclass[:7] == "docbook":
|
|
|
|
return "docbook"
|
|
|
|
return "latex"
|
|
|
|
|
|
|
|
|
2005-08-18 17:33:26 +00:00
|
|
|
def trim_eol(line):
|
|
|
|
" Remove end of line char(s)."
|
|
|
|
if line[-2:-1] == '\r':
|
|
|
|
return line[:-2]
|
|
|
|
else:
|
|
|
|
return line[:-1]
|
|
|
|
|
|
|
|
|
2007-02-13 16:57:48 +00:00
|
|
|
def get_encoding(language, inputencoding, format, cjk_encoding):
|
2007-09-14 14:37:14 +00:00
|
|
|
" Returns enconding of the lyx file"
|
2006-08-15 07:08:58 +00:00
|
|
|
if format > 248:
|
|
|
|
return "utf8"
|
2007-02-13 16:57:48 +00:00
|
|
|
# CJK-LyX encodes files using the current locale encoding.
|
|
|
|
# This means that files created by CJK-LyX can only be converted using
|
|
|
|
# the correct locale settings unless the encoding is given as commandline
|
|
|
|
# argument.
|
|
|
|
if cjk_encoding == 'auto':
|
|
|
|
return locale.getpreferredencoding()
|
2007-09-14 14:37:14 +00:00
|
|
|
elif cjk_encoding:
|
2007-02-13 16:57:48 +00:00
|
|
|
return cjk_encoding
|
2006-08-07 14:10:41 +00:00
|
|
|
from lyx2lyx_lang import lang
|
2007-01-13 14:36:54 +00:00
|
|
|
if inputencoding == "auto" or inputencoding == "default":
|
2006-08-07 14:10:41 +00:00
|
|
|
return lang[language][3]
|
2007-01-13 14:36:54 +00:00
|
|
|
if inputencoding == "":
|
2006-08-07 14:10:41 +00:00
|
|
|
return "latin1"
|
|
|
|
# python does not know the alias latin9
|
|
|
|
if inputencoding == "latin9":
|
|
|
|
return "iso-8859-15"
|
|
|
|
return inputencoding
|
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
##
|
|
|
|
# Class
|
|
|
|
#
|
2007-09-14 14:37:14 +00:00
|
|
|
class LyX_base:
|
2004-08-15 16:29:04 +00:00
|
|
|
"""This class carries all the information of the LyX file."""
|
2006-08-09 22:18:27 +00:00
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
def __init__(self, end_format = 0, input = "", output = "", error = "",
|
|
|
|
debug = default_debug__, try_hard = 0, cjk_encoding = '',
|
2007-02-13 16:57:48 +00:00
|
|
|
language = "english", encoding = "auto"):
|
|
|
|
|
2004-10-28 11:21:27 +00:00
|
|
|
"""Arguments:
|
|
|
|
end_format: final format that the file should be converted. (integer)
|
|
|
|
input: the name of the input source, if empty resort to standard input.
|
|
|
|
output: the name of the output file, if empty use the standard output.
|
|
|
|
error: the name of the error file, if empty use the standard error.
|
|
|
|
debug: debug level, O means no debug, as its value increases be more verbose.
|
|
|
|
"""
|
2005-12-01 19:00:07 +00:00
|
|
|
self.choose_io(input, output)
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
if error:
|
|
|
|
self.err = open(error, "w")
|
|
|
|
else:
|
|
|
|
self.err = sys.stderr
|
|
|
|
|
|
|
|
self.debug = debug
|
2005-07-05 11:14:13 +00:00
|
|
|
self.try_hard = try_hard
|
2007-02-13 16:57:48 +00:00
|
|
|
self.cjk_encoding = cjk_encoding
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
if end_format:
|
|
|
|
self.end_format = self.lyxformat(end_format)
|
|
|
|
else:
|
|
|
|
self.end_format = get_end_format()
|
|
|
|
|
|
|
|
self.backend = "latex"
|
|
|
|
self.textclass = "article"
|
2006-02-13 07:48:26 +00:00
|
|
|
# This is a hack: We use '' since we don't know the default
|
|
|
|
# layout of the text class. LyX will parse it as default layout.
|
|
|
|
# FIXME: Read the layout file and use the real default layout
|
|
|
|
self.default_layout = ''
|
2004-08-15 16:29:04 +00:00
|
|
|
self.header = []
|
2005-08-18 17:33:26 +00:00
|
|
|
self.preamble = []
|
2004-08-15 16:29:04 +00:00
|
|
|
self.body = []
|
2005-07-05 15:28:44 +00:00
|
|
|
self.status = 0
|
2006-08-09 22:18:27 +00:00
|
|
|
self.encoding = encoding
|
|
|
|
self.language = language
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
def warning(self, message, debug_level= default_debug__):
|
|
|
|
""" Emits warning to self.error, if the debug_level is less
|
|
|
|
than the self.debug."""
|
2004-08-15 16:29:04 +00:00
|
|
|
if debug_level <= self.debug:
|
2005-07-06 17:40:38 +00:00
|
|
|
self.err.write("Warning: " + message + "\n")
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def error(self, message):
|
2005-07-05 15:28:44 +00:00
|
|
|
" Emits a warning and exits if not in try_hard mode."
|
2004-08-15 16:29:04 +00:00
|
|
|
self.warning(message)
|
2005-07-05 15:28:44 +00:00
|
|
|
if not self.try_hard:
|
|
|
|
self.warning("Quiting.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
self.status = 2
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def read(self):
|
2007-10-21 18:06:00 +00:00
|
|
|
"""Reads a file into the self.header and
|
2007-09-14 14:37:14 +00:00
|
|
|
self.body parts, from self.input."""
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
while True:
|
2004-08-15 16:29:04 +00:00
|
|
|
line = self.input.readline()
|
|
|
|
if not line:
|
|
|
|
self.error("Invalid LyX file.")
|
|
|
|
|
2005-08-18 17:33:26 +00:00
|
|
|
line = trim_eol(line)
|
2004-08-15 16:29:04 +00:00
|
|
|
if check_token(line, '\\begin_preamble'):
|
2005-08-18 17:33:26 +00:00
|
|
|
while 1:
|
|
|
|
line = self.input.readline()
|
|
|
|
if not line:
|
|
|
|
self.error("Invalid LyX file.")
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2005-08-18 17:33:26 +00:00
|
|
|
line = trim_eol(line)
|
|
|
|
if check_token(line, '\\end_preamble'):
|
|
|
|
break
|
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
if line.split()[:0] in ("\\layout",
|
|
|
|
"\\begin_layout", "\\begin_body"):
|
|
|
|
|
|
|
|
self.warning("Malformed LyX file:"
|
|
|
|
"Missing '\\end_preamble'."
|
|
|
|
"\nAdding it now and hoping"
|
|
|
|
"for the best.")
|
2005-08-18 17:33:26 +00:00
|
|
|
|
|
|
|
self.preamble.append(line)
|
|
|
|
|
|
|
|
if check_token(line, '\\end_preamble'):
|
|
|
|
continue
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2006-08-02 15:45:44 +00:00
|
|
|
line = line.strip()
|
2005-08-18 17:33:26 +00:00
|
|
|
if not line:
|
|
|
|
continue
|
2005-07-06 18:35:35 +00:00
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
if line.split()[0] in ("\\layout", "\\begin_layout",
|
|
|
|
"\\begin_body", "\\begin_deeper"):
|
2005-08-18 17:33:26 +00:00
|
|
|
self.body.append(line)
|
|
|
|
break
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
self.header.append(line)
|
|
|
|
|
|
|
|
self.textclass = get_value(self.header, "\\textclass", 0)
|
|
|
|
self.backend = get_backend(self.textclass)
|
|
|
|
self.format = self.read_format()
|
2007-09-14 14:37:14 +00:00
|
|
|
self.language = get_value(self.header, "\\language", 0,
|
|
|
|
default = "english")
|
|
|
|
self.inputencoding = get_value(self.header, "\\inputencoding",
|
|
|
|
0, default = "auto")
|
|
|
|
self.encoding = get_encoding(self.language,
|
|
|
|
self.inputencoding, self.format,
|
|
|
|
self.cjk_encoding)
|
2004-08-15 16:29:04 +00:00
|
|
|
self.initial_version = self.read_version()
|
|
|
|
|
2006-08-07 14:10:41 +00:00
|
|
|
# Second pass over header and preamble, now we know the file encoding
|
|
|
|
for i in range(len(self.header)):
|
|
|
|
self.header[i] = self.header[i].decode(self.encoding)
|
|
|
|
for i in range(len(self.preamble)):
|
|
|
|
self.preamble[i] = self.preamble[i].decode(self.encoding)
|
|
|
|
|
|
|
|
# Read document body
|
|
|
|
while 1:
|
|
|
|
line = self.input.readline().decode(self.encoding)
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
self.body.append(trim_eol(line))
|
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def write(self):
|
2004-10-28 11:21:27 +00:00
|
|
|
" Writes the LyX file to self.output."
|
2004-08-15 16:29:04 +00:00
|
|
|
self.set_version()
|
|
|
|
self.set_format()
|
2007-04-25 16:14:59 +00:00
|
|
|
self.set_textclass()
|
2006-08-09 22:18:27 +00:00
|
|
|
if self.encoding == "auto":
|
2007-09-14 14:37:14 +00:00
|
|
|
self.encoding = get_encoding(self.language, self.encoding,
|
|
|
|
self.format, self.cjk_encoding)
|
2005-08-18 17:33:26 +00:00
|
|
|
if self.preamble:
|
|
|
|
i = find_token(self.header, '\\textclass', 0) + 1
|
|
|
|
preamble = ['\\begin_preamble'] + self.preamble + ['\\end_preamble']
|
|
|
|
if i == 0:
|
|
|
|
self.error("Malformed LyX file: Missing '\\textclass'.")
|
|
|
|
else:
|
|
|
|
header = self.header[:i] + preamble + self.header[i:]
|
|
|
|
else:
|
|
|
|
header = self.header
|
|
|
|
|
2007-10-21 18:06:00 +00:00
|
|
|
for line in header + [''] + self.body:
|
2006-08-07 14:10:41 +00:00
|
|
|
self.output.write(line.encode(self.encoding)+"\n")
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
|
2005-12-01 19:00:07 +00:00
|
|
|
def choose_io(self, input, output):
|
|
|
|
"""Choose input and output streams, dealing transparently with
|
|
|
|
compressed files."""
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2005-12-01 19:00:07 +00:00
|
|
|
if output:
|
|
|
|
self.output = open(output, "wb")
|
|
|
|
else:
|
|
|
|
self.output = sys.stdout
|
|
|
|
|
|
|
|
if input and input != '-':
|
|
|
|
self.dir = os.path.dirname(os.path.abspath(input))
|
|
|
|
try:
|
|
|
|
gzip.open(input).readline()
|
|
|
|
self.input = gzip.open(input)
|
|
|
|
self.output = gzip.GzipFile(mode="wb", fileobj=self.output)
|
|
|
|
except:
|
|
|
|
self.input = open(input)
|
|
|
|
else:
|
2006-07-27 18:30:13 +00:00
|
|
|
self.dir = ''
|
2005-12-01 19:00:07 +00:00
|
|
|
self.input = sys.stdin
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def lyxformat(self, format):
|
2004-10-28 11:21:27 +00:00
|
|
|
" Returns the file format representation, an integer."
|
2004-08-15 16:29:04 +00:00
|
|
|
result = format_re.match(format)
|
|
|
|
if result:
|
|
|
|
format = int(result.group(1) + result.group(2))
|
2006-08-07 14:10:41 +00:00
|
|
|
elif format == '2':
|
|
|
|
format = 200
|
2004-08-15 16:29:04 +00:00
|
|
|
else:
|
|
|
|
self.error(str(format) + ": " + "Invalid LyX file.")
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
if format in formats_list():
|
2004-08-15 16:29:04 +00:00
|
|
|
return format
|
|
|
|
|
|
|
|
self.error(str(format) + ": " + "Format not supported.")
|
|
|
|
return None
|
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def read_version(self):
|
2007-09-14 14:37:14 +00:00
|
|
|
""" Searchs for clues of the LyX version used to write the
|
|
|
|
file, returns the most likely value, or None otherwise."""
|
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
for line in self.header:
|
|
|
|
if line[0] != "#":
|
|
|
|
return None
|
|
|
|
|
2006-08-07 14:10:41 +00:00
|
|
|
line = line.replace("fix",".")
|
2004-08-15 16:29:04 +00:00
|
|
|
result = original_version.match(line)
|
|
|
|
if result:
|
2006-08-07 14:10:41 +00:00
|
|
|
# Special know cases: reLyX and KLyX
|
|
|
|
if line.find("reLyX") != -1 or line.find("KLyX") != -1:
|
|
|
|
return "0.12"
|
|
|
|
|
|
|
|
res = result.group(1)
|
|
|
|
if not res:
|
|
|
|
self.warning(line)
|
|
|
|
#self.warning("Version %s" % result.group(1))
|
|
|
|
return res
|
|
|
|
self.warning(str(self.header[:2]))
|
2004-08-15 16:29:04 +00:00
|
|
|
return None
|
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def set_version(self):
|
2004-10-28 11:21:27 +00:00
|
|
|
" Set the header with the version used."
|
2007-09-14 14:37:14 +00:00
|
|
|
self.header[0] = " ".join(["#LyX %s created this file." % version__,
|
|
|
|
"For more info see http://www.lyx.org/"])
|
2004-08-15 16:29:04 +00:00
|
|
|
if self.header[1][0] == '#':
|
|
|
|
del self.header[1]
|
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def read_format(self):
|
2004-10-28 11:21:27 +00:00
|
|
|
" Read from the header the fileformat of the present LyX file."
|
2004-08-15 16:29:04 +00:00
|
|
|
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):
|
2004-10-28 11:21:27 +00:00
|
|
|
" Set the file format of the file, in the header."
|
2004-10-09 21:32:56 +00:00
|
|
|
if self.format <= 217:
|
2005-01-05 10:57:37 +00:00
|
|
|
format = str(float(self.format)/100)
|
2004-08-15 16:29:04 +00:00
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
format = str(self.format)
|
2004-08-15 16:29:04 +00:00
|
|
|
i = find_token(self.header, "\\lyxformat", 0)
|
|
|
|
self.header[i] = "\\lyxformat %s" % format
|
|
|
|
|
|
|
|
|
2007-04-25 16:14:59 +00:00
|
|
|
def set_textclass(self):
|
|
|
|
i = find_token(self.header, "\\textclass", 0)
|
|
|
|
self.header[i] = "\\textclass %s" % self.textclass
|
|
|
|
|
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
def set_parameter(self, param, value):
|
2004-10-28 11:21:27 +00:00
|
|
|
" Set the value of the header parameter."
|
2004-10-16 22:56:10 +00:00
|
|
|
i = find_token(self.header, '\\' + param, 0)
|
|
|
|
if i == -1:
|
2005-09-28 14:29:23 +00:00
|
|
|
self.warning('Parameter not found in the header: %s' % param, 3)
|
2004-10-16 22:56:10 +00:00
|
|
|
return
|
|
|
|
self.header[i] = '\\%s %s' % (param, str(value))
|
|
|
|
|
|
|
|
|
2006-02-13 07:48:26 +00:00
|
|
|
def is_default_layout(self, layout):
|
|
|
|
" Check whether a layout is the default layout of this class."
|
|
|
|
# FIXME: Check against the real text class default layout
|
|
|
|
if layout == 'Standard' or layout == self.default_layout:
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
def convert(self):
|
2004-10-28 11:21:27 +00:00
|
|
|
"Convert from current (self.format) to self.end_format."
|
2004-10-16 22:56:10 +00:00
|
|
|
mode, convertion_chain = self.chain()
|
|
|
|
self.warning("convertion chain: " + str(convertion_chain), 3)
|
|
|
|
|
|
|
|
for step in convertion_chain:
|
2005-01-05 18:52:59 +00:00
|
|
|
steps = getattr(__import__("lyx_" + step), mode)
|
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
self.warning("Convertion step: %s - %s" % (step, mode),
|
|
|
|
default_debug__ + 1)
|
2005-01-05 18:52:59 +00:00
|
|
|
if not steps:
|
2007-09-14 14:37:14 +00:00
|
|
|
self.error("The convertion to an older "
|
|
|
|
"format (%s) is not implemented." % self.format)
|
2005-01-05 18:52:59 +00:00
|
|
|
|
2005-01-06 16:52:08 +00:00
|
|
|
multi_conv = len(steps) != 1
|
2005-01-05 18:52:59 +00:00
|
|
|
for version, table in steps:
|
2005-01-06 16:52:08 +00:00
|
|
|
if multi_conv and \
|
|
|
|
(self.format >= version and mode == "convert") or\
|
|
|
|
(self.format <= version and mode == "revert"):
|
2005-01-05 18:52:59 +00:00
|
|
|
continue
|
2005-01-06 16:52:08 +00:00
|
|
|
|
2005-01-05 18:52:59 +00:00
|
|
|
for conv in table:
|
2005-01-06 16:52:08 +00:00
|
|
|
init_t = time.time()
|
2005-07-05 11:14:13 +00:00
|
|
|
try:
|
|
|
|
conv(self)
|
|
|
|
except:
|
2007-09-14 14:37:14 +00:00
|
|
|
self.warning("An error ocurred in %s, %s" %
|
|
|
|
(version, str(conv)),
|
|
|
|
default_debug__)
|
2005-07-05 11:14:13 +00:00
|
|
|
if not self.try_hard:
|
|
|
|
raise
|
2005-07-05 15:28:44 +00:00
|
|
|
self.status = 2
|
2005-07-05 11:14:13 +00:00
|
|
|
else:
|
2007-09-14 14:37:14 +00:00
|
|
|
self.warning("%lf: Elapsed time on %s" %
|
|
|
|
(time.time() - init_t,
|
|
|
|
str(conv)), default_debug__ +
|
|
|
|
1)
|
2005-01-05 18:52:59 +00:00
|
|
|
self.format = version
|
|
|
|
if self.end_format == self.format:
|
|
|
|
return
|
2004-10-16 22:56:10 +00:00
|
|
|
|
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
def chain(self):
|
2007-09-14 14:37:14 +00:00
|
|
|
""" This is where all the decisions related with the
|
|
|
|
convertion are taken. It returns a list of modules needed to
|
|
|
|
convert the LyX file from self.format to self.end_format"""
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
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:
|
2007-09-14 14:37:14 +00:00
|
|
|
self.warning("Version does not match file format, "
|
|
|
|
"discarding it. (Version %s, format %d)" %
|
|
|
|
(self.initial_version, self.format))
|
2004-08-15 16:29:04 +00:00
|
|
|
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:
|
2007-08-17 15:48:41 +00:00
|
|
|
if first_step and len(step[1]) == 1:
|
2004-08-15 16:29:04 +00:00
|
|
|
first_step = 0
|
|
|
|
continue
|
|
|
|
steps.append(step[0])
|
|
|
|
else:
|
|
|
|
mode = "revert"
|
2005-09-28 14:29:23 +00:00
|
|
|
relation_format = format_relation[:]
|
2004-08-15 16:29:04 +00:00
|
|
|
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()
|
|
|
|
|
2007-08-12 13:25:36 +00:00
|
|
|
self.warning("Convertion mode: %s\tsteps%s" %(mode, steps), 10)
|
2004-08-15 16:29:04 +00:00
|
|
|
return mode, steps
|
2004-10-16 22:56:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_toc(self, depth = 4):
|
2004-10-28 11:21:27 +00:00
|
|
|
" Returns the TOC of this LyX document."
|
2007-09-14 14:37:14 +00:00
|
|
|
paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2,
|
|
|
|
'Subsection' : 3, 'Subsubsection': 4}
|
2004-10-16 22:56:10 +00:00
|
|
|
allowed_insets = ['Quotes']
|
2007-09-14 14:37:14 +00:00
|
|
|
allowed_parameters = ('\\paragraph_spacing', '\\noindent',
|
|
|
|
'\\align', '\\labelwidthstring',
|
|
|
|
"\\start_of_appendix", "\\leftindent")
|
2004-10-16 22:56:10 +00:00
|
|
|
sections = []
|
|
|
|
for section in paragraphs_filter.keys():
|
|
|
|
sections.append('\\begin_layout %s' % section)
|
|
|
|
|
|
|
|
toc_par = []
|
|
|
|
i = 0
|
|
|
|
while 1:
|
|
|
|
i = find_tokens(self.body, sections, i)
|
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
|
|
|
|
j = find_end_of(self.body, i + 1, '\\begin_layout', '\\end_layout')
|
|
|
|
if j == -1:
|
|
|
|
self.warning('Incomplete file.', 0)
|
|
|
|
break
|
|
|
|
|
2006-08-02 15:45:44 +00:00
|
|
|
section = self.body[i].split()[1]
|
2004-10-16 22:56:10 +00:00
|
|
|
if section[-1] == '*':
|
|
|
|
section = section[:-1]
|
|
|
|
|
|
|
|
par = []
|
|
|
|
|
|
|
|
k = i + 1
|
|
|
|
# skip paragraph parameters
|
2007-09-14 14:37:14 +00:00
|
|
|
while not self.body[k].strip() or self.body[k].split()[0] \
|
|
|
|
in allowed_parameters:
|
|
|
|
k += 1
|
2004-10-16 22:56:10 +00:00
|
|
|
|
|
|
|
while k < j:
|
|
|
|
if check_token(self.body[k], '\\begin_inset'):
|
2006-08-02 15:45:44 +00:00
|
|
|
inset = self.body[k].split()[1]
|
2004-10-16 22:56:10 +00:00
|
|
|
end = find_end_of_inset(self.body, k)
|
|
|
|
if end == -1 or end > j:
|
|
|
|
self.warning('Malformed file.', 0)
|
|
|
|
|
|
|
|
if inset in allowed_insets:
|
|
|
|
par.extend(self.body[k: end+1])
|
|
|
|
k = end + 1
|
|
|
|
else:
|
|
|
|
par.append(self.body[k])
|
2007-09-14 14:37:14 +00:00
|
|
|
k += 1
|
2004-10-16 22:56:10 +00:00
|
|
|
|
|
|
|
# trim empty lines in the end.
|
2007-09-09 18:37:50 +00:00
|
|
|
while par and par[-1].strip() == '':
|
2004-10-16 22:56:10 +00:00
|
|
|
par.pop()
|
|
|
|
|
|
|
|
toc_par.append(Paragraph(section, par))
|
|
|
|
|
|
|
|
i = j + 1
|
|
|
|
|
|
|
|
return toc_par
|
|
|
|
|
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
class File(LyX_base):
|
2004-10-28 11:21:27 +00:00
|
|
|
" This class reads existing LyX files."
|
2007-09-14 14:37:14 +00:00
|
|
|
|
|
|
|
def __init__(self, end_format = 0, input = "", output = "", error = "",
|
|
|
|
debug = default_debug__, try_hard = 0, cjk_encoding = ''):
|
|
|
|
LyX_base.__init__(self, end_format, input, output, error,
|
|
|
|
debug, try_hard, cjk_encoding)
|
2004-10-16 22:56:10 +00:00
|
|
|
self.read()
|
|
|
|
|
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
class NewFile(LyX_base):
|
2004-10-28 11:21:27 +00:00
|
|
|
" This class is to create new LyX files."
|
2004-10-16 22:56:10 +00:00
|
|
|
def set_header(self, **params):
|
|
|
|
# set default values
|
|
|
|
self.header.extend([
|
2007-09-14 14:37:14 +00:00
|
|
|
"#LyX xxxx created this file."
|
|
|
|
"For more info see http://www.lyx.org/",
|
2004-10-16 22:56:10 +00:00
|
|
|
"\\lyxformat xxx",
|
|
|
|
"\\begin_document",
|
|
|
|
"\\begin_header",
|
|
|
|
"\\textclass article",
|
|
|
|
"\\language english",
|
|
|
|
"\\inputencoding auto",
|
The Grand Font Selection Redesign:
* lib/lyx2lyx/LyX.py (format_relation): add file format 247 (from Georg BAUM).
* lib/lyx2lyx/lyx_1_5.py: add convert_font_settings, revert_font_settings (from Georg BAUM).
* lib/chkconfig.ltx: Test for newly supported font packages
* lib/doc/LaTeXConfig.lyx.in: document newly supported font packages
* lib/doc/UserGuide.lyx: document new UI.
* lib/doc/Extended.lyx: update PostScript font documentation
* development/FORMAT: document file format change 246->247.
* src/tex-strings.[Ch]: new strings tex_fonts_roman, tex_fonts_sans,
tex_fonts_monospaced (with GUI equivalents).
* src/buffer.C: Format up to 247.
* src/bufferparams.C:
new params fontsRoman, fontsSans, fontsTypewriter, fontsDefaultFamily,
fontsSC, fontsOSF, fontsSansScale and fontsTypewriterScale
(LyXFont const BufferParams::getFont): consider switch of default family.
(string const BufferParams::loadFonts): new method to get all the LaTeX
font stuff done.
* src/paragraph.C
(LyXFont const Paragraph::getFont):
(LyXFont const Paragraph::getLabelFont):
(LyXFont const Paragraph::getLayoutFont): user buffer's not textclass's
default font
* src/text.C
(int LyXText::leftMargin):
(int LyXText::rightMargin): user buffer's not textclass's default font
* src/text2.C
(LyXFont LyXText::getFont):
(LyXFont LyXText::getLayoutFont):
(LyXFont LyXText::getLabelFont): check if the family of the default document
font has been customized.
* src/frontends/gtk/GDocument.[Ch]: implement new font ui (from Georg BAUM).
* src/frontends/gtk/glade/document.glade: implement new font ui (from Georg BAUM).
* src/frontends/qt3/Makefile.dialogs: add new FontModuleBase
* src/frontends/qt3/ui/FontModuleBase.ui: new File
* src/frontends/qt3/ui/TextLayoutModuleBase.ui: remove font widgets
* src/frontends/qt3/QDocument.C
* src/frontends/qt3/QDocumentDialog.[Ch]: implement new font ui
* src/frontends/qt4/Makefile.dialogs: add new FontUi
* src/frontends/qt4/QDocumentDialog.[Ch]: implement new font ui
* src/frontends/qt4/ui/FontUi.ui: new File
* src/frontends/qt4/ui/TextLayoutUi.ui: remove font widgets
* src/frontends/qt4/ui/compile_uic.sh: add new FontUi
* src/frontends/xforms/FormDocument.[Ch]: implement new font ui
* src/frontends/xforms/forms/form_document.fd: add new font tab.
* src/frontends/controllers/ControlDocument.[Ch]
(char ControlDocument::fontfamilies):
(char ControlDocument::fontfamilies_gui):
(bool ControlDocument::isFontAvailable):
(bool ControlDocument::providesSC):
(bool ControlDocument::providesOSF):
(bool ControlDocument::providesScale): new methods, providing font info.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@14143 a592a061-630c-0410-9148-cb99ea01b6c8
2006-06-19 08:10:17 +00:00
|
|
|
"\\font_roman default",
|
|
|
|
"\\font_sans default",
|
|
|
|
"\\font_typewriter default",
|
|
|
|
"\\font_default_family default",
|
|
|
|
"\\font_sc false",
|
|
|
|
"\\font_osf false",
|
|
|
|
"\\font_sf_scale 100",
|
|
|
|
"\\font_tt_scale 100",
|
2004-10-16 22:56:10 +00:00
|
|
|
"\\graphics default",
|
|
|
|
"\\paperfontsize default",
|
|
|
|
"\\papersize default",
|
|
|
|
"\\use_geometry false",
|
|
|
|
"\\use_amsmath 1",
|
|
|
|
"\\cite_engine basic",
|
|
|
|
"\\use_bibtopic false",
|
|
|
|
"\\paperorientation portrait",
|
|
|
|
"\\secnumdepth 3",
|
|
|
|
"\\tocdepth 3",
|
|
|
|
"\\paragraph_separation indent",
|
|
|
|
"\\defskip medskip",
|
|
|
|
"\\quotes_language english",
|
|
|
|
"\\papercolumns 1",
|
|
|
|
"\\papersides 1",
|
|
|
|
"\\paperpagestyle default",
|
|
|
|
"\\tracking_changes false",
|
|
|
|
"\\end_header"])
|
|
|
|
|
|
|
|
self.format = get_end_format()
|
|
|
|
for param in params:
|
|
|
|
self.set_parameter(param, params[param])
|
|
|
|
|
|
|
|
|
|
|
|
def set_body(self, paragraphs):
|
|
|
|
self.body.extend(['\\begin_body',''])
|
|
|
|
|
|
|
|
for par in paragraphs:
|
|
|
|
self.body.extend(par.asLines())
|
|
|
|
|
|
|
|
self.body.extend(['','\\end_body', '\\end_document'])
|
|
|
|
|
|
|
|
|
|
|
|
class Paragraph:
|
2007-09-14 14:37:14 +00:00
|
|
|
# unfinished implementation, it is missing the Text and Insets
|
|
|
|
# representation.
|
2004-10-28 11:21:27 +00:00
|
|
|
" This class represents the LyX paragraphs."
|
2004-10-16 22:56:10 +00:00
|
|
|
def __init__(self, name, body=[], settings = [], child = []):
|
2004-10-28 11:21:27 +00:00
|
|
|
""" Parameters:
|
|
|
|
name: paragraph name.
|
|
|
|
body: list of lines of body text.
|
|
|
|
child: list of paragraphs that descend from this paragraph.
|
|
|
|
"""
|
2004-10-16 22:56:10 +00:00
|
|
|
self.name = name
|
|
|
|
self.body = body
|
|
|
|
self.settings = settings
|
|
|
|
self.child = child
|
|
|
|
|
|
|
|
def asLines(self):
|
2007-09-14 14:37:14 +00:00
|
|
|
""" Converts the paragraph to a list of strings, representing
|
|
|
|
it in the LyX file."""
|
|
|
|
|
2004-10-16 22:56:10 +00:00
|
|
|
result = ['','\\begin_layout %s' % self.name]
|
|
|
|
result.extend(self.settings)
|
|
|
|
result.append('')
|
|
|
|
result.extend(self.body)
|
|
|
|
result.append('\\end_layout')
|
|
|
|
|
|
|
|
if not self.child:
|
|
|
|
return result
|
|
|
|
|
|
|
|
result.append('\\begin_deeper')
|
|
|
|
for node in self.child:
|
|
|
|
result.extend(node.asLines())
|
|
|
|
result.append('\\end_deeper')
|
|
|
|
|
|
|
|
return result
|