#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
+ #find_token_backwards, is_in_inset, del_token, check_token
-from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
+from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert
#from lyx2lyx_tools import insert_to_preamble, \
-# put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
+# lyx2latex, latex_length, revert_flex_inset, \
# revert_font_attrs, hex2ratio, str2bool
####################################################################
try:
while True:
# first, let's find out if we need to do anything
- # cell type 3 is multirow begin cell
i = find_token(document.body, '<cell ', i)
if i == -1:
return
k = document.body[i].find('"', j + 8)
value = document.body[i][j + 8 : k]
if value == "0":
- rgx = re.compile(r'rotate="[^"]+?"')
+ rgx = re.compile(r' rotate="[^"]+?"')
# remove rotate option
document.body[i] = rgx.sub('', document.body[i])
elif value == "90":
- rgx = re.compile(r'rotate="[^"]+?"')
+ rgx = re.compile(r' rotate="[^"]+?"')
document.body[i] = rgx.sub('rotate="true"', document.body[i])
else:
- rgx = re.compile(r'rotate="[^"]+?"')
+ rgx = re.compile(r' rotate="[^"]+?"')
load_rotating = True
# remove rotate option
document.body[i] = rgx.sub('', document.body[i])
i = 0
while True:
# first, let's find out if we need to do anything
- # cell type 3 is multirow begin cell
i = find_token(document.body, '<cell ', i)
if i == -1:
return
i += 1
+def revert_table_rotation(document):
+ "Revert table rotations to TeX-code"
+
+ load_rotating = False
+ i = 0
+ try:
+ while True:
+ # first, let's find out if we need to do anything
+ i = find_token(document.body, '<features ', i)
+ if i == -1:
+ return
+ j = document.body[i].find('rotate="')
+ if j != -1:
+ end_table = find_token(document.body, '</lyxtabular>', j)
+ k = document.body[i].find('"', j + 8)
+ value = document.body[i][j + 8 : k]
+ if value == "0":
+ rgx = re.compile(r' rotate="[^"]+?"')
+ # remove rotate option
+ document.body[i] = rgx.sub('', document.body[i])
+ elif value == "90":
+ rgx = re.compile(r'rotate="[^"]+?"')
+ document.body[i] = rgx.sub('rotate="true"', document.body[i])
+ else:
+ rgx = re.compile(r' rotate="[^"]+?"')
+ load_rotating = True
+ # remove rotate option
+ document.body[i] = rgx.sub('', document.body[i])
+ # write ERT
+ document.body[end_table + 3 : end_table + 3] = \
+ put_cmd_in_ert("\\end{turn}")
+ document.body[i - 2 : i - 2] = \
+ put_cmd_in_ert("\\begin{turn}{" + value + "}")
+
+ i += 1
+
+ finally:
+ if load_rotating:
+ add_to_preamble(document, ["\\@ifundefined{turnbox}{\usepackage{rotating}}{}"])
+
+
+def convert_table_rotation(document):
+ 'Convert table rotation statements from "true" to "90"'
+
+ i = 0
+ while True:
+ # first, let's find out if we need to do anything
+ i = find_token(document.body, '<features ', i)
+ if i == -1:
+ return
+ j = document.body[i].find('rotate="true"')
+ if j != -1:
+ rgx = re.compile(r'rotate="[^"]+?"')
+ # convert "true" to "90"
+ document.body[i] = rgx.sub('rotate="90"', document.body[i])
+
+ i += 1
+
+
+def convert_listoflistings(document):
+ 'Convert ERT \lstlistoflistings to TOC lstlistoflistings inset'
+ # We can support roundtrip because the command is so simple
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset ERT", i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed lyx document: Can't find end of ERT inset")
+ i += 1
+ continue
+ ert = get_ert(document.body, i)
+ if ert == "\\lstlistoflistings{}":
+ document.body[i:j] = ["\\begin_inset CommandInset toc", "LatexCommand lstlistoflistings", ""]
+ i = i + 4
+ else:
+ i = j + 1
+
+
+def revert_listoflistings(document):
+ 'Convert TOC lstlistoflistings inset to ERT lstlistoflistings'
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset CommandInset toc", i)
+ if i == -1:
+ return
+ if document.body[i+1] == "LatexCommand lstlistoflistings":
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed lyx document: Can't find end of TOC inset")
+ i += 1
+ continue
+ subst = put_cmd_in_ert("\\lstlistoflistings{}")
+ document.body[i:j+1] = subst
+ add_to_preamble(document, ["\\usepackage{listings}"])
+ i = i + 1
+
+
+def convert_use_amssymb(document):
+ "insert use_package amssymb"
+ regexp = re.compile(r'(\\use_package\s+amsmath)')
+ i = find_re(document.header, regexp, 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Can't find \\use_package amsmath.")
+ return;
+ value = get_value(document.header, "\\use_package" , i).split()[1]
+ useamsmath = 0
+ try:
+ useamsmath = int(value)
+ except:
+ document.warning("Invalid \\use_package amsmath: " + value + ". Assuming auto.")
+ useamsmath = 1
+ j = find_token(document.preamble, "\\usepackage{amssymb}", 0)
+ if j == -1:
+ document.header.insert(i + 1, "\\use_package amssymb %d" % useamsmath)
+ else:
+ document.header.insert(i + 1, "\\use_package amssymb 2")
+ del document.preamble[j]
+
+
+def revert_use_amssymb(document):
+ "remove use_package amssymb"
+ regexp1 = re.compile(r'(\\use_package\s+amsmath)')
+ regexp2 = re.compile(r'(\\use_package\s+amssymb)')
+ i = find_re(document.header, regexp1, 0)
+ j = find_re(document.header, regexp2, 0)
+ value1 = "1" # default is auto
+ value2 = "1" # default is auto
+ if i != -1:
+ value1 = get_value(document.header, "\\use_package" , i).split()[1]
+ if j != -1:
+ value2 = get_value(document.header, "\\use_package" , j).split()[1]
+ del document.header[j]
+ if value1 != value2 and value2 == "2": # on
+ add_to_preamble(document, ["\\usepackage{amssymb}"])
+
+
##
# Conversion hub
#
[425, []],
[426, []],
[427, []],
- [428, [convert_cell_rotation]]
+ [428, [convert_cell_rotation]],
+ [429, [convert_table_rotation]],
+ [430, [convert_listoflistings]],
+ [431, [convert_use_amssymb]],
]
revert = [
+ [430, [revert_use_amssymb]],
+ [429, [revert_listoflistings]],
+ [428, [revert_table_rotation]],
[427, [revert_cell_rotation]],
[426, [revert_tipa]],
[425, [revert_verbatim]],