cewebs-didaktiki 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,423 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ require 'json'
3
+ require 'open3'
4
+ require 'securerandom'
5
+
6
+ module Wiki
7
+ class Parser
8
+ # pages of the wiki
9
+ class XHTML < ReDel::HTML
10
+ attr_reader :discussion_pos
11
+
12
+ # c is added to links, in order to stay in a compartment
13
+ def initialize(storage,rev,strings,persons,c='',discussion=false) #{{{
14
+ super()
15
+ @storage = storage
16
+ @rev = rev
17
+ @strings = strings
18
+ @persons = persons
19
+ @c = c
20
+ @discussion = discussion
21
+ @discussion_pos = []
22
+ @links = []
23
+ @references = []
24
+ @cites = []
25
+ @figures = {}
26
+ @formulas = {}
27
+ @graphs = {}
28
+ @tables = {}
29
+ @figure = 0
30
+ @sections = []
31
+ @section = -1
32
+ @toc = false
33
+ @columns = 0
34
+ @stream = -1
35
+ end #}}}
36
+
37
+ ### everything that can be referenced before it occurs
38
+
39
+ def finish_prepare #{{{
40
+ @cites = @storage.update_references(@references,@cites)
41
+ @figure = 0
42
+ end #}}}
43
+ def finish_output; @storage.add_links(@links.compact); end
44
+
45
+ (1..3).each do |level|
46
+ define_method("mPP_SECTION#{level}") { |data| @sections << [level,data[1]] unless data.nil? }
47
+ end
48
+ def mPP_TOC(data) @toc = true; end
49
+ def mPP_COLUMN(data) @columns += 1; end
50
+ def mPP_DIGRAPH(data) @graphs[data[1]] = @graphs.length + 1; end
51
+ def mPP_FORMULA2(data) @formulas[data[1]] = @formulas.length + 1 unless data[1].nil?; end
52
+ def mPP_TABLE(data) @tables[data[2][0]] = @tables.length + 1 unless data[2].nil?; end
53
+ def mPP_REF(data) @references << data[0]; end
54
+ def mPP_CITE(data) @cites << data[0]; end
55
+ def mPP_IMAGE(data) #{{{
56
+ return if data[1][1] == ''
57
+ @figure += 1
58
+ oid = @storage.get_object_id(data[1][0],:images,@rev)
59
+ @figures[oid] = @figure unless oid.nil?
60
+ end #}}}
61
+
62
+ # text of the page with a new comment, which always is a paragraph of its own
63
+ def add_comment(text,id,comment) #{{{
64
+ position = @discussion_pos[id] or return
65
+ text = text.b
66
+ before = text[0,position]
67
+ rest = text[before.length..]
68
+ rest = rest.sub(/\A[\t ]*(\n+|\z)/,'') unless before == '' || before.end_with?("\n")
69
+ gap = if before.match?(/(\A|\n[\t ]*\n)\z/) then ''
70
+ else before.end_with?("\n") ? "\n" : "\n\n"
71
+ end
72
+ (before + gap + comment.b + "\n\n" + rest).force_encoding('UTF-8')
73
+ end #}}}
74
+
75
+ private
76
+
77
+ def escape(name); Wiki::Encoding::escape(name); end
78
+ def string(name); @strings.sfind(name); end
79
+
80
+ def after(node,cls=nil,offset=0) #{{{
81
+ return unless @discussion
82
+ @discussion_pos << position(node) + offset
83
+ id = @discussion_pos.length - 1
84
+ div_ :id => "discuss_#{id}", :class => cls do
85
+ a_ :href => "javascript:discuss(#{id})" do
86
+ icon 'internet-group-chat', string('comment')
87
+ end
88
+ nil
89
+ end
90
+ nil
91
+ end #}}}
92
+ def icon(name,text) #{{{
93
+ img_ :class => 'align-top', :src => "CEWebS://small/icon/#{name}", :width => 16, :height => 16, :alt => text, :title => text
94
+ end #}}}
95
+ def missing(text) #{{{
96
+ img_ :src => 'CEWebS://large/icon/image-missing', :width => 22, :height => 22, :alt => text
97
+ end #}}}
98
+ # [1], with a link to whatever has this number
99
+ def number(color,link,text) #{{{
100
+ span_ :class => "background-base#{color}" do
101
+ text_ '['
102
+ text ? a_(:href => link) { text.to_s } : text_('?')
103
+ text_ ']'
104
+ end
105
+ end #}}}
106
+ def resource(type,name) #{{{
107
+ "CEWebS://self/file/aresource#{@c}&amp;CEWebS_type=#{type}&amp;CEWebS_file=#{escape(name)}&amp;CEWebS_what=#{@storage.subject}&amp;CEWebS_rev=#{@rev}"
108
+ end #}}}
109
+
110
+ ### structure
111
+
112
+ def heading(level,text) #{{{
113
+ element_! "h#{level}", :id => escape(text) do
114
+ a_ :id => "section_#{@section += 1}"
115
+ text_ text
116
+ if @toc
117
+ text_ ' '
118
+ a_ :class => 'noprint', :href => '#toc' do
119
+ img_ :src => 'CEWebS://small/icon/go-up', :width => 16, :height => 16, :alt => '(up)'
120
+ end
121
+ end
122
+ nil
123
+ end
124
+ end #}}}
125
+
126
+ def P_TOC(data) #{{{
127
+ div_ :class => 'border important' do
128
+ a_ :id => 'toc'
129
+ @sections.each_with_index do |(level,text),i|
130
+ div_ do
131
+ indent(level-1) do
132
+ strong_ { '&#8226;' } if level > 1
133
+ space_ if level > 1
134
+ element_!(level > 1 ? 'span' : 'strong') { a_(:href => "#section_#{i}") { text }; nil }
135
+ nil
136
+ end
137
+ nil
138
+ end
139
+ end
140
+ nil
141
+ end
142
+ end #}}}
143
+ def indent(level,&blk) #{{{
144
+ return blk.call if level == 0
145
+ div_(:class => 'margin-left-huge') { indent(level-1,&blk); nil }
146
+ end #}}}
147
+
148
+ def P_COLUMNS(data) #{{{
149
+ div_ :class => ["n#{@columns}",data[0] ? 'border' : nil,data[1] ? 'columns-centered' : 'columns'].compact.join(' ') do
150
+ div_ { yield }
151
+ div_ :class => 'float-off'
152
+ nil
153
+ end
154
+ end #}}}
155
+ def P_COLUMN(data) div_(:class => 'column') { yield }; end
156
+
157
+ def P_CODE(data,node) #{{{
158
+ code(data[:data].gsub(/\[file:([^\]]+)\]/) {
159
+ ReDel::XHTML::web(@storage.get_object($1,:files,@rev).data.dup.force_encoding('UTF-8').scrub) rescue ''
160
+ })
161
+ after node
162
+ end #}}}
163
+
164
+ def P_BLOCKQUOTE(data,node) #{{{
165
+ div_ :class => 'margin-left-huge margin-right-huge' do
166
+ div_(:class => 'margin-left-huge margin-right-huge') { yield }
167
+ nil
168
+ end
169
+ after node
170
+ end #}}}
171
+
172
+ def P_TABLE(data,node) #{{{
173
+ label, caption = data[2]
174
+ table = lambda do
175
+ cls = [label ? nil : 'margin-bottom-huge',data[0] ? 'border' : nil].compact
176
+ table_(:class => cls.empty? ? nil : cls.join(' ')) { yield }
177
+ nil
178
+ end
179
+ if label
180
+ div_ :class => 'center margin-top-big margin-bottom-big' do
181
+ a_ :id => "table_#{@tables[label]}"
182
+ table.call
183
+ description 'D', "#{string('table')} #{@tables[label]}", caption
184
+ end
185
+ elsif data[1]
186
+ div_(:class => 'center') { table.call }
187
+ else
188
+ table.call
189
+ end
190
+ after node
191
+ end #}}}
192
+ def P_TR(data) tr_ { yield }; end
193
+ def P_TD(data) #{{{
194
+ element_!(data[2] ? 'th' : 'td',data[0].merge(:class => "margin-right-big margin-left-big #{data[1]}".strip)) { yield }
195
+ end #}}}
196
+ def P_TAB(data) number('D',"#table_#{@tables[data]}",@tables[data]); end
197
+
198
+ def description(color,label,text) #{{{
199
+ div_ :class => 'margin-top-big' do
200
+ em_ do
201
+ span_(:class => "background-base#{color}") { "#{label}:" }
202
+ text_ " #{text}"
203
+ end
204
+ nil
205
+ end
206
+ nil
207
+ end #}}}
208
+
209
+ ### links
210
+
211
+ def P_LINK(data) #{{{
212
+ @links << data[0]
213
+ anchor = data[1] == '' ? '' : "##{escape(data[1])}"
214
+ if @storage.exist?(data[0])
215
+ a_(:href => "CEWebS://self/document/show#{@c}&amp;CEWebS_what=#{escape(data[0])}#{anchor}") { yield }
216
+ else
217
+ a_(:class => 'urgent', :href => "CEWebS://self/document/edit#{@c}&amp;CEWebS_what=#{escape(data[0])}&amp;CEWebS_back=#{@storage.subject}#{anchor}") { yield }
218
+ end
219
+ end #}}}
220
+ def P_LINK_MAIL(data,&blk) external("mailto:#{data}",&blk); end
221
+ def P_LINK_WEB(data,&blk) external(data,&blk); end
222
+ def external(link) #{{{
223
+ text_ '&#171;'
224
+ a_(:href => link) { yield }
225
+ text_ '&#187;'
226
+ end #}}}
227
+ def P_FILE(data) #{{{
228
+ a_(:href => "CEWebS://self/file/afile#{@c}&amp;CEWebS_what=#{@storage.subject}&amp;CEWebS_rev=#{@rev}&amp;CEWebS_file=#{escape(data)}") { yield }
229
+ end #}}}
230
+
231
+ def P_REF(data,node) #{{{
232
+ dl_ :class => 'margin-top-zero margin-bottom-zero' do
233
+ dt_ do
234
+ span_ :class => 'background-baseB' do
235
+ a_ :id => "reference_#{escape(data[0])}"
236
+ text_ data[0]
237
+ end
238
+ nil
239
+ end
240
+ dd_ { yield }
241
+ nil
242
+ end
243
+ after node
244
+ end #}}}
245
+ def P_CITE(data) #{{{
246
+ ref = escape(data[0])
247
+ span_ :class => 'background-baseB' do
248
+ text_ '['
249
+ if @cites.key?(ref)
250
+ a_(:href => "CEWebS://self/document/show#{@c}&amp;CEWebS_what=#{@cites[ref]}#reference_#{ref}") { data[0] }
251
+ text_ ", #{data[1]}" unless data[1] == ''
252
+ else
253
+ text_ '?'
254
+ end
255
+ text_ ']'
256
+ end
257
+ end #}}}
258
+
259
+ ### compartments
260
+
261
+ def compartment(id,text,details=nil) #{{{
262
+ li_ do
263
+ a_(:href => "CEWebS://self/document/show&amp;CEWebS_c=#{id}") { text }
264
+ text_ " (#{details})" if details
265
+ end
266
+ nil
267
+ end #}}}
268
+ def compartments(list) #{{{
269
+ return if list.empty?
270
+ ul_ :class => 'margin-top-zero margin-bottom-zero' do
271
+ list.each { |e| compartment(*e) }
272
+ nil
273
+ end
274
+ end #}}}
275
+ # persons that aren't admins, first of every value
276
+ def members(*attributes) #{{{
277
+ @persons.find("/persons/person[@admin='false']").map { |e| attributes.map { |a| e.attributes[a].to_s } }.uniq.reject { |e| e.include?('') }
278
+ end #}}}
279
+
280
+ def P_COMPARTMENT(data) a_(:href => "CEWebS://self/document/show&amp;CEWebS_c=#{escape(data)}") { yield }; end
281
+ def P_USER_COMPARTMENTS(data) #{{{
282
+ users = @persons.find("/persons/person[@admin='false']").select { |u| data.nil? || u.attributes['group'] == data }
283
+ compartments(users.map { |u| [escape(u.attributes['id']),ReDel::XHTML::web(u.attributes['name'])] })
284
+ end #}}}
285
+ def P_TEAM_COMPARTMENTS(data) #{{{
286
+ teams = members('group','team').select { |g,_| data.nil? || g == data }.sort_by { |g,t| [g.to_i,t.to_i] }
287
+ compartments(teams.map { |g,t|
288
+ names = @persons.find('/persons/person').select { |p| p.attributes['group'] == g && p.attributes['team'] == t }
289
+ [
290
+ "g#{escape(g)}t#{escape(t)}",
291
+ data.nil? ? string('groupteam') % [g,t] : string('team') % t,
292
+ names.map { |p| ReDel::XHTML::web(p.attributes['name']) }.join(', ')
293
+ ]
294
+ })
295
+ end #}}}
296
+ def P_GROUP_COMPARTMENTS(data) #{{{
297
+ compartments(members('group').flatten.sort_by(&:to_i).map { |g| ["g#{escape(g)}",string('group') % g] })
298
+ end #}}}
299
+
300
+ ### content of other services
301
+
302
+ def P_INCLUDE(data) include_('span',data); end
303
+ def P_PINCLUDE(data) include_('div',data); end
304
+ def include_(element,data) #{{{
305
+ id = "include_#{SecureRandom.hex(8)}"
306
+ element_! element, :id => id
307
+ javascript_ <<~JS
308
+ fetch("CEWebS://#{data[:service]}/chunk/#{data[:command]}".replace(/&amp;/gi,'&'), {
309
+ method: 'post',
310
+ body: new URLSearchParams(#{data[:parameters].gsub('&amp;','&').to_json})
311
+ }).then(function(r) { return r.text(); }).then(function(text) { document.getElementById('#{id}').innerHTML = text; });
312
+ JS
313
+ end #}}}
314
+
315
+ ### media
316
+
317
+ def P_STREAM(data) #{{{
318
+ video_ :id => "stream_#{@stream += 1}", :height => 406, :width => 720, :controls => 'controls', :src => data
319
+ end #}}}
320
+
321
+ def P_IMAGE(data,node) #{{{
322
+ name, caption = data[1]
323
+ position = {
324
+ 'left' => nil,
325
+ 'float-right' => 'float-right margin-left-huge center',
326
+ 'float-left' => 'float-left margin-right-huge center'
327
+ }.fetch(data[0],'center')
328
+ @figure += 1
329
+ div_ :class => "#{position} margin-bottom-big margin-top-big".strip do
330
+ a_ :id => "image_#{@figure}" unless caption == ''
331
+ if image = @storage.get_object(name,:images,@rev)
332
+ img_ :class => data[2] ? 'border' : nil, :src => resource('image',name), :width => image.meta['width'], :height => image.meta['height'], :alt => caption
333
+ else
334
+ missing caption
335
+ end
336
+ description 'C', "#{string('figure')} #{@figure}", caption unless caption == ''
337
+ end
338
+ after node
339
+ end #}}}
340
+ def P_FIGURE(data) #{{{
341
+ figure = @figures[@storage.get_object_id(data,:images,@rev)]
342
+ number 'C', "#image_#{figure}", figure
343
+ end #}}}
344
+
345
+ def P_APPLET(data,node) #{{{
346
+ name, caption = data[1]
347
+ div_ :class => 'center margin-bottom-big margin-top-big' do
348
+ if applet = @storage.get_object(name,:applets,@rev)
349
+ jar = applet.meta['type'] == 'jar'
350
+ size = { :width => applet.meta['width'], :height => applet.meta['height'] }
351
+ object_ size.merge(jar ? { :classid => "java:#{applet.meta['cname']}" } : { :type => 'application/x-shockwave-flash', :data => resource('applet',name) }) do
352
+ param_ :name => jar ? 'archive' : 'movie', :value => resource('applet',name)
353
+ end
354
+ else
355
+ missing caption
356
+ end
357
+ div_(:class => 'margin-top-big') { em_ { "#{string('applet')}: #{caption}" }; nil } unless caption == ''
358
+ nil
359
+ end
360
+ after node
361
+ end #}}}
362
+
363
+ def P_DIGRAPH(data) #{{{
364
+ tool, name, graph = data
365
+ graph = graph.gsub(/\[\[([^\]]+)\]\]/) { "CEWebS://self/document/show&CEWebS_what=#{escape($1.split('|',2)[0])}" }
366
+ png, err, status = Open3.capture3(tool,'-Tpng',:stdin_data => graph,:binmode => true)
367
+ map = status.success? ? Open3.capture3(tool,'-Tcmapx',:stdin_data => graph)[0].force_encoding('UTF-8') : ''
368
+ map = '' unless map.include?('<area')
369
+ text_ map
370
+ div_ :class => 'margin-bottom-big margin-top-big' do
371
+ a_ :id => "graph_#{name}"
372
+ if status.success?
373
+ width, height = ReDel::Image::size(png)
374
+ # a new object also is a new revision for the cache of the engine
375
+ old = @storage.get_object(name,:graphs,@rev)
376
+ @storage.add_object(name,png,{ 'width' => width, 'height' => height },:graphs,@rev) if old.nil? || old.data != png
377
+ img_ :src => resource('graph',name), :usemap => map == '' ? nil : "##{name}", :width => width, :height => height, :alt => "#{string('graph')} #{@graphs[name]}"
378
+ else
379
+ missing name
380
+ div_ err.force_encoding('UTF-8').scrub
381
+ nil
382
+ end
383
+ end
384
+ end #}}}
385
+ def P_GRAPH(data) number('G',"#graph_#{data}",@graphs[data]); end
386
+
387
+ def P_FORMULA2(data,node) #{{{
388
+ tex, label = data
389
+ if label
390
+ div_ :class => 'center' do
391
+ a_ :id => "formula_#{@formulas[label]}"
392
+ formula "\\[#{tex}\\]"
393
+ div_ { em_ { "#{string('formula')} #{@formulas[label]}" }; nil }
394
+ nil
395
+ end
396
+ else
397
+ formula "\\[#{tex}\\]"
398
+ end
399
+ after node, 'margin-bottom-huge'
400
+ end #}}}
401
+ def P_FORMULA(data) #{{{
402
+ number 'C', "#formula_#{@formulas[data]}", @formulas[data] && "#{string('formula')} #{@formulas[data]}"
403
+ end #}}}
404
+
405
+ ### exports
406
+
407
+ def P_EXPORTHTML(data) export('html',"&amp;CEWebS_style=#{escape(data)}"); end
408
+ def P_EXPORTHTMLINDEX(data) export('html',"&amp;CEWebS_asindex=&amp;CEWebS_style=#{escape(data)}"); end
409
+ def P_EXPORTHTMLSINDEX(data) export('html',"&amp;CEWebS_asindex=&amp;CEWebS_sort=&amp;CEWebS_style=#{escape(data)}"); end
410
+ def P_EXPORTLATEX(data) export('latex'); end
411
+ def P_EXPORTLATEXINDEX(data) export('latex','&amp;CEWebS_asindex='); end
412
+ def export(type,parameters='') #{{{
413
+ div_ do
414
+ a_ :href => "CEWebS://self/file/#{type}#{@c}&amp;CEWebS_what=#{@storage.subject}#{parameters}" do
415
+ icon 'document-save-as', string("export#{type}")
416
+ text_ " #{string("export#{type}")}"
417
+ end
418
+ nil
419
+ end
420
+ end #}}}
421
+ end
422
+ end
423
+ end
@@ -0,0 +1,8 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ require 'redel'
3
+ require_relative 'encoding'
4
+ require_relative 'grammar_didaktiki'
5
+ require_relative 'grammar_rename'
6
+ require_relative 'output_xhtml'
7
+ require_relative 'output_latex'
8
+ require_relative 'output_rename'