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.
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # CEWebS Didaktiki
2
+
3
+ A wiki: pages that can be edited by everybody, with discussions and attached files.
4
+
5
+ ```sh
6
+ cewebs-didaktiki new didaktiki # scaffold a server
7
+ didaktiki/didaktiki start # port 9023
8
+ cewebs-aman service aman didaktiki # the AMan in the directory aman offers the wiki
9
+ ```
10
+
11
+ See the README of `cewebs-transformationengine` for how all parts of CEWebS work together.
12
+
13
+ ## License
14
+
15
+ GPL-3.0-or-later, see LICENSE.GPL.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rubygems/package_task'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.test_files = FileList['./test/tc_*.rb']
7
+ t.verbose = false
8
+ end
9
+
10
+ spec = eval(File.read('cewebs-didaktiki.gemspec'))
11
+ Gem::PackageTask.new(spec) do |pkg|
12
+ FileUtils.mkdir 'pkg' rescue nil
13
+ FileUtils.rm_rf Dir.glob('pkg/*')
14
+ FileUtils.ln_sf "#{pkg.name}.gem", "pkg/#{spec.name}.gem"
15
+ end
16
+
17
+ task :default => :gem
18
+
19
+ task :push => :gem do |r|
20
+ sh "gem push pkg/#{spec.name}.gem"
21
+ end
22
+
23
+ task :install => :gem do |r|
24
+ sh "gem install pkg/#{spec.name}.gem"
25
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cewebs-didaktiki"
3
+ s.version = "3.0.1"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.license = "GPL-3.0-or-later"
6
+ s.summary = "Wiki for CEWebS"
7
+
8
+ s.description = "Pages that can be edited by everybody, with discussions and attached files."
9
+
10
+ s.files = Dir['{server/**/*,tools/**/*,lib/**/*}'] + %w(LICENSE.GPL Rakefile cewebs-didaktiki.gemspec README.md AUTHORS)
11
+ s.require_path = 'lib'
12
+ s.extra_rdoc_files = ['README.md']
13
+ s.bindir = 'tools'
14
+ s.executables = ['cewebs-didaktiki']
15
+ s.test_files = Dir['test/tc_*.rb']
16
+
17
+ s.required_ruby_version = '>=3.2'
18
+
19
+ s.authors = ['Juergen eTM Mangler']
20
+ s.email = 'juergen.mangler@gmail.com'
21
+ s.homepage = 'http://www.cewebs.org'
22
+
23
+ s.add_runtime_dependency 'cewebs-servicesbase', '~> 3.0'
24
+ s.add_runtime_dependency 'diff-lcs', '~> 1.6'
25
+ s.add_runtime_dependency 'mimemagic', '~> 0.4'
26
+ end
@@ -0,0 +1,48 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ module Didaktiki
3
+ # what new elements in the configuration look like
4
+ class Elements < MarkUS
5
+ template 'persons/person' do
6
+ person_ :id => CEWebS::key[0..7], :admin => 'false', :name => '', :email => '', :pass => '', :group => '', :team => ''
7
+ end
8
+ template 'custom/exceptions/exception' do
9
+ exception_
10
+ end
11
+ template 'custom/edit/person' do
12
+ person_ :id => ''
13
+ end
14
+ template 'custom/compartments/compartment' do
15
+ compartment_ :name => '', :type => 'private'
16
+ end
17
+ template 'custom/compartments/compartment/person' do
18
+ person_ :id => ''
19
+ end
20
+ NAMES = ['persons/person','custom/exceptions/exception','custom/edit/person','custom/compartments/compartment','custom/compartments/compartment/person']
21
+ end
22
+
23
+ class Administration < CEWebS::Administration
24
+ def dynamic(instance,part,path) #{{{
25
+ if path == 'strings/strings'
26
+ ele = XML::Smart.open_unprotected(CEWebS::data('Default','strings.xml')).find('/strings/strings').first
27
+ ele.attributes['xml:lang'] = "xx-#{('a'..'z').to_a.sample(2).join}"
28
+ ele.dump
29
+ elsif Elements::NAMES.include?(path)
30
+ Elements.new.xml_!(path)
31
+ end
32
+ end #}}}
33
+
34
+ def entry_points(instance,language) #{{{
35
+ strings = XML::Smart.open(CEWebS::data(instance,'strings.xml')).lfind('/strings/strings',language)
36
+ point = lambda do |parameter,name,string|
37
+ name = Wiki::Encoding::escape(name)
38
+ { :command => 'show', :parameters => "CEWebS_#{parameter}=#{name}", :description => strings.sfind(string) % ReDel::XHTML::unweb(Wiki::Encoding::unescape(name)) }
39
+ end
40
+ custom = XML::Smart.open(CEWebS::data(instance,'custom.xml'))
41
+ ret = custom.find('/custom/exceptions/exception').map { |e| point.call('what',e.to_s,'entrypoint_root') }
42
+ ret += custom.find('/custom/compartments/compartment/@name').map { |e| point.call('c',e.to_s,'entrypoint_compartment') }
43
+ linking = CEWebS::data(instance,'linking.xml')
44
+ ret += XML::Smart.open(linking).find('/main/page/@name').map { |e| point.call('what',e.to_s,'entrypoint_page') } if File.exist?(linking)
45
+ ret
46
+ end #}}}
47
+ end
48
+ end
@@ -0,0 +1,253 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ require 'digest/md5'
3
+ require 'timeout'
4
+ require 'mimemagic'
5
+
6
+ module Didaktiki
7
+ # what a request is about. All names are markup (A &amp; B), see Wiki::Encoding.
8
+ class Context
9
+ attr_reader :r, :custom, :persons, :access, :compartment
10
+
11
+ def initialize(r) #{{{
12
+ @r = r
13
+ @custom = XML::Smart.open(file('custom.xml'))
14
+ @persons = XML::Smart.open(file('persons.xml'))
15
+ @compartment = name('c')
16
+ @access = Access.new(@custom,@persons,r.user,ReDel::XHTML::unweb(@compartment))
17
+ end #}}}
18
+
19
+ def file(name); CEWebS::data(@r.instance,name); end
20
+
21
+ # names are part of links, so they are encoded
22
+ def name(parameter); Wiki::Encoding::unescape(@r.raw(parameter).to_s.delete("\r\n")); end
23
+
24
+ def what; @what ||= name('what') == '' ? 'Main' : name('what'); end
25
+ def path; @compartment == '' ? CEWebS::data(@r.instance) : CEWebS::data(@r.instance,Wiki::Encoding::escape(@compartment)); end
26
+ def storage(name=what); name == what ? (@storage ||= Wiki::Storage::Simple.new(path,name)) : Wiki::Storage::Simple.new(path,name); end
27
+ def strings; @strings ||= XML::Smart.open(file('strings.xml')).lfind('/strings/strings',@r.language); end
28
+
29
+ # parameter for links
30
+ def cl; @compartment == '' ? '' : "&amp;CEWebS_c=#{Wiki::Encoding::escape(@compartment)}"; end
31
+ def link(command,what=self.what,mode='document')
32
+ "CEWebS://self/#{mode}/#{command}#{cl}" + (what ? "&amp;CEWebS_what=#{Wiki::Encoding::escape(what)}" : '')
33
+ end
34
+
35
+ def files(*more); more + %w{persons.xml strings.xml custom.xml}.map { |f| file(f) }; end
36
+
37
+ def xhtml(text,storage=self.storage,rev=storage.current,discussion=false) #{{{
38
+ output = Wiki::Parser::XHTML.new(storage,rev,strings,@persons,cl,discussion)
39
+ [Timeout.timeout(10) { Wiki::Parser::Didaktiki.parse(text).prepare(output).output(output) },output]
40
+ end #}}}
41
+ end
42
+
43
+ class Delivery < CEWebS::Delivery
44
+ TYPES = { 'image' => :images, 'applet' => :applets, 'graph' => :graphs }
45
+
46
+ def changes; %w{save ascurrent delete discuss image file}; end
47
+
48
+ def prepare(r) #{{{
49
+ c = Context.new(r)
50
+ key = lambda { |*parts| Digest::MD5.hexdigest([r.command,*parts,c.compartment,r.language.join(',')].join('_')) }
51
+ return CEWebS::Prepare.new(:cachekey => '', :authenticate => true) unless c.access.compartment?
52
+ lock = !!(c.storage.get_meta('plock'))
53
+ case r.command
54
+ when 'show'
55
+ CEWebS::Prepare.new(:cachekey => key.call(c.what,r.user), :authenticate => lock)
56
+ when 'backlinks'
57
+ CEWebS::Prepare.new(:cachekey => key.call(c.what), :authenticate => lock)
58
+ when 'versions'
59
+ CEWebS::Prepare.new(:cachekey => key.call(c.what,r.user), :authenticate => true)
60
+ when 'pages'
61
+ CEWebS::Prepare.new(:cachekey => key.call(r.user), :authenticate => true)
62
+ when 'latex', 'html', 'edit', 'save', 'versionshow', 'diff', 'ascurrent', 'image', 'file', 'delete', 'discuss', 'exportwiki'
63
+ CEWebS::Prepare.new(:cachekey => '', :authenticate => true)
64
+ when 'aresource'
65
+ CEWebS::Prepare.new(:cachekey => c.storage.get_object_id(c.name('file'),type(r),rev(c)).to_s, :authenticate => lock)
66
+ when 'afile'
67
+ CEWebS::Prepare.new(
68
+ :cachekey => c.storage.get_object_id(c.name('file'),:files,rev(c)).to_s,
69
+ :authenticate => lock || !!(c.storage.get_object_meta(c.name('file'),'lock',:files,rev(c)))
70
+ )
71
+ else unknown(r)
72
+ end
73
+ end #}}}
74
+
75
+ def document(r) #{{{
76
+ c = Context.new(r)
77
+ return message(c,'accessdenied') unless readable?(c)
78
+ view = View.new(c)
79
+ case r.command
80
+ when 'show'; show(c)
81
+ when 'versionshow'; versionshow(c)
82
+ when 'discuss'; discuss(c)
83
+ when 'backlinks'
84
+ modified = r.newer(*c.files(c.storage.cpath + 'revisions.xml',File.join(c.path,'linking.xml'))) or return nil
85
+ CEWebS::Document.new(:content => view.render(:backlinks), :modified => modified)
86
+ when 'pages'
87
+ modified = r.newer(*c.files(File.join(c.path,'linking.xml'))) or return nil
88
+ CEWebS::Document.new(:content => view.render(:pages), :modified => modified)
89
+ when 'versions'
90
+ return message(c,'accessdenied') unless c.access.edit?
91
+ modified = r.newer(*c.files(c.storage.cpath + 'revisions.xml')) or return nil
92
+ CEWebS::Document.new(:content => view.render(:versions), :modified => modified, :access => [back(c)])
93
+ when 'diff'
94
+ return message(c,'accessdenied') unless c.access.edit?
95
+ from, to = r['from'].to_i, r['to'].to_i
96
+ return message(c,'diffsame') if from == to || c.storage.get_page(from).nil? || c.storage.get_page(to).nil?
97
+ CEWebS::Document.new(
98
+ :content => view.render([:diff,c.storage.get_diff(from,to)]),
99
+ :access => [back(c)]
100
+ )
101
+ when 'delete'
102
+ return message(c,'deleteforbidden') unless c.access.admin?
103
+ c.storage.del_page
104
+ redirect(c.link('pages',nil))
105
+ when 'edit', 'save', 'ascurrent', 'image', 'file'
106
+ c.access.edit? ? work(c) : message(c,'forbidden')
107
+ else unknown(r)
108
+ end
109
+ end #}}}
110
+
111
+ def file(r) #{{{
112
+ c = Context.new(r)
113
+ raise CEWebS::Error.new(c.strings.sfind('accessdenied'),403) unless readable?(c)
114
+ case r.command
115
+ when 'aresource'; object(c,type(r),'image/png')
116
+ when 'afile'; object(c,:files)
117
+ when 'html'
118
+ CEWebS::File_.new(:content => Export::html(c), :name => 'export.html', :mime => 'text/html', :transform => true)
119
+ when 'latex', 'exportwiki'
120
+ raise CEWebS::Error.new(c.strings.sfind('accessdenied'),403) unless c.access.edit?
121
+ CEWebS::File_.new(:content => Export::send(r.command,c), :name => r.command == 'latex' ? 'export.tgz' : 'Wiki.tgz', :mime => 'application/x-gtar')
122
+ else unknown(r)
123
+ end
124
+ end #}}}
125
+
126
+ private
127
+
128
+ def type(r); TYPES[r.raw('type')] || :images; end
129
+ def rev(c); c.r.raw('rev').to_s == '' ? c.storage.current : c.r.raw('rev').to_i; end
130
+ def back(c); { :href => c.link('show'), :text => c.strings.sfind('back') }; end
131
+ def redirect(url); CEWebS::Document.new(:headers => [{ :name => 'Location', :value => url.gsub('&amp;','&') }]); end
132
+ def message(c,name); CEWebS::Document.new(:content => View.new(c).render([:message,name])); end
133
+
134
+ def readable?(c) #{{{
135
+ c.access.compartment? && (%w{pages backlinks}.include?(c.r.command) || !c.storage.get_meta('alock') || c.access.edit?)
136
+ end #}}}
137
+
138
+ def object(c,type,mime=nil) #{{{
139
+ name = c.name('file')
140
+ obj = c.storage.get_object(name,type,rev(c))
141
+ raise CEWebS::Error.new('not found',404) if obj.nil? || obj.data.nil?
142
+ return nil unless obj.modified > c.r.modified
143
+ mime ||= (MimeMagic.by_path(ReDel::XHTML::unweb(name)) || MimeMagic.by_magic(obj.data) || 'application/octet-stream').to_s
144
+ CEWebS::File_.new(:content => obj.data, :name => ReDel::XHTML::unweb(name), :mime => mime, :modified => obj.modified)
145
+ end #}}}
146
+
147
+ def show(c) #{{{
148
+ s = c.storage
149
+ modified = c.r.newer(*c.files(s.cpath + 'revisions.xml'))
150
+ modified ||= Time.now if s.new?
151
+ return nil if modified.nil?
152
+ if s.new?
153
+ text = "== #{c.what}\n#{c.strings.sfind('newpage')}"
154
+ meta = {}
155
+ else
156
+ page = s.get_page
157
+ text = page.data
158
+ meta = page.meta
159
+ end
160
+ content = c.xhtml(text,s,s.current,meta['discussion'])[0]
161
+ access = [{ :href => c.link('pages',nil), :text => c.strings.sfind('pages') }]
162
+ access << { :href => c.link('versions'), :text => c.strings.sfind('versions') } unless s.new?
163
+ access << { :href => c.link('edit'), :text => c.strings.sfind('edit') }
164
+ CEWebS::Document.new(
165
+ :content => View.new(c).render([:show,content,text,meta['conflict']]),
166
+ :modified => modified,
167
+ :access => access,
168
+ :search => {
169
+ :href => c.link('show').gsub('&amp;','&'),
170
+ :description => [c.compartment,c.what].reject { |e| e == '' }.join(': '),
171
+ :document => content
172
+ }
173
+ )
174
+ end #}}}
175
+
176
+ def versionshow(c) #{{{
177
+ page = c.storage.get_page(rev(c)) or return message(c,'accessdenied')
178
+ CEWebS::Document.new(
179
+ :content => c.xhtml(page.data,c.storage,rev(c))[0],
180
+ :access => [
181
+ { :href => c.link('ascurrent') + "&amp;CEWebS_rev=#{rev(c)}", :text => c.strings.sfind('ascurrent'), :post => 'true' },
182
+ { :href => c.link('show'), :text => c.strings.sfind('current') }
183
+ ]
184
+ )
185
+ end #}}}
186
+
187
+ def discuss(c) #{{{
188
+ page = c.storage.get_page
189
+ comment = c.r['comment'].to_s.strip
190
+ id = c.r.raw('id').to_s
191
+ if page && page.meta['discussion'] && comment != '' && id =~ /\A\d+\z/
192
+ # positions are bytes, they are only known after the page has been created
193
+ output = c.xhtml(page.data,c.storage,c.storage.current,true)[1]
194
+ user = c.access.name == '' ? c.r.user : c.access.name
195
+ entry = "'''#{user}, #{Time.now.strftime(c.strings.sfind('discussiondate'))}:''' #{comment}"
196
+ text = output.add_comment(page.data,id.to_i,entry)
197
+ c.storage.add_page(text.sub(/\s*\z/,''),'','Comment added',page.meta,user) if text
198
+ end
199
+ redirect(c.link('show'))
200
+ end #}}}
201
+
202
+ def work(c) #{{{
203
+ r = c.r
204
+ s = c.storage
205
+ case r.command
206
+ when 'save'
207
+ meta = %w{alock plock notexport discussion}.to_h { |m| [m,r.raw(m) == 'true'] }
208
+ name = r['name'].to_s.strip
209
+ return save(c,r['text'].to_s,r['eid'].to_s,'New Revision',meta,r.raw('rev').to_i,name == '' ? c.what : name)
210
+ when 'ascurrent'
211
+ page = s.get_page(rev(c)) or return message(c,'accessdenied')
212
+ return save(c,page.data,'',"Promoted from #{rev(c)}",page.meta,s.current,c.what)
213
+ when 'image'
214
+ [:images_orig,:images].each { |t| s.del_object(c.name('del'),t) } if r.raw('del').to_s != ''
215
+ add_image(c)
216
+ when 'file'
217
+ s.add_object_meta(c.name('meta'),{ 'lock' => r.raw('meta_lock') != 'false' },:files) if r.raw('meta').to_s != ''
218
+ s.del_object(c.name('del'),:files) if r.raw('del').to_s != ''
219
+ file = upload(c)
220
+ s.add_object(file.value,file.data,{ 'lock' => r.raw('lock') != 'false' },:files) if file
221
+ end
222
+ # the page has to be created again, because it shows images
223
+ FileUtils.touch(s.cpath + 'revisions.xml') if %w{image file}.include?(r.command) && !s.new?
224
+ CEWebS::Document.new(
225
+ :content => View.new(c).render([:edit,r['text'],{ 'image' => 'images', 'file' => 'files' }[r.command] || 'help']),
226
+ :meta => [{ :httpequiv => 'expires', :content => '0' }],
227
+ :access => [back(c)]
228
+ )
229
+ end #}}}
230
+
231
+ def upload(c) #{{{
232
+ file = c.r.parameter('file')
233
+ file if file && file.raw.to_s != '' && file.data.to_s != ''
234
+ end #}}}
235
+
236
+ def add_image(c) #{{{
237
+ file = upload(c) or return
238
+ factor = 10 - c.r.raw('max').to_i.clamp(0,7)
239
+ width, height = %w{maxwidth maxheight}.map { |m| c.custom.find("number(/custom/object/@#{m})").to_i }
240
+ png = ReDel::Image::shrink(file.data,width * factor / 10,height) or return
241
+ width, height = ReDel::Image::size(png)
242
+ c.storage.add_object(file.value,file.data,nil,:images_orig)
243
+ c.storage.add_object(file.value,png,{ 'width' => width, 'height' => height },:images)
244
+ end #}}}
245
+
246
+ def save(c,text,eid,comment,meta,base,name) #{{{
247
+ user = c.access.name == '' ? 'Unknown' : c.access.name
248
+ c.storage.add_page(text.delete("\r").sub(/\s*\z/,''),eid,comment,meta.reject { |k,_| k == 'conflict' },user,base)
249
+ what = name == c.what ? name : c.storage.move_page(name,user)
250
+ redirect(c.link('show',what))
251
+ end #}}}
252
+ end
253
+ end
@@ -0,0 +1,67 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ module Didaktiki
3
+ # Compartments are separate wikis for a person, a group (g1), a team (g1t2), or
4
+ # everybody listed in the configuration.
5
+ class Access
6
+ def initialize(custom,persons,user,compartment) #{{{
7
+ @custom = custom
8
+ @persons = persons
9
+ @user = user
10
+ @c = compartment
11
+ @person = persons.find('/persons/person').find { |p| p.attributes['id'] == user } if user != ''
12
+ end #}}}
13
+
14
+ def name; @person ? @person.attributes['name'].to_s : ''; end
15
+ def admin?; @person ? @person.attributes['admin'] == 'true' : false; end
16
+
17
+ def edit? #{{{
18
+ return compartment?('edit') if @c != ''
19
+ admin? || listed?
20
+ end #}}}
21
+
22
+ def compartment?(method='access') #{{{
23
+ return true if @c == ''
24
+ schema = schema() or return false
25
+ return true if admin? || listed?
26
+
27
+ group = @person && "g#{@person.attributes['group']}"
28
+ case schema
29
+ when :person; @user == @c && allowed?(:person) || public?(:person,method)
30
+ when :group; group == @c && allowed?(:group) || public?(:group,method)
31
+ when :team; !group.nil? && "#{group}t#{@person.attributes['team']}" == @c && allowed?(:team) || public?(:team,method)
32
+ when :compartment
33
+ node = compartment
34
+ node.find('person/@id').map(&:to_s).include?(@user) || (method == 'access' && node.attributes['type'] == 'public')
35
+ end
36
+ end #}}}
37
+
38
+ private
39
+
40
+ def schema #{{{
41
+ case @c
42
+ when /\Ag[\d-]+\z/; :group
43
+ when /\Ag[\d-]+t\d+\z/; :team
44
+ else
45
+ if @persons.find('/persons/person/@id').any? { |x| x.to_s == @c }
46
+ :person
47
+ elsif compartment
48
+ :compartment
49
+ end
50
+ end
51
+ end #}}}
52
+ def compartment; @custom.find('/custom/compartments/compartment').find { |x| x.attributes['name'] == @c }; end
53
+
54
+ def allowed?(type); @custom.find("string(/custom/compartments/@#{type})") != 'no'; end
55
+ def public?(type,method); method == 'access' && @custom.find("string(/custom/compartments/@#{type})") == 'public'; end
56
+
57
+ # everybody who may edit all pages
58
+ def listed? #{{{
59
+ listed = @custom.find('/custom/edit/person/@id').any? { |x| x.to_s == @user }
60
+ case @custom.find('string(/custom/edit/@restriction)')
61
+ when 'allow'; listed
62
+ when 'deny'; !listed
63
+ else false
64
+ end
65
+ end #}}}
66
+ end
67
+ end
@@ -0,0 +1,24 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ require 'redel'
3
+
4
+ module Wiki
5
+ # names of pages and files <=> names usable in urls and in the file system
6
+ module Encoding
7
+ CHARS = /[^\-_.!~*'()a-zA-Z\d;:@=$,\[\]]/.freeze
8
+ LCHARS = /[^\-_.!~*'()a-zA-Z\d;@=$,\[\]]/.freeze
9
+
10
+ def self::escape(string)
11
+ ret = ReDel::XHTML::unweb(string.to_s).gsub(CHARS) { |a| "~#{a.ord}~" }
12
+ # . and .. are directories
13
+ ret =~ /\A\.+\z/ ? ret.gsub('.','~46~') : ret
14
+ end
15
+ def self::latex(string)
16
+ ReDel::XHTML::unweb(string.to_s).gsub(LCHARS) { |a| "+#{a.ord}+" }
17
+ end
18
+
19
+ # returns markup
20
+ def self::unescape(string)
21
+ ReDel::XHTML::web(string.to_s.gsub(/~(\d+)~/) { [$1.to_i].pack('U') })
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,73 @@
1
+ # Please see the LICENSE.GPL file for copyright and distribution information
2
+ require 'tmpdir'
3
+ require 'open3'
4
+
5
+ module Didaktiki
6
+ module Export
7
+ # storage and page for everything that may be exported
8
+ def self::pages(c,names) #{{{
9
+ names.filter_map do |n|
10
+ storage = c.storage(n)
11
+ page = storage.get_page
12
+ [storage,page] if page && !page.meta['notexport'] && (!page.meta['alock'] || c.access.edit?)
13
+ end
14
+ end #}}}
15
+ def self::selected(c)
16
+ c.r.parameter('asindex') ? c.storage.get_links : [c.what]
17
+ end
18
+
19
+ def self::archive #{{{
20
+ Dir.mktmpdir do |dir|
21
+ yield dir
22
+ Open3.capture2('tar','-C',dir,'-czf','-','.',:binmode => true)[0]
23
+ end
24
+ end #}}}
25
+
26
+ def self::html(c) #{{{
27
+ names = selected(c)
28
+ names = names.sort if c.r.parameter('sort')
29
+ content = pages(c,names).map { |storage,page| c.xhtml(page.data,storage)[0].strip }.join
30
+ View.new(c).render([:export,c.name('style'),content])
31
+ end #}}}
32
+
33
+ def self::latex(c) #{{{
34
+ width = c.custom.find('number(/custom/object/@maxwidth)').to_i
35
+ cites, refs, inputs = [], [], +''
36
+ archive do |dir|
37
+ pages(c,selected(c)).each do |storage,page|
38
+ name = Wiki::Encoding::latex(storage.name).gsub('/','+47+')
39
+ inputs << "\\input{#{name}.tex}\n"
40
+ latex = Wiki::Parser::LaTeX.new(storage,storage.current,c.strings,width,cites,refs)
41
+ File.write(File.join(dir,"#{name}.tex"),Wiki::Parser::Didaktiki.parse(page.data).prepare(latex).output(latex).strip)
42
+ objects(dir,'Images',storage,:images_orig)
43
+ end
44
+ used = refs.select { |name,_| cites.include?(name) }.each_with_index.map { |(_,text),i| text.sub('xxx',(i+1).to_s) }
45
+ bib = used.empty? ? '' : "\\begin{thebibliography}{}\n #{used.join("\n ")}\n\\end{thebibliography}\n"
46
+ File.write(File.join(dir,'__references.tex'),bib)
47
+ File.write(File.join(dir,'__list_of_exported_pages.tex'),inputs)
48
+ end
49
+ end #}}}
50
+
51
+ def self::exportwiki(c) #{{{
52
+ names = XML::Smart.open(File.join(c.path,'linking.xml')).find('/main/page/@name').map { |n| Wiki::Encoding::unescape(n.to_s) }
53
+ archive do |dir|
54
+ pages(c,names).each do |storage,page|
55
+ data = File.join(dir,'Data',storage.subject)
56
+ FileUtils.mkdir_p(data)
57
+ File.write(File.join(data,'markup'),page.data)
58
+ File.write(File.join(data,'meta'),page.meta.map { |k,v| "#{k}: #{v}\n" }.join)
59
+ objects(dir,'Images',storage,:images)
60
+ objects(dir,'Files',storage,:files)
61
+ end
62
+ end
63
+ end #}}}
64
+
65
+ def self::objects(dir,target,storage,type) #{{{
66
+ storage.get_object_list(type).each do |obj|
67
+ path = File.join(dir,target,storage.subject)
68
+ FileUtils.mkdir_p(path)
69
+ File.binwrite(File.join(path,obj.data),storage.get_object(Wiki::Encoding::unescape(obj.data),type).data)
70
+ end
71
+ end #}}}
72
+ end
73
+ end