mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-22 05:16:21 +00:00
cca93dacae
Add get_bool_value to parser_tools to be used in other places. Make the upgrade convertion seamless from lyx writing the file (regarding microtypes, there are still other dragons to chase. :-)
201 lines
6.6 KiB
Python
201 lines
6.6 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_token, find_end_of, 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
|
|
|
|
|
|
##
|
|
# Conversion hub
|
|
#
|
|
|
|
supported_versions = ["2.3.0", "2.3"]
|
|
convert = [
|
|
[509, [convert_microtype]],
|
|
[510, [convert_dateinset]],
|
|
[511, [convert_ibranches]]
|
|
]
|
|
|
|
revert = [
|
|
[510, [revert_ibranches]],
|
|
[509, []],
|
|
[508, [revert_microtype]]
|
|
]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pass
|