Class: Patricia::Wiki

Inherits:
Object
  • Object
show all
Defined in:
lib/patricia/patricia.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_dir, **options) ⇒ Wiki

Returns a new instance of Wiki.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/patricia/patricia.rb', line 69

def initialize(input_dir, **options)
  options = {:output_dir => 'output', :js => [], :css =>
    []}.merge(options)
  @dirs = {}
  @output_dirs = []
  @input_dir = input_dir
  @output_dir = options[:output_dir]
  @asset_files = []
  options[:css] = [] if options[:css] == nil
  options[:js] = [] if options[:js] == nil
  @css = options[:css]
  @js = options[:js]

  directory_hash
  @toc = build_toc
end

Instance Attribute Details

#asset_filesObject (readonly)

Returns the value of attribute asset_files.



66
67
68
# File 'lib/patricia/patricia.rb', line 66

def asset_files
  @asset_files
end

#dirsObject (readonly)

Returns the value of attribute dirs.



66
67
68
# File 'lib/patricia/patricia.rb', line 66

def dirs
  @dirs
end

Returns the value of attribute footer.



66
67
68
# File 'lib/patricia/patricia.rb', line 66

def footer
  @footer
end

#headerObject (readonly)

Returns the value of attribute header.



66
67
68
# File 'lib/patricia/patricia.rb', line 66

def header
  @header
end

#input_dirObject (readonly)

Returns the value of attribute input_dir.



66
67
68
# File 'lib/patricia/patricia.rb', line 66

def input_dir
  @input_dir
end

#output_dirsObject (readonly)

Returns the value of attribute output_dirs.



66
67
68
# File 'lib/patricia/patricia.rb', line 66

def output_dirs
  @output_dirs
end

Instance Method Details

#_css_tags(paths) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/patricia/patricia.rb', line 86

def _css_tags(paths)
  tags = ''
  paths.each do |path|
    tags << "\n" +
      '<link rel="stylesheet" href="' + path + '" type="text/css">'
  end
  tags
end

#_js_tags(paths) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/patricia/patricia.rb', line 95

def _js_tags(paths)
  tags = ''
  paths.each do |path|
    tags << "\n" + '<script type="text/javascript" src="' + path +
      '" type="text/css"></script>'
  end
  tags
end


213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/patricia/patricia.rb', line 213

def _list_link(path)
  basename = File.basename(path).split('-').map(&:capitalize).join(' ')
  pp = _without_extension(_without_input_dir(path))
  p = pp.gsub(/\.\//, '')  # .gsub(/\//, ' > ')
  if File.file?(path)
    # '<li><a href="' + _without_extension(_without_input_dir(path)) +
    #   '">' + _without_extension(basename).sub(/\.\//, '') + '</a>'
    '<li><a title="' + p + '" href="' + pp + '">' +
      _without_extension(basename).sub(/\.\//, '') + '</a>'
  else
    '<li><span title="' + p + '">' + basename + '</span>'
  end
end

#_without_extension(path) ⇒ Object



125
126
127
# File 'lib/patricia/patricia.rb', line 125

def _without_extension(path)
  File.join(File.dirname(path), File.basename(path, '.*'))
end

#_without_input_dir(path) ⇒ Object



129
130
131
# File 'lib/patricia/patricia.rb', line 129

def _without_input_dir(path)
  path.sub(/#{@input_dir}/, '')
end

#build_toc(*args) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/patricia/patricia.rb', line 227

def build_toc(*args)
  dirs = args[0] || @dirs
  html = args[1] || ''
  dirs.each_pair do |k, v|
    next if v == @input_dir
    if v.class == Array
      html << '<ul>' if !v.empty?
      v.each do |child|
        if child.class == Hash
          build_toc child, html
        else
          ext = File.extname(child).sub(/\./, '')
          next if !Converter.converters.keys.include?(ext)
          html << _list_link(child)
        end
      end
      html << '</ul>' if !v.empty?
    else
      html << _list_link(v)
    end
  end
  html << '</li>'
  html
end

#directory_hash(*args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/patricia/patricia.rb', line 104

def directory_hash(*args)
  path = args[0] || @input_dir
  data = {:data => (args[0] || path)}
  data[:children] = children = []
  Dir.foreach(path) do |entry|
    next if entry == '..' || entry == '.'
    full_path = File.join(path, entry)
    if File.directory?(full_path)
      children << directory_hash(full_path)
    else
      children << full_path
    end
  end
  @dirs = data
end

#page(content) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/patricia/patricia.rb', line 181

def page(content)
  <<-PAGE
<!DOCTYPE html>
<html>
  <head>
<title></title>
#{_css_tags(@css) if @css.length > 0}
<meta charset="utf8"/>

<!-- Turn caching off. -->
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

<base href="/" target="_self"/>
  </head>
  <body>
<div id="toc">
#{@toc}
</div>
<div id="content">
#{content}
</div>
  <body>
#{_js_tags(@js)}
  </body>
</html>
PAGE
end

#render(*dirs) ⇒ Object



120
121
122
123
# File 'lib/patricia/patricia.rb', line 120

def render(*dirs)
  FileUtils.rm_r(@output_dir) if Dir.exists?(@output_dir)
  render_files *dirs
end

#render_files(*dirs) ⇒ Object



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
# File 'lib/patricia/patricia.rb', line 133

def render_files(*dirs)
  dirs = dirs[0] || @dirs
  dirs.each_pair do |k, v|
    if v.class == Array
      v.each do |child|
        if child.class == Hash
          render_files child
        else
          ext = File.extname(child).sub(/\./, '')
          if Converter.converters.keys.include?(ext)
            Dir.mkdir(@output_dir) unless Dir.exists?(@output_dir)
            if File.file?(child)
              output_child_dir =
                _without_input_dir(_without_extension(child))
            else
              output_child_dir =
                _without_input_dir(child)
            end
            FileUtils.mkdir_p(File.join(@output_dir,
                                        output_child_dir))
            @output_dirs <<
              _without_extension(_without_input_dir(output_child_dir))
            File.open(@output_dir + '/' + output_child_dir +
                      '/index.html', 'w') do |f|
              f.puts page(Converter::to_html(child))
            end
          else
            # Copy images etc. over
            src = child
            dest = File.join(@output_dir,
                             _without_input_dir(File.dirname(child)),
                             File.basename(child))
            @asset_files << dest
            # Do not mk the output dir struct within itself.
            if _without_input_dir(File.dirname(child)) !=
                @input_dir.gsub(/\/$/, '')
              FileUtils.mkdir_p(File.dirname(dest))
            else
              next
            end
            FileUtils.cp(src, dest)
          end
        end
      end
    end
  end
end