This repository was archived by the owner on Oct 4, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 883
/
Copy pathfirepad.rb
63 lines (59 loc) · 1.9 KB
/
firepad.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# This module requires that a global FIREBASE object is initialized, from the gem
# `firebase` (https://rubygems.org/gems/firebase). If you don't want to do that, it's
# pretty easy to tweak this code to take the FIREBASE object as a param, or even
# to hit the REST endpoints yourself using whatever HTTP client you prefer.
#
# To load the final text value of a pad created through the official JavaScript
# Firepad client, call `Firepad.load "PADNAME"`. This will check for a snapshot,
# then mutate a string in place to produce the final output. We also account for
# JavaScript's shitty string representation (JS expresses the poop emoji as two chars).
moduleFirepad
defself.loadpad_path
ifcheckpoint=FIREBASE.get("#{pad_path}/checkpoint").body
doc=checkpoint['o'].first
doc=''unlessdoc.is_a?String
history=FIREBASE.get("#{pad_path}/history",orderBy: '"$key"',startAt: "\"#{checkpoint['id']}\"").body.sort
history.shift
else
doc=''
history=FIREBASE.get("#{pad_path}/history").body
returnnilunlesshistory
history=history.sort
end
doc.pad_surrogate_pairs!
history.eachdo |_,ops|
idx=0
ops['o'].eachdo |op|
ifop.is_a?Fixnum
ifop > 0
# retain
idx += op
else
# delete
doc.slice!idx, -op
end
else
# insert
op.pad_surrogate_pairs!
doc.insertidx,op
idx += op.length
end
end
raise"The operation didn't operate on the whole string."ifidx != doc.length
end
doc.delete!"\0".freeze
doc
end
end
classString
defpad_surrogate_pairs!
offset=0
self.each_codepoint.with_indexdo |codepoint,idx|
ifcodepoint >= 0x10000 && codepoint <= 0x10FFFF
self.insertidx + offset,"\0".freeze
offset += 1
end
end
self
end
end