2016-06-03 05:40:21 +00:00
|
|
|
# -*- 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.
|
|
|
|
|
2016-12-10 11:54:12 +00:00
|
|
|
from parser_tools import find_end_of, find_token_backwards, find_end_of_layout#,
|
2016-12-10 10:53:42 +00:00
|
|
|
# find_token, find_tokens, \
|
2016-12-10 11:54:12 +00:00
|
|
|
# find_token_exact, find_end_of_inset, \
|
2016-12-10 10:53:42 +00:00
|
|
|
# is_in_inset, get_value, get_quoted_value, \
|
2016-07-13 14:01:17 +00:00
|
|
|
# del_token, check_token, get_option_value, get_bool_value
|
2016-06-03 05:40:21 +00:00
|
|
|
|
2016-07-13 14:01:17 +00:00
|
|
|
from parser_tools import find_token, find_end_of_inset, get_value, \
|
2016-12-10 10:53:42 +00:00
|
|
|
get_bool_value, get_containing_layout
|
2016-06-17 19:11:53 +00:00
|
|
|
|
2016-10-22 13:33:59 +00:00
|
|
|
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
|
|
|
|
# get_ert, lyx2latex, \
|
2016-06-03 05:40:21 +00:00
|
|
|
# lyx2verbatim, length_in_bp, convert_info_insets
|
|
|
|
# insert_to_preamble, latex_length, revert_flex_inset, \
|
|
|
|
# revert_font_attrs, hex2ratio, str2bool
|
|
|
|
|
2016-06-19 19:23:25 +00:00
|
|
|
from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
|
2016-06-03 05:40:21 +00:00
|
|
|
|
|
|
|
####################################################################
|
|
|
|
# Private helper functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
###
|
|
|
|
### Conversion and reversion routines
|
|
|
|
###
|
|
|
|
###############################################################################
|
|
|
|
|
2016-06-17 19:11:53 +00:00
|
|
|
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.")
|
2016-07-13 14:01:17 +00:00
|
|
|
i = len(document.header) - 1
|
|
|
|
|
2016-06-17 19:11:53 +00:00
|
|
|
j = find_token(document.preamble, "\\usepackage{microtype}", 0)
|
|
|
|
if j == -1:
|
2016-07-13 14:01:17 +00:00
|
|
|
document.header.insert(i + 1, "\\use_microtype false")
|
2016-06-17 19:11:53 +00:00
|
|
|
else:
|
2016-07-13 14:01:17 +00:00
|
|
|
document.header.insert(i + 1, "\\use_microtype true")
|
2016-06-17 19:11:53 +00:00
|
|
|
del document.preamble[j]
|
|
|
|
|
|
|
|
|
2016-06-03 05:40:21 +00:00
|
|
|
def revert_microtype(document):
|
|
|
|
" Remove microtype settings. "
|
|
|
|
i = find_token(document.header, "\\use_microtype", 0)
|
|
|
|
if i == -1:
|
|
|
|
return
|
2016-07-13 14:01:17 +00:00
|
|
|
use_microtype = get_bool_value(document.header, "\\use_microtype" , i)
|
2016-06-03 05:40:21 +00:00
|
|
|
del document.header[i]
|
2016-07-13 14:01:17 +00:00
|
|
|
if use_microtype:
|
2016-06-17 19:11:53 +00:00
|
|
|
add_to_preamble(document, ["\\usepackage{microtype}"])
|
2016-06-03 05:40:21 +00:00
|
|
|
|
|
|
|
|
2016-06-19 19:23:25 +00:00
|
|
|
def convert_dateinset(document):
|
|
|
|
' Convert date external inset to ERT '
|
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2016-06-19 19:23:25 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2016-12-07 17:38:41 +00:00
|
|
|
def convert_inputenc(document):
|
|
|
|
" Replace no longer supported input encoding settings. "
|
|
|
|
i = find_token(document.header, "\\inputenc", 0)
|
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
if get_value(document.header, "\\inputencoding", i) == "pt254":
|
|
|
|
document.header[i] = "\\inputencoding pt154"
|
|
|
|
|
|
|
|
|
2016-07-12 03:56:32 +00:00
|
|
|
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
|
2016-08-04 09:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
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",
|
2016-12-07 17:38:41 +00:00
|
|
|
" Family Roman",
|
|
|
|
" Series Medium",
|
|
|
|
" Shape Up",
|
|
|
|
" Size Normal",
|
|
|
|
" Color None",
|
2016-08-04 09:42:06 +00:00
|
|
|
"EndFont",
|
|
|
|
"Preamble",
|
2016-12-07 17:38:41 +00:00
|
|
|
" \\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}",
|
|
|
|
" }",
|
2016-08-04 09:42:06 +00:00
|
|
|
"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
|
|
|
|
|
2016-06-03 05:40:21 +00:00
|
|
|
|
2016-10-16 13:33:23 +00:00
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-16 13:33:23 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package babel"
|
2016-10-16 13:33:23 +00:00
|
|
|
k = find_token(document.header, "\\options", 0)
|
|
|
|
if k != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[k] = document.header[k].replace("\\options", "\\options bosnian,")
|
2016-10-16 13:33:23 +00:00
|
|
|
else:
|
2016-12-07 17:38:41 +00:00
|
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
|
|
document.header.insert(l + 1, "\\options bosnian")
|
2016-10-16 13:33:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-16 13:33:23 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package babel"
|
2016-10-16 13:33:23 +00:00
|
|
|
k = find_token(document.header, "\\options", 0)
|
|
|
|
if k != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[k] = document.header[k].replace("\\options", "\\options friulan,")
|
2016-10-16 13:33:23 +00:00
|
|
|
else:
|
2016-12-07 17:38:41 +00:00
|
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
|
|
document.header.insert(l + 1, "\\options friulan")
|
2016-10-16 13:33:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-16 13:33:23 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package babel"
|
2016-10-16 13:33:23 +00:00
|
|
|
k = find_token(document.header, "\\options", 0)
|
|
|
|
if k != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[k] = document.header[k].replace("\\options", "\\options macedonian,")
|
2016-10-16 13:33:23 +00:00
|
|
|
else:
|
2016-12-07 17:38:41 +00:00
|
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
|
|
document.header.insert(l + 1, "\\options macedonian")
|
2016-10-16 13:33:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-16 13:33:23 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package babel"
|
2016-10-16 13:33:23 +00:00
|
|
|
k = find_token(document.header, "\\options", 0)
|
|
|
|
if k != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[k] = document.header[k].replace("\\options", "\\options piedmontese,")
|
2016-10-16 13:33:23 +00:00
|
|
|
else:
|
2016-12-07 17:38:41 +00:00
|
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
|
|
document.header.insert(l + 1, "\\options piedmontese")
|
2016-10-16 13:33:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-16 13:33:23 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package babel"
|
2016-10-16 13:33:23 +00:00
|
|
|
k = find_token(document.header, "\\options", 0)
|
|
|
|
if k != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[k] = document.header[k].replace("\\options", "\\options romansh,")
|
2016-10-16 13:33:23 +00:00
|
|
|
else:
|
2016-12-07 17:38:41 +00:00
|
|
|
l = find_token(document.header, "\\use_default_options", 0)
|
|
|
|
document.header.insert(l + 1, "\\options romansh")
|
2016-10-16 13:33:23 +00:00
|
|
|
|
|
|
|
|
2016-10-22 13:33:59 +00:00
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-22 13:33:59 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package default"
|
2016-10-22 13:33:59 +00:00
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-22 13:33:59 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package default"
|
2016-10-22 13:33:59 +00:00
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-22 13:33:59 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package default"
|
2016-10-22 13:33:59 +00:00
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-22 13:33:59 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package default"
|
2016-10-22 13:33:59 +00:00
|
|
|
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", ""]
|
|
|
|
|
|
|
|
|
2016-10-27 22:21:58 +00:00
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-27 22:21:58 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package default"
|
2016-10-27 22:21:58 +00:00
|
|
|
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:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[i] = "\\language english"
|
2016-10-27 22:21:58 +00:00
|
|
|
j = find_token(document.header, "\\language_package default", 0)
|
|
|
|
if j != -1:
|
2016-12-07 17:38:41 +00:00
|
|
|
document.header[j] = "\\language_package default"
|
2016-10-27 22:21:58 +00:00
|
|
|
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", ""]
|
|
|
|
|
|
|
|
|
2016-12-10 10:53:42 +00:00
|
|
|
def revert_quotes(document):
|
|
|
|
" Revert Quote Insets in verbatim or Hebrew context to plain quotes "
|
|
|
|
|
|
|
|
# First handle verbatim insets
|
|
|
|
i = 0
|
|
|
|
j = 0
|
|
|
|
while i < len(document.body):
|
|
|
|
words = document.body[i].split()
|
|
|
|
if len(words) > 1 and words[0] == "\\begin_inset" and \
|
2016-12-10 11:54:12 +00:00
|
|
|
( words[1] in ["ERT", "listings"] or words[2] in ["URL", "Chunk", "Sweave", "S/R"] ):
|
2016-12-10 10:53:42 +00:00
|
|
|
j = find_end_of_inset(document.body, i)
|
|
|
|
if j == -1:
|
|
|
|
document.warning("Malformed LyX document: Can't find end of " + words[1] + " inset at line " + str(i))
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
while True:
|
|
|
|
k = find_token(document.body, '\\begin_inset Quotes', i, j)
|
|
|
|
if k == -1:
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
l = find_end_of_inset(document.body, k)
|
|
|
|
if l == -1:
|
|
|
|
document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
|
2016-12-10 11:54:12 +00:00
|
|
|
i = k
|
|
|
|
continue
|
|
|
|
replace = "\""
|
|
|
|
if document.body[k].endswith("s"):
|
|
|
|
replace = "'"
|
|
|
|
document.body[k:l+1] = [replace]
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Now verbatim layouts
|
|
|
|
i = 0
|
|
|
|
j = 0
|
|
|
|
while i < len(document.body):
|
|
|
|
words = document.body[i].split()
|
|
|
|
if len(words) > 1 and words[0] == "\\begin_layout" and \
|
|
|
|
words[1] in ["Verbatim", "Verbatim*", "Code", "Author_Email", "Author_URL"]:
|
|
|
|
j = find_end_of_layout(document.body, i)
|
|
|
|
if j == -1:
|
|
|
|
document.warning("Malformed LyX document: Can't find end of " + words[1] + " layout at line " + str(i))
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
while True:
|
|
|
|
k = find_token(document.body, '\\begin_inset Quotes', i, j)
|
|
|
|
if k == -1:
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
l = find_end_of_inset(document.body, k)
|
|
|
|
if l == -1:
|
|
|
|
document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
|
2016-12-10 10:53:42 +00:00
|
|
|
i = k
|
|
|
|
continue
|
|
|
|
replace = "\""
|
|
|
|
if document.body[k].endswith("s"):
|
|
|
|
replace = "'"
|
|
|
|
document.body[k:l+1] = [replace]
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Now handle Hebrew
|
|
|
|
i = 0
|
|
|
|
j = 0
|
|
|
|
while True:
|
|
|
|
k = find_token(document.body, '\\begin_inset Quotes', i, j)
|
|
|
|
if k == -1:
|
|
|
|
return
|
|
|
|
l = find_end_of_inset(document.body, k)
|
|
|
|
if l == -1:
|
|
|
|
document.warning("Malformed LyX document: Can't find end of Quote inset at line " + str(k))
|
|
|
|
i = k
|
|
|
|
continue
|
|
|
|
hebrew = False
|
|
|
|
parent = get_containing_layout(document.body, k)
|
|
|
|
ql = find_token_backwards(document.body, "\\lang", k)
|
|
|
|
if ql == -1 or ql < parent[1]:
|
|
|
|
hebrew = document.language == "hebrew"
|
|
|
|
elif document.body[ql] == "\\lang hebrew":
|
|
|
|
hebrew = True
|
|
|
|
if hebrew:
|
|
|
|
replace = "\""
|
|
|
|
if document.body[k].endswith("s"):
|
|
|
|
replace = "'"
|
|
|
|
document.body[k:l+1] = [replace]
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-03 05:40:21 +00:00
|
|
|
##
|
|
|
|
# Conversion hub
|
|
|
|
#
|
|
|
|
|
|
|
|
supported_versions = ["2.3.0", "2.3"]
|
|
|
|
convert = [
|
2016-06-19 19:23:25 +00:00
|
|
|
[509, [convert_microtype]],
|
2016-07-12 03:56:32 +00:00
|
|
|
[510, [convert_dateinset]],
|
2016-08-04 09:42:06 +00:00
|
|
|
[511, [convert_ibranches]],
|
2016-10-16 13:33:23 +00:00
|
|
|
[512, [convert_beamer_article_styles]],
|
2016-10-22 13:33:59 +00:00
|
|
|
[513, []],
|
2016-10-27 22:21:58 +00:00
|
|
|
[514, []],
|
2016-12-07 17:38:41 +00:00
|
|
|
[515, []],
|
|
|
|
[516, [convert_inputenc]],
|
2016-12-10 10:53:42 +00:00
|
|
|
[517, []]
|
2016-06-03 05:40:21 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
revert = [
|
2016-12-10 10:53:42 +00:00
|
|
|
[516, [revert_quotes]],
|
|
|
|
[515, []],
|
2016-10-27 22:21:58 +00:00
|
|
|
[514, [revert_urdu, revert_syriac]],
|
2016-10-22 13:33:59 +00:00
|
|
|
[513, [revert_amharic, revert_asturian, revert_kannada, revert_khmer]],
|
2016-10-16 13:33:23 +00:00
|
|
|
[512, [revert_bosnian, revert_friulan, revert_macedonian, revert_piedmontese, revert_romansh]],
|
2016-08-04 09:42:06 +00:00
|
|
|
[511, [revert_beamer_article_styles]],
|
2016-07-12 03:56:32 +00:00
|
|
|
[510, [revert_ibranches]],
|
2016-06-19 19:23:25 +00:00
|
|
|
[509, []],
|
2016-06-03 05:40:21 +00:00
|
|
|
[508, [revert_microtype]]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|