X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Flyx2lyx%2Flyx_2_1.py;h=4143dda06cddb77660ddbb8db16847542f37a7a4;hb=9da74fe2078e24e1e7891784ecbfe33ff77e7f85;hp=5feefd8653c713aed72ecc2e16b69ac3cebeabcb;hpb=8f36aef17ab47aea0088648285c251415c133337;p=lyx.git diff --git a/lib/lyx2lyx/lyx_2_1.py b/lib/lyx2lyx/lyx_2_1.py index 5feefd8653..4143dda06c 100644 --- a/lib/lyx2lyx/lyx_2_1.py +++ b/lib/lyx2lyx/lyx_2_1.py @@ -17,7 +17,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -""" Convert files to the file format generated by lyx 2.1""" +""" Convert files to the file format generated by LyX 2.1""" import re, string import unicodedata @@ -383,7 +383,10 @@ def convert_use_package(document, pkg): del document.preamble[j] -def revert_use_package(document, pkg, commands): +def revert_use_package(document, pkg, commands, oldauto): + # oldauto defines how the version we are reverting to behaves: + # if it is true, the old version uses the package automatically. + # if it is false, the old version never uses the package. regexp = re.compile(r'(\\use_package\s+%s)' % pkg) i = find_re(document.header, regexp, 0) value = "1" # default is auto @@ -392,7 +395,7 @@ def revert_use_package(document, pkg, commands): del document.header[i] if value == "2": # on add_to_preamble(document, ["\\usepackage{" + pkg + "}"]) - elif value == "1": # auto + elif value == "1" and not oldauto: # auto i = 0 while True: i = find_token(document.body, '\\begin_inset Formula', i) @@ -423,7 +426,7 @@ def revert_use_mathtools(document): "coloneqq", "Coloneqq", "coloneq", "Coloneq", "eqqcolon", \ "Eqqcolon", "eqcolon", "Eqcolon", "colonapprox", \ "Colonapprox", "colonsim", "Colonsim"] - revert_use_package(document, "mathtools", commands) + revert_use_package(document, "mathtools", commands, False) def convert_use_stmaryrd(document): @@ -466,7 +469,7 @@ def revert_use_stmaryrd(document): "varcopyright", "longarrownot", "Longarrownot", \ "Mapsto", "mapsfrom", "Mapsfrom" "Longmapsto", \ "longmapsfrom", "Longmapsfrom"] - revert_use_package(document, "stmaryrd", commands) + revert_use_package(document, "stmaryrd", commands, False) @@ -478,7 +481,7 @@ def convert_use_stackrel(document): def revert_use_stackrel(document): "remove use_package stackrel" commands = ["stackrel"] - revert_use_package(document, "stackrel", commands) + revert_use_package(document, "stackrel", commands, False) def convert_cite_engine_type(document): @@ -512,11 +515,11 @@ def revert_cite_engine_type(document): document.header[i] = "\\cite_engine natbib_" + engine_type -# this is the same, as revert_use_cancel() +# this is the same, as revert_use_cancel() except for the default def revert_cancel(document): "add cancel to the preamble if necessary" commands = ["cancelto", "cancel", "bcancel", "xcancel"] - revert_use_package(document, "cancel", commands) + revert_use_package(document, "cancel", commands, False) def revert_verbatim(document): @@ -541,7 +544,7 @@ def revert_verbatim(document): return j = find_end_of_layout(document.body, i) if j == -1: - document.warning("Malformed lyx document: Can't find end of Verbatim layout") + document.warning("Malformed LyX document: Can't find end of Verbatim layout") i += 1 continue # delete all line breaks insets (there are no other insets) @@ -591,18 +594,18 @@ def revert_tipa(document): return j = find_end_of_inset(document.body, i) if j == -1: - document.warning("Malformed lyx document: Can't find end of IPA inset") + document.warning("Malformed LyX document: Can't find end of IPA inset") i += 1 continue Multipar = False n = find_token(document.body, "\\begin_layout", i, j) if n == -1: - document.warning("Malformed lyx document: IPA inset has no embedded layout") + document.warning("Malformed LyX document: IPA inset has no embedded layout") i += 1 continue m = find_end_of_layout(document.body, n) if m == -1: - document.warning("Malformed lyx document: Can't find end of embedded layout") + document.warning("Malformed LyX document: Can't find end of embedded layout") i += 1 continue content = document.body[n+1:m] @@ -747,7 +750,7 @@ def convert_listoflistings(document): return j = find_end_of_inset(document.body, i) if j == -1: - document.warning("Malformed lyx document: Can't find end of ERT inset") + document.warning("Malformed LyX document: Can't find end of ERT inset") i += 1 continue ert = get_ert(document.body, i) @@ -768,7 +771,7 @@ def revert_listoflistings(document): 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") + document.warning("Malformed LyX document: Can't find end of TOC inset") i += 1 continue subst = put_cmd_in_ert("\\lstlistoflistings{}") @@ -824,7 +827,7 @@ def convert_use_cancel(document): def revert_use_cancel(document): "remove use_package cancel" commands = ["cancel", "bcancel", "xcancel", "cancelto"] - revert_use_package(document, "cancel", commands) + revert_use_package(document, "cancel", commands, True) def revert_ancientgreek(document): @@ -1273,7 +1276,7 @@ def convert_latexargs(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[1] @@ -1326,7 +1329,7 @@ def revert_latexargs(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[1] @@ -1342,7 +1345,7 @@ def revert_latexargs(document): # Revert to old syntax document.body[p] = "\\begin_inset Argument" if j == -1: - document.warning("Malformed lyx document: Can't find end of Argument inset") + document.warning("Malformed LyX document: Can't find end of Argument inset") continue if val > 0: args[val] = document.body[p : j + 1] @@ -2022,7 +2025,7 @@ def revert_itemargs(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[3] @@ -2085,7 +2088,7 @@ def convert_beamerargs(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[1] @@ -2342,7 +2345,7 @@ def revert_beamerargs(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[1] @@ -2522,7 +2525,7 @@ def revert_beamerargs2(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[1] @@ -2604,7 +2607,7 @@ def revert_beamerargs3(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[1] @@ -2758,7 +2761,7 @@ def revert_beamerblocks(document): # Find containing paragraph layout parent = get_containing_layout(document.body, i) if parent == False: - document.warning("Malformed lyx document: Can't find parent paragraph layout") + document.warning("Malformed LyX document: Can't find parent paragraph layout") i = i + 1 continue parbeg = parent[1] @@ -2889,7 +2892,7 @@ def convert_overprint(document): # Find end of sequence j = find_end_of_sequence(document.body, i) if j == -1: - document.warning("Malformed lyx document. Cannot find end of Overprint sequence!") + document.warning("Malformed LyX document. Cannot find end of Overprint sequence!") i = i + 1 continue endseq = j @@ -2905,7 +2908,7 @@ def convert_overprint(document): if argbeg != -1: argend = find_end_of_layout(document.body, argbeg) if argend == -1: - document.warning("Malformed lyx document. Cannot find end of Overprint argument!") + document.warning("Malformed LyX document. Cannot find end of Overprint argument!") i = i + 1 continue beginPlain = find_token(document.body, "\\begin_layout Plain Layout", argbeg) @@ -2943,7 +2946,7 @@ def revert_overprint(document): # Find end of sequence j = find_end_of_sequence(document.body, i) if j == -1: - document.warning("Malformed lyx document. Cannot find end of Overprint sequence!") + document.warning("Malformed LyX document. Cannot find end of Overprint sequence!") i = i + 1 continue endseq = j @@ -2968,7 +2971,7 @@ def revert_overprint(document): if argbeg != -1: argend = find_end_of_inset(document.body, argbeg) if argend == -1: - document.warning("Malformed lyx document. Cannot find end of Overprint argument!") + document.warning("Malformed LyX document. Cannot find end of Overprint argument!") i = i + 1 continue beginPlain = find_token(document.body, "\\begin_layout Plain Layout", argbeg) @@ -2991,7 +2994,7 @@ def revert_overprint(document): if document.body[p] == "\\begin_layout Overprint": q = find_end_of_layout(document.body, p) if q == -1: - document.warning("Malformed lyx document. Cannot find end of Overprint layout!") + document.warning("Malformed LyX document. Cannot find end of Overprint layout!") p += 1 continue subst = ["\\begin_layout Standard"] + put_cmd_in_ert("\\onslide") @@ -2999,7 +3002,7 @@ def revert_overprint(document): if argbeg != -1: argend = find_end_of_inset(document.body, argbeg) if argend == -1: - document.warning("Malformed lyx document. Cannot find end of Overprint item argument!") + document.warning("Malformed LyX document. Cannot find end of Overprint item argument!") p += 1 continue beginPlain = find_token(document.body, "\\begin_layout Plain Layout", argbeg) @@ -3032,7 +3035,7 @@ def revert_frametitle(document): return j = find_end_of_layout(document.body, i) if j == -1: - document.warning("Malformed lyx document: Can't find end of FrameTitle layout") + document.warning("Malformed LyX document: Can't find end of FrameTitle layout") i = i + 1 continue endlay = j @@ -3084,7 +3087,7 @@ def convert_epigraph(document): return j = find_end_of_layout(document.body, i) if j == -1: - document.warning("Malformed lyx document: Can't find end of Epigraph layout") + document.warning("Malformed LyX document: Can't find end of Epigraph layout") i = i + 1 continue endlay = j @@ -3125,7 +3128,7 @@ def revert_epigraph(document): return j = find_end_of_layout(document.body, i) if j == -1: - document.warning("Malformed lyx document: Can't find end of Epigraph layout") + document.warning("Malformed LyX document: Can't find end of Epigraph layout") i = i + 1 continue endlay = j @@ -3158,7 +3161,6 @@ def convert_captioninsets(document): return document.body[i] = "\\begin_inset Caption Standard" i = i + 1 - def revert_captioninsets(document): @@ -3175,7 +3177,7 @@ def revert_captioninsets(document): def convert_captionlayouts(document): " Convert caption layouts to caption insets. " - + caption_dict = { "Captionabove": "Above", "Captionbelow": "Below", @@ -3184,7 +3186,7 @@ def convert_captionlayouts(document): "CenteredCaption" : "Centered", "Bicaption" : "Bicaption", } - + i = 0 while True: i = find_token(document.body, "\\begin_layout", i) @@ -3311,7 +3313,7 @@ def revert_fragileframe(document): # Find end of sequence j = find_end_of_sequence(document.body, i) if j == -1: - document.warning("Malformed lyx document. Cannot find end of FragileFrame sequence!") + document.warning("Malformed LyX document. Cannot find end of FragileFrame sequence!") i = i + 1 continue endseq = j @@ -3414,7 +3416,7 @@ def revert_newframes(document): # Find end of sequence j = find_end_of_sequence(document.body, i) if j == -1: - document.warning("Malformed lyx document. Cannot find end of Frame sequence!") + document.warning("Malformed LyX document. Cannot find end of Frame sequence!") i = i + 1 continue endseq = j @@ -3627,6 +3629,28 @@ def revert_kurier_fonts(document): add_to_preamble(document, "\\renewcommand{\\sfdefault}{%s}" % sf) document.header[k] = "\\font_sans default" +def revert_iwona_fonts(document): + " Revert iwona font definition to LaTeX " + + i = find_token(document.header, "\\font_math", 0) + if i != -1: + if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: + val = get_value(document.header, "\\font_math", i) + if val == "iwona-math": + add_to_preamble(document, "\\let\\Myrmdefault\\rmdefault\n" \ + "\\usepackage[math]{iwona}\n" \ + "\\renewcommand{\\rmdefault}{\\Myrmdefault}") + document.header[i] = "\\font_math auto" + + if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: + iwona_fonts = ["iwona", "iwonac", "iwonal", "iwonalc"] + k = find_token(document.header, "\\font_sans iwona", 0) + if k != -1: + sf = get_value(document.header, "\\font_sans", k) + if sf in iwona_fonts: + add_to_preamble(document, "\\renewcommand{\\sfdefault}{%s}" % sf) + document.header[k] = "\\font_sans default" + def revert_new_libertines(document): " Revert new libertine font definition to LaTeX " @@ -3669,6 +3693,440 @@ def revert_new_libertines(document): document.header[k] = "\\font_sans default" +def convert_lyxframes(document): + " Converts old beamer frames to new style " + + beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"] + if document.textclass not in beamer_classes: + return + + framebeg = ["BeginFrame", "BeginPlainFrame"] + frameend = ["EndFrame", "BeginFrame", "BeginPlainFrame", "AgainFrame", "Section", "Section*", + "Subsection", "Subsection*", "Subsubsection", "Subsubsection*"] + for lay in framebeg: + i = 0 + while True: + i = find_token_exact(document.body, "\\begin_layout " + lay, i) + if i == -1: + break + parent = get_containing_layout(document.body, i) + if parent == False or parent[1] != i: + document.warning("Wrong parent layout!") + i += 1 + continue + frametype = parent[0] + j = parent[2] + parbeg = parent[3] + if i != -1: + # Step I: Convert ERT arguments + # FIXME: This currently only works if the arguments are in one single ERT + ertend = i + if document.body[parbeg] == "\\begin_inset ERT": + ertend = find_end_of_inset(document.body, parbeg) + if ertend == -1: + document.warning("Malformed LyX document: missing ERT \\end_inset") + continue + ertcont = parbeg + 5 + if document.body[ertcont].startswith("[<"): + # This is a default overlay specification + # strip off the [< + document.body[ertcont] = document.body[ertcont][2:] + if document.body[ertcont].endswith(">]"): + # strip off the >] + document.body[ertcont] = document.body[ertcont][:-2] + elif document.body[ertcont].endswith("]"): + # divide the args + tok = document.body[ertcont].find('>][') + if tok != -1: + subst = [document.body[ertcont][:tok], + '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 3', + 'status collapsed', '', '\\begin_layout Plain Layout', + document.body[ertcont][tok + 3:-1]] + document.body[ertcont : ertcont + 1] = subst + ertend += 11 + # Convert to ArgInset + document.body[parbeg] = "\\begin_inset Argument 2" + elif document.body[ertcont].startswith("<"): + # This is an overlay specification + # strip off the < + document.body[ertcont] = document.body[ertcont][1:] + if document.body[ertcont].endswith(">"): + # strip off the > + document.body[ertcont] = document.body[ertcont][:-1] + # Convert to ArgInset + document.body[parbeg] = "\\begin_inset Argument 1" + elif document.body[ertcont].endswith(">]"): + # divide the args + tok = document.body[ertcont].find('>[<') + if tok != -1: + document.body[ertcont : ertcont + 1] = [document.body[ertcont][:tok], + '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 2', + 'status collapsed', '', '\\begin_layout Plain Layout', + document.body[ertcont][tok + 3:-2]] + # Convert to ArgInset + document.body[parbeg] = "\\begin_inset Argument 1" + ertend += 11 + elif document.body[ertcont].endswith("]"): + # divide the args + tok = document.body[ertcont].find('>[<') + if tok != -1: + # divide the args + tokk = document.body[ertcont].find('>][') + if tokk != -1: + document.body[ertcont : ertcont + 1] = [document.body[ertcont][:tok], + '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 2', + 'status collapsed', '', '\\begin_layout Plain Layout', + document.body[ertcont][tok + 3:tokk], + '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 3', + 'status collapsed', '', '\\begin_layout Plain Layout', + document.body[ertcont][tokk + 3:-1]] + ertend += 22 + else: + tokk = document.body[ertcont].find('>[') + if tokk != -1: + document.body[ertcont : ertcont + 1] = [document.body[ertcont][:tokk], + '\\end_layout', '', '\\end_inset', '', '', '\\begin_inset Argument 3', + 'status collapsed', '', '\\begin_layout Plain Layout', + document.body[ertcont][tokk + 2:-1]] + ertend += 11 + # Convert to ArgInset + document.body[parbeg] = "\\begin_inset Argument 1" + elif document.body[ertcont].startswith("["): + # This is an ERT option + # strip off the [ + document.body[ertcont] = document.body[ertcont][1:] + if document.body[ertcont].endswith("]"): + # strip off the ] + document.body[ertcont] = document.body[ertcont][:-1] + # Convert to ArgInset + document.body[parbeg] = "\\begin_inset Argument 3" + # End of argument conversion + # Step II: Now rename the layout and convert the title to an argument + j = find_end_of_layout(document.body, i) + document.body[j : j + 1] = ['\\end_layout', '', '\\end_inset', '', '\\end_layout'] + if lay == "BeginFrame": + document.body[i] = "\\begin_layout Frame" + else: + document.body[i] = "\\begin_layout PlainFrame" + document.body[ertend + 1 : ertend + 1] = ['\\begin_inset Argument 4', + 'status open', '', '\\begin_layout Plain Layout'] + # Step III: find real frame end + j = j + 8 + jj = j + while True: + fend = find_token(document.body, "\\begin_layout", jj) + if fend == -1: + document.warning("Malformed LyX document: No real frame end!") + return + val = get_value(document.body, "\\begin_layout", fend) + if val not in frameend: + jj = fend + 1 + continue + old = document.body[fend] + if val == frametype: + document.body[fend : fend] = ['\\end_deeper', '', '\\begin_layout Separator', '', '\\end_layout'] + else: + document.body[fend : fend] = ['\\end_deeper'] + document.body[j + 1 : j + 1] = ['', '\\begin_deeper'] + break + i = j + + +def remove_endframes(document): + " Remove deprecated beamer endframes " + + beamer_classes = ["beamer", "article-beamer", "scrarticle-beamer"] + if document.textclass not in beamer_classes: + return + + i = 0 + while True: + i = find_token_exact(document.body, "\\begin_layout EndFrame", i) + if i == -1: + break + j = find_end_of_layout(document.body, i) + if j == -1: + document.warning("Malformed LyX document: Missing \\end_layout to EndFrame") + i = i + 1 + continue + del document.body[i : j + 1] + + +def revert_powerdot_flexes(document): + " Reverts powerdot flex insets " + + if document.textclass != "powerdot": + return + + flexes = {"Onslide" : "\\onslide", + "Onslide*" : "\\onslide*", + "Onslide+" : "\\onslide+"} + rx = re.compile(r'^\\begin_inset Flex (.+)$') + + i = 0 + while True: + i = find_token(document.body, "\\begin_inset Flex", i) + if i == -1: + return + m = rx.match(document.body[i]) + if m: + flextype = m.group(1) + z = find_end_of_inset(document.body, i) + if z == -1: + document.warning("Can't find end of Flex " + flextype + " inset.") + i += 1 + continue + if flextype in flexes: + pre = put_cmd_in_ert(flexes[flextype]) + arg = find_token(document.body, "\\begin_inset Argument 1", i, z) + if arg != -1: + argend = find_end_of_inset(document.body, arg) + if argend == -1: + document.warning("Can't find end of Argument!") + i += 1 + continue + # Find containing paragraph layout + beginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg) + endPlain = find_end_of_layout(document.body, beginPlain) + argcontent = document.body[beginPlain + 1 : endPlain] + # Adjust range end + z = z - len(document.body[arg : argend + 1]) + # Remove arg inset + del document.body[arg : argend + 1] + pre += put_cmd_in_ert("{") + argcontent + put_cmd_in_ert("}") + pre += put_cmd_in_ert("{") + beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i) + endPlain = find_end_of_layout(document.body, beginPlain) + # Adjust range end + z = z - len(document.body[i : beginPlain + 1]) + z += len(pre) + document.body[i : beginPlain + 1] = pre + post = put_cmd_in_ert("}") + document.body[z - 2 : z + 1] = post + i += 1 + + +def revert_powerdot_pause(document): + " Reverts powerdot pause layout to ERT " + + if document.textclass != "powerdot": + return + + i = 0 + while True: + i = find_token(document.body, "\\begin_layout Pause", i) + if i == -1: + return + j = find_end_of_layout(document.body, i) + if j == -1: + document.warning("Malformed LyX document: Can't find end of Pause layout") + i = i + 1 + continue + endlay = j + subst = ["\\begin_layout Standard"] + put_cmd_in_ert("\\pause") + for p in range(i, j): + if p >= endlay: + break + arg = find_token(document.body, "\\begin_inset Argument 1", i, j) + if arg != -1: + beginPlain = find_token(document.body, "\\begin_layout Plain Layout", p) + endPlain = find_end_of_layout(document.body, beginPlain) + endInset = find_end_of_inset(document.body, p) + content = document.body[beginPlain + 1 : endPlain] + # Adjust range end + endlay = endlay - len(document.body[p : endInset + 1]) + # Remove arg inset + del document.body[p : endInset + 1] + subst += put_cmd_in_ert("[") + content + put_cmd_in_ert("]") + + document.body[i : i + 1] = subst + i = endlay + + +def revert_powerdot_itemargs(document): + " Reverts powerdot item arguments to ERT " + + if document.textclass != "powerdot": + return + + i = 0 + list_layouts = ["Itemize", "ItemizeType1", "Enumerate", "EnumerateType1"] + rx = re.compile(r'^\\begin_inset Argument (\S+)$') + + while True: + i = find_token(document.body, "\\begin_inset Argument", i) + if i == -1: + return + # Find containing paragraph layout + parent = get_containing_layout(document.body, i) + if parent == False: + document.warning("Malformed LyX document: Can't find parent paragraph layout") + i = i + 1 + continue + parbeg = parent[1] + parend = parent[2] + realparbeg = parent[3] + layoutname = parent[0] + realparend = parend + for p in range(parbeg, parend): + if p >= realparend: + i = realparend + break + if layoutname in list_layouts: + m = rx.match(document.body[p]) + if m: + argnr = m.group(1) + if argnr == "item:1": + j = find_end_of_inset(document.body, i) + # Find containing paragraph layout + beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i) + endPlain = find_end_of_layout(document.body, beginPlain) + content = document.body[beginPlain + 1 : endPlain] + del document.body[i:j+1] + subst = put_cmd_in_ert("[") + content + put_cmd_in_ert("]") + document.body[realparbeg : realparbeg] = subst + elif argnr == "item:2": + j = find_end_of_inset(document.body, i) + # Find containing paragraph layout + beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i) + endPlain = find_end_of_layout(document.body, beginPlain) + content = document.body[beginPlain + 1 : endPlain] + del document.body[i:j+1] + subst = put_cmd_in_ert("<") + content + put_cmd_in_ert(">") + document.body[realparbeg : realparbeg] = subst + + i = realparend + + +def revert_powerdot_columns(document): + " Reverts powerdot twocolumn to TeX-code " + if document.textclass != "powerdot": + return + + rx = re.compile(r'^\\begin_inset Argument (\S+)$') + i = 0 + while True: + i = find_token(document.body, "\\begin_layout Twocolumn", i) + if i == -1: + return + j = find_end_of_layout(document.body, i) + if j == -1: + document.warning("Malformed LyX document: Can't find end of Twocolumn layout") + i = i + 1 + continue + endlay = j + document.body[j : j] = put_cmd_in_ert("}") + document.body[j : j] + endlay += len(put_cmd_in_ert("}")) + subst = ["\\begin_layout Standard"] + put_cmd_in_ert("\\twocolumn") + for p in range(i, j): + if p >= endlay: + break + m = rx.match(document.body[p]) + if m: + argnr = m.group(1) + if argnr == "1": + beginPlain = find_token(document.body, "\\begin_layout Plain Layout", p) + endPlain = find_end_of_layout(document.body, beginPlain) + endInset = find_end_of_inset(document.body, p) + content = document.body[beginPlain + 1 : endPlain] + # Adjust range end + endlay = endlay - len(document.body[p : endInset + 1]) + # Remove arg inset + del document.body[p : endInset + 1] + subst += put_cmd_in_ert("[") + content + put_cmd_in_ert("]") + elif argnr == "2": + beginPlain = find_token(document.body, "\\begin_layout Plain Layout", p) + endPlain = find_end_of_layout(document.body, beginPlain) + endInset = find_end_of_inset(document.body, p) + content = document.body[beginPlain + 1 : endPlain] + # Adjust range end + endlay = endlay - len(document.body[p : endInset + 1]) + # Remove arg inset + del document.body[p : endInset + 1] + subst += put_cmd_in_ert("{") + content + put_cmd_in_ert("}") + + subst += put_cmd_in_ert("{") + document.body[i : i + 1] = subst + i = endlay + + +def revert_mbox_fbox(document): + 'Convert revert mbox/fbox boxes to TeX-code' + i = 0 + while True: + i = find_token(document.body, "\\begin_inset Box", i) + if i == -1: + return + j = find_token(document.body, "width", i) + if j != i + 7: + document.warning("Malformed LyX document: Can't find box width") + return + width = get_value(document.body, "width", j) + k = find_end_of_inset(document.body, j) + if k == -1: + document.warning("Malformed LyX document: Can't find end of box inset") + i += 1 + continue + BeginLayout = find_token(document.body, "\\begin_layout Plain Layout", j) + EndLayout = find_token(document.body, "\\end_layout", BeginLayout) + # replace if width is "-999col%" + if (width == '"-999col%"'): + document.body[EndLayout:k + 1] = put_cmd_in_ert("}") + if document.body[i] == "\\begin_inset Box Frameless": + document.body[i:BeginLayout + 1] = put_cmd_in_ert("\\mbox{") + if document.body[i] == "\\begin_inset Box Boxed": + document.body[i:BeginLayout + 1] = put_cmd_in_ert("\\fbox{") + i = i + 1 + + +def revert_starred_caption(document): + " Reverts unnumbered longtable caption insets " + + i = 0 + while True: + i = find_token(document.body, "\\begin_inset Caption LongTableNoNumber", i) + if i == -1: + return + # This is not equivalent, but since the caption inset is a full blown + # text inset a true conversion to ERT is too difficult. + document.body[i] = "\\begin_inset Caption Standard" + i = i + 1 + + +def revert_forced_local_layout(document): + i = 0 + while True: + i = find_token(document.header, "\\begin_forced_local_layout", i) + if i == -1: + return + j = find_end_of(document.header, i, "\\begin_forced_local_layout", "\\end_forced_local_layout") + if j == -1: + # this should not happen + break + regexp = re.compile(r'\s*forcelocal', re.IGNORECASE) + k = find_re(document.header, regexp, i, j) + while k != -1: + del document.header[k] + j = j - 1 + k = find_re(document.header, regexp, i, j) + k = find_token(document.header, "\\begin_local_layout", 0) + if k == -1: + document.header[i] = "\\begin_local_layout" + document.header[j] = "\\end_local_layout" + else: + l = find_end_of(document.header, k, "\\begin_local_layout", "\\end_local_layout") + if j == -1: + # this should not happen + break + lines = document.header[i+1 : j] + if k > i: + document.header[k+1 : k+1] = lines + document.header[i : j ] = [] + else: + document.header[i : j ] = [] + document.header[k+1 : k+1] = lines + + ## # Conversion hub # @@ -3726,9 +4184,21 @@ convert = [ [462, []], [463, [convert_encodings]], [464, [convert_use_cancel]], + [465, [convert_lyxframes, remove_endframes]], + [466, []], + [467, []], + [468, []], + [469, []], + [470, []] ] revert = [ + [469, [revert_forced_local_layout]], + [468, [revert_starred_caption]], + [467, [revert_mbox_fbox]], + [466, [revert_iwona_fonts]], + [465, [revert_powerdot_flexes, revert_powerdot_pause, revert_powerdot_itemargs, revert_powerdot_columns]], + [464, []], [463, [revert_use_cancel]], [462, [revert_encodings]], [461, [revert_new_libertines]],