Skip to content
Newer
Older
100755 245 lines (228 sloc) 5.4 KB
2cd00f2 @jdpage fixed bugs in gbdb.rb
authored
1 #!/usr/bin/env ruby
2
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
3 require 'sha1'
4 require 'cgi'
5 require 'markaby'
6 require 'sequel'
92e4566 @jdpage Continued porting
authored
7
8 module Element
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
9 def self.link(href, text = nil)
92e4566 @jdpage Continued porting
authored
10 phref = href.dup
11 unless phref.include? "/" or phref.include? "?" or phref[0].to_c == "_"
12 phref = "mailto:" + phref if phref.include? "@" unless phref =~ /^mailto:/
13 phref += "' class='redlink" unless phref =~ /^mailto:/ or Page.exists phref
14 end
15 l = "<a href='%s'>" % phref
16 if text; l << text; else; l << href; end;
17 l << "</a>"
18 end
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
19 def self.title(page, extra, linkmode)
92e4566 @jdpage Continued porting
authored
20 if linkmode == :plain
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
21 "<h1>%s #{link page, Links.niceify(page)}</h1>" % extra
92e4566 @jdpage Continued porting
authored
22 elsif linkmode == :refs
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
23 "<h1>%s #{link page+".refs", Links.niceify(page)}</h1>" % extra
92e4566 @jdpage Continued porting
authored
24 else
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
25 "<h1>%s #{Links.niceify(page)}</h1>" % extra
92e4566 @jdpage Continued porting
authored
26 end
27 end
28 end
29
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
30 class Links
31 def initialize(table)
296fb56 @jdpage Added classes for tables
authored
32 @table = table
33 end
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
34 def clear(id)
35 @table.filter(:page => id).delete
296fb56 @jdpage Added classes for tables
authored
36 end
92e4566 @jdpage Continued porting
authored
37 def add(from, to)
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
38 @table.insert(:page => from, :ref => to) unless \
39 @table.filter({:page => from, :ref => to}.sql_and).count > 0
92e4566 @jdpage Continued porting
authored
40 end
296fb56 @jdpage Added classes for tables
authored
41 def self.internal?(href)
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
42 !(href.include? "/" or href.include? "?" or href =~ /^mailto:/ or href.include? "@")
43 end
44 def self.niceify(href)
92e4566 @jdpage Continued porting
authored
45 end
296fb56 @jdpage Added classes for tables
authored
46 def to(ref)
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
47 @table.filter(:ref => ref).each do |row|
48 yield row[:page]
296fb56 @jdpage Added classes for tables
authored
49 end
50 end
51 def from(page)
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
52 @table.filter(:page => page).each do |row|
53 yield row[:ref]
92e4566 @jdpage Continued porting
authored
54 end
55 end
56 end
57
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
58 class Auth
59 def self.create(db, table)
60 tab =
61 [:userid => "INTEGER PRIMARY KEY"] +
62 [:name => "TEXT UNIQUE"] +
63 [:password => "TEXT"] +
64 [:permissions => "INTEGER"] +
65 [:token => "TEXT"]
66 Table.create db, table, tab, {}
67 end
68 def initialize(db, table)
69 @tab = Table.new db, table
70 end
71 def adduser(name, passwd, perms)
48a2000 @jdpage some stuff I recovered from a vim .swp
authored
72 @tab.add({:userid => "NULL", :name => name, :password => SHA1.hexdigest(passwd), :permissions => perms, :token => ""})
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
73 end
74 def auth(name, passwd)
75 user = @tab.search_by :name, name
76 pwhash = user[:password]
77 return :nouser unless pwhash
78 return :badpass unless pwhash == SHA1.hexdigest(passwd)
79 user[:token] = SHA1.hexdigest rand.to_s
80 end
81 def tokenauth(token)
82 if @tab.search_by :token, token; true; else; false; end
83 end
84 def perms(name)
85 user = @tab.search_by :name, name
86 nil unless user
87 user[:permissions]
88 end
92e4566 @jdpage Continued porting
authored
89 end
90
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
91 module SuperString
92 def nwm2html()
93 out = []
94 nxt = nil
95 self.split("\n").each do |line|
96 if line =~ /^ {4,}.*$/
97 out += ["#{nxt}<pre>"] unless nxt == "</pre>"
98 out += [line.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&lt;")]
99 nxt = "</pre>"
100 next
101 end
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
102 line.gsub!(/\\./) {|s| "&##{s};"}
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
103 if line =~ /^(\={2,6})(.*?)\1$/
104 line = "<h#{$1}>#{$2}</h#{$1}>"
105 nxt = ""
106 elsif line =~ /^# (.*)$/
107 out += ["#{nxt}<ol>"] unless nxt == "</ol>"
108 line = "<li>#{$1}</li>"
109 nxt = "</ol>"
110 elsif line =~ /^\* (.*)$/
111 out += ["#{nxt}<ul>"] unless nxt == "</ul>"
112 line = "<li>#{$1}</li>"
113 nxt = "</ul>"
114 elsif line =~ /^[[:space:]]*$/
115 out += [nxt]
116 nxt = ""
117 else
118 out += ["#{nxt}<p>"] unless nxt == "</p>"
119 set nxt "</p>"
120 end
121 line.gsub! /\[\[(?!\[)(.*?)(?: (.*?))?\]\]/ do |match|
122 texturl = Links.uglify($1)
123 unless $2
124 Element.link(texturl, Links.niceify($1))
125 else
126 Element.link(texturl, $2)
127 end
128 end
129
54d869e @jdpage just saving some changes I made before I rewrite the lot
authored
130 line.gsub!(/\{\{(.*?) (.*?)\}\}/, '<img src="\1" alt="\2" title="\2" />')
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
131 line.gsub!(/\b_(.*?[^\\])_\b/, '<em>\1</em>')
132 line.gsub!(/(?!\b)\*(.*?[^\\])\*(?!\b)/, '<strong>\1</strong>')
133 line.gsub!(/``(.*?)''/, '&ldquo;\1&rdquo;')
134 line.gsub!(/(?!\b)@(.*?)@(?!\b)/, '<code>\1</code>')
135 out += [line]
136 end
137 out += [nxt]
138 out.join ""
139 end
140 def textile2html()
141 end
142 def markdown2html()
143 end
144 def diff(b)
145 a = self.split
146 b = b.split "\n" if b.respond_to? :lines
147
148 asubs, bsubs = lcs a, b
92e4566 @jdpage Continued porting
authored
149
7577d5a @jdpage Added diff, patch, and wikimarkup
authored
150 # If a line number is not in asubs, it's a deletion from a
151 dlines = []
152 a.length.times {|k| dlines += [k] unless asubs.member? k}
153
154 # If a line number is not in bsubs, it's an addition to a
155 alines = []
156 b.length.times {|k| alines += [[k, b[k]]] unless bsubs.member? k}
157
158 "d#{dlines.join ","}\n#{alines.collect {|i| "#{i[0]}\t#{i[1]}"}.join "\n"}"
159 end
160 def lcs(a, b)
161 eqv = {}
162 b.each_with_index do |str, idx|
163 eqv[str] = [] unless eqv.member? str
164 eqv[str] += [idx]
165 end
166
167 dlcs = [[-1, -1, []], [a.length, b.length, []]]
168 k = 0
169 i = 0
170 a.each do |str|
171 if eqv.member? str
172 c = dlcs[0]; r = 0
173 eqv[str].each do |j|
174 max = k; min = r; s = k + 1
175 while max >= min
176 mid = (max + min) / 2
177 bmid = dlcs[mid][1]
178 if j == bmid
179 break
180 elsif j < bmid
181 max = mid - 1
182 else
183 s = mid
184 min = mid + 1
185 end
186 end
187 next if j == dlcs[mid][1] || s > k
188 newc = [i, j, dlcs[s]]
189 dlcs[r] = c
190 c = newc
191 r = s + 1
192 if s >= k
193 dlcs += dlcs[-1]
194 k += 1
195 break
196 end
197 end
198 dlcs[r] = c
199 end
200 i += 1
201 end
202 q = dlcs[k]
203 seta = []; setb = []
204 while q[0] >= 0
205 k -= 1
206 seta[k] = q[0]
207 setb[k] = q[1]
208 q = q[2]
209 end
210
211 [seta, setb]
212 end
213 def patch(patch)
214 dlines = []
215 alines = []
216 a = self.split "\n"
217 patch.split("\n").each do |line|
218 if line[0].chr == "d"
219 dlines += line[1..-1].split(",").collect {|i| i.to_i}
220 else
221 lnum, text = line.split "\t", 2
222 alines += [[lnum.to_i, text]]
223 end
224 end
225 dlines.each {|d| a.delete_at d}
226 alines.each {|aline| a.insert(aline[0], aline[1])}
227 a.join("\n")
228 end
229 def patch!(patch)
230 self[0..-1] = patch(patch)
231 end
232 def scriptpage(ruby)
233 end
234
235 private :lcs
236 end
237
238 class String
239 include SuperString
240 end
48a2000 @jdpage some stuff I recovered from a vim .swp
authored
241
242 def handle_cgi
243
244 end
Something went wrong with that request. Please try again.