import difflib
import os.path
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()

sample_data_in_base = False
collapse_whitespace = True
base_file_name = "base.html"

def ignore_ws(c):
    return c in " \t\n\r"

filename1 = tkFileDialog.askopenfilename(title= "First file?")
f1 = file(filename1,'r').readlines()
filename2 = tkFileDialog.askopenfilename(title= "Second file?")
f2 = file(filename2,'r').readlines()

d = difflib.ndiff(f1,f2, charjunk=ignore_ws)

base = ""
r1 = r2 = '{% extends "'+base_file_name+'" %}'

block_open = False
block = 0

for line in d:
    # equal line, goes into base, not r1 or r2
    if line[:2] == "  ":
        #block tag open? close it
        if block_open:
            tag = "{% endblock " + str(block) + " %}\n"
            base += tag
            r1 += tag
            r2 += tag
            block += 1
            block_open = False
        base += line[2:]
        continue
    # else: different line
    # block tag closed? open it
    if not block_open:
        tag = "{% block " + str(block) + " %}\n"
        base += tag
        r1 += tag
        r2 += tag
        block_open = True
    # line from f1? put into r1 (and maybe base)
    if line[:2] == "- ":
        r1 += line[2:]
        if sample_data_in_base:
            base += line[2:]
    # line from f2? put into r2
    if line[:2] == "+ ":
        r2 += line[2:]

for name,text in [(base_file_name,base),(filename1,r1),(filename2,r2)]:
    root,ext = os.path.splitext(name)
    if text!=base:
        name = root+"_refactored"+ext
    f=file(name,'w')
    f.writelines(text)
    f.close()