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 -*-
|
2018-02-23 07:58:16 +00:00
|
|
|
# Copyright (C) 2002-2018 The LyX Team
|
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
|
2011-08-25 23:10:36 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
" The LyX module has all the rules related with different lyx file formats."
|
|
|
|
|
2019-05-15 15:32:09 +00:00
|
|
|
from parser_tools import (get_value, check_token, find_token, find_tokens,
|
|
|
|
find_end_of, find_complete_lines)
|
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
|
2016-06-12 19:21:15 +00:00
|
|
|
import io
|
2017-03-15 10:06:30 +00:00
|
|
|
import codecs
|
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
|
2018-04-28 13:42:31 +00:00
|
|
|
stable_version = True
|
2007-05-31 12:02:47 +00:00
|
|
|
except: # we are running from build directory so assume the last version
|
2018-03-26 14:41:11 +00:00
|
|
|
version__ = '2.4'
|
2018-04-28 13:42:31 +00:00
|
|
|
stable_version = False
|
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
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
# Provide support for both python 2 and 3
|
|
|
|
PY2 = sys.version_info[0] == 2
|
|
|
|
# End of code to support for both python 2 and 3
|
|
|
|
|
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*)")
|
2019-06-05 13:06:09 +00:00
|
|
|
original_version = re.compile(b".*?LyX ([\\d.]*)")
|
|
|
|
original_tex2lyx_version = re.compile(b".*?tex2lyx ([\\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)),
|
2008-11-15 22:55:53 +00:00
|
|
|
("1_1_5", [216], ["1.1", "1.1.5","1.1.5.1","1.1.5.2"]),
|
|
|
|
("1_1_6_0", [217], ["1.1", "1.1.6","1.1.6.1","1.1.6.2"]),
|
|
|
|
("1_1_6_3", [218], ["1.1", "1.1.6.3","1.1.6.4"]),
|
2007-09-14 14:37:14 +00:00
|
|
|
("1_2", [220], minor_versions("1.2" , 4)),
|
|
|
|
("1_3", [221], minor_versions("1.3" , 7)),
|
2015-12-20 16:37:29 +00:00
|
|
|
# Note that range(i,j) is up to j *excluded*.
|
2015-03-11 12:04:46 +00:00
|
|
|
("1_4", list(range(222,246)), minor_versions("1.4" , 5)),
|
|
|
|
("1_5", list(range(246,277)), minor_versions("1.5" , 7)),
|
|
|
|
("1_6", list(range(277,346)), minor_versions("1.6" , 10)),
|
2015-04-06 04:00:11 +00:00
|
|
|
("2_0", list(range(346,414)), minor_versions("2.0" , 8)),
|
2016-04-12 00:41:23 +00:00
|
|
|
("2_1", list(range(414,475)), minor_versions("2.1" , 5)),
|
2018-02-23 07:58:16 +00:00
|
|
|
("2_2", list(range(475,509)), minor_versions("2.2" , 4)),
|
|
|
|
("2_3", list(range(509,545)), minor_versions("2.3" , 0)),
|
|
|
|
("2_4", (), minor_versions("2.4" , 0))
|
2011-06-04 13:08:22 +00:00
|
|
|
]
|
2004-10-09 21:32:56 +00:00
|
|
|
|
2008-05-08 10:01:38 +00:00
|
|
|
####################################################################
|
|
|
|
# This is useful just for development versions #
|
|
|
|
# if the list of supported formats is empty get it from last step #
|
|
|
|
if not format_relation[-1][1]:
|
|
|
|
step, mode = format_relation[-1][0], "convert"
|
|
|
|
convert = getattr(__import__("lyx_" + step), mode)
|
|
|
|
format_relation[-1] = (step,
|
|
|
|
[conv[0] for conv in convert],
|
|
|
|
format_relation[-1][2])
|
|
|
|
# #
|
|
|
|
####################################################################
|
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
|
|
|
|
|
|
|
|
2008-11-15 22:55:53 +00:00
|
|
|
def format_info():
|
2018-04-28 13:42:31 +00:00
|
|
|
" Returns a list with the supported file formats."
|
|
|
|
template = """
|
|
|
|
%s\tstable format: %s
|
|
|
|
\tstable versions: %s
|
|
|
|
\tdevelopment formats: %s
|
2008-11-15 22:55:53 +00:00
|
|
|
"""
|
2018-04-28 13:42:31 +00:00
|
|
|
|
|
|
|
out = "version: formats and versions"
|
2008-11-15 22:55:53 +00:00
|
|
|
for version in format_relation:
|
|
|
|
major = str(version[2][0])
|
|
|
|
versions = str(version[2][1:])
|
|
|
|
if len(version[1]) == 1:
|
|
|
|
formats = str(version[1][0])
|
2018-04-28 13:42:31 +00:00
|
|
|
stable_format = str(version[1][0])
|
|
|
|
elif not stable_version and major == version__:
|
|
|
|
stable_format = "-- not yet --"
|
|
|
|
versions = "-- not yet --"
|
|
|
|
formats = "%s - %s" % (version[1][0], version[1][-1])
|
2008-11-15 22:55:53 +00:00
|
|
|
else:
|
2018-04-28 13:42:31 +00:00
|
|
|
formats = "%s - %s" % (version[1][0], version[1][-2])
|
|
|
|
stable_format = str(version[1][-1])
|
|
|
|
|
|
|
|
out += template % (major, stable_format, versions, formats)
|
2008-11-15 22:55:53 +00:00
|
|
|
return out + '\n'
|
|
|
|
|
|
|
|
|
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."
|
2011-06-04 13:08:22 +00:00
|
|
|
# this check will fail only when we have a new version
|
|
|
|
# and there is no format change yet.
|
|
|
|
if format_relation[-1][1]:
|
|
|
|
return format_relation[-1][1][-1]
|
|
|
|
return format_relation[-2][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"
|
2008-11-07 19:17:35 +00:00
|
|
|
if textclass.startswith("docbook") or textclass.startswith("agu-"):
|
2004-08-15 16:29:04 +00:00
|
|
|
return "docbook"
|
|
|
|
return "latex"
|
|
|
|
|
|
|
|
|
2005-08-18 17:33:26 +00:00
|
|
|
def trim_eol(line):
|
|
|
|
" Remove end of line char(s)."
|
2014-05-29 12:44:38 +00:00
|
|
|
if line[-1] != '\n' and line[-1] != '\r':
|
2014-05-29 11:15:07 +00:00
|
|
|
# May happen for the last line of a document
|
|
|
|
return line
|
2005-08-18 17:33:26 +00:00
|
|
|
if line[-2:-1] == '\r':
|
|
|
|
return line[:-2]
|
|
|
|
else:
|
|
|
|
return line[:-1]
|
|
|
|
|
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
def trim_eol_binary(line):
|
|
|
|
" Remove end of line char(s)."
|
|
|
|
if line[-1] != 10 and line[-1] != 13:
|
|
|
|
# May happen for the last line of a document
|
|
|
|
return line
|
|
|
|
if line[-2:-1] == 13:
|
|
|
|
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"
|
2008-02-09 07:52:26 +00:00
|
|
|
if inputencoding == "utf8x":
|
|
|
|
return "utf8"
|
2006-08-07 14:10:41 +00:00
|
|
|
# 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."""
|
2008-03-18 19:41:50 +00:00
|
|
|
|
2016-06-16 20:05:56 +00:00
|
|
|
def __init__(self, end_format = 0, input = u'', output = u'', error = u'',
|
|
|
|
debug = default_debug__, try_hard = 0, cjk_encoding = u'',
|
|
|
|
final_version = u'', systemlyxdir = u'', language = u'english',
|
|
|
|
encoding = u'auto'):
|
2007-02-13 16:57:48 +00:00
|
|
|
|
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.
|
|
|
|
"""
|
2016-06-12 19:21:15 +00:00
|
|
|
self.choose_input(input)
|
|
|
|
self.output = 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)
|
2008-11-15 22:55:53 +00:00
|
|
|
|
|
|
|
# In case the target version and format are both specified
|
|
|
|
# verify that they are compatible. If not send a warning
|
|
|
|
# and ignore the version.
|
|
|
|
if final_version:
|
|
|
|
message = "Incompatible version %s for specified format %d" % (
|
|
|
|
final_version, self.end_format)
|
|
|
|
for version in format_relation:
|
|
|
|
if self.end_format in version[1]:
|
|
|
|
if final_version not in version[2]:
|
|
|
|
self.warning(message)
|
|
|
|
final_version = ""
|
|
|
|
elif final_version:
|
|
|
|
for version in format_relation:
|
|
|
|
if final_version in version[2]:
|
|
|
|
# set the last format for that version
|
|
|
|
self.end_format = version[1][-1]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
final_version = ""
|
2004-08-15 16:29:04 +00:00
|
|
|
else:
|
|
|
|
self.end_format = get_end_format()
|
|
|
|
|
2008-11-15 22:55:53 +00:00
|
|
|
if not final_version:
|
|
|
|
for step in format_relation:
|
|
|
|
if self.end_format in step[1]:
|
|
|
|
final_version = step[2][1]
|
|
|
|
self.final_version = final_version
|
|
|
|
self.warning("Final version: %s" % self.final_version, 10)
|
|
|
|
self.warning("Final format: %d" % self.end_format, 10)
|
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
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
|
2015-11-11 20:35:12 +00:00
|
|
|
self.systemlyxdir = systemlyxdir
|
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:
|
2010-02-12 13:32:38 +00:00
|
|
|
self.warning("Quitting.")
|
2005-07-05 15:28:44 +00:00
|
|
|
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
|
|
|
|
2016-06-11 12:45:18 +00:00
|
|
|
# First pass: Read header to determine file encoding
|
2016-06-12 19:21:15 +00:00
|
|
|
# If we are running under python3 then all strings are binary in this
|
|
|
|
# pass. In some cases we need to convert binary to unicode in order to
|
|
|
|
# use our parser tools. Since we do not know the true encoding yet we
|
|
|
|
# use latin1. This works since a) the parts we are interested in are
|
|
|
|
# pure ASCII (subset of latin1) and b) in contrast to pure ascii or
|
|
|
|
# utf8, one can decode any 8byte string using latin1.
|
2017-03-15 10:06:30 +00:00
|
|
|
first_line = True
|
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:
|
2016-06-11 12:45:18 +00:00
|
|
|
# eof found before end of header
|
|
|
|
self.error("Invalid LyX file: Missing body.")
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2017-03-15 10:06:30 +00:00
|
|
|
if first_line:
|
|
|
|
# Remove UTF8 BOM marker if present
|
|
|
|
if line.startswith(codecs.BOM_UTF8):
|
|
|
|
line = line[len(codecs.BOM_UTF8):]
|
|
|
|
|
|
|
|
first_line = False
|
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
if PY2:
|
|
|
|
line = trim_eol(line)
|
|
|
|
decoded = line
|
|
|
|
else:
|
|
|
|
line = trim_eol_binary(line)
|
|
|
|
decoded = line.decode('latin1')
|
|
|
|
if check_token(decoded, '\\begin_preamble'):
|
2016-06-24 19:03:59 +00:00
|
|
|
while True:
|
2005-08-18 17:33:26 +00:00
|
|
|
line = self.input.readline()
|
|
|
|
if not line:
|
2016-06-11 12:45:18 +00:00
|
|
|
# eof found before end of header
|
|
|
|
self.error("Invalid LyX file: Missing body.")
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
if PY2:
|
|
|
|
line = trim_eol(line)
|
|
|
|
decoded = line
|
|
|
|
else:
|
|
|
|
line = trim_eol_binary(line)
|
|
|
|
decoded = line.decode('latin1')
|
|
|
|
if check_token(decoded, '\\end_preamble'):
|
2005-08-18 17:33:26 +00:00
|
|
|
break
|
2008-03-18 19:41:50 +00:00
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
if decoded.split()[:0] in ("\\layout",
|
2007-09-14 14:37:14 +00:00
|
|
|
"\\begin_layout", "\\begin_body"):
|
2008-03-18 19:41:50 +00:00
|
|
|
|
2007-09-14 14:37:14 +00:00
|
|
|
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)
|
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
if check_token(decoded, '\\end_preamble'):
|
2005-08-18 17:33:26 +00:00
|
|
|
continue
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2016-06-24 18:57:28 +00:00
|
|
|
line = line.rstrip()
|
2005-08-18 17:33:26 +00:00
|
|
|
if not line:
|
|
|
|
continue
|
2005-07-06 18:35:35 +00:00
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
if decoded.split()[0] in ("\\layout", "\\begin_layout",
|
2007-09-14 14:37:14 +00:00
|
|
|
"\\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)
|
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
if PY2:
|
|
|
|
i = find_token(self.header, '\\textclass', 0)
|
|
|
|
else:
|
|
|
|
i = find_token(self.header, b'\\textclass', 0)
|
2008-03-18 19:41:50 +00:00
|
|
|
if i == -1:
|
|
|
|
self.warning("Malformed LyX file: Missing '\\textclass'.")
|
2016-06-12 19:21:15 +00:00
|
|
|
if PY2:
|
|
|
|
i = find_token(self.header, '\\lyxformat', 0) + 1
|
|
|
|
self.header[i:i] = ['\\textclass article']
|
|
|
|
else:
|
|
|
|
i = find_token(self.header, b'\\lyxformat', 0) + 1
|
|
|
|
self.header[i:i] = [b'\\textclass article']
|
|
|
|
|
|
|
|
if PY2:
|
|
|
|
self.textclass = get_value(self.header, "\\textclass", 0,
|
|
|
|
default = "")
|
|
|
|
self.language = get_value(self.header, "\\language", 0,
|
|
|
|
default = "english")
|
|
|
|
self.inputencoding = get_value(self.header, "\\inputencoding", 0,
|
|
|
|
default = "auto")
|
|
|
|
else:
|
|
|
|
self.textclass = get_value(self.header, b"\\textclass", 0,
|
|
|
|
default = b"")
|
|
|
|
self.language = get_value(self.header, b"\\language", 0,
|
|
|
|
default = b"english").decode('ascii')
|
|
|
|
self.inputencoding = get_value(self.header, b"\\inputencoding", 0,
|
|
|
|
default = b"auto").decode('ascii')
|
|
|
|
self.format = self.read_format()
|
2017-03-27 09:36:30 +00:00
|
|
|
self.initial_format = self.format
|
2007-09-14 14:37:14 +00:00
|
|
|
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
|
2013-02-23 11:23:29 +00:00
|
|
|
# Do not forget the textclass (Debian bug #700828)
|
|
|
|
self.textclass = self.textclass.decode(self.encoding)
|
2016-06-12 19:21:15 +00:00
|
|
|
self.backend = get_backend(self.textclass)
|
2006-08-07 14:10:41 +00:00
|
|
|
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)
|
2016-06-11 12:45:18 +00:00
|
|
|
for i in range(len(self.body)):
|
|
|
|
self.body[i] = self.body[i].decode(self.encoding)
|
2006-08-07 14:10:41 +00:00
|
|
|
|
|
|
|
# Read document body
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2006-08-07 14:10:41 +00:00
|
|
|
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."
|
2016-06-12 19:21:15 +00:00
|
|
|
self.choose_output(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']
|
2008-03-18 19:41:50 +00:00
|
|
|
header = self.header[:i] + preamble + self.header[i:]
|
2005-08-18 17:33:26 +00:00
|
|
|
else:
|
|
|
|
header = self.header
|
|
|
|
|
2019-01-23 09:12:28 +00:00
|
|
|
for line in header + [u''] + self.body:
|
|
|
|
self.output.write(line+u'\n')
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
def choose_output(self, output):
|
|
|
|
"""Choose output streams dealing transparently with
|
2005-12-01 19:00:07 +00:00
|
|
|
compressed files."""
|
2004-08-15 16:29:04 +00:00
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
# This is a bit complicated, because we need to be compatible both with
|
|
|
|
# python 2 and python 3. Therefore we handle the encoding here and not
|
|
|
|
# when writing individual lines and may need up to 3 layered file like
|
|
|
|
# interfaces.
|
|
|
|
if self.compressed:
|
|
|
|
if output:
|
|
|
|
outputfileobj = open(output, 'wb')
|
|
|
|
else:
|
|
|
|
# We cannot not use stdout directly since it needs text, not bytes in python 3
|
|
|
|
outputfileobj = os.fdopen(sys.stdout.fileno(), 'wb')
|
|
|
|
# We cannot not use gzip.open() since it is not supported by python 2
|
|
|
|
zipbuffer = gzip.GzipFile(mode='wb', fileobj=outputfileobj)
|
|
|
|
# We do not want to use different newlines on different OSes inside zipped files
|
|
|
|
self.output = io.TextIOWrapper(zipbuffer, encoding=self.encoding, newline='\n')
|
2005-12-01 19:00:07 +00:00
|
|
|
else:
|
2016-06-12 19:21:15 +00:00
|
|
|
if output:
|
|
|
|
self.output = io.open(output, 'w', encoding=self.encoding)
|
|
|
|
else:
|
|
|
|
self.output = io.open(sys.stdout.fileno(), 'w', encoding=self.encoding)
|
|
|
|
|
2005-12-01 19:00:07 +00:00
|
|
|
|
2016-06-12 19:21:15 +00:00
|
|
|
def choose_input(self, input):
|
|
|
|
"""Choose input stream, dealing transparently with
|
|
|
|
compressed files."""
|
|
|
|
|
|
|
|
# Since we do not know the encoding yet we need to read the input as
|
|
|
|
# bytes in binary mode, and convert later to unicode.
|
2016-06-16 20:05:56 +00:00
|
|
|
if input and input != u'-':
|
2005-12-01 19:00:07 +00:00
|
|
|
self.dir = os.path.dirname(os.path.abspath(input))
|
|
|
|
try:
|
|
|
|
gzip.open(input).readline()
|
|
|
|
self.input = gzip.open(input)
|
2016-06-12 19:21:15 +00:00
|
|
|
self.compressed = True
|
2005-12-01 19:00:07 +00:00
|
|
|
except:
|
2016-06-12 19:21:15 +00:00
|
|
|
self.input = open(input, 'rb')
|
|
|
|
self.compressed = False
|
2005-12-01 19:00:07 +00:00
|
|
|
else:
|
2016-06-16 20:05:56 +00:00
|
|
|
self.dir = u''
|
2016-06-12 19:21:15 +00:00
|
|
|
self.input = os.fdopen(sys.stdin.fileno(), 'rb')
|
|
|
|
self.compressed = False
|
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:
|
2019-06-05 13:06:09 +00:00
|
|
|
if line[0:1] != b"#":
|
2004-08-15 16:29:04 +00:00
|
|
|
return None
|
|
|
|
|
2019-06-05 13:06:09 +00:00
|
|
|
line = line.replace(b"fix",b".")
|
2014-05-12 20:06:24 +00:00
|
|
|
# need to test original_tex2lyx_version first because tex2lyx
|
|
|
|
# writes "#LyX file created by tex2lyx 2.2"
|
|
|
|
result = original_tex2lyx_version.match(line)
|
|
|
|
if not result:
|
|
|
|
result = original_version.match(line)
|
|
|
|
if result:
|
|
|
|
# Special know cases: reLyX and KLyX
|
2019-06-05 13:06:09 +00:00
|
|
|
if line.find(b"reLyX") != -1 or line.find(b"KLyX") != -1:
|
2014-05-12 20:06:24 +00:00
|
|
|
return "0.12"
|
2004-08-15 16:29:04 +00:00
|
|
|
if result:
|
2006-08-07 14:10:41 +00:00
|
|
|
res = result.group(1)
|
|
|
|
if not res:
|
|
|
|
self.warning(line)
|
|
|
|
#self.warning("Version %s" % result.group(1))
|
2019-06-05 13:06:09 +00:00
|
|
|
return res.decode('ascii') if not PY2 else res
|
2006-08-07 14:10:41 +00:00
|
|
|
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."
|
2015-03-31 14:03:48 +00:00
|
|
|
|
|
|
|
initial_comment = " ".join(["#LyX %s created this file." % version__,
|
2017-12-17 01:24:26 +00:00
|
|
|
"For more info see https://www.lyx.org/"])
|
2015-03-31 14:03:48 +00:00
|
|
|
|
|
|
|
# Simple heuristic to determine the comment that always starts
|
|
|
|
# a lyx file
|
|
|
|
if self.header[0].startswith("#"):
|
|
|
|
self.header[0] = initial_comment
|
|
|
|
else:
|
|
|
|
self.header.insert(0, initial_comment)
|
|
|
|
|
|
|
|
# Old lyx files had a two lines comment header:
|
|
|
|
# 1) the first line had the user who had created it
|
|
|
|
# 2) the second line had the lyx version used
|
|
|
|
# later we decided that 1) was a privacy risk for no gain
|
|
|
|
# here we remove the second line effectively erasing 1)
|
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:
|
2016-06-12 19:21:15 +00:00
|
|
|
if PY2:
|
|
|
|
result = fileformat.match(line)
|
|
|
|
else:
|
|
|
|
result = fileformat.match(line.decode('ascii'))
|
2004-08-15 16:29:04 +00:00
|
|
|
if result:
|
|
|
|
return self.lyxformat(result.group(1))
|
|
|
|
else:
|
2016-06-12 19:21:15 +00:00
|
|
|
self.error("Invalid LyX File: Missing format.")
|
2004-08-15 16:29:04 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2008-01-10 07:22:43 +00:00
|
|
|
#Note that the module will be added at the END of the extant ones
|
|
|
|
def add_module(self, module):
|
2019-05-15 15:32:09 +00:00
|
|
|
" Append module to the modules list."
|
2008-01-10 07:22:43 +00:00
|
|
|
i = find_token(self.header, "\\begin_modules", 0)
|
|
|
|
if i == -1:
|
|
|
|
#No modules yet included
|
|
|
|
i = find_token(self.header, "\\textclass", 0)
|
|
|
|
if i == -1:
|
|
|
|
self.warning("Malformed LyX document: No \\textclass!!")
|
|
|
|
return
|
|
|
|
modinfo = ["\\begin_modules", module, "\\end_modules"]
|
|
|
|
self.header[i + 1: i + 1] = modinfo
|
|
|
|
return
|
|
|
|
j = find_token(self.header, "\\end_modules", i)
|
|
|
|
if j == -1:
|
2008-01-12 06:43:44 +00:00
|
|
|
self.warning("(add_module)Malformed LyX document: No \\end_modules.")
|
2008-01-10 07:22:43 +00:00
|
|
|
return
|
|
|
|
k = find_token(self.header, module, i)
|
|
|
|
if k != -1 and k < j:
|
|
|
|
return
|
|
|
|
self.header.insert(j, module)
|
|
|
|
|
|
|
|
|
2019-05-15 15:32:09 +00:00
|
|
|
def del_module(self, module):
|
|
|
|
" Delete `module` from module list, return success."
|
|
|
|
modlist = self.get_module_list()
|
|
|
|
if module not in modlist:
|
|
|
|
return False
|
2019-08-13 05:28:12 +00:00
|
|
|
self.set_module_list([line for line in modlist if line != module])
|
2019-05-15 15:32:09 +00:00
|
|
|
return True
|
|
|
|
|
This commit changes the way individual LyXModule's are represented, both internally and in the .lyx files. The earlier version represented them by their `descriptive name', e.g., "Endnote" or "Theorems (AMS)", these being the same names used in the UI. This was a mistake, as becomes readily apparent when one starts to think about translating these strings. The modules ought to be represented by their filename, without the extension, just as TextClass's are.
The changes that accomplish this part are in ModuleList.{h,cpp}, configure.py, and the *.module files themselves. This is a format change, and the lyx2lyx is in those files.
By itself, that change would not be major, except for the fact that we do not want the module to be represented in the UI by its filename---e.g., theorems-std---but rather by a descriptive name, such as "Theorems". But that change turns out to be wholly non-trivial. The mechanism for choosing modules was the same as---indeed, was borrowed from---that in GuiCitation: You get a list of modules, and choosing them involves moving strings from one QListView to another. The models underlying these views are just QStringListModels, which means that, when you want to know what modules have been selected, you see what strings are in the "selected" QListView. But these are just the descriptive names, and we can't look up a module by its descriptive name if it's been translated. That, indeed, was the whole point of the change to the new representation.
So, we need a more complicated model underlying the QListView, one that will pair an identifying string---the filename minus the extension, in this case---with each item. This turns out not to be terribly difficult, though it took rather a while for me to understand why it's not difficult. There are two parts:
(i) GuiSelectionManger gets re-written to use any QAbstractListModel, not just a QStringListModel. This actually seems to improve the code, independently.
(ii) We then subclass QAbstractListModel to get the associated ID string, using the Qt::UserRole slot associated with each item to store its ID. This would be almost completely trivial if QAbstractListItem::itemData() included the QVariant associated with this role, but it doesn't, so there are some additional hoops through which to jump.
The new model, a GuiIdListModel, is defined in the files by that name. The changes in GuiSelectionManger.{h,cpp} make it more abstract; the changes in GuiDocument.{h,cpp} adapt it to the new framework.
I've also updated the module documenation to accord with this change.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22501 a592a061-630c-0410-9148-cb99ea01b6c8
2008-01-12 04:28:12 +00:00
|
|
|
def get_module_list(self):
|
2019-05-15 15:32:09 +00:00
|
|
|
" Return list of modules."
|
This commit changes the way individual LyXModule's are represented, both internally and in the .lyx files. The earlier version represented them by their `descriptive name', e.g., "Endnote" or "Theorems (AMS)", these being the same names used in the UI. This was a mistake, as becomes readily apparent when one starts to think about translating these strings. The modules ought to be represented by their filename, without the extension, just as TextClass's are.
The changes that accomplish this part are in ModuleList.{h,cpp}, configure.py, and the *.module files themselves. This is a format change, and the lyx2lyx is in those files.
By itself, that change would not be major, except for the fact that we do not want the module to be represented in the UI by its filename---e.g., theorems-std---but rather by a descriptive name, such as "Theorems". But that change turns out to be wholly non-trivial. The mechanism for choosing modules was the same as---indeed, was borrowed from---that in GuiCitation: You get a list of modules, and choosing them involves moving strings from one QListView to another. The models underlying these views are just QStringListModels, which means that, when you want to know what modules have been selected, you see what strings are in the "selected" QListView. But these are just the descriptive names, and we can't look up a module by its descriptive name if it's been translated. That, indeed, was the whole point of the change to the new representation.
So, we need a more complicated model underlying the QListView, one that will pair an identifying string---the filename minus the extension, in this case---with each item. This turns out not to be terribly difficult, though it took rather a while for me to understand why it's not difficult. There are two parts:
(i) GuiSelectionManger gets re-written to use any QAbstractListModel, not just a QStringListModel. This actually seems to improve the code, independently.
(ii) We then subclass QAbstractListModel to get the associated ID string, using the Qt::UserRole slot associated with each item to store its ID. This would be almost completely trivial if QAbstractListItem::itemData() included the QVariant associated with this role, but it doesn't, so there are some additional hoops through which to jump.
The new model, a GuiIdListModel, is defined in the files by that name. The changes in GuiSelectionManger.{h,cpp} make it more abstract; the changes in GuiDocument.{h,cpp} adapt it to the new framework.
I've also updated the module documenation to accord with this change.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22501 a592a061-630c-0410-9148-cb99ea01b6c8
2008-01-12 04:28:12 +00:00
|
|
|
i = find_token(self.header, "\\begin_modules", 0)
|
|
|
|
if (i == -1):
|
|
|
|
return []
|
|
|
|
j = find_token(self.header, "\\end_modules", i)
|
|
|
|
return self.header[i + 1 : j]
|
|
|
|
|
|
|
|
|
|
|
|
def set_module_list(self, mlist):
|
2019-05-15 15:32:09 +00:00
|
|
|
i = find_token(self.header, "\\begin_modules", 0)
|
|
|
|
if (i == -1):
|
This commit changes the way individual LyXModule's are represented, both internally and in the .lyx files. The earlier version represented them by their `descriptive name', e.g., "Endnote" or "Theorems (AMS)", these being the same names used in the UI. This was a mistake, as becomes readily apparent when one starts to think about translating these strings. The modules ought to be represented by their filename, without the extension, just as TextClass's are.
The changes that accomplish this part are in ModuleList.{h,cpp}, configure.py, and the *.module files themselves. This is a format change, and the lyx2lyx is in those files.
By itself, that change would not be major, except for the fact that we do not want the module to be represented in the UI by its filename---e.g., theorems-std---but rather by a descriptive name, such as "Theorems". But that change turns out to be wholly non-trivial. The mechanism for choosing modules was the same as---indeed, was borrowed from---that in GuiCitation: You get a list of modules, and choosing them involves moving strings from one QListView to another. The models underlying these views are just QStringListModels, which means that, when you want to know what modules have been selected, you see what strings are in the "selected" QListView. But these are just the descriptive names, and we can't look up a module by its descriptive name if it's been translated. That, indeed, was the whole point of the change to the new representation.
So, we need a more complicated model underlying the QListView, one that will pair an identifying string---the filename minus the extension, in this case---with each item. This turns out not to be terribly difficult, though it took rather a while for me to understand why it's not difficult. There are two parts:
(i) GuiSelectionManger gets re-written to use any QAbstractListModel, not just a QStringListModel. This actually seems to improve the code, independently.
(ii) We then subclass QAbstractListModel to get the associated ID string, using the Qt::UserRole slot associated with each item to store its ID. This would be almost completely trivial if QAbstractListItem::itemData() included the QVariant associated with this role, but it doesn't, so there are some additional hoops through which to jump.
The new model, a GuiIdListModel, is defined in the files by that name. The changes in GuiSelectionManger.{h,cpp} make it more abstract; the changes in GuiDocument.{h,cpp} adapt it to the new framework.
I've also updated the module documenation to accord with this change.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22501 a592a061-630c-0410-9148-cb99ea01b6c8
2008-01-12 04:28:12 +00:00
|
|
|
#No modules yet included
|
2008-01-12 06:43:44 +00:00
|
|
|
tclass = find_token(self.header, "\\textclass", 0)
|
|
|
|
if tclass == -1:
|
This commit changes the way individual LyXModule's are represented, both internally and in the .lyx files. The earlier version represented them by their `descriptive name', e.g., "Endnote" or "Theorems (AMS)", these being the same names used in the UI. This was a mistake, as becomes readily apparent when one starts to think about translating these strings. The modules ought to be represented by their filename, without the extension, just as TextClass's are.
The changes that accomplish this part are in ModuleList.{h,cpp}, configure.py, and the *.module files themselves. This is a format change, and the lyx2lyx is in those files.
By itself, that change would not be major, except for the fact that we do not want the module to be represented in the UI by its filename---e.g., theorems-std---but rather by a descriptive name, such as "Theorems". But that change turns out to be wholly non-trivial. The mechanism for choosing modules was the same as---indeed, was borrowed from---that in GuiCitation: You get a list of modules, and choosing them involves moving strings from one QListView to another. The models underlying these views are just QStringListModels, which means that, when you want to know what modules have been selected, you see what strings are in the "selected" QListView. But these are just the descriptive names, and we can't look up a module by its descriptive name if it's been translated. That, indeed, was the whole point of the change to the new representation.
So, we need a more complicated model underlying the QListView, one that will pair an identifying string---the filename minus the extension, in this case---with each item. This turns out not to be terribly difficult, though it took rather a while for me to understand why it's not difficult. There are two parts:
(i) GuiSelectionManger gets re-written to use any QAbstractListModel, not just a QStringListModel. This actually seems to improve the code, independently.
(ii) We then subclass QAbstractListModel to get the associated ID string, using the Qt::UserRole slot associated with each item to store its ID. This would be almost completely trivial if QAbstractListItem::itemData() included the QVariant associated with this role, but it doesn't, so there are some additional hoops through which to jump.
The new model, a GuiIdListModel, is defined in the files by that name. The changes in GuiSelectionManger.{h,cpp} make it more abstract; the changes in GuiDocument.{h,cpp} adapt it to the new framework.
I've also updated the module documenation to accord with this change.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22501 a592a061-630c-0410-9148-cb99ea01b6c8
2008-01-12 04:28:12 +00:00
|
|
|
self.warning("Malformed LyX document: No \\textclass!!")
|
|
|
|
return
|
2019-05-15 15:32:09 +00:00
|
|
|
i = j = tclass + 1
|
2019-06-05 13:06:09 +00:00
|
|
|
else:
|
2019-05-15 15:32:09 +00:00
|
|
|
j = find_token(self.header, "\\end_modules", i)
|
|
|
|
if j == -1:
|
|
|
|
self.warning("(set_module_list) Malformed LyX document: No \\end_modules.")
|
|
|
|
return
|
|
|
|
j += 1
|
|
|
|
if mlist:
|
|
|
|
mlist = ['\\begin_modules'] + mlist + ['\\end_modules']
|
|
|
|
self.header[i:j] = mlist
|
This commit changes the way individual LyXModule's are represented, both internally and in the .lyx files. The earlier version represented them by their `descriptive name', e.g., "Endnote" or "Theorems (AMS)", these being the same names used in the UI. This was a mistake, as becomes readily apparent when one starts to think about translating these strings. The modules ought to be represented by their filename, without the extension, just as TextClass's are.
The changes that accomplish this part are in ModuleList.{h,cpp}, configure.py, and the *.module files themselves. This is a format change, and the lyx2lyx is in those files.
By itself, that change would not be major, except for the fact that we do not want the module to be represented in the UI by its filename---e.g., theorems-std---but rather by a descriptive name, such as "Theorems". But that change turns out to be wholly non-trivial. The mechanism for choosing modules was the same as---indeed, was borrowed from---that in GuiCitation: You get a list of modules, and choosing them involves moving strings from one QListView to another. The models underlying these views are just QStringListModels, which means that, when you want to know what modules have been selected, you see what strings are in the "selected" QListView. But these are just the descriptive names, and we can't look up a module by its descriptive name if it's been translated. That, indeed, was the whole point of the change to the new representation.
So, we need a more complicated model underlying the QListView, one that will pair an identifying string---the filename minus the extension, in this case---with each item. This turns out not to be terribly difficult, though it took rather a while for me to understand why it's not difficult. There are two parts:
(i) GuiSelectionManger gets re-written to use any QAbstractListModel, not just a QStringListModel. This actually seems to improve the code, independently.
(ii) We then subclass QAbstractListModel to get the associated ID string, using the Qt::UserRole slot associated with each item to store its ID. This would be almost completely trivial if QAbstractListItem::itemData() included the QVariant associated with this role, but it doesn't, so there are some additional hoops through which to jump.
The new model, a GuiIdListModel, is defined in the files by that name. The changes in GuiSelectionManger.{h,cpp} make it more abstract; the changes in GuiDocument.{h,cpp} adapt it to the new framework.
I've also updated the module documenation to accord with this change.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22501 a592a061-630c-0410-9148-cb99ea01b6c8
2008-01-12 04:28:12 +00:00
|
|
|
|
|
|
|
|
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."
|
2015-11-01 21:27:58 +00:00
|
|
|
if self.format == self.end_format:
|
2015-11-01 21:32:52 +00:00
|
|
|
self.warning("No conversion needed: Target format %s "
|
|
|
|
"same as current format!" % self.format, default_debug__)
|
2015-11-01 21:27:58 +00:00
|
|
|
return
|
|
|
|
|
2010-09-17 12:26:55 +00:00
|
|
|
mode, conversion_chain = self.chain()
|
|
|
|
self.warning("conversion chain: " + str(conversion_chain), 3)
|
2004-10-16 22:56:10 +00:00
|
|
|
|
2010-09-17 12:26:55 +00:00
|
|
|
for step in conversion_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:
|
2010-09-17 12:26:55 +00:00
|
|
|
self.error("The conversion to an older "
|
2007-09-14 14:37:14 +00:00
|
|
|
"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
|
2010-09-17 12:26:55 +00:00
|
|
|
conversion are taken. It returns a list of modules needed to
|
2007-09-14 14:37:14 +00:00
|
|
|
convert the LyX file from self.format to self.end_format"""
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
|
|
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 = []
|
2017-03-27 09:36:30 +00:00
|
|
|
if (initial_step, self.initial_format) < (final_step, self.end_format):
|
2004-08-15 16:29:04 +00:00
|
|
|
mode = "convert"
|
2008-11-15 22:55:53 +00:00
|
|
|
full_steps = []
|
2004-08-15 16:29:04 +00:00
|
|
|
for step in format_relation:
|
2008-11-15 22:55:53 +00:00
|
|
|
if initial_step <= step[0] <= final_step and step[2][0] <= self.final_version:
|
|
|
|
full_steps.append(step)
|
|
|
|
if full_steps[0][1][-1] == self.format:
|
|
|
|
full_steps = full_steps[1:]
|
|
|
|
for step in full_steps:
|
|
|
|
steps.append(step[0])
|
2004-08-15 16:29:04 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2019-05-15 15:32:09 +00:00
|
|
|
def append_local_layout(self, new_layout):
|
|
|
|
" Append `new_layout` to the local layouts."
|
|
|
|
# new_layout may be a string or a list of strings (lines)
|
|
|
|
try:
|
|
|
|
new_layout = new_layout.splitlines()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
i = find_token(self.header, "\\begin_local_layout", 0)
|
|
|
|
if i == -1:
|
|
|
|
k = find_token(self.header, "\\language", 0)
|
|
|
|
if k == -1:
|
|
|
|
# this should not happen
|
|
|
|
self.warning("Malformed LyX document! No \\language header found!")
|
|
|
|
return
|
|
|
|
self.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
|
|
|
|
i = k - 1
|
|
|
|
|
|
|
|
j = find_end_of(self.header, i, "\\begin_local_layout", "\\end_local_layout")
|
|
|
|
if j == -1:
|
|
|
|
# this should not happen
|
|
|
|
self.warning("Malformed LyX document: Can't find end of local layout!")
|
|
|
|
return
|
|
|
|
|
|
|
|
self.header[i+1 : i+1] = new_layout
|
|
|
|
|
2019-07-07 21:31:12 +00:00
|
|
|
def del_local_layout(self, layout_def):
|
|
|
|
" Delete `layout_def` from local layouts, return success."
|
|
|
|
i = find_complete_lines(self.header, layout_def)
|
2019-05-15 15:32:09 +00:00
|
|
|
if i == -1:
|
|
|
|
return False
|
2019-07-07 21:31:12 +00:00
|
|
|
j = i+len(layout_def)
|
2019-05-15 15:32:09 +00:00
|
|
|
if (self.header[i-1] == "\\begin_local_layout" and
|
|
|
|
self.header[j] == "\\end_local_layout"):
|
|
|
|
i -=1
|
|
|
|
j +=1
|
|
|
|
self.header[i:j] = []
|
|
|
|
return True
|
|
|
|
|
|
|
|
def del_from_header(self, lines):
|
|
|
|
" Delete `lines` from the document header, return success."
|
|
|
|
i = find_complete_lines(self.header, lines)
|
|
|
|
if i == -1:
|
|
|
|
return False
|
|
|
|
j = i + len(lines)
|
|
|
|
self.header[i:j] = []
|
|
|
|
return True
|
|
|
|
|
2010-11-05 21:14:49 +00:00
|
|
|
# Part of an unfinished attempt to make lyx2lyx gave a more
|
|
|
|
# structured view of the document.
|
2010-11-05 21:13:33 +00:00
|
|
|
# def get_toc(self, depth = 4):
|
|
|
|
# " Returns the TOC of this LyX document."
|
|
|
|
# paragraphs_filter = {'Title' : 0,'Chapter' : 1, 'Section' : 2,
|
|
|
|
# 'Subsection' : 3, 'Subsubsection': 4}
|
|
|
|
# allowed_insets = ['Quotes']
|
|
|
|
# allowed_parameters = ('\\paragraph_spacing', '\\noindent',
|
|
|
|
# '\\align', '\\labelwidthstring',
|
|
|
|
# "\\start_of_appendix", "\\leftindent")
|
|
|
|
# sections = []
|
|
|
|
# for section in paragraphs_filter.keys():
|
|
|
|
# sections.append('\\begin_layout %s' % section)
|
|
|
|
|
|
|
|
# toc_par = []
|
|
|
|
# i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
# while True:
|
2010-11-05 21:13:33 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
# section = self.body[i].split()[1]
|
|
|
|
# if section[-1] == '*':
|
|
|
|
# section = section[:-1]
|
|
|
|
|
|
|
|
# par = []
|
|
|
|
|
|
|
|
# k = i + 1
|
|
|
|
# # skip paragraph parameters
|
|
|
|
# while not self.body[k].strip() or self.body[k].split()[0] \
|
|
|
|
# in allowed_parameters:
|
|
|
|
# k += 1
|
|
|
|
|
|
|
|
# while k < j:
|
|
|
|
# if check_token(self.body[k], '\\begin_inset'):
|
|
|
|
# inset = self.body[k].split()[1]
|
|
|
|
# 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])
|
|
|
|
# k += 1
|
|
|
|
|
|
|
|
# # trim empty lines in the end.
|
|
|
|
# while par and par[-1].strip() == '':
|
|
|
|
# par.pop()
|
|
|
|
|
|
|
|
# toc_par.append(Paragraph(section, par))
|
|
|
|
|
|
|
|
# i = j + 1
|
|
|
|
|
|
|
|
# return toc_par
|
2004-10-16 22:56:10 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-16 20:05:56 +00:00
|
|
|
def __init__(self, end_format = 0, input = u'', output = u'', error = u'',
|
|
|
|
debug = default_debug__, try_hard = 0, cjk_encoding = u'',
|
|
|
|
final_version = u'', systemlyxdir = u''):
|
2007-09-14 14:37:14 +00:00
|
|
|
LyX_base.__init__(self, end_format, input, output, error,
|
2015-11-11 20:35:12 +00:00
|
|
|
debug, try_hard, cjk_encoding, final_version,
|
|
|
|
systemlyxdir)
|
2004-10-16 22:56:10 +00:00
|
|
|
self.read()
|
|
|
|
|
|
|
|
|
2014-07-04 17:55:44 +00:00
|
|
|
# FIXME: header settings are completely outdated, don't use like this
|
2010-11-05 21:13:33 +00:00
|
|
|
#class NewFile(LyX_base):
|
|
|
|
# " This class is to create new LyX files."
|
|
|
|
# def set_header(self, **params):
|
|
|
|
# # set default values
|
|
|
|
# self.header.extend([
|
|
|
|
# "#LyX xxxx created this file."
|
|
|
|
# "For more info see http://www.lyx.org/",
|
|
|
|
# "\\lyxformat xxx",
|
|
|
|
# "\\begin_document",
|
|
|
|
# "\\begin_header",
|
|
|
|
# "\\textclass article",
|
|
|
|
# "\\language english",
|
|
|
|
# "\\inputencoding auto",
|
|
|
|
# "\\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",
|
|
|
|
# "\\graphics default",
|
|
|
|
# "\\paperfontsize default",
|
|
|
|
# "\\papersize default",
|
|
|
|
# "\\use_geometry false",
|
|
|
|
# "\\use_amsmath 1",
|
|
|
|
# "\\cite_engine basic",
|
|
|
|
# "\\use_bibtopic false",
|
2014-07-04 17:55:44 +00:00
|
|
|
# "\\use_indices false",
|
2010-11-05 21:13:33 +00:00
|
|
|
# "\\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'])
|
|
|
|
|
|
|
|
|
2010-11-05 21:14:49 +00:00
|
|
|
# Part of an unfinished attempt to make lyx2lyx gave a more
|
|
|
|
# structured view of the document.
|
2010-11-05 21:13:33 +00:00
|
|
|
#class Paragraph:
|
|
|
|
# # unfinished implementation, it is missing the Text and Insets
|
|
|
|
# # representation.
|
|
|
|
# " This class represents the LyX paragraphs."
|
|
|
|
# def __init__(self, name, body=[], settings = [], child = []):
|
|
|
|
# """ Parameters:
|
|
|
|
# name: paragraph name.
|
|
|
|
# body: list of lines of body text.
|
|
|
|
# child: list of paragraphs that descend from this paragraph.
|
|
|
|
# """
|
|
|
|
# self.name = name
|
|
|
|
# self.body = body
|
|
|
|
# self.settings = settings
|
|
|
|
# self.child = child
|
|
|
|
|
|
|
|
# def asLines(self):
|
|
|
|
# """ Converts the paragraph to a list of strings, representing
|
|
|
|
# it in the LyX file."""
|
|
|
|
|
|
|
|
# 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
|