2004-04-14 08:45:46 +00:00
|
|
|
|
# This file is part of lyx2lyx
|
|
|
|
|
# -*- coding: iso-8859-1 -*-
|
|
|
|
|
# Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
|
|
|
|
|
# Copyright (C) 2002-2004 Jos<6F> 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.
|
|
|
|
|
|
|
|
|
|
import re
|
2004-04-29 09:24:29 +00:00
|
|
|
|
from os import access, F_OK
|
|
|
|
|
import os.path
|
2004-04-14 08:45:46 +00:00
|
|
|
|
from parser_tools import find_token, find_end_of_inset, get_next_paragraph, \
|
|
|
|
|
get_paragraph, get_value, del_token, is_nonempty_line,\
|
2004-04-29 09:24:29 +00:00
|
|
|
|
find_tokens, find_end_of, find_token2
|
|
|
|
|
from sys import stdin
|
2004-04-14 08:45:46 +00:00
|
|
|
|
from string import replace, split, find, strip, join
|
|
|
|
|
|
2004-12-03 18:39:51 +00:00
|
|
|
|
from lyx_0_12 import update_latexaccents
|
|
|
|
|
|
2004-08-04 15:45:26 +00:00
|
|
|
|
##
|
|
|
|
|
# Remove \color default
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def remove_color_default(file):
|
2004-08-04 15:45:26 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\color default", i)
|
2004-08-04 15:45:26 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = replace(file.body[i], "\\color default",
|
2004-08-04 15:45:26 +00:00
|
|
|
|
"\\color inherit")
|
|
|
|
|
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
##
|
|
|
|
|
# Add \end_header
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def add_end_header(file):
|
|
|
|
|
file.header.append("\\end_header");
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def rm_end_header(file):
|
|
|
|
|
i = find_token(file.header, "\\end_header", 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# \SpecialChar ~ -> \InsetSpace ~
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_spaces(file):
|
|
|
|
|
for i in range(len(file.body)):
|
|
|
|
|
file.body[i] = replace(file.body[i],"\\SpecialChar ~","\\InsetSpace ~")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_spaces(file):
|
|
|
|
|
for i in range(len(file.body)):
|
|
|
|
|
file.body[i] = replace(file.body[i],"\\InsetSpace ~", "\\SpecialChar ~")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# BibTeX changes
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_bibtex(file):
|
|
|
|
|
for i in range(len(file.body)):
|
|
|
|
|
file.body[i] = replace(file.body[i],"\\begin_inset LatexCommand \\BibTeX",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_inset LatexCommand \\bibtex")
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_bibtex(file):
|
|
|
|
|
for i in range(len(file.body)):
|
|
|
|
|
file.body[i] = replace(file.body[i], "\\begin_inset LatexCommand \\bibtex",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_inset LatexCommand \\BibTeX")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Remove \lyxparent
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def remove_insetparent(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\begin_inset LatexCommand \\lyxparent", i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i:i+3]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Inset External
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_external(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",')
|
|
|
|
|
external_header = "\\begin_inset External"
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, external_header, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
look = external_rexp.search(file.body[i])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
args = ['','']
|
|
|
|
|
if look:
|
|
|
|
|
args[0] = look.group(1)
|
|
|
|
|
args[1] = look.group(2)
|
|
|
|
|
#FIXME: if the previous search fails then warn
|
|
|
|
|
|
|
|
|
|
if args[0] == "RasterImage":
|
|
|
|
|
# Convert a RasterImage External Inset to a Graphics Inset.
|
|
|
|
|
top = "\\begin_inset Graphics"
|
|
|
|
|
if args[1]:
|
|
|
|
|
filename = "\tfilename " + args[1]
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i+1] = [top, filename]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
else:
|
|
|
|
|
# Convert the old External Inset format to the new.
|
|
|
|
|
top = external_header
|
|
|
|
|
template = "\ttemplate " + args[0]
|
|
|
|
|
if args[1]:
|
|
|
|
|
filename = "\tfilename " + args[1]
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i+1] = [top, template, filename]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 2
|
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i+1] = [top, template]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_external_1(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
external_header = "\\begin_inset External"
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, external_header, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
template = split(file.body[i+1])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
template.reverse()
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i+1]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
filename = split(file.body[i+1])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
filename.reverse()
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i+1]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
params = split(file.body[i+1])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
params.reverse()
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.body[i+1]: del file.body[i+1]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = file.body[i] + " " + template[0]+ ', "' + filename[0] + '", " '+ join(params[1:]) + '"'
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_external_2(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
draft_token = '\tdraft'
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_inset External', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_end_of_inset(file.body, i + 1)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1:
|
|
|
|
|
#this should not happen
|
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
k = find_token(file.body, draft_token, i+1, j-1)
|
|
|
|
|
if (k != -1 and len(draft_token) == len(file.body[k])):
|
|
|
|
|
del file.body[k]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = j + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Comment
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_comment(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
comment = "\\layout Comment"
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, comment, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i+1] = ["\\layout Standard","","",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_inset Comment",
|
|
|
|
|
"collapsed true","",
|
|
|
|
|
"\\layout Standard"]
|
|
|
|
|
i = i + 7
|
|
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
|
old_i = i
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\layout", i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = len(file.body) - 1
|
|
|
|
|
file.body[i:i] = ["\\end_inset","",""]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_token(file.body, '\\begin_deeper', old_i, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1: j = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
k = find_token(file.body, '\\begin_inset', old_i, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if k == -1: k = i + 1
|
|
|
|
|
|
|
|
|
|
if j < i and j < k:
|
|
|
|
|
i = j
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
|
|
|
|
i = find_end_of( file.body, i, "\\begin_deeper","\\end_deeper")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
#This case should not happen
|
|
|
|
|
#but if this happens deal with it greacefully adding
|
|
|
|
|
#the missing \end_deeper.
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = len(file.body) - 1
|
|
|
|
|
file.body[i:i] = ["\end_deeper",""]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if k < i:
|
|
|
|
|
i = k
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_end_of( file.body, i, "\\begin_inset","\\end_inset")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
#This case should not happen
|
|
|
|
|
#but if this happens deal with it greacefully adding
|
|
|
|
|
#the missing \end_inset.
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = len(file.body) - 1
|
|
|
|
|
file.body[i:i] = ["\\end_inset","","","\\end_inset","",""]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if find(file.body[i], comment) == -1:
|
|
|
|
|
file.body[i:i] = ["\\end_inset"]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i+1] = ["\\layout Standard"]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_comment(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_tokens(file.body, ["\\begin_inset Comment", "\\begin_inset Greyedout"], i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = "\\begin_inset Note"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Add \end_layout
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def add_end_layout(file):
|
|
|
|
|
i = find_token(file.body, '\\layout', 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
struct_stack = ["\\layout"]
|
2004-05-11 16:13:33 +00:00
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_tokens(file.body, ["\\begin_inset", "\\end_inset", "\\layout",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_deeper", "\\end_deeper", "\\the_end"], i)
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
token = split(file.body[i])[0]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if token == "\\begin_inset":
|
|
|
|
|
struct_stack.append(token)
|
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if token == "\\end_inset":
|
|
|
|
|
tail = struct_stack.pop()
|
|
|
|
|
if tail == "\\layout":
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i,"")
|
|
|
|
|
file.body.insert(i,"\\end_layout")
|
2004-05-11 16:13:33 +00:00
|
|
|
|
i = i + 2
|
2004-04-14 08:45:46 +00:00
|
|
|
|
#Check if it is the correct tag
|
|
|
|
|
struct_stack.pop()
|
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if token == "\\layout":
|
|
|
|
|
tail = struct_stack.pop()
|
|
|
|
|
if tail == token:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i,"")
|
|
|
|
|
file.body.insert(i,"\\end_layout")
|
2004-05-11 16:13:33 +00:00
|
|
|
|
i = i + 3
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
|
|
|
|
struct_stack.append(tail)
|
|
|
|
|
i = i + 1
|
|
|
|
|
struct_stack.append(token)
|
|
|
|
|
continue
|
|
|
|
|
|
2004-05-11 16:13:33 +00:00
|
|
|
|
if token == "\\begin_deeper":
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i,"")
|
|
|
|
|
file.body.insert(i,"\\end_layout")
|
2004-05-11 16:13:33 +00:00
|
|
|
|
i = i + 3
|
|
|
|
|
struct_stack.append(token)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if token == "\\end_deeper":
|
2004-08-18 13:48:33 +00:00
|
|
|
|
if struct_stack[-1] == '\\layout':
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, '\\end_layout')
|
2004-08-18 13:48:33 +00:00
|
|
|
|
i = i + 1
|
2004-05-11 16:13:33 +00:00
|
|
|
|
struct_stack.pop()
|
2004-08-18 13:48:33 +00:00
|
|
|
|
i = i + 1
|
2004-04-14 08:45:46 +00:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
#case \end_document
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, "")
|
|
|
|
|
file.body.insert(i, "\\end_layout")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def rm_end_layout(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\end_layout', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
2004-05-11 16:13:33 +00:00
|
|
|
|
# Handle change tracking keywords
|
2004-04-14 08:45:46 +00:00
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def insert_tracking_changes(file):
|
|
|
|
|
i = find_token(file.header, "\\tracking_changes", 0)
|
2004-05-11 16:13:33 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header.append("\\tracking_changes 0")
|
2004-05-11 16:13:33 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
|
|
|
|
|
def rm_tracking_changes(file):
|
|
|
|
|
i = find_token(file.header, "\\author", 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i != -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.header, "\\tracking_changes", 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def rm_body_changes(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\change_", i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# \layout -> \begin_layout
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def layout2begin_layout(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\layout', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = replace(file.body[i], '\\layout', '\\begin_layout')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def begin_layout2layout(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_layout', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = replace(file.body[i], '\\begin_layout', '\\layout')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# valignment="center" -> valignment="middle"
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_valignment_middle(body, start, end):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
for i in range(start, end):
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if re.search('^<(column|cell) .*valignment="center".*>$', body[i]):
|
|
|
|
|
body[i] = replace(body[i], 'valignment="center"', 'valignment="middle"')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_table_valignment_middle(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_inset Tabular', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_end_of_inset(file.body, i + 1)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1:
|
|
|
|
|
#this should not happen
|
2004-10-09 21:32:56 +00:00
|
|
|
|
convert_valignment_middle(file.body, i + 1, len(file.body))
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
convert_valignment_middle(file.body, i + 1, j)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = j + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_table_valignment_middle(body, start, end):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
for i in range(start, end):
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if re.search('^<(column|cell) .*valignment="middle".*>$', body[i]):
|
|
|
|
|
body[i] = replace(body[i], 'valignment="middle"', 'valignment="center"')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_valignment_middle(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_inset Tabular', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_end_of_inset(file.body, i + 1)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1:
|
|
|
|
|
#this should not happen
|
2004-10-09 21:32:56 +00:00
|
|
|
|
revert_table_valignment_middle(file.body, i + 1, len(file.body))
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
revert_table_valignment_middle(file.body, i + 1, j)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = j + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# \the_end -> \end_document
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_end_document(file):
|
|
|
|
|
i = find_token(file.body, "\\the_end", 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.append("\\end_document")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = "\\end_document"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_end_document(file):
|
|
|
|
|
i = find_token(file.body, "\\end_document", 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.append("\\the_end")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = "\\the_end"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Convert line and page breaks
|
|
|
|
|
# Old:
|
|
|
|
|
#\layout Standard
|
|
|
|
|
#\line_top \line_bottom \pagebreak_top \pagebreak_bottom \added_space_top xxx \added_space_bottom yyy
|
|
|
|
|
#0
|
|
|
|
|
#
|
|
|
|
|
# New:
|
|
|
|
|
#\begin layout Standard
|
|
|
|
|
#
|
2004-10-26 21:16:44 +00:00
|
|
|
|
#\newpage
|
2004-04-14 08:45:46 +00:00
|
|
|
|
#
|
|
|
|
|
#\lyxline
|
|
|
|
|
#\begin_inset VSpace xxx
|
|
|
|
|
#\end_inset
|
|
|
|
|
#
|
|
|
|
|
#\end_layout
|
|
|
|
|
#\begin_layout Standard
|
|
|
|
|
#
|
|
|
|
|
#0
|
|
|
|
|
#\end_layout
|
|
|
|
|
#\begin_layout Standard
|
|
|
|
|
#
|
|
|
|
|
#\begin_inset VSpace xxx
|
|
|
|
|
#\end_inset
|
2004-10-26 21:16:44 +00:00
|
|
|
|
#\lyxline
|
2004-04-14 08:45:46 +00:00
|
|
|
|
#
|
|
|
|
|
#\newpage
|
|
|
|
|
#
|
|
|
|
|
#\end_layout
|
2004-10-26 21:16:44 +00:00
|
|
|
|
def convert_breaks(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\begin_layout", i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
line_top = find(file.body[i],"\\line_top")
|
|
|
|
|
line_bot = find(file.body[i],"\\line_bottom")
|
|
|
|
|
pb_top = find(file.body[i],"\\pagebreak_top")
|
|
|
|
|
pb_bot = find(file.body[i],"\\pagebreak_bottom")
|
|
|
|
|
vspace_top = find(file.body[i],"\\added_space_top")
|
|
|
|
|
vspace_bot = find(file.body[i],"\\added_space_bottom")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if line_top == -1 and line_bot == -1 and pb_bot == -1 and pb_top == -1 and vspace_top == -1 and vspace_bot == -1:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for tag in "\\line_top", "\\line_bottom", "\\pagebreak_top", "\\pagebreak_bottom":
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = replace(file.body[i], tag, "")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if vspace_top != -1:
|
|
|
|
|
# the position could be change because of the removal of other
|
|
|
|
|
# paragraph properties above
|
2004-10-09 21:32:56 +00:00
|
|
|
|
vspace_top = find(file.body[i],"\\added_space_top")
|
|
|
|
|
tmp_list = split(file.body[i][vspace_top:])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
vspace_top_value = tmp_list[1]
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = file.body[i][:vspace_top] + join(tmp_list[2:])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if vspace_bot != -1:
|
|
|
|
|
# the position could be change because of the removal of other
|
|
|
|
|
# paragraph properties above
|
2004-10-09 21:32:56 +00:00
|
|
|
|
vspace_bot = find(file.body[i],"\\added_space_bottom")
|
|
|
|
|
tmp_list = split(file.body[i][vspace_bot:])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
vspace_bot_value = tmp_list[1]
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = file.body[i][:vspace_bot] + join(tmp_list[2:])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = strip(file.body[i])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
# Create an empty paragraph for line and page break that belong
|
|
|
|
|
# above the paragraph
|
|
|
|
|
if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
|
2004-10-26 21:16:44 +00:00
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
paragraph_above = ['','\\begin_layout Standard','','']
|
|
|
|
|
|
|
|
|
|
if pb_top != -1:
|
|
|
|
|
paragraph_above.extend(['\\newpage ',''])
|
|
|
|
|
|
|
|
|
|
if vspace_top != -1:
|
2004-08-16 11:27:51 +00:00
|
|
|
|
paragraph_above.extend(['\\begin_inset VSpace ' + vspace_top_value,'\\end_inset','',''])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if line_top != -1:
|
|
|
|
|
paragraph_above.extend(['\\lyxline ',''])
|
|
|
|
|
|
|
|
|
|
paragraph_above.extend(['\\end_layout',''])
|
|
|
|
|
|
|
|
|
|
#inset new paragraph above the current paragraph
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i-2:i-2] = paragraph_above
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + len(paragraph_above)
|
|
|
|
|
|
|
|
|
|
# Ensure that nested style are converted later.
|
2004-10-09 21:32:56 +00:00
|
|
|
|
k = find_end_of(file.body, i, "\\begin_layout", "\\end_layout")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if k == -1:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if pb_top !=-1 or line_top != -1 or vspace_bot != -1:
|
2004-10-26 21:16:44 +00:00
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
paragraph_bellow = ['','\\begin_layout Standard','','']
|
|
|
|
|
|
|
|
|
|
if line_bot != -1:
|
|
|
|
|
paragraph_bellow.extend(['\\lyxline ',''])
|
|
|
|
|
|
|
|
|
|
if vspace_bot != -1:
|
2004-08-16 11:27:51 +00:00
|
|
|
|
paragraph_bellow.extend(['\\begin_inset VSpace ' + vspace_bot_value,'\\end_inset','',''])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if pb_bot != -1:
|
|
|
|
|
paragraph_bellow.extend(['\\newpage ',''])
|
|
|
|
|
|
|
|
|
|
paragraph_bellow.extend(['\\end_layout',''])
|
|
|
|
|
|
|
|
|
|
#inset new paragraph above the current paragraph
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[k + 1: k + 1] = paragraph_bellow
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Notes
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_note(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_tokens(file.body, ["\\begin_inset Note",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_inset Comment",
|
|
|
|
|
"\\begin_inset Greyedout"], i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = file.body[i][0:13] + 'Note ' + file.body[i][13:]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_note(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
note_header = "\\begin_inset Note "
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, note_header, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = "\\begin_inset " + file.body[i][len(note_header):]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Box
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_box(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_tokens(file.body, ["\\begin_inset Boxed",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_inset Doublebox",
|
|
|
|
|
"\\begin_inset Frameless",
|
|
|
|
|
"\\begin_inset ovalbox",
|
|
|
|
|
"\\begin_inset Ovalbox",
|
|
|
|
|
"\\begin_inset Shadowbox"], i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = file.body[i][0:13] + 'Box ' + file.body[i][13:]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_box(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
box_header = "\\begin_inset Box "
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, box_header, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = "\\begin_inset " + file.body[i][len(box_header):]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Collapse
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_collapsable(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_tokens(file.body, ["\\begin_inset Box",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_inset Branch",
|
|
|
|
|
"\\begin_inset CharStyle",
|
|
|
|
|
"\\begin_inset Float",
|
|
|
|
|
"\\begin_inset Foot",
|
|
|
|
|
"\\begin_inset Marginal",
|
|
|
|
|
"\\begin_inset Note",
|
|
|
|
|
"\\begin_inset OptArg",
|
|
|
|
|
"\\begin_inset Wrap"], i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Seach for a line starting 'collapsed'
|
|
|
|
|
# If, however, we find a line starting '\begin_layout'
|
|
|
|
|
# (_always_ present) then break with a warning message
|
|
|
|
|
i = i + 1
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if (file.body[i] == "collapsed false"):
|
|
|
|
|
file.body[i] = "status open"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i] == "collapsed true"):
|
|
|
|
|
file.body[i] = "status collapsed"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i][:13] == "\\begin_layout"):
|
|
|
|
|
file.warning("Malformed LyX file.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_collapsable(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_tokens(file.body, ["\\begin_inset Box",
|
2004-04-14 08:45:46 +00:00
|
|
|
|
"\\begin_inset Branch",
|
|
|
|
|
"\\begin_inset CharStyle",
|
|
|
|
|
"\\begin_inset Float",
|
|
|
|
|
"\\begin_inset Foot",
|
|
|
|
|
"\\begin_inset Marginal",
|
|
|
|
|
"\\begin_inset Note",
|
|
|
|
|
"\\begin_inset OptArg",
|
|
|
|
|
"\\begin_inset Wrap"], i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Seach for a line starting 'status'
|
|
|
|
|
# If, however, we find a line starting '\begin_layout'
|
|
|
|
|
# (_always_ present) then break with a warning message
|
|
|
|
|
i = i + 1
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if (file.body[i] == "status open"):
|
|
|
|
|
file.body[i] = "collapsed false"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i] == "status collapsed" or
|
|
|
|
|
file.body[i] == "status inlined"):
|
|
|
|
|
file.body[i] = "collapsed true"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i][:13] == "\\begin_layout"):
|
|
|
|
|
file.warning("Malformed LyX file.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# ERT
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_ert(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\begin_inset ERT", i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Seach for a line starting 'status'
|
|
|
|
|
# If, however, we find a line starting '\begin_layout'
|
|
|
|
|
# (_always_ present) then break with a warning message
|
|
|
|
|
i = i + 1
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if (file.body[i] == "status Open"):
|
|
|
|
|
file.body[i] = "status open"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i] == "status Collapsed"):
|
|
|
|
|
file.body[i] = "status collapsed"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i] == "status Inlined"):
|
|
|
|
|
file.body[i] = "status inlined"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i][:13] == "\\begin_layout"):
|
|
|
|
|
file.warning("Malformed LyX file.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_ert(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\begin_inset ERT", i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Seach for a line starting 'status'
|
|
|
|
|
# If, however, we find a line starting '\begin_layout'
|
|
|
|
|
# (_always_ present) then break with a warning message
|
|
|
|
|
i = i + 1
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if (file.body[i] == "status open"):
|
|
|
|
|
file.body[i] = "status Open"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i] == "status collapsed"):
|
|
|
|
|
file.body[i] = "status Collapsed"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i] == "status inlined"):
|
|
|
|
|
file.body[i] = "status Inlined"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i][:13] == "\\begin_layout"):
|
|
|
|
|
file.warning("Malformed LyX file.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Minipages
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_minipage(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
""" Convert minipages to the box inset.
|
|
|
|
|
We try to use the same order of arguments as lyx does.
|
|
|
|
|
"""
|
|
|
|
|
pos = ["t","c","b"]
|
|
|
|
|
inner_pos = ["c","t","b","s"]
|
|
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\begin_inset Minipage", i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = "\\begin_inset Box Frameless"
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
# convert old to new position using the pos list
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.body[i][:8] == "position":
|
|
|
|
|
file.body[i] = 'position "%s"' % pos[int(file.body[i][9])]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'position "%s"' % pos[0])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'hor_pos "c"')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'has_inner_box 1')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
# convert the inner_position
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.body[i][:14] == "inner_position":
|
|
|
|
|
file.body[i] = 'inner_pos "%s"' % inner_pos[int(file.body[i][15])]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert('inner_pos "%s"' % inner_pos[0])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
# We need this since the new file format has a height and width
|
|
|
|
|
# in a different order.
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.body[i][:6] == "height":
|
|
|
|
|
height = file.body[i][6:]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
# test for default value of 221 and convert it accordingly
|
|
|
|
|
if height == ' "0pt"':
|
|
|
|
|
height = ' "1pt"'
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
|
|
|
|
height = ' "1pt"'
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.body[i][:5] == "width":
|
|
|
|
|
width = file.body[i][5:]
|
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
|
|
|
|
width = ' "0"'
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.body[i][:9] == "collapsed":
|
|
|
|
|
if file.body[i][9:] == "true":
|
2004-04-14 08:45:46 +00:00
|
|
|
|
status = "collapsed"
|
|
|
|
|
else:
|
|
|
|
|
status = "open"
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
|
|
|
|
status = "collapsed"
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'use_parbox 0')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'width' + width)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'special "none"')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'height' + height)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'height_special "totalheight"')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'status ' + status)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------------------------
|
|
|
|
|
# Convert backslashes into valid ERT code, append the converted text to
|
2004-10-09 21:32:56 +00:00
|
|
|
|
# file.body[i] and return the (maybe incremented) line index i
|
|
|
|
|
def convert_ertbackslash(body, i, ert):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
for c in ert:
|
|
|
|
|
if c == '\\':
|
2004-10-09 21:32:56 +00:00
|
|
|
|
body[i] = body[i] + '\\backslash '
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
body.insert(i, '')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
body[i] = body[i] + c
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return i
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_vspace(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
# Get default spaceamount
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.header, '\\defskip', 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
defskipamount = 'medskip'
|
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
defskipamount = split(file.header[i])[1]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
# Convert the insets
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_inset VSpace', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
spaceamount = split(file.body[i])[2]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
# Are we at the beginning or end of a paragraph?
|
|
|
|
|
paragraph_start = 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
start = get_paragraph(file.body, i) + 1
|
2004-04-14 08:45:46 +00:00
|
|
|
|
for k in range(start, i):
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if is_nonempty_line(file.body[k]):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
paragraph_start = 0
|
|
|
|
|
break
|
|
|
|
|
paragraph_end = 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_end_of_inset(file.body, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed LyX file: Missing '\\end_inset'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
2004-10-09 21:32:56 +00:00
|
|
|
|
end = get_next_paragraph(file.body, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
for k in range(j + 1, end):
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if is_nonempty_line(file.body[k]):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
paragraph_end = 0
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Convert to paragraph formatting if we are at the beginning or end
|
|
|
|
|
# of a paragraph and the resulting paragraph would not be empty
|
|
|
|
|
if ((paragraph_start and not paragraph_end) or
|
|
|
|
|
(paragraph_end and not paragraph_start)):
|
|
|
|
|
# The order is important: del and insert invalidate some indices
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[j]
|
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if paragraph_start:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(start, '\\added_space_top ' + spaceamount + ' ')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(start, '\\added_space_bottom ' + spaceamount + ' ')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Convert to ERT
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '',
|
2004-04-14 08:45:46 +00:00
|
|
|
|
'\\layout Standard', '', '\\backslash ']
|
|
|
|
|
i = i + 6
|
|
|
|
|
if spaceamount[-1] == '*':
|
|
|
|
|
spaceamount = spaceamount[:-1]
|
|
|
|
|
keep = 1
|
|
|
|
|
else:
|
|
|
|
|
keep = 0
|
|
|
|
|
|
|
|
|
|
# Replace defskip by the actual value
|
|
|
|
|
if spaceamount == 'defskip':
|
|
|
|
|
spaceamount = defskipamount
|
|
|
|
|
|
|
|
|
|
# LaTeX does not know \\smallskip* etc
|
|
|
|
|
if keep:
|
|
|
|
|
if spaceamount == 'smallskip':
|
|
|
|
|
spaceamount = '\\smallskipamount'
|
|
|
|
|
elif spaceamount == 'medskip':
|
|
|
|
|
spaceamount = '\\medskipamount'
|
|
|
|
|
elif spaceamount == 'bigskip':
|
|
|
|
|
spaceamount = '\\bigskipamount'
|
|
|
|
|
elif spaceamount == 'vfill':
|
|
|
|
|
spaceamount = '\\fill'
|
|
|
|
|
|
|
|
|
|
# Finally output the LaTeX code
|
|
|
|
|
if (spaceamount == 'smallskip' or spaceamount == 'medskip' or
|
|
|
|
|
spaceamount == 'bigskip' or spaceamount == 'vfill'):
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, spaceamount)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else :
|
|
|
|
|
if keep:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'vspace*{')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'vspace{')
|
|
|
|
|
i = convert_ertbackslash(file.body, i, spaceamount)
|
|
|
|
|
file.body[i] = file.body[i] + '}'
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
# Convert a LyX length into valid ERT code and append it to body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
# Return the (maybe incremented) line index i
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_ertlen(body, i, len, special):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
units = {"text%":"\\textwidth", "col%":"\\columnwidth",
|
|
|
|
|
"page%":"\\pagewidth", "line%":"\\linewidth",
|
|
|
|
|
"theight%":"\\textheight", "pheight%":"\\pageheight"}
|
|
|
|
|
|
|
|
|
|
# Convert special lengths
|
|
|
|
|
if special != 'none':
|
|
|
|
|
len = '%f\\' % len2value(len) + special
|
|
|
|
|
|
|
|
|
|
# Convert LyX units to LaTeX units
|
|
|
|
|
for unit in units.keys():
|
|
|
|
|
if find(len, unit) != -1:
|
|
|
|
|
len = '%f' % (len2value(len) / 100) + units[unit]
|
|
|
|
|
break
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
# Convert backslashes and insert the converted length into body
|
|
|
|
|
return convert_ertbackslash(body, i, len)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Return the value of len without the unit in numerical form
|
|
|
|
|
def len2value(len):
|
|
|
|
|
result = re.search('([+-]?[0-9.]+)', len)
|
|
|
|
|
if result:
|
|
|
|
|
return float(result.group(1))
|
|
|
|
|
# No number means 1.0
|
|
|
|
|
return 1.0
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_frameless_box(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
pos = ['t', 'c', 'b']
|
|
|
|
|
inner_pos = ['c', 't', 'b', 's']
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_inset Frameless', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_end_of_inset(file.body, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed LyX file: Missing '\\end_inset'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
# Gather parameters
|
|
|
|
|
params = {'position':'0', 'hor_pos':'c', 'has_inner_box':'1',
|
|
|
|
|
'inner_pos':'1', 'use_parbox':'0', 'width':'100col%',
|
|
|
|
|
'special':'none', 'height':'1in',
|
|
|
|
|
'height_special':'totalheight', 'collapsed':'false'}
|
|
|
|
|
for key in params.keys():
|
2004-10-09 21:32:56 +00:00
|
|
|
|
value = replace(get_value(file.body, key, i, j), '"', '')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if value != "":
|
|
|
|
|
if key == 'position':
|
|
|
|
|
# convert new to old position: 'position "t"' -> 0
|
|
|
|
|
value = find_token(pos, value, 0)
|
|
|
|
|
if value != -1:
|
|
|
|
|
params[key] = value
|
|
|
|
|
elif key == 'inner_pos':
|
|
|
|
|
# convert inner position
|
|
|
|
|
value = find_token(inner_pos, value, 0)
|
|
|
|
|
if value != -1:
|
|
|
|
|
params[key] = value
|
|
|
|
|
else:
|
|
|
|
|
params[key] = value
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = del_token(file.body, key, i, j)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
# Convert to minipage or ERT?
|
|
|
|
|
# Note that the inner_position and height parameters of a minipage
|
|
|
|
|
# inset are ignored and not accessible for the user, although they
|
|
|
|
|
# are present in the file format and correctly read in and written.
|
|
|
|
|
# Therefore we convert to ERT if they do not have their LaTeX
|
|
|
|
|
# defaults. These are:
|
|
|
|
|
# - the value of "position" for "inner_pos"
|
|
|
|
|
# - "\totalheight" for "height"
|
|
|
|
|
if (params['use_parbox'] != '0' or
|
|
|
|
|
params['has_inner_box'] != '1' or
|
|
|
|
|
params['special'] != 'none' or
|
|
|
|
|
inner_pos[params['inner_pos']] != pos[params['position']] or
|
|
|
|
|
params['height_special'] != 'totalheight' or
|
|
|
|
|
len2value(params['height']) != 1.0):
|
|
|
|
|
|
|
|
|
|
# Convert to ERT
|
|
|
|
|
if params['collapsed'] == 'true':
|
|
|
|
|
params['collapsed'] = 'Collapsed'
|
|
|
|
|
else:
|
|
|
|
|
params['collapsed'] = 'Open'
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i : i] = ['\\begin_inset ERT', 'status ' + params['collapsed'],
|
2004-04-14 08:45:46 +00:00
|
|
|
|
'', '\\layout Standard', '', '\\backslash ']
|
|
|
|
|
i = i + 6
|
|
|
|
|
if params['use_parbox'] == '1':
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'parbox')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(i, 'begin{minipage}')
|
|
|
|
|
file.body[i] = file.body[i] + '[' + pos[params['position']] + ']['
|
|
|
|
|
i = convert_ertlen(file.body, i, params['height'], params['height_special'])
|
|
|
|
|
file.body[i] = file.body[i] + '][' + inner_pos[params['inner_pos']] + ']{'
|
|
|
|
|
i = convert_ertlen(file.body, i, params['width'], params['special'])
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if params['use_parbox'] == '1':
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = file.body[i] + '}{'
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i] = file.body[i] + '}'
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i] = ['', '\\end_inset']
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 2
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_end_of_inset(file.body, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed LyX file: Missing '\\end_inset'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[j-1:j-1] = ['\\begin_inset ERT', 'status ' + params['collapsed'],
|
2004-04-14 08:45:46 +00:00
|
|
|
|
'', '\\layout Standard', '']
|
|
|
|
|
j = j + 4
|
|
|
|
|
if params['use_parbox'] == '1':
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body.insert(j, '}')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[j:j] = ['\\backslash ', 'end{minipage}']
|
2004-04-14 08:45:46 +00:00
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
# Convert to minipage
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[i:i] = ['\\begin_inset Minipage',
|
2004-04-14 08:45:46 +00:00
|
|
|
|
'position %d' % params['position'],
|
|
|
|
|
'inner_position %d' % params['inner_pos'],
|
|
|
|
|
'height "' + params['height'] + '"',
|
|
|
|
|
'width "' + params['width'] + '"',
|
|
|
|
|
'collapsed ' + params['collapsed']]
|
|
|
|
|
i = i + 6
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Convert jurabib
|
|
|
|
|
#
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_jurabib(file):
|
|
|
|
|
i = find_token(file.header, '\\use_numerical_citations', 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\use_numerical_citations'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header.insert(i + 1, '\\use_jurabib 0')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_jurabib(file):
|
|
|
|
|
i = find_token(file.header, '\\use_jurabib', 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\use_jurabib'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if get_value(file.header, '\\use_jurabib', 0) != "0":
|
|
|
|
|
file.warning("Conversion of '\\use_jurabib = 1' not yet implemented.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
# Don't remove '\\use_jurabib' so that people will get warnings by lyx
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Convert bibtopic
|
|
|
|
|
#
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_bibtopic(file):
|
|
|
|
|
i = find_token(file.header, '\\use_jurabib', 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\use_jurabib'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header.insert(i + 1, '\\use_bibtopic 0')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_bibtopic(file):
|
|
|
|
|
i = find_token(file.header, '\\use_bibtopic', 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\use_bibtopic'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if get_value(file.header, '\\use_bibtopic', 0) != "0":
|
|
|
|
|
file.warning("Conversion of '\\use_bibtopic = 1' not yet implemented.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
# Don't remove '\\use_jurabib' so that people will get warnings by lyx
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Sideway Floats
|
|
|
|
|
#
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_float(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_inset Float', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
# Seach for a line starting 'wide'
|
|
|
|
|
# If, however, we find a line starting '\begin_layout'
|
|
|
|
|
# (_always_ present) then break with a warning message
|
|
|
|
|
i = i + 1
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if (file.body[i][:4] == "wide"):
|
|
|
|
|
file.body.insert(i + 1, 'sideways false')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
2004-10-09 21:32:56 +00:00
|
|
|
|
elif (file.body[i][:13] == "\\begin_layout"):
|
|
|
|
|
file.warning("Malformed lyx file.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
break
|
|
|
|
|
i = i + 1
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_float(file):
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, '\\begin_inset Float', i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_end_of_inset(file.body, i)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
if j == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\end_inset'.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if get_value(file.body, 'sideways', i, j) != "false":
|
|
|
|
|
file.warning("Conversion of 'sideways true' not yet implemented.")
|
2004-04-14 08:45:46 +00:00
|
|
|
|
# Don't remove 'sideways' so that people will get warnings by lyx
|
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del_token(file.body, 'sideways', i, j)
|
2004-04-14 08:45:46 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
2004-05-11 16:13:33 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_graphics(file):
|
2004-04-29 09:24:29 +00:00
|
|
|
|
""" Add extension to filenames of insetgraphics if necessary.
|
|
|
|
|
"""
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\begin_inset Graphics", i)
|
2004-04-29 09:24:29 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
j = find_token2(file.body, "filename", i)
|
2004-04-29 09:24:29 +00:00
|
|
|
|
if j == -1:
|
|
|
|
|
return
|
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
filename = split(file.body[j])[1]
|
|
|
|
|
absname = os.path.normpath(os.path.join(file.dir, filename))
|
|
|
|
|
if file.input == stdin and not os.path.isabs(filename):
|
2004-04-29 09:24:29 +00:00
|
|
|
|
# We don't know the directory and cannot check the file.
|
|
|
|
|
# We could use a heuristic and take the current directory,
|
|
|
|
|
# and we could try to find out if filename has an extension,
|
|
|
|
|
# but that would be just guesses and could be wrong.
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("""Warning: Can not determine whether file
|
2004-04-29 09:24:29 +00:00
|
|
|
|
%s
|
|
|
|
|
needs an extension when reading from standard input.
|
|
|
|
|
You may need to correct the file manually or run
|
|
|
|
|
lyx2lyx again with the .lyx file as commandline argument.""" % filename)
|
|
|
|
|
continue
|
|
|
|
|
# This needs to be the same algorithm as in pre 233 insetgraphics
|
|
|
|
|
if access(absname, F_OK):
|
|
|
|
|
continue
|
|
|
|
|
if access(absname + ".ps", F_OK):
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[j] = replace(file.body[j], filename, filename + ".ps")
|
2004-04-29 09:24:29 +00:00
|
|
|
|
continue
|
|
|
|
|
if access(absname + ".eps", F_OK):
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[j] = replace(file.body[j], filename, filename + ".eps")
|
2004-04-29 09:24:29 +00:00
|
|
|
|
|
|
|
|
|
|
2004-05-11 16:13:33 +00:00
|
|
|
|
##
|
|
|
|
|
# Convert firstname and surname from styles -> char styles
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_names(file):
|
2004-05-11 16:13:33 +00:00
|
|
|
|
""" Convert in the docbook backend from firstname and surname style
|
|
|
|
|
to charstyles.
|
|
|
|
|
"""
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.backend != "docbook":
|
2004-05-11 16:13:33 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.body, "\\begin_layout Author", i)
|
2004-05-11 16:13:33 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
2004-10-09 21:32:56 +00:00
|
|
|
|
while file.body[i] == "":
|
2004-05-11 16:13:33 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.body[i][:11] != "\\end_layout" or file.body[i+2][:13] != "\\begin_deeper":
|
2004-05-11 16:13:33 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
k = i
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_end_of( file.body, i+3, "\\begin_deeper","\\end_deeper")
|
2004-05-11 16:13:33 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
# something is really wrong, abort
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Missing \\end_deeper, after style Author.")
|
|
|
|
|
file.warning("Aborted attempt to parse FirstName and Surname.")
|
2004-05-11 16:13:33 +00:00
|
|
|
|
return
|
|
|
|
|
firstname, surname = "", ""
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
name = file.body[k:i]
|
2004-05-11 16:13:33 +00:00
|
|
|
|
|
|
|
|
|
j = find_token(name, "\\begin_layout FirstName", 0)
|
|
|
|
|
if j != -1:
|
|
|
|
|
j = j + 1
|
|
|
|
|
while(name[j] != "\\end_layout"):
|
|
|
|
|
firstname = firstname + name[j]
|
|
|
|
|
j = j + 1
|
|
|
|
|
|
|
|
|
|
j = find_token(name, "\\begin_layout Surname", 0)
|
|
|
|
|
if j != -1:
|
|
|
|
|
j = j + 1
|
|
|
|
|
while(name[j] != "\\end_layout"):
|
|
|
|
|
surname = surname + name[j]
|
|
|
|
|
j = j + 1
|
|
|
|
|
|
|
|
|
|
# delete name
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[k+2:i+1]
|
2004-05-11 16:13:33 +00:00
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.body[k-1:k-1] = ["", "",
|
2004-05-11 16:13:33 +00:00
|
|
|
|
"\\begin_inset CharStyle Firstname",
|
|
|
|
|
"status inlined",
|
|
|
|
|
"",
|
|
|
|
|
"\\begin_layout Standard",
|
|
|
|
|
"",
|
|
|
|
|
"%s" % firstname,
|
|
|
|
|
"\end_layout",
|
|
|
|
|
"",
|
2004-08-16 11:27:51 +00:00
|
|
|
|
"\end_inset",
|
2004-05-11 16:13:33 +00:00
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"\\begin_inset CharStyle Surname",
|
|
|
|
|
"status inlined",
|
|
|
|
|
"",
|
|
|
|
|
"\\begin_layout Standard",
|
|
|
|
|
"",
|
|
|
|
|
"%s" % surname,
|
|
|
|
|
"\\end_layout",
|
|
|
|
|
"",
|
2004-08-16 11:27:51 +00:00
|
|
|
|
"\\end_inset",
|
2004-05-11 16:13:33 +00:00
|
|
|
|
""]
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_names(file):
|
2004-05-11 16:13:33 +00:00
|
|
|
|
""" Revert in the docbook backend from firstname and surname char style
|
|
|
|
|
to styles.
|
|
|
|
|
"""
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.backend != "docbook":
|
2004-05-11 16:13:33 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
2004-05-13 20:44:35 +00:00
|
|
|
|
##
|
|
|
|
|
# \use_natbib 1 \cite_engine <style>
|
|
|
|
|
# \use_numerical_citations 0 -> where <style> is one of
|
|
|
|
|
# \use_jurabib 0 "basic", "natbib_authoryear",
|
|
|
|
|
# "natbib_numerical" or "jurabib"
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_cite_engine(file):
|
|
|
|
|
a = find_token(file.header, "\\use_natbib", 0)
|
2004-05-13 20:44:35 +00:00
|
|
|
|
if a == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\use_natbib'.")
|
2004-05-13 20:44:35 +00:00
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
b = find_token(file.header, "\\use_numerical_citations", 0)
|
2004-05-13 20:44:35 +00:00
|
|
|
|
if b == -1 or b != a+1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\use_numerical_citations'.")
|
2004-05-13 20:44:35 +00:00
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
c = find_token(file.header, "\\use_jurabib", 0)
|
2004-05-13 20:44:35 +00:00
|
|
|
|
if c == -1 or c != b+1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\use_jurabib'.")
|
2004-05-13 20:44:35 +00:00
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
use_natbib = int(split(file.header[a])[1])
|
|
|
|
|
use_numerical_citations = int(split(file.header[b])[1])
|
|
|
|
|
use_jurabib = int(split(file.header[c])[1])
|
2004-05-13 20:44:35 +00:00
|
|
|
|
|
|
|
|
|
cite_engine = "basic"
|
|
|
|
|
if use_natbib:
|
|
|
|
|
if use_numerical_citations:
|
|
|
|
|
cite_engine = "natbib_numerical"
|
|
|
|
|
else:
|
|
|
|
|
cite_engine = "natbib_authoryear"
|
|
|
|
|
elif use_jurabib:
|
|
|
|
|
cite_engine = "jurabib"
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[a:c+1]
|
|
|
|
|
file.header.insert(a, "\\cite_engine " + cite_engine)
|
2004-05-13 20:44:35 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_cite_engine(file):
|
|
|
|
|
i = find_token(file.header, "\\cite_engine", 0)
|
2004-05-13 20:44:35 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\cite_engine'.")
|
2004-05-13 20:44:35 +00:00
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
cite_engine = split(file.header[i])[1]
|
2004-05-13 20:44:35 +00:00
|
|
|
|
|
|
|
|
|
use_natbib = '0'
|
|
|
|
|
use_numerical = '0'
|
|
|
|
|
use_jurabib = '0'
|
|
|
|
|
if cite_engine == "natbib_numerical":
|
|
|
|
|
use_natbib = '1'
|
|
|
|
|
use_numerical = '1'
|
|
|
|
|
elif cite_engine == "natbib_authoryear":
|
|
|
|
|
use_natbib = '1'
|
|
|
|
|
elif cite_engine == "jurabib":
|
|
|
|
|
use_jurabib = '1'
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
|
|
|
|
file.header.insert(i, "\\use_jurabib " + use_jurabib)
|
|
|
|
|
file.header.insert(i, "\\use_numerical_citations " + use_numerical)
|
|
|
|
|
file.header.insert(i, "\\use_natbib " + use_natbib)
|
2004-05-13 20:44:35 +00:00
|
|
|
|
|
|
|
|
|
|
2004-08-14 18:41:27 +00:00
|
|
|
|
##
|
|
|
|
|
# Paper package
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_paperpackage(file):
|
|
|
|
|
i = find_token(file.header, "\\paperpackage", 0)
|
2004-07-01 14:40:59 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\paperpackage'.")
|
2004-07-01 14:40:59 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
packages = {'a4':'none', 'a4wide':'a4', 'widemarginsa4':'a4wide'}
|
2004-10-09 21:32:56 +00:00
|
|
|
|
paperpackage = split(file.header[i])[1]
|
|
|
|
|
file.header[i] = replace(file.header[i], paperpackage, packages[paperpackage])
|
2004-07-01 14:40:59 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_paperpackage(file):
|
|
|
|
|
i = find_token(file.header, "\\paperpackage", 0)
|
2004-07-01 14:40:59 +00:00
|
|
|
|
if i == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed lyx file: Missing '\\paperpackage'.")
|
2004-07-01 14:40:59 +00:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
packages = {'none':'a4', 'a4':'a4wide', 'a4wide':'widemarginsa4',
|
|
|
|
|
'widemarginsa4':''}
|
2004-10-09 21:32:56 +00:00
|
|
|
|
paperpackage = split(file.header[i])[1]
|
|
|
|
|
file.header[i] = replace(file.header[i], paperpackage, packages[paperpackage])
|
2004-07-01 14:40:59 +00:00
|
|
|
|
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
##
|
2004-08-14 18:41:27 +00:00
|
|
|
|
# Bullets
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def convert_bullets(file):
|
2004-08-14 18:41:27 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.header, "\\bullet", i)
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.header[i][:12] == '\\bulletLaTeX':
|
|
|
|
|
file.header[i] = file.header[i] + ' ' + strip(file.header[i+1])
|
2004-08-14 18:41:27 +00:00
|
|
|
|
n = 3
|
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header[i] = file.header[i] + ' ' + strip(file.header[i+1]) +\
|
|
|
|
|
' ' + strip(file.header[i+2]) + ' ' + strip(file.header[i+3])
|
2004-08-14 18:41:27 +00:00
|
|
|
|
n = 5
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i+1:i + n]
|
2004-08-14 18:41:27 +00:00
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def revert_bullets(file):
|
2004-08-14 18:41:27 +00:00
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
i = find_token(file.header, "\\bullet", i)
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
2004-10-09 21:32:56 +00:00
|
|
|
|
if file.header[i][:12] == '\\bulletLaTeX':
|
|
|
|
|
n = find(file.header[i], '"')
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if n == -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed header.")
|
2004-08-14 18:41:27 +00:00
|
|
|
|
return
|
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header[i:i+1] = [file.header[i][:n-1],'\t' + file.header[i][n:], '\\end_bullet']
|
2004-08-14 18:41:27 +00:00
|
|
|
|
i = i + 3
|
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
frag = split(file.header[i])
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if len(frag) != 5:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.warning("Malformed header.")
|
2004-08-14 18:41:27 +00:00
|
|
|
|
return
|
|
|
|
|
else:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header[i:i+1] = [frag[0] + ' ' + frag[1],
|
2004-08-14 18:41:27 +00:00
|
|
|
|
'\t' + frag[2],
|
|
|
|
|
'\t' + frag[3],
|
|
|
|
|
'\t' + frag[4],
|
|
|
|
|
'\\end_bullet']
|
|
|
|
|
i = i + 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# \begin_header and \begin_document
|
2004-04-14 08:45:46 +00:00
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def add_begin_header(file):
|
|
|
|
|
i = find_token(file.header, '\\lyxformat', 0)
|
|
|
|
|
file.header.insert(i+1, '\\begin_header')
|
|
|
|
|
file.header.insert(i+1, '\\begin_document')
|
2004-08-14 18:41:27 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def remove_begin_header(file):
|
|
|
|
|
i = find_token(file.header, "\\begin_document", 0)
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if i != -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
|
|
|
|
i = find_token(file.header, "\\begin_header", 0)
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if i != -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.header[i]
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2004-08-14 18:41:27 +00:00
|
|
|
|
|
|
|
|
|
##
|
2004-10-09 21:32:56 +00:00
|
|
|
|
# \begin_file.body and \end_file.body
|
2004-08-14 18:41:27 +00:00
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def add_begin_body(file):
|
|
|
|
|
file.body.insert(0, '\\begin_body')
|
|
|
|
|
file.body.insert(1, '')
|
|
|
|
|
i = find_token(file.body, "\\end_document", 0)
|
|
|
|
|
file.body.insert(i, '\\end_body')
|
|
|
|
|
|
|
|
|
|
def remove_begin_body(file):
|
|
|
|
|
i = find_token(file.body, "\\begin_body", 0)
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if i != -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
|
|
|
|
if not file.body[i]:
|
|
|
|
|
del file.body[i]
|
|
|
|
|
i = find_token(file.body, "\\end_body", 0)
|
2004-08-14 18:41:27 +00:00
|
|
|
|
if i != -1:
|
2004-10-09 21:32:56 +00:00
|
|
|
|
del file.body[i]
|
2004-08-14 18:41:27 +00:00
|
|
|
|
|
|
|
|
|
|
2004-08-15 21:52:13 +00:00
|
|
|
|
##
|
|
|
|
|
# \papersize
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def normalize_papersize(file):
|
|
|
|
|
i = find_token(file.header, '\\papersize', 0)
|
2004-08-15 21:52:13 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
tmp = split(file.header[i])
|
2004-08-15 21:52:13 +00:00
|
|
|
|
if tmp[1] == "Default":
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header[i] = '\\papersize default'
|
2004-08-15 21:52:13 +00:00
|
|
|
|
return
|
|
|
|
|
if tmp[1] == "Custom":
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header[i] = '\\papersize custom'
|
2004-08-15 21:52:13 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def denormalize_papersize(file):
|
|
|
|
|
i = find_token(file.header, '\\papersize', 0)
|
2004-08-15 21:52:13 +00:00
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
tmp = split(file.header[i])
|
2004-08-15 21:52:13 +00:00
|
|
|
|
if tmp[1] == "custom":
|
2004-10-09 21:32:56 +00:00
|
|
|
|
file.header[i] = '\\papersize Custom'
|
2004-08-15 21:52:13 +00:00
|
|
|
|
|
|
|
|
|
|
2004-08-16 11:27:51 +00:00
|
|
|
|
##
|
|
|
|
|
# Strip spaces at end of command line
|
|
|
|
|
#
|
2004-10-09 21:32:56 +00:00
|
|
|
|
def strip_end_space(file):
|
|
|
|
|
for i in range(len(file.body)):
|
|
|
|
|
if file.body[i][:1] == '\\':
|
|
|
|
|
file.body[i] = strip(file.body[i])
|
2004-08-16 11:27:51 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-09 21:32:56 +00:00
|
|
|
|
##
|
|
|
|
|
# Use boolean values for \use_geometry, \use_bibtopic and \tracking_changes
|
|
|
|
|
#
|
|
|
|
|
def use_x_boolean(file):
|
|
|
|
|
bin2bool = {'0': 'false', '1': 'true'}
|
|
|
|
|
for use in '\\use_geometry', '\\use_bibtopic', '\\tracking_changes':
|
|
|
|
|
i = find_token(file.header, use, 0)
|
|
|
|
|
if i == -1:
|
|
|
|
|
continue
|
|
|
|
|
decompose = split(file.header[i])
|
|
|
|
|
file.header[i] = decompose[0] + ' ' + bin2bool[decompose[1]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def use_x_binary(file):
|
|
|
|
|
bool2bin = {'false': '0', 'true': '1'}
|
|
|
|
|
for use in '\\use_geometry', '\\use_bibtopic', '\\tracking_changes':
|
|
|
|
|
i = find_token(file.header, use, 0)
|
|
|
|
|
if i == -1:
|
|
|
|
|
continue
|
|
|
|
|
decompose = split(file.header[i])
|
|
|
|
|
file.header[i] = decompose[0] + ' ' + bool2bin[decompose[1]]
|
|
|
|
|
|
2005-01-06 16:52:08 +00:00
|
|
|
|
##
|
|
|
|
|
# Place all the paragraph parameters in their own line
|
|
|
|
|
#
|
|
|
|
|
def normalize_paragraph_params(file):
|
|
|
|
|
body = file.body
|
2005-01-06 18:32:44 +00:00
|
|
|
|
allowed_parameters = '\\paragraph_spacing', '\\noindent', '\\align', '\\labelwidthstring', "\\start_of_appendix"
|
2005-01-06 16:52:08 +00:00
|
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
|
|
|
|
i = find_token(file.body, '\\begin_layout', i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
while 1:
|
|
|
|
|
if strip(body[i]) and split(body[i])[0] not in allowed_parameters:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
j = find(body[i],'\\', 1)
|
|
|
|
|
|
|
|
|
|
if j != -1:
|
|
|
|
|
body[i:i+1] = [strip(body[i][:j]), body[i][j:]]
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
|
2004-08-14 18:41:27 +00:00
|
|
|
|
##
|
|
|
|
|
# Convertion hub
|
|
|
|
|
#
|
2004-08-15 16:29:04 +00:00
|
|
|
|
|
2005-01-05 18:52:59 +00:00
|
|
|
|
convert = [[223, [insert_tracking_changes, add_end_header, remove_color_default,
|
|
|
|
|
convert_spaces, convert_bibtex, remove_insetparent]],
|
|
|
|
|
[224, [convert_external, convert_comment]],
|
|
|
|
|
[225, [add_end_layout, layout2begin_layout, convert_end_document,
|
|
|
|
|
convert_table_valignment_middle, convert_breaks]],
|
|
|
|
|
[226, [convert_note]],
|
|
|
|
|
[227, [convert_box]],
|
|
|
|
|
[228, [convert_collapsable, convert_ert]],
|
|
|
|
|
[229, [convert_minipage]],
|
|
|
|
|
[230, [convert_jurabib]],
|
|
|
|
|
[231, [convert_float]],
|
|
|
|
|
[232, [convert_bibtopic]],
|
|
|
|
|
[233, [convert_graphics, convert_names]],
|
|
|
|
|
[234, [convert_cite_engine]],
|
|
|
|
|
[235, [convert_paperpackage]],
|
|
|
|
|
[236, [convert_bullets, add_begin_header, add_begin_body,
|
|
|
|
|
normalize_papersize, strip_end_space]],
|
|
|
|
|
[237, [use_x_boolean]],
|
2005-01-06 16:52:08 +00:00
|
|
|
|
[238, [update_latexaccents]],
|
|
|
|
|
[239, [normalize_paragraph_params]]]
|
2005-01-05 18:52:59 +00:00
|
|
|
|
|
2005-01-06 16:52:08 +00:00
|
|
|
|
revert = [[238, []],
|
|
|
|
|
[237, []],
|
2005-01-05 18:52:59 +00:00
|
|
|
|
[236, [use_x_binary]],
|
|
|
|
|
[235, [denormalize_papersize, remove_begin_body,remove_begin_header,
|
|
|
|
|
revert_bullets]],
|
|
|
|
|
[234, [revert_paperpackage]],
|
|
|
|
|
[233, [revert_cite_engine]],
|
|
|
|
|
[232, [revert_names]],
|
|
|
|
|
[231, [revert_bibtopic]],
|
|
|
|
|
[230, [revert_float]],
|
|
|
|
|
[229, [revert_jurabib]],
|
|
|
|
|
[228, []],
|
|
|
|
|
[227, [revert_collapsable, revert_ert]],
|
|
|
|
|
[226, [revert_box, revert_external_2]],
|
|
|
|
|
[225, [revert_note]],
|
|
|
|
|
[224, [rm_end_layout, begin_layout2layout, revert_end_document,
|
|
|
|
|
revert_valignment_middle, convert_vspace, convert_frameless_box]],
|
|
|
|
|
[223, [revert_external_2, revert_comment]],
|
|
|
|
|
[221, [rm_end_header, revert_spaces, revert_bibtex,
|
|
|
|
|
rm_tracking_changes, rm_body_changes]]]
|
2004-10-26 21:16:44 +00:00
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
pass
|