mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-25 10:58:52 +00:00
90f6eb01a4
File format change. On format reversion, we need to put some extra code in the local layout that emulates the 2.3 behavior. Simply Input'ing the respective layouts is unfortunately not enough, due to the insertion order. See #9977
286 lines
10 KiB
Python
286 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
# This file is part of lyx2lyx
|
|
# -*- coding: utf-8 -*-
|
|
# Copyright (C) 2016 The LyX team
|
|
#
|
|
# 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.
|
|
|
|
""" Convert files to the file format generated by lyx 2.3"""
|
|
|
|
import re, string
|
|
import unicodedata
|
|
import sys, os
|
|
|
|
# Uncomment only what you need to import, please.
|
|
|
|
from parser_tools import find_end_of#, find_token, find_tokens, \
|
|
# find_token_exact, find_end_of_inset, find_end_of_layout, \
|
|
# find_token_backwards, is_in_inset, get_value, get_quoted_value, \
|
|
# del_token, check_token, get_option_value, get_bool_value
|
|
|
|
from parser_tools import find_token, find_end_of_inset, get_value, \
|
|
get_bool_value
|
|
|
|
#from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert, lyx2latex, \
|
|
# lyx2verbatim, length_in_bp, convert_info_insets
|
|
# insert_to_preamble, latex_length, revert_flex_inset, \
|
|
# revert_font_attrs, hex2ratio, str2bool
|
|
|
|
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
|
|
|
|
####################################################################
|
|
# Private helper functions
|
|
|
|
|
|
|
|
###############################################################################
|
|
###
|
|
### Conversion and reversion routines
|
|
###
|
|
###############################################################################
|
|
|
|
def convert_microtype(document):
|
|
" Add microtype settings. "
|
|
i = find_token(document.header, "\\font_tt_scale" , 0)
|
|
if i == -1:
|
|
document.warning("Malformed LyX document: Can't find \\font_tt_scale.")
|
|
i = len(document.header) - 1
|
|
|
|
j = find_token(document.preamble, "\\usepackage{microtype}", 0)
|
|
if j == -1:
|
|
document.header.insert(i + 1, "\\use_microtype false")
|
|
else:
|
|
document.header.insert(i + 1, "\\use_microtype true")
|
|
del document.preamble[j]
|
|
|
|
|
|
def revert_microtype(document):
|
|
" Remove microtype settings. "
|
|
i = find_token(document.header, "\\use_microtype", 0)
|
|
if i == -1:
|
|
return
|
|
use_microtype = get_bool_value(document.header, "\\use_microtype" , i)
|
|
del document.header[i]
|
|
if use_microtype:
|
|
add_to_preamble(document, ["\\usepackage{microtype}"])
|
|
|
|
|
|
def convert_dateinset(document):
|
|
' Convert date external inset to ERT '
|
|
i = 0
|
|
while True:
|
|
i = find_token(document.body, "\\begin_inset External", i)
|
|
if i == -1:
|
|
return
|
|
j = find_end_of_inset(document.body, i)
|
|
if j == -1:
|
|
document.warning("Malformed lyx document: Missing '\\end_inset' in convert_dateinset.")
|
|
i += 1
|
|
continue
|
|
if get_value(document.body, 'template', i, j) == "Date":
|
|
document.body[i : j + 1] = put_cmd_in_ert("\\today ")
|
|
i += 1
|
|
continue
|
|
|
|
|
|
def convert_ibranches(document):
|
|
' Add "inverted 0" to branch insets'
|
|
i = 0
|
|
while True:
|
|
i = find_token(document.body, "\\begin_inset Branch", i)
|
|
if i == -1:
|
|
return
|
|
document.body.insert(i + 1, "inverted 0")
|
|
i += 1
|
|
|
|
|
|
def revert_ibranches(document):
|
|
' Convert inverted branches to explicit anti-branches'
|
|
# Get list of branches
|
|
ourbranches = {}
|
|
i = 0
|
|
while True:
|
|
i = find_token(document.header, "\\branch", i)
|
|
if i == -1:
|
|
break
|
|
branch = document.header[i][8:].strip()
|
|
if document.header[i+1].startswith("\\selected "):
|
|
#document.warning(document.header[i+1])
|
|
#document.warning(document.header[i+1][10])
|
|
selected = int(document.header[i+1][10])
|
|
else:
|
|
document.warning("Malformed LyX document: No selection indicator for branch " + branch)
|
|
selected = 1
|
|
|
|
# the value tells us whether the branch is selected
|
|
ourbranches[document.header[i][8:].strip()] = selected
|
|
i += 1
|
|
|
|
# Figure out what inverted branches, if any, have been used
|
|
# and convert them to "Anti-OldBranch"
|
|
ibranches = {}
|
|
i = 0
|
|
while True:
|
|
i = find_token(document.body, "\\begin_inset Branch", i)
|
|
if i == -1:
|
|
break
|
|
if not document.body[i+1].startswith("inverted "):
|
|
document.warning("Malformed LyX document: Missing 'inverted' tag!")
|
|
i += 1
|
|
continue
|
|
inverted = document.body[i+1][9]
|
|
#document.warning(document.body[i+1])
|
|
|
|
if inverted == "1":
|
|
branch = document.body[i][20:].strip()
|
|
#document.warning(branch)
|
|
if not branch in ibranches:
|
|
antibranch = "Anti-" + branch
|
|
while antibranch in ibranches:
|
|
antibranch = "x" + antibranch
|
|
ibranches[branch] = antibranch
|
|
else:
|
|
antibranch = ibranches[branch]
|
|
#document.warning(antibranch)
|
|
document.body[i] = "\\begin_inset Branch " + antibranch
|
|
|
|
# remove "inverted" key
|
|
del document.body[i+1]
|
|
i += 1
|
|
|
|
# now we need to add the new branches to the header
|
|
for old, new in ibranches.iteritems():
|
|
i = find_token(document.header, "\\branch " + old, 0)
|
|
if i == -1:
|
|
document.warning("Can't find branch %s even though we found it before!" % (old))
|
|
continue
|
|
j = find_token(document.header, "\\end_branch", i)
|
|
if j == -1:
|
|
document.warning("Malformed LyX document! Can't find end of branch " + old)
|
|
continue
|
|
# ourbranches[old] - 1 inverts the selection status of the old branch
|
|
lines = ["\\branch " + new,
|
|
"\\selected " + str(ourbranches[old] - 1)]
|
|
# these are the old lines telling us color, etc.
|
|
lines += document.header[i+2 : j+1]
|
|
document.header[i:i] = lines
|
|
|
|
|
|
def revert_beamer_article_styles(document):
|
|
" Include (scr)article styles in beamer article "
|
|
|
|
beamer_articles = ["article-beamer", "scrarticle-beamer"]
|
|
if document.textclass not in beamer_articles:
|
|
return
|
|
|
|
inclusion = "article.layout"
|
|
if document.textclass == "scrarticle-beamer":
|
|
inclusion = "scrartcl.layout"
|
|
|
|
while True:
|
|
i = find_token(document.header, "\\begin_local_layout", 0)
|
|
if i == -1:
|
|
k = find_token(document.header, "\\language", 0)
|
|
if k == -1:
|
|
# this should not happen
|
|
document.warning("Malformed LyX document! No \\language header found!")
|
|
break
|
|
document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
|
|
i = find_token(document.header, "\\begin_local_layout", 0)
|
|
if i != -1:
|
|
j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
|
|
if j == -1:
|
|
# this should not happen
|
|
break
|
|
|
|
document.header[i+1 : i+1] = ["### Inserted by lyx2lyx (more [scr]article styles) ###",
|
|
"Input " + inclusion,
|
|
"Input beamer.layout",
|
|
"Provides geometry 0",
|
|
"Provides hyperref 0",
|
|
"DefaultFont",
|
|
" Family Roman",
|
|
" Series Medium",
|
|
" Shape Up",
|
|
" Size Normal",
|
|
" Color None",
|
|
"EndFont",
|
|
"Preamble",
|
|
" \\usepackage{beamerarticle,pgf}",
|
|
" % this default might be overridden by plain title style",
|
|
" \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
|
|
" \\AtBeginDocument{",
|
|
" \\let\\origtableofcontents=\\tableofcontents",
|
|
" \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
|
|
" \\def\\gobbletableofcontents#1{\\origtableofcontents}",
|
|
" }",
|
|
"EndPreamble",
|
|
"### End of insertion by lyx2lyx (more [scr]article styles) ###"]
|
|
return
|
|
|
|
|
|
def convert_beamer_article_styles(document):
|
|
" Remove included (scr)article styles in beamer article "
|
|
|
|
beamer_articles = ["article-beamer", "scrarticle-beamer"]
|
|
if document.textclass not in beamer_articles:
|
|
return
|
|
|
|
while True:
|
|
i = find_token(document.header, "\\begin_local_layout", 0)
|
|
if i == -1:
|
|
return
|
|
|
|
j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
|
|
if j == -1:
|
|
# this should not happen
|
|
break
|
|
|
|
k = find_token(document.header, "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
|
|
if k != -1:
|
|
l = find_token(document.header, "### End of insertion by lyx2lyx (more [scr]article styles) ###", i, j)
|
|
if l == -1:
|
|
# this should not happen
|
|
document.warning("End of lyx2lyx local layout insertion not found!")
|
|
break
|
|
|
|
document.header[k : l + 1] = []
|
|
|
|
return
|
|
|
|
|
|
##
|
|
# Conversion hub
|
|
#
|
|
|
|
supported_versions = ["2.3.0", "2.3"]
|
|
convert = [
|
|
[509, [convert_microtype]],
|
|
[510, [convert_dateinset]],
|
|
[511, [convert_ibranches]],
|
|
[512, [convert_beamer_article_styles]]
|
|
]
|
|
|
|
revert = [
|
|
[511, [revert_beamer_article_styles]],
|
|
[510, [revert_ibranches]],
|
|
[509, []],
|
|
[508, [revert_microtype]]
|
|
]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pass
|