+2004-05-11 José Matos <jamatos@lyx.org>
+
+ * layouts/db_stdtitle.inc: obsolete FirstName and Surname.
+
2004-05-03 José Matos <jamatos@lyx.org>
* scripts/legacy_lyxpreview2ppm.py: bug fix for temporary file
Style Author
LatexType Environment
LatexName author
- KeepEmpty 1
End
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
AlignPossible Block
LabelType No_Label
End
+
+
+Style FirstName
+ ObsoletedBy Standard
+End
+
+Style Surname
+ ObsoletedBy Standard
+End
\ No newline at end of file
+2004-05-11 José Matos <jamatos@lyx.org>
+
+
+ * 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 <jamatos@lyx.org>
* lyx_1_4.py (convert_graphics):
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
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:
self.warning("Quiting.")
sys.exit(1)
+
def usage():
print """Usage: lyx2lyx [options] [file]
Convert old lyx file <file> to newer format, files can be compressed with gzip.
except:
opt.input = open(file)
+
def main(argv):
version = "1.4.0cvs"
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 == "":
set_format(header, opt.format)
write_file(header, body, opt)
+
if __name__ == "__main__":
main(sys.argv)
i = i + 1
struct_stack = ["\\layout"]
+
while 1:
i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
"\\begin_deeper", "\\end_deeper", "\\the_end"], i)
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
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
##
-# 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:
del_token(lines, 'sideways', i, j)
i = i + 1
+
def convert_graphics(lines, opt):
""" Add extension to filenames of insetgraphics if necessary.
"""
# 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
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)
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
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):
return i
return -1
+
def find_token2(lines, token, start, end = 0):
if end == 0:
end = len(lines)
return i
return -1
+
def find_tokens(lines, tokens, start, end = 0):
if end == 0:
end = len(lines)
return i
return -1
+
def find_re(lines, rexp, start, end = 0):
if end == 0:
end = len(lines)
return i
return -1
+
def find_token_backwards(lines, token, start):
m = len(token)
for i in xrange(start, -1, -1):
return i
return -1
+
def find_tokens_backwards(lines, tokens, start):
for i in xrange(start, -1, -1):
line = lines[i]
return i
return -1
+
def get_value(lines, token, start, end = 0):
i = find_token2(lines, token, start, end)
if i == -1:
else:
return ""
+
def del_token(lines, token, i, j):
k = find_token2(lines, token, i, j)
if k == -1:
del lines[k]
return j-1
+
# Finds the paragraph that contains line i.
def get_paragraph(lines, i):
while i != -1:
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:
i = find_end_of_inset(lines, i)
return -1
+
def find_end_of(lines, i, start_token, end_token):
count = 1
n = len(lines)
return i
return -1
+
# Finds the matching \end_inset
def find_beginning_of(lines, i, start_token, end_token):
count = 1
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, "<lyxtabular", "</lyxtabular")
+
def get_tabular_lines(lines, i):
result = []
i = i+1
i = i+1
return result
+
def is_nonempty_line(line):
return line != " "*len(line)
+
def find_nonempty_line(lines, start, end = 0):
if end == 0:
end = len(lines)
return i
return -1
+
##
# Tools for file reading
#
break
body.append(line[:-1])
+
def write_file(header, body, opt):
for line in header:
opt.output.write(line+"\n")
for line in body:
opt.output.write(line+"\n")
+
##
# lyx version
#
return result.group(1)
return None
+
def set_version(lines, version):
lines[0] = "#LyX %s created this file. For more info see http://www.lyx.org/" % version
if lines[1][0] == '#':
("1_3", [221], ["1.3.0","1.3.1","1.3.2","1.3.3","1.3.4","1.3"]),
("1_4", [223,224,225,226,227,228,229,230,231,232,233], ["1.4.0cvs","1.4"])]
+
def lyxformat(format, opt):
result = format_re.match(format)
if result:
opt.error(str(format) + ": " + "Format no supported.")
return None
+
def read_format(header, opt):
for line in header:
result = fileformat.match(line)
opt.error("Invalid LyX File.")
return None
+
def set_format(lines, number):
if int(number) <= 217:
number = float(number)/100
i = find_token(lines, "\\lyxformat", 0)
lines[i] = "\\lyxformat %s" % number
+
def get_end_format():
return format_relation[-1:][0][1][-1:][0]
+
+def get_backend(textclass):
+ if textclass == "linuxdoc" or textclass == "manpage":
+ return "linuxdoc"
+ if textclass[:7] == "docbook":
+ return "docbook"
+ return "latex"
+
+
def chain(opt, initial_version):
""" This is where all the decisions related with the convertion are taken"""