X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Flyx2lyx%2Flyx_1_4.py;h=9c7c7a594e5e29b2a4cc4cbcb390f7ac98e55bf1;hb=9a1b26a156c913f484ca2293fb2ec1c4986d2a3e;hp=e67e86f8f56a4e0acb3755b1578a684677554c12;hpb=1a4b549a534c70cc8c336b0baeeb523d07c9cb58;p=lyx.git diff --git a/lib/lyx2lyx/lyx_1_4.py b/lib/lyx2lyx/lyx_1_4.py index e67e86f8f5..9c7c7a594e 100644 --- a/lib/lyx2lyx/lyx_1_4.py +++ b/lib/lyx2lyx/lyx_1_4.py @@ -16,7 +16,7 @@ # # 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. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """ Convert files to the file format generated by lyx 1.4""" @@ -24,7 +24,7 @@ import re from os import access, F_OK import os.path from parser_tools import check_token, find_token, \ - get_value, del_token, is_nonempty_line, \ + get_value, is_nonempty_line, \ find_tokens, find_end_of, find_beginning_of, find_token_exact, find_tokens_exact, \ find_re, find_tokens_backwards from sys import stdin @@ -84,13 +84,28 @@ def find_end_of_inset(lines, i): "Finds the matching \end_inset" return find_end_of(lines, i, "\\begin_inset", "\\end_inset") +def del_token(lines, token, start, end): + """ del_token(lines, token, start, end) -> int + + Find the lower line in lines where token is the first element and + delete that line. + + Returns the number of lines remaining.""" + + k = find_token_exact(lines, token, start, end) + if k == -1: + return end + else: + del lines[k] + return end - 1 + # End of helper functions #################################################################### def remove_color_default(document): " Remove \color default" i = 0 - while 1: + while True: i = find_token(document.body, "\\color default", i) if i == -1: return @@ -164,7 +179,7 @@ def revert_spaces(document): " \InsetSpace ~ -> \SpecialChar ~" regexp = re.compile(r'(.*)(\\InsetSpace\s+)(\S+)') i = 0 - while 1: + while True: i = find_re(document.body, regexp, i) if i == -1: break @@ -221,7 +236,7 @@ def revert_eqref(document): "\\begin_inset LatexCommand \\eqref -> ERT" regexp = re.compile(r'^\\begin_inset\s+LatexCommand\s+\\eqref') i = 0 - while 1: + while True: i = find_re(document.body, regexp, i) if i == -1: break @@ -249,7 +264,7 @@ def revert_bibtex(document): def remove_insetparent(document): " Remove \lyxparent" i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_inset LatexCommand \\lyxparent", i) if i == -1: break @@ -261,7 +276,7 @@ def convert_external(document): external_rexp = re.compile(r'\\begin_inset External ([^,]*),"([^"]*)",') external_header = "\\begin_inset External" i = 0 - while 1: + while True: i = find_token(document.body, external_header, i) if i == -1: break @@ -296,7 +311,7 @@ def revert_external_1(document): " Revert inset External." external_header = "\\begin_inset External" i = 0 - while 1: + while True: i = find_token(document.body, external_header, i) if i == -1: break @@ -321,7 +336,7 @@ def revert_external_2(document): " Revert inset External. (part II)" draft_token = '\tdraft' i = 0 - while 1: + while True: i = find_token(document.body, '\\begin_inset External', i) if i == -1: break @@ -339,7 +354,7 @@ def convert_comment(document): " Convert \\layout comment" i = 0 comment = "\\layout Comment" - while 1: + while True: i = find_token(document.body, comment, i) if i == -1: return @@ -350,7 +365,7 @@ def convert_comment(document): '\\layout %s' % document.default_layout] i = i + 7 - while 1: + while True: old_i = i i = find_token(document.body, "\\layout", i) if i == -1: @@ -372,7 +387,7 @@ def convert_comment(document): #but if this happens deal with it greacefully adding #the missing \end_deeper. i = len(document.body) - 1 - document.body[i:i] = ["\end_deeper",""] + document.body[i:i] = ["\\end_deeper",""] return else: del document.body[i] @@ -403,7 +418,7 @@ def convert_comment(document): def revert_comment(document): " Revert comments" i = 0 - while 1: + while True: i = find_tokens(document.body, ["\\begin_inset Comment", "\\begin_inset Greyedout"], i) if i == -1: @@ -422,7 +437,7 @@ def add_end_layout(document): i = i + 1 struct_stack = ["\\layout"] - while 1: + while True: i = find_tokens(document.body, ["\\begin_inset", "\\end_inset", "\\layout", "\\begin_deeper", "\\end_deeper", "\\the_end"], i) @@ -466,6 +481,9 @@ def add_end_layout(document): document.body.insert(i,"") document.body.insert(i,"\\end_layout") i = i + 3 + # consecutive begin_deeper only insert one end_layout + while document.body[i].startswith('\\begin_deeper'): + i += 1 struct_stack.append(token) continue @@ -486,7 +504,7 @@ def add_end_layout(document): def rm_end_layout(document): " Remove \end_layout" i = 0 - while 1: + while True: i = find_token(document.body, '\\end_layout', i) if i == -1: @@ -517,7 +535,7 @@ def rm_tracking_changes(document): def rm_body_changes(document): " Remove body changes." i = 0 - while 1: + while True: i = find_token(document.body, "\\change_", i) if i == -1: return @@ -528,7 +546,7 @@ def rm_body_changes(document): def layout2begin_layout(document): " \layout -> \begin_layout " i = 0 - while 1: + while True: i = find_token(document.body, '\\layout', i) if i == -1: return @@ -540,7 +558,7 @@ def layout2begin_layout(document): def begin_layout2layout(document): " \begin_layout -> \layout " i = 0 - while 1: + while True: i = find_token(document.body, '\\begin_layout', i) if i == -1: return @@ -560,7 +578,7 @@ def convert_table_valignment_middle(document): " Convert table valignment, center -> middle" regexp = re.compile(r'^\\begin_inset\s+Tabular') i = 0 - while 1: + while True: i = find_re(document.body, regexp, i) if i == -1: return @@ -584,7 +602,7 @@ def revert_valignment_middle(document): " Convert table valignment, middle -> center" regexp = re.compile(r'^\\begin_inset\s+Tabular') i = 0 - while 1: + while True: i = find_re(document.body, regexp, i) if i == -1: return @@ -659,7 +677,7 @@ parskip} attribute_values = ['default', 'default', 'default', 'default', 'default', 'default', 'default', 'none', document.language] i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_layout", i) if i == -1: return @@ -817,7 +835,7 @@ parskip} def convert_note(document): " Convert Notes. " i = 0 - while 1: + while True: i = find_tokens(document.body, ["\\begin_inset Note", "\\begin_inset Comment", "\\begin_inset Greyedout"], i) @@ -832,7 +850,7 @@ def revert_note(document): " Revert Notes. " note_header = "\\begin_inset Note " i = 0 - while 1: + while True: i = find_token(document.body, note_header, i) if i == -1: break @@ -844,7 +862,7 @@ def revert_note(document): def convert_box(document): " Convert Boxes. " i = 0 - while 1: + while True: i = find_tokens(document.body, ["\\begin_inset Boxed", "\\begin_inset Doublebox", "\\begin_inset Frameless", @@ -862,7 +880,7 @@ def revert_box(document): " Revert Boxes." box_header = "\\begin_inset Box " i = 0 - while 1: + while True: i = find_token(document.body, box_header, i) if i == -1: break @@ -871,10 +889,10 @@ def revert_box(document): i = i + 1 -def convert_collapsable(document): +def convert_collapsible(document): " Convert collapsed insets. " i = 0 - while 1: + while True: i = find_tokens_exact(document.body, ["\\begin_inset Box", "\\begin_inset Branch", "\\begin_inset CharStyle", @@ -891,7 +909,7 @@ def convert_collapsable(document): # If, however, we find a line starting '\begin_layout' # (_always_ present) then break with a warning message i = i + 1 - while 1: + while True: if (document.body[i] == "collapsed false"): document.body[i] = "status open" break @@ -906,10 +924,10 @@ def convert_collapsable(document): i = i + 1 -def revert_collapsable(document): +def revert_collapsible(document): " Revert collapsed insets. " i = 0 - while 1: + while True: i = find_tokens_exact(document.body, ["\\begin_inset Box", "\\begin_inset Branch", "\\begin_inset CharStyle", @@ -926,7 +944,7 @@ def revert_collapsable(document): # If, however, we find a line starting '\begin_layout' # (_always_ present) then break with a warning message i = i + 1 - while 1: + while True: if (document.body[i] == "status open"): document.body[i] = "collapsed false" break @@ -945,7 +963,7 @@ def revert_collapsable(document): def convert_ert(document): " Convert ERT. " i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_inset ERT", i) if i == -1: break @@ -954,7 +972,7 @@ def convert_ert(document): # If, however, we find a line starting '\begin_layout' # (_always_ present) then break with a warning message i = i + 1 - while 1: + while True: if (document.body[i] == "status Open"): document.body[i] = "status open" break @@ -975,7 +993,7 @@ def convert_ert(document): def revert_ert(document): " Revert ERT. " i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_inset ERT", i) if i == -1: break @@ -984,7 +1002,7 @@ def revert_ert(document): # If, however, we find a line starting '\begin_layout' # (_always_ present) then break with a warning message i = i + 1 - while 1: + while True: if (document.body[i] == "status open"): document.body[i] = "status Open" break @@ -1010,7 +1028,7 @@ def convert_minipage(document): inner_pos = ["c","t","b","s"] i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_inset Minipage", i) if i == -1: return @@ -1181,7 +1199,7 @@ def revert_breaks(document): # Convert the insets i = 0 - while 1: + while True: i = find_tokens(document.body, tokens, i) if i == -1: return @@ -1477,7 +1495,7 @@ def convert_len(len, special): len = '%f\\' % len2value(len) + special # Convert LyX units to LaTeX units - for unit in units.keys(): + for unit in list(units.keys()): if len.find(unit) != -1: len = '%f' % (len2value(len) / 100) + units[unit] break @@ -1536,7 +1554,7 @@ def convert_frameless_box(document): pos = ['t', 'c', 'b'] inner_pos = ['c', 't', 'b', 's'] i = 0 - while 1: + while True: i = find_token(document.body, '\\begin_inset Frameless', i) if i == -1: return @@ -1553,7 +1571,7 @@ def convert_frameless_box(document): 'inner_pos':1, 'use_parbox':'0', 'width':'100col%', 'special':'none', 'height':'1in', 'height_special':'totalheight', 'collapsed':'false'} - for key in params.keys(): + for key in list(params.keys()): value = get_value(document.body, key, i, j).replace('"', '') if value != "": if key == 'position': @@ -1715,7 +1733,7 @@ def convert_frameless_box(document): def remove_branches(document): " Remove branches. " i = 0 - while 1: + while True: i = find_token(document.header, "\\branch", i) if i == -1: break @@ -1727,7 +1745,7 @@ def remove_branches(document): del document.header[i:j+1] i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_inset Branch", i) if i == -1: return @@ -1742,7 +1760,7 @@ def remove_branches(document): # If, however, we find a line starting '\layout' # (_always_ present) then break with a warning message collapsed_found = 0 - while 1: + while True: if (document.body[i][:9] == "collapsed"): del document.body[i] collapsed_found = 1 @@ -1803,7 +1821,7 @@ def revert_bibtopic(document): def convert_float(document): " Convert sideway floats. " i = 0 - while 1: + while True: i = find_token_exact(document.body, '\\begin_inset Float', i) if i == -1: return @@ -1811,7 +1829,7 @@ def convert_float(document): # If, however, we find a line starting '\begin_layout' # (_always_ present) then break with a warning message i = i + 1 - while 1: + while True: if (document.body[i][:4] == "wide"): document.body.insert(i + 1, 'sideways false') break @@ -1823,20 +1841,38 @@ def convert_float(document): def revert_float(document): - " Revert sideway floats. " + " Revert sideways floats. " i = 0 - while 1: + while True: i = find_token_exact(document.body, '\\begin_inset Float', i) if i == -1: return + line = document.body[i] + r = re.compile(r'\\begin_inset Float (.*)$') + m = r.match(line) + floattype = m.group(1) + if floattype != "figure" and floattype != "table": + i = i + 1 + continue j = find_end_of_inset(document.body, i) if j == -1: document.warning("Malformed lyx document: Missing '\\end_inset'.") i = i + 1 continue if get_value(document.body, 'sideways', i, j) != "false": - document.warning("Conversion of 'sideways true' not yet implemented.") - # Don't remove 'sideways' so that people will get warnings by lyx + l = find_token(document.body, "\\begin_layout Standard", i + 1, j) + if l == -1: + document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Float inset.") + return + document.body[j] = '\\layout Standard\n\\begin_inset ERT\nstatus Collapsed\n\n' \ + '\\layout Standard\n\n\n\\backslash\n' \ + 'end{sideways' + floattype + '}\n\n\\end_inset\n' + del document.body[i+1:l-1] + document.body[i] = '\\begin_inset ERT\nstatus Collapsed\n\n' \ + '\\layout Standard\n\n\n\\backslash\n' \ + 'begin{sideways' + floattype + '}\n\n\\end_inset\n\n' + add_to_preamble(document, + ['\\usepackage{rotfloat}\n']) i = i + 1 continue del_token(document.body, 'sideways', i, j) @@ -1847,7 +1883,7 @@ def convert_graphics(document): """ Add extension to documentnames of insetgraphics if necessary. """ i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_inset Graphics", i) if i == -1: return @@ -1857,8 +1893,7 @@ def convert_graphics(document): return i = i + 1 filename = document.body[j].split()[1] - absname = os.path.normpath(os.path.join(document.dir, filename)) - if document.input == stdin and not os.path.isabs(filename): + if document.dir == u'' and not os.path.isabs(filename): # We don't know the directory and cannot check the document. # We could use a heuristic and take the current directory, # and we could try to find out if documentname has an extension, @@ -1869,6 +1904,7 @@ def convert_graphics(document): You may need to correct the document manually or run lyx2lyx again with the .lyx document as commandline argument.""" % filename) continue + absname = os.path.normpath(os.path.join(document.dir, filename)) # This needs to be the same algorithm as in pre 233 insetgraphics if access(absname, F_OK): continue @@ -1888,7 +1924,7 @@ def convert_names(document): i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_layout Author", i) if i == -1: return @@ -2056,7 +2092,7 @@ def revert_paperpackage(document): def convert_bullets(document): " Convert bullets. " i = 0 - while 1: + while True: i = find_token(document.header, "\\bullet", i) if i == -1: return @@ -2074,7 +2110,7 @@ def convert_bullets(document): def revert_bullets(document): " Revert bullets. " i = 0 - while 1: + while True: i = find_token(document.header, "\\bullet", i) if i == -1: return @@ -2199,13 +2235,13 @@ def normalize_paragraph_params(document): "\\leftindent" i = 0 - while 1: + while True: i = find_token(document.body, '\\begin_layout', i) if i == -1: return i = i + 1 - while 1: + while True: if body[i].strip() and body[i].split()[0] not in allowed_parameters: break @@ -2246,7 +2282,7 @@ def convert_ert_paragraphs(document): '\\emph', '\\numeric', '\\bar', '\\noun', '\\color', '\\lang'] i = 0 - while 1: + while True: i = find_token(document.body, '\\begin_inset ERT', i) if i == -1: return @@ -2258,7 +2294,7 @@ def convert_ert_paragraphs(document): # convert non-standard paragraphs to standard k = i - while 1: + while True: k = find_token(document.body, "\\begin_layout", k, j) if k == -1: break @@ -2278,7 +2314,7 @@ def convert_ert_paragraphs(document): # insert an empty paragraph before each paragraph but the first k = i first_pagraph = 1 - while 1: + while True: k = find_token(document.body, "\\begin_layout", k, j) if k == -1: break @@ -2293,16 +2329,16 @@ def convert_ert_paragraphs(document): # convert \\newline to new paragraph k = i - while 1: + while True: k = find_token(document.body, "\\newline", k, j) if k == -1: break document.body[k:k+1] = ["\\end_layout", "", '\\begin_layout %s' % document.default_layout] - k = k + 4 - j = j + 3 + k = k + 3 + j = j + 2 # We need an empty line if document.default_layout == '' - if document.body[k-1] != '': - document.body.insert(k-1, '') + if document.body[k] != '': + document.body.insert(k, '') k = k + 1 j = j + 1 i = i + 1 @@ -2311,7 +2347,7 @@ def convert_ert_paragraphs(document): def revert_ert_paragraphs(document): " Remove double paragraph breaks. " i = 0 - while 1: + while True: i = find_token(document.body, '\\begin_inset ERT', i) if i == -1: return @@ -2323,7 +2359,7 @@ def revert_ert_paragraphs(document): # replace paragraph breaks with \newline k = i - while 1: + while True: k = find_token(document.body, "\\end_layout", k, j) l = find_token(document.body, "\\begin_layout", k, j) if k == -1 or l == -1: @@ -2334,7 +2370,7 @@ def revert_ert_paragraphs(document): # replace double \newlines with paragraph breaks k = i - while 1: + while True: k = find_token(document.body, "\\newline", k, j) if k == -1: break @@ -2366,7 +2402,7 @@ def convert_french(document): # Change language in the document body regexp = re.compile(r'^\\lang\s+frenchb') i = 0 - while 1: + while True: i = find_re(document.body, regexp, i) if i == -1: break @@ -2412,7 +2448,7 @@ def convert_sgml_paragraphs(document): return i = 0 - while 1: + while True: i = find_token(document.body, "\\begin_layout SGML", i) if i == -1: @@ -2427,7 +2463,7 @@ def convert_sgml_paragraphs(document): i = i + 10 ## -# Convertion hub +# Conversion hub # supported_versions = ["1.4.%d" % i for i in range(3)] + ["1.4"] @@ -2438,7 +2474,7 @@ convert = [[222, [insert_tracking_changes, add_end_header, convert_amsmath]], convert_table_valignment_middle, convert_breaks]], [226, [convert_note]], [227, [convert_box]], - [228, [convert_collapsable, convert_ert]], + [228, [convert_collapsible, convert_ert]], [229, [convert_minipage]], [230, [convert_jurabib]], [231, [convert_float]], @@ -2476,7 +2512,7 @@ revert = [[244, []], [230, [revert_float]], [229, [revert_jurabib]], [228, []], - [227, [revert_collapsable, revert_ert]], + [227, [revert_collapsible, revert_ert]], [226, [revert_box, revert_external_2]], [225, [revert_note]], [224, [rm_end_layout, begin_layout2layout, revert_end_document,