From: José Matox Date: Tue, 11 May 2004 16:13:33 +0000 (+0000) Subject: whitespace, bugfixes, and convertions dependent on the document type, if need. X-Git-Tag: 1.6.10~15231 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=34b9ed8ac93b3d1e6c7ab0dda0aea88d15eeebc9;p=features.git whitespace, bugfixes, and convertions dependent on the document type, if need. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8749 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/lib/ChangeLog b/lib/ChangeLog index d016268632..ff28bd90e7 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,7 @@ +2004-05-11 José Matos + + * layouts/db_stdtitle.inc: obsolete FirstName and Surname. + 2004-05-03 José Matos * scripts/legacy_lyxpreview2ppm.py: bug fix for temporary file diff --git a/lib/layouts/db_stdtitle.inc b/lib/layouts/db_stdtitle.inc index 97067bf645..d952643647 100644 --- a/lib/layouts/db_stdtitle.inc +++ b/lib/layouts/db_stdtitle.inc @@ -20,7 +20,6 @@ End Style Author LatexType Environment LatexName author - KeepEmpty 1 End @@ -32,30 +31,6 @@ Style Authorgroup End -Style FirstName - Margin Static - LatexType Paragraph - LatexName firstname - ParIndent MM - ParSkip 0.4 - Align Center - AlignPossible Center - LabelType No_Label -End - - -Style Surname - Margin Static - LatexType Paragraph - LatexName surname - ParIndent MM - ParSkip 0.4 - Align Center - AlignPossible Center - LabelType No_Label -End - - Style Date LatexType Paragraph LatexName date @@ -116,3 +91,12 @@ Style RevisionRemark AlignPossible Block LabelType No_Label End + + +Style FirstName + ObsoletedBy Standard +End + +Style Surname + ObsoletedBy Standard +End \ No newline at end of file diff --git a/lib/lyx2lyx/ChangeLog b/lib/lyx2lyx/ChangeLog index 68fa99d36b..d69e9740c9 100644 --- a/lib/lyx2lyx/ChangeLog +++ b/lib/lyx2lyx/ChangeLog @@ -1,3 +1,16 @@ +2004-05-11 José Matos + + + * parser_tools.py (get_backend): get the document backend. + * lyx2lyx (main): new member of the document structure, + the textclass and backend, that default to article and latex. + * lyx_1_4.py (add_end_layout): fix bug of mixed \begin_deeper + and \end_layout + (insert_tracking_changes): insert \tracking_changes 0, if not present + in the header. + (convert_names, revert_names): for docbook documents transform + the styles FirstName and Surname into character styles. + 2004-05-03 José Matos * lyx_1_4.py (convert_graphics): diff --git a/lib/lyx2lyx/lyx2lyx b/lib/lyx2lyx/lyx2lyx index ffe36e893c..f1489147ce 100755 --- a/lib/lyx2lyx/lyx2lyx +++ b/lib/lyx2lyx/lyx2lyx @@ -21,7 +21,8 @@ import gzip import sys import os.path from parser_tools import read_file, write_file, read_version, set_version, \ - read_format, set_format, chain, lyxformat, get_value + read_format, set_format, chain, lyxformat, get_value, get_backend + # Allow the dummy object to be able to carry related data # like a C struct @@ -34,6 +35,8 @@ class struct: self.dir = "" self.start = None self.end = None + self.backend = "latex" + self.textclass = "article" def warning(self, message, debug_level= 1): if debug_level <= self.debug: @@ -44,6 +47,7 @@ class struct: self.warning("Quiting.") sys.exit(1) + def usage(): print """Usage: lyx2lyx [options] [file] Convert old lyx file to newer format, files can be compressed with gzip. @@ -104,6 +108,7 @@ def parse_options(argv, version, opt): except: opt.input = open(file) + def main(argv): version = "1.4.0cvs" @@ -117,6 +122,9 @@ def main(argv): read_file(header, body, opt) initial_version = read_version(header) + + opt.textclass = get_value(header, "\\textclass", 0) + opt.backend = get_backend( opt.textclass) opt.format = read_format(header, opt) opt.language = get_value(header, "\\language", 0) if opt.language == "": @@ -133,5 +141,6 @@ def main(argv): set_format(header, opt.format) write_file(header, body, opt) + if __name__ == "__main__": main(sys.argv) diff --git a/lib/lyx2lyx/lyx_1_4.py b/lib/lyx2lyx/lyx_1_4.py index 50a42c468e..5282d0e77a 100644 --- a/lib/lyx2lyx/lyx_1_4.py +++ b/lib/lyx2lyx/lyx_1_4.py @@ -248,6 +248,7 @@ def add_end_layout(lines): i = i + 1 struct_stack = ["\\layout"] + while 1: i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout", "\\begin_deeper", "\\end_deeper", "\\the_end"], i) @@ -262,8 +263,9 @@ def add_end_layout(lines): if token == "\\end_inset": tail = struct_stack.pop() if tail == "\\layout": + lines.insert(i,"") lines.insert(i,"\\end_layout") - i = i + 1 + i = i + 2 #Check if it is the correct tag struct_stack.pop() i = i + 1 @@ -272,20 +274,32 @@ def add_end_layout(lines): if token == "\\layout": tail = struct_stack.pop() if tail == token: + lines.insert(i,"") lines.insert(i,"\\end_layout") - i = i + 2 + i = i + 3 else: struct_stack.append(tail) i = i + 1 struct_stack.append(token) continue - if token == "\\begin_deeper" or token == "\\end_deeper": + if token == "\\begin_deeper": + lines.insert(i,"") lines.insert(i,"\\end_layout") - i = i + 2 + i = i + 3 + struct_stack.append(token) + continue + + if token == "\\end_deeper": + lines.insert(i,"") + lines.insert(i,"\\end_layout") + i = i + 3 + while struct_stack[-1] != "\\begin_deeper": + struct_stack.pop() continue #case \end_document + lines.insert(i, "") lines.insert(i, "\\end_layout") return @@ -302,8 +316,13 @@ def rm_end_layout(lines): ## -# Remove change tracking keywords +# Handle change tracking keywords # +def insert_tracking_changes(lines): + i = find_token(lines, "\\tracking_changes", 0) + if i == -1: + lines.append("\\tracking_changes 0") + def rm_tracking_changes(lines): i = find_token(lines, "\\author", 0) if i != -1: @@ -1118,6 +1137,7 @@ def revert_float(lines, opt): del_token(lines, 'sideways', i, j) i = i + 1 + def convert_graphics(lines, opt): """ Add extension to filenames of insetgraphics if necessary. """ @@ -1138,7 +1158,7 @@ def convert_graphics(lines, opt): # We could use a heuristic and take the current directory, # and we could try to find out if filename has an extension, # but that would be just guesses and could be wrong. - opt.warning("""Warning: Can not determine wether file + opt.warning("""Warning: Can not determine whether file %s needs an extension when reading from standard input. You may need to correct the file manually or run @@ -1154,12 +1174,98 @@ def convert_graphics(lines, opt): lines[j] = replace(lines[j], filename, filename + ".eps") +## +# Convert firstname and surname from styles -> char styles +# +def convert_names(lines, opt): + """ Convert in the docbook backend from firstname and surname style + to charstyles. + """ + if opt.backend != "docbook": + return + + i = 0 + + while 1: + i = find_token(lines, "\\begin_layout Author", i) + if i == -1: + return + + i = i + 1 + while lines[i] == "": + i = i + 1 + + if lines[i][:11] != "\\end_layout" or lines[i+2][:13] != "\\begin_deeper": + i = i + 1 + continue + + k = i + i = find_end_of( lines, i+3, "\\begin_deeper","\\end_deeper") + if i == -1: + # something is really wrong, abort + opt.warning("Missing \\end_deeper,after style Author") + opt.warning("Aborted attempt to parse FirstName and Surname") + return + firstname, surname = "", "" + + name = lines[k:i] + + j = find_token(name, "\\begin_layout FirstName", 0) + if j != -1: + j = j + 1 + while(name[j] != "\\end_layout"): + firstname = firstname + name[j] + j = j + 1 + + j = find_token(name, "\\begin_layout Surname", 0) + if j != -1: + j = j + 1 + while(name[j] != "\\end_layout"): + surname = surname + name[j] + j = j + 1 + + # delete name + del lines[k+2:i+1] + + lines[k-1:k-1] = ["", "", + "\\begin_inset CharStyle Firstname", + "status inlined", + "", + "\\begin_layout Standard", + "", + "%s" % firstname, + "\end_layout", + "", + "\end_inset ", + "", + "", + "\\begin_inset CharStyle Surname", + "status inlined", + "", + "\\begin_layout Standard", + "", + "%s" % surname, + "\\end_layout", + "", + "\\end_inset ", + ""] + + +def revert_names(lines, opt): + """ Revert in the docbook backend from firstname and surname char style + to styles. + """ + if opt.backend != "docbook": + return + + ## # Convertion hub # def convert(header, body, opt): if opt.format < 223: + insert_tracking_changes(header) add_end_header(header) convert_spaces(body) convert_bibtex(body) @@ -1220,10 +1326,12 @@ def convert(header, body, opt): if opt.format < 233: convert_graphics(body, opt) + convert_names(body, opt) opt.format = 233 def revert(header, body, opt): if opt.format > 232: + revert_names(body, opt) opt.format = 232 if opt.end == opt.format: return diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py index ac4651c4c3..676710f4b7 100644 --- a/lib/lyx2lyx/parser_tools.py +++ b/lib/lyx2lyx/parser_tools.py @@ -19,11 +19,13 @@ import string import re + def check_token(line, token): if line[:len(token)] == token: return 1 return 0 + # We need to check that the char after the token is space, but I think # we can ignore this def find_token(lines, token, start, end = 0): @@ -35,6 +37,7 @@ def find_token(lines, token, start, end = 0): return i return -1 + def find_token2(lines, token, start, end = 0): if end == 0: end = len(lines) @@ -44,6 +47,7 @@ def find_token2(lines, token, start, end = 0): return i return -1 + def find_tokens(lines, tokens, start, end = 0): if end == 0: end = len(lines) @@ -54,6 +58,7 @@ def find_tokens(lines, tokens, start, end = 0): return i return -1 + def find_re(lines, rexp, start, end = 0): if end == 0: end = len(lines) @@ -62,6 +67,7 @@ def find_re(lines, rexp, start, end = 0): return i return -1 + def find_token_backwards(lines, token, start): m = len(token) for i in xrange(start, -1, -1): @@ -70,6 +76,7 @@ def find_token_backwards(lines, token, start): return i return -1 + def find_tokens_backwards(lines, tokens, start): for i in xrange(start, -1, -1): line = lines[i] @@ -78,6 +85,7 @@ def find_tokens_backwards(lines, tokens, start): return i return -1 + def get_value(lines, token, start, end = 0): i = find_token2(lines, token, start, end) if i == -1: @@ -87,6 +95,7 @@ def get_value(lines, token, start, end = 0): else: return "" + def del_token(lines, token, i, j): k = find_token2(lines, token, i, j) if k == -1: @@ -95,6 +104,7 @@ def del_token(lines, token, i, j): del lines[k] return j-1 + # Finds the paragraph that contains line i. def get_paragraph(lines, i): while i != -1: @@ -105,6 +115,7 @@ def get_paragraph(lines, i): i = find_beginning_of_inset(lines, i) return -1 + # Finds the paragraph after the paragraph that contains line i. def get_next_paragraph(lines, i): while i != -1: @@ -114,6 +125,7 @@ def get_next_paragraph(lines, i): i = find_end_of_inset(lines, i) return -1 + def find_end_of(lines, i, start_token, end_token): count = 1 n = len(lines) @@ -127,6 +139,7 @@ def find_end_of(lines, i, start_token, end_token): return i return -1 + # Finds the matching \end_inset def find_beginning_of(lines, i, start_token, end_token): count = 1 @@ -140,17 +153,21 @@ def find_beginning_of(lines, i, start_token, end_token): return i return -1 + # Finds the matching \end_inset def find_end_of_inset(lines, i): return find_end_of(lines, i, "\\begin_inset", "\\end_inset") + # Finds the matching \end_inset def find_beginning_of_inset(lines, i): return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset") + def find_end_of_tabular(lines, i): return find_end_of(lines, i, "