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.
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2016-06-19 19:23:25 +00:00
|
|
|
from parser_tools import find_token, find_end_of_inset, get_value
|
2016-06-17 19:11:53 +00:00
|
|
|
|
2016-06-03 05:40:21 +00:00
|
|
|
#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
|
|
|
|
|
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.")
|
|
|
|
return;
|
|
|
|
j = find_token(document.preamble, "\\usepackage{microtype}", 0)
|
|
|
|
if j == -1:
|
|
|
|
document.header.insert(i + 1, "\\use_microtype 0")
|
|
|
|
else:
|
|
|
|
document.header.insert(i + 1, "\\use_microtype 1")
|
|
|
|
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-06-17 19:11:53 +00:00
|
|
|
value = get_value(document.header, "\\use_microtype" , i).split()[0]
|
2016-06-03 05:40:21 +00:00
|
|
|
del document.header[i]
|
2016-06-17 19:11:53 +00:00
|
|
|
if value == "1":
|
|
|
|
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
|
|
|
|
while 1:
|
|
|
|
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-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]],
|
|
|
|
[510, [convert_dateinset]]
|
2016-06-03 05:40:21 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
revert = [
|
2016-06-19 19:23:25 +00:00
|
|
|
[509, []],
|
2016-06-03 05:40:21 +00:00
|
|
|
[508, [revert_microtype]]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|