File: 01sp.rb

package info (click to toggle)
hiki 0.8.6-1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 1,772 kB
  • ctags: 1,746
  • sloc: ruby: 20,067; lisp: 926; sh: 269; makefile: 10
file content (186 lines) | stat: -rw-r--r-- 5,040 bytes parent folder | download | duplicates (3)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# 01sp.rb - select-plugins plugin $Revision: 1.4 $

=begin ChangeLog
See ../ChangeLog for changes after this.

* Thu Aug 28, 2003 zunda <zunda at freeshell.org>
- 1.3
- simpler configuration display

* Tue Aug 26, 2003 zunda <zunda at freeshell.org>
- 1.2
- option defaults are flipped
- Typo for @options are fixed

* Tue Aug 26, 2003 zunda <zunda at freeshell.org>
- 1.1
- English translation

* Fri Aug 22, 2003 zunda <zunda at freeshell.org>
- 1.1.2.6
- bug fix: check conf mode before updating the options

* Fri Aug 22, 2003 zunda <zunda at freeshell.org>
- 1.1.2.5
- following options are added: thanks to kaz
- @options['select_plugins.hidesource']
- @options['select_plugins.hidemandatory']
- @options['select_plugins.newdefault']
- new plugins are marked in the list until the user configures the selections

* Wed Aug 20, 2003 zunda <zunda at freeshell.org>
- 1.1.2.1
- first release
=end ChangeLog

SP_PREFIX = 'sp'
@sp_path = ( @conf["#{SP_PREFIX}.path"] || "#{PATH}/misc/plugin" ).to_a
@sp_path = @sp_path.collect do |path|
  /\/$/ =~ path ? path.chop : path
end

# get plugin option
def sp_option( key )
  @conf["#{SP_PREFIX}.#{key}"]
end

# hash of paths from array of dirs
def sp_hash_from_dirs( dirs )
  r = Hash.new
  dirs.each do |dir|
    Dir::glob( "#{dir}/*.rb" ).each do |path|
      filename = File.basename( path )
      r[filename] ||= path
    end
  end
  r
end

# url of the document
def sp_doc_url( file )
  case @conf.lang
  when 'ja'
    "http://hikiwiki.org/ja/#{CGI::escape( file )}.html"
  else
    "http://hikiwiki.org/en/#{CGI::escape( file )}.html"
  end
end

def collect_plugins( sp_opt )
  # categorize the available plugins
  used = []
  notused = []
  unknown = []
  # File.basenmame needed to read option from 01sp.rb <= 1.10
  selected_array = sp_option( 'selected' ) ? sp_option( 'selected' ).split( /\n/ ).collect{ |p| File.basename( p ) } : []
  notselected_array = sp_option( 'notselected' ) ? sp_option( 'notselected').split( /\n/ ).collect{ |p| File.basename( p ) } : []
  sp_opt.keys.each do |path|
    if selected_array.include?( path ) then
      used << path
    elsif notselected_array.include?( path ) then
      notused << path
    else
      unknown << path
    end
  end
  [used, notused, unknown]
end

# <li> list of plugins
def sp_li_plugins( paths, with_checkbox, is_checked )
  paths.collect { |path| File.basename( path ) }.sort.inject('') do |result, file|
    checkbox = with_checkbox ? %Q!<input name="#{SP_PREFIX}.#{CGI::escapeHTML( file )}" type="checkbox" value="t"#{is_checked ? ' checked' : ''}>! : ''
    result << %Q!<li>#{checkbox}<a href="#{sp_doc_url( file )}">#{CGI::escapeHTML( file )}</a>!
  end
end

# lists of plugins
def sp_list_plugins( sp_opt, with_checkbox )
  r = ''
  if sp_opt.empty?
    return @sp_label_noplugin
  else
    used, notused, unknown = collect_plugins( sp_opt )

    # list up
    r << @sp_label_please_select
    unless unknown.empty? then
      r << @sp_label_new
      r << "<ul>\n" 
      r << sp_li_plugins( unknown, with_checkbox, sp_option( 'usenew' ) )
      r << "</ul>\n"
    end

    # selected plugins
    unless used.empty? then
      r << @sp_label_used
      r << "<ul>\n"
      r << sp_li_plugins( used, with_checkbox, true )
      r << "</ul>\n"
    end

    # not selected plugins
    unless notused.empty? then
      r << @sp_label_notused
      r << "<ul>\n"
      r << sp_li_plugins( notused, with_checkbox, false )
      r << "</ul>\n"
    end
  end
  r
end

# things needed to configure this plugin
if SP_PREFIX == @cgi.params['conf'][0]
  # list of plugins
  @sp_opt = sp_hash_from_dirs( @sp_path )

  # update options
  # we have to do this when we are eval'ed to update the config menu
  if /saveconf/ =~ @mode
    @conf["#{SP_PREFIX}.selected"] = ''
    @conf["#{SP_PREFIX}.notselected"] = ''
    @sp_opt.each_key do |file|
      if 't' == @cgi.params["#{SP_PREFIX}.#{file}"][0]
        @conf["#{SP_PREFIX}.selected"] << "#{file}\n"
      else
        @conf["#{SP_PREFIX}.notselected"] << "#{file}\n"
      end
    end
  end
end

# configuration menu
# options are updated when we are eval'ed
add_conf_proc( SP_PREFIX, @sp_label ) do
  r = ''
  r << @sp_label_description
  r << sp_list_plugins( @sp_opt, true )
end

# Finally, we can eval the selected plugins as tdiary.rb does
if sp_option( 'selected' )
  sp_option( 'selected' ).untaint.split( /\n/ ).collect{ |p| File.basename( p ) }.sort.each do |filename|
    @sp_path.each do |dir|
      path = "#{dir}/#{filename}"
      if File.readable?( path )
        begin
          load_plugin( path )
          @plugin_files << path
        rescue Exception
          raise PluginError::new( "Plugin error in '#{path}'.\n#{$!}" )
        end
        break
      end
    end
  end
end

# The `show_plugins' plugin is enabled in show_plugins.rb
def show_plugins
  used = @conf["#{SP_PREFIX}.selected"].split( /\n/ ).collect{ |p| File.basename( p ) }.sort
  used.empty? ? '' : "<ul>\n" + sp_li_plugins( used, false, true ) + "</ul>\n"
end

export_plugin_methods()