X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Flyx2lyx%2Flyx_2_4.py;h=2b03ada0d89b6877973d33e5cc290a8b896e0e5e;hb=aca303135257f979ba09c9ab70147d4b51cef230;hp=941703a163a499879c53a97a1ad7cfba0302a556;hpb=57dc817581faf54babaf3db0c602bac205dc6855;p=lyx.git diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py index 941703a163..2b03ada0d8 100644 --- a/lib/lyx2lyx/lyx_2_4.py +++ b/lib/lyx2lyx/lyx_2_4.py @@ -24,12 +24,13 @@ import sys, os # Uncomment only what you need to import, please. -from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, get_bool_value, get_value) +from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, +get_bool_value, get_value, get_quoted_value) # del_token, del_value, del_complete_lines, # find_complete_lines, find_end_of, # find_re, find_substring, find_token_backwards, -# get_containing_inset, get_containing_layout, get_value, -# get_quoted_value, is_in_inset, set_bool_value +# get_containing_inset, get_containing_layout, +# is_in_inset, set_bool_value # find_tokens, find_token_exact, check_token, get_option_value from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble) @@ -213,6 +214,119 @@ def revert_nospellcheck(document): del document.body[i] +def revert_floatpclass(document): + " Remove float placement params 'document' and 'class' " + + i = 0 + i = find_token(document.header, "\\float_placement class", 0) + if i != -1: + del document.header[i] + + i = 0 + while True: + i = find_token(document.body, '\\begin_inset Float', i) + if i == -1: + break + j = find_end_of_inset(document.body, i) + k = find_token(document.body, 'placement class', i, i + 2) + if k == -1: + k = find_token(document.body, 'placement document', i, i + 2) + if k != -1: + del document.body[k] + i = j + continue + del document.body[k] + + +def revert_floatalignment(document): + " Remove float alignment params " + + i = 0 + i = find_token(document.header, "\\float_alignment", 0) + galignment = "" + if i != -1: + galignment = get_value(document.header, "\\float_alignment", i) + del document.header[i] + + i = 0 + while True: + i = find_token(document.body, '\\begin_inset Float', i) + if i == -1: + break + j = find_end_of_inset(document.body, i) + if j == -1: + document.warning("Malformed LyX document: Can't find end of inset at line " + str(i)) + i += 1 + k = find_token(document.body, 'alignment', i, i + 4) + if k == -1: + i = j + continue + alignment = get_value(document.body, "alignment", k) + if alignment == "document": + alignment = galignment + del document.body[k] + l = find_token(document.body, "\\begin_layout Plain Layout", i, j) + if l == -1: + document.warning("Can't find float layout!") + i = j + continue + alcmd = [] + if alignment == "left": + alcmd = put_cmd_in_ert("\\raggedright{}") + elif alignment == "center": + alcmd = put_cmd_in_ert("\\centering{}") + elif alignment == "right": + alcmd = put_cmd_in_ert("\\raggedleft{}") + if len(alcmd) > 0: + document.body[l+1:l+1] = alcmd + i = j + + +def revert_tuftecite(document): + " Revert \cite commands in tufte classes " + + tufte = ["tufte-book", "tufte-handout"] + if document.textclass not in tufte: + return + + i = 0 + while (True): + i = find_token(document.body, "\\begin_inset CommandInset citation", i) + if i == -1: + break + j = find_end_of_inset(document.body, i) + if j == -1: + document.warning("Can't find end of citation inset at line %d!!" %(i)) + i += 1 + continue + k = find_token(document.body, "LatexCommand", i, j) + if k == -1: + document.warning("Can't find LatexCommand for citation inset at line %d!" %(i)) + i = j + 1 + continue + cmd = get_value(document.body, "LatexCommand", k) + if cmd != "cite": + i = j + 1 + continue + pre = get_quoted_value(document.body, "before", i, j) + post = get_quoted_value(document.body, "after", i, j) + key = get_quoted_value(document.body, "key", i, j) + if not key: + document.warning("Citation inset at line %d does not have a key!" %(i)) + key = "???" + # Replace command with ERT + res = "\\cite" + if pre: + res += "[" + pre + "]" + if post: + res += "[" + post + "]" + elif pre: + res += "[]" + res += "{" + key + "}" + document.body[i:j+1] = put_cmd_in_ert([res]) + i = j + 1 + + ## # Conversion hub # @@ -225,11 +339,15 @@ convert = [ [548, []], [549, []], [550, [convert_fontenc]], - [551, []] + [551, []], + [552, []], + [553, []] ] revert = [ - [549, [revert_nospellcheck]], + [552, [revert_tuftecite]], + [551, [revert_floatpclass, revert_floatalignment]], + [550, [revert_nospellcheck]], [549, [revert_fontenc]], [548, []],# dummy format change [547, [revert_lscape]],