mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-25 02:49:46 +00:00
87c00f3383
- fileformat change - I could not update the tex2lyx testfiles because of bug #10440 (there is now a probable fix)
514 lines
20 KiB
Python
514 lines
20 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
|
|
|
|
|
|
def revert_bosnian(document):
|
|
"Set the document language to English but assure Bosnian output"
|
|
|
|
if document.language == "bosnian":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language bosnian", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package babel"
|
|
k = find_token(document.header, "\\options", 0)
|
|
if k != -1:
|
|
document.header[k] = document.header[k].replace("\\options", "\\options bosnian,")
|
|
else:
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
document.header.insert(l + 1, "\\options bosnian")
|
|
|
|
|
|
def revert_friulan(document):
|
|
"Set the document language to English but assure Friulan output"
|
|
|
|
if document.language == "friulan":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language friulan", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package babel"
|
|
k = find_token(document.header, "\\options", 0)
|
|
if k != -1:
|
|
document.header[k] = document.header[k].replace("\\options", "\\options friulan,")
|
|
else:
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
document.header.insert(l + 1, "\\options friulan")
|
|
|
|
|
|
def revert_macedonian(document):
|
|
"Set the document language to English but assure Macedonian output"
|
|
|
|
if document.language == "macedonian":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language macedonian", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package babel"
|
|
k = find_token(document.header, "\\options", 0)
|
|
if k != -1:
|
|
document.header[k] = document.header[k].replace("\\options", "\\options macedonian,")
|
|
else:
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
document.header.insert(l + 1, "\\options macedonian")
|
|
|
|
|
|
def revert_piedmontese(document):
|
|
"Set the document language to English but assure Piedmontese output"
|
|
|
|
if document.language == "piedmontese":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language piedmontese", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package babel"
|
|
k = find_token(document.header, "\\options", 0)
|
|
if k != -1:
|
|
document.header[k] = document.header[k].replace("\\options", "\\options piedmontese,")
|
|
else:
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
document.header.insert(l + 1, "\\options piedmontese")
|
|
|
|
|
|
def revert_romansh(document):
|
|
"Set the document language to English but assure Romansh output"
|
|
|
|
if document.language == "romansh":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language romansh", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package babel"
|
|
k = find_token(document.header, "\\options", 0)
|
|
if k != -1:
|
|
document.header[k] = document.header[k].replace("\\options", "\\options romansh,")
|
|
else:
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
document.header.insert(l + 1, "\\options romansh")
|
|
|
|
|
|
def revert_amharic(document):
|
|
"Set the document language to English but assure Amharic output"
|
|
|
|
if document.language == "amharic":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language amharic", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package default"
|
|
add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{amharic}}"])
|
|
document.body[2 : 2] = ["\\begin_layout Standard",
|
|
"\\begin_inset ERT", "status open", "",
|
|
"\\begin_layout Plain Layout", "", "",
|
|
"\\backslash",
|
|
"resetdefaultlanguage{amharic}",
|
|
"\\end_layout", "", "\\end_inset", "", "",
|
|
"\\end_layout", ""]
|
|
|
|
|
|
def revert_asturian(document):
|
|
"Set the document language to English but assure Asturian output"
|
|
|
|
if document.language == "asturian":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language asturian", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package default"
|
|
add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{asturian}}"])
|
|
document.body[2 : 2] = ["\\begin_layout Standard",
|
|
"\\begin_inset ERT", "status open", "",
|
|
"\\begin_layout Plain Layout", "", "",
|
|
"\\backslash",
|
|
"resetdefaultlanguage{asturian}",
|
|
"\\end_layout", "", "\\end_inset", "", "",
|
|
"\\end_layout", ""]
|
|
|
|
|
|
def revert_kannada(document):
|
|
"Set the document language to English but assure Kannada output"
|
|
|
|
if document.language == "kannada":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language kannada", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package default"
|
|
add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{kannada}}"])
|
|
document.body[2 : 2] = ["\\begin_layout Standard",
|
|
"\\begin_inset ERT", "status open", "",
|
|
"\\begin_layout Plain Layout", "", "",
|
|
"\\backslash",
|
|
"resetdefaultlanguage{kannada}",
|
|
"\\end_layout", "", "\\end_inset", "", "",
|
|
"\\end_layout", ""]
|
|
|
|
|
|
def revert_khmer(document):
|
|
"Set the document language to English but assure Khmer output"
|
|
|
|
if document.language == "khmer":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language khmer", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package default"
|
|
add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{khmer}}"])
|
|
document.body[2 : 2] = ["\\begin_layout Standard",
|
|
"\\begin_inset ERT", "status open", "",
|
|
"\\begin_layout Plain Layout", "", "",
|
|
"\\backslash",
|
|
"resetdefaultlanguage{khmer}",
|
|
"\\end_layout", "", "\\end_inset", "", "",
|
|
"\\end_layout", ""]
|
|
|
|
|
|
def revert_urdu(document):
|
|
"Set the document language to English but assure Urdu output"
|
|
|
|
if document.language == "urdu":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language urdu", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package default"
|
|
add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{urdu}}"])
|
|
document.body[2 : 2] = ["\\begin_layout Standard",
|
|
"\\begin_inset ERT", "status open", "",
|
|
"\\begin_layout Plain Layout", "", "",
|
|
"\\backslash",
|
|
"resetdefaultlanguage{urdu}",
|
|
"\\end_layout", "", "\\end_inset", "", "",
|
|
"\\end_layout", ""]
|
|
|
|
|
|
def revert_syriac(document):
|
|
"Set the document language to English but assure Syriac output"
|
|
|
|
if document.language == "syriac":
|
|
document.language = "english"
|
|
i = find_token(document.header, "\\language syriac", 0)
|
|
if i != -1:
|
|
document.header[i] = "\\language english"
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
if j != -1:
|
|
document.header[j] = "\\language_package default"
|
|
add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{syriac}}"])
|
|
document.body[2 : 2] = ["\\begin_layout Standard",
|
|
"\\begin_inset ERT", "status open", "",
|
|
"\\begin_layout Plain Layout", "", "",
|
|
"\\backslash",
|
|
"resetdefaultlanguage{syriac}",
|
|
"\\end_layout", "", "\\end_inset", "", "",
|
|
"\\end_layout", ""]
|
|
|
|
|
|
##
|
|
# Conversion hub
|
|
#
|
|
|
|
supported_versions = ["2.3.0", "2.3"]
|
|
convert = [
|
|
[509, [convert_microtype]],
|
|
[510, [convert_dateinset]],
|
|
[511, [convert_ibranches]],
|
|
[512, [convert_beamer_article_styles]],
|
|
[513, []],
|
|
[514, []],
|
|
[515, []]
|
|
]
|
|
|
|
revert = [
|
|
[514, [revert_urdu, revert_syriac]],
|
|
[513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
|
|
[512, [revert_bosnian, revert_friulan, revert_macedonian, revert_piedmontese, revert_romansh]],
|
|
[511, [revert_beamer_article_styles]],
|
|
[510, [revert_ibranches]],
|
|
[509, []],
|
|
[508, [revert_microtype]]
|
|
]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pass
|