5 from lxml.html.diff import htmldiff
10 parser = optparse.OptionParser(
11 usage="%prog [OPTIONS] FILE1 FILE2\n"
12 "%prog --annotate [OPTIONS] INFO1 FILE1 INFO2 FILE2 ...",
13 description=description,
21 help="File to write the difference to",
28 help="Do an annotation")
33 options, args = parser.parse_args(args)
34 if options.annotation:
35 return annotate(options, args)
37 print 'Error: you must give two files'
41 input1 = read_file(file1)
42 input2 = read_file(file2)
43 body1 = split_body(input1)[1]
44 pre, body2, post = split_body(input2)
45 result = htmldiff(body1, body2)
46 result = pre + result + post
47 if options.output == '-':
48 if not result.endswith('\n'):
50 sys.stdout.write(result)
52 f = open(options.output, 'wb')
56 def read_file(filename):
59 elif not os.path.exists(filename):
61 "Input file %s does not exist" % filename)
63 f = open(filename, 'rb')
68 body_start_re = re.compile(
69 r"<body.*?>", re.I|re.S)
70 body_end_re = re.compile(
71 r"</body.*?>", re.I|re.S)
74 match = body_start_re.search(html)
76 pre = html[:match.end()]
77 html = html[match.end():]
78 match = body_end_re.search(html)
80 post = html[match.start():]
81 html = html[:match.start()]
82 return pre, html, post
84 def annotate(options, args):
85 print "Not yet implemented"