Commit 01ae1d6a authored by Michael Murtaugh's avatar Michael Murtaugh

etherpad

parent 4f9b42fb
......@@ -59,8 +59,116 @@ def changeset_parse (c) :
ops.append(parse_op(m))
return ret
def perform_changeset (text, c):
textpos = 0
bank = c['bank']
bankpos = 0
newtext = ''
current_attributes = []
# loop through the operations
# rebuilding the final text
for op in c['ops']:
if op['op'] == "attr":
current_attributes.append(op['index'])
elif op['op'] == "insert":
newtextposition = len(newtext)
insertion_text = bank[bankpos:bankpos+op['chars']]
newtext += insertion_text
bankpos += op['chars']
# todo PROCESS attributes
# NB on insert, the (original/old/previous) textpos does *not* increment...
elif op['op'] == "delete":
newtextposition = len(newtext) # is this right?
# todo PROCESS attributes
textpos += op['chars']
elif op['op'] == "hold":
newtext += text[textpos:textpos+op['chars']]
textpos += op['chars']
# append rest of old text...
newtext += text[textpos:]
return newtext
def perform_changeset_curline (text, c):
textpos = 0
curline = 0
curline_charpos = 0
bank = c['bank']
bankpos = 0
newtext = ''
current_attributes = []
# loop through the operations
# rebuilding the final text
for op in c['ops']:
if op['op'] == "attr":
current_attributes.append(op['index'])
elif op['op'] == "insert":
newtextposition = len(newtext)
insertion_text = bank[bankpos:bankpos+op['chars']]
newtext += insertion_text
bankpos += op['chars']
if 'lines' in op:
curline += op['lines']
curline_charpos = 0
else:
curline_charpos += op['chars']
# todo PROCESS attributes
# NB on insert, the (original/old/previous) textpos does *not* increment...
elif op['op'] == "delete":
newtextposition = len(newtext) # is this right?
# todo PROCESS attributes
textpos += op['chars']
elif op['op'] == "hold":
newtext += text[textpos:textpos+op['chars']]
textpos += op['chars']
if 'lines' in op:
curline += op['lines']
curline_charpos = 0
else:
curline_charpos += op['chars']
# append rest of old text...
newtext += text[textpos:]
return newtext, curline, curline_charpos
if __name__ == "__main__":
import sys
import argparse
import json
print (json.dumps(changeset_parse("Z:9kj>1|8=al=o4*1a|1+1$"), indent=2))
from time import sleep
ap = argparse.ArgumentParser("")
ap.add_argument("--pad", type=argparse.FileType('r'), default=sys.stdin)
ap.add_argument("--padname", default="pad")
args = ap.parse_args()
padname = args.padname
data = json.load(args.pad)
pad_data = data["pad:{0}".format(padname)]
last_rev = pad_data['head']
text = "\n"
for r in range(0, last_rev+1):
d = data["pad:{0}:revs:{1}".format(padname, r)]
cs = changeset_parse(d['changeset'])
text, linepos, charpos = perform_changeset_curline(text, cs)
lines = text.splitlines()
if charpos == 0 and linepos > 0:
line = lines[linepos-1]
charpos = len(line)
else:
line = lines[linepos]
pre, post = line[:charpos], line[charpos:]
pre = pre[-40:]
post = post[:40]
sys.stdout.write("\r{0}|{1}{2}".format(pre, post, " "*64))
sys.stdout.flush()
sleep(0.25)
print ()
# print (text)
# print (json.dumps(changeset_parse("Z:9kj>1|8=al=o4*1a|1+1$"), indent=2))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment