Class: Gonzui::MarkupServlet

Inherits:
GonzuiAbstractServlet show all
Defined in:
lib/gonzui/webapp/markup.rb

Constant Summary

Constants included from URIMaker

URIMaker::ParamTable

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GonzuiAbstractServlet

#format_html, #get_media_type, #get_mime_type, #init_servlet, #initialize, #log, #make_path, #set_content_type_text_html

Methods included from HTMLMaker

#make_content_script_type, #make_css, #make_footer, #make_format_select, #make_h1, #make_html, #make_license_select, #make_meta, #make_meta_and_css, #make_navi, #make_property_select, #make_script, #make_search_form, #make_spacer, #make_status_line, #make_title

Methods included from URIMaker

#decompose_search_query, #escape_path, #get_default_query_value, #get_query_value, #get_short_name, #make_advanced_search_uri, #make_doc_uri, #make_google_uri, #make_lineno_uri, #make_markup_uri, #make_search_uri, #make_search_uri_partial, #make_source_uri, #make_stat_uri, #make_top_uri, #make_uri_general, #make_uri_with_options

Methods included from GetText

#gettext, #gettext_noop, #load_catalog, #set_catalog

Methods included from Util

assert, assert_equal, assert_equal_all, assert_non_nil, assert_not_reached, benchmark, command_exist?, commify, eprintf, format_bytes, program_name, protect_from_signals, require_command, set_verbosity, shell_escape, unix?, vprintf, windows?, wprintf

Constructor Details

This class inherits a constructor from Gonzui::GonzuiAbstractServlet

Class Method Details

.mount_pointObject



126
127
128
# File 'lib/gonzui/webapp/markup.rb', line 126

def self.mount_point
  "markup"
end

Instance Method Details

#calc_ncolumns(items) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/gonzui/webapp/markup.rb', line 174

def calc_ncolumns(items)
  sum = items.inject(0) {|a, b| 
    name = b.first
    a + name.length 
  }
  average = sum / items.length
  ncolumns = [40 / average, items.length].min
  ncolumns = 1 if ncolumns == 0
  return ncolumns
end

#choose_methodObject



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/gonzui/webapp/markup.rb', line 395

def choose_method
  method = :make_not_found
  if @path.empty?
    method = :list_packages
    title = make_title(_("List of Packages"))
    status_title = _("List of Packages")
  else
    @path_id = @dbm.get_path_id(@path)
    @package_name = @path.split("/").first
    if @path_id 
      method = :markup_file 
    elsif @dbm.has_package?(@package_name)
      method = :list_files 
    end
    title = make_title(@path)
    status_title = make_breadcrumbs(@path)
  end
  return method, title, status_title
end

#do_GET(request, response) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/gonzui/webapp/markup.rb', line 415

def do_GET(request, response)
  init_servlet(request, response)
  log(@path)

  @path = make_path
  @to = nil
  @total = nil

  method, title, status_title = choose_method

  html = make_html
  head = [:head, title, make_script, *make_meta_and_css]
  body = [:body, {:onload => "initCache();",
                  :onclick => "clearHighlight();" }]
  body.push(make_h1)
  body.push(make_search_form)
  content = self.send(method)

  status_line = make_status_line(status_title, method)
  body.push(status_line)
  body.push(content)
  body.push(make_footer)

  html.push(head)
  html.push(body)
  set_response(html)
end

#do_make_navi(from) ⇒ Object



283
284
285
# File 'lib/gonzui/webapp/markup.rb', line 283

def do_make_navi(from)
  make_markup_uri("", nil, :from => from)
end

#do_make_status_line(method) ⇒ Object



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
# File 'lib/gonzui/webapp/markup.rb', line 145

def do_make_status_line(method)
  items = []

  case method
  when :markup_file
    if @path_id
      bare = [:a, {:href => make_source_uri(@path)}, 
        _("bare source")]
      permlink = [:a, {:href => make_markup_uri(@path)}, 
        _("permlink")]
      items.push(bare)
      items.push(permlink)
    end
  when :list_files
    if @total
      items.push([:span, [:strong, @total], _(" contents.")])
    end
  when :list_packages
    if @to and @total
      packages = [:span, _("Packages "), 
        [:strong, commify(@from + 1)], " - ",
        [:strong, commify(@to)], _(" of "),
        [:strong, commify(@total)], _(".")]
      items.push(packages)
    end
  end
  return items
end

#gzip(string) ⇒ Object



376
377
378
379
380
381
382
# File 'lib/gonzui/webapp/markup.rb', line 376

def gzip(string)
  strio = StringIO.new
  writer = Zlib::GzipWriter.new(strio)
  writer.print(string)
  writer.finish
  return strio.string
end

#list_filesObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/gonzui/webapp/markup.rb', line 259

def list_files
  path_parts = @path.split("/")
  depth = path_parts.length
  
  package_id = @dbm.get_package_id(@package_name)
  path_ids = @dbm.get_path_ids(package_id)
  paths = path_ids.map {|path_id|
    @dbm.get_path(path_id)
  }.map {|path| 
    path.split("/")
  }.find_all {|parts|
    parts.length > depth and parts[0, depth] == path_parts
  }.map {|parts|
    file_p = parts.length == depth + 1
    [parts[0, depth + 1].join("/"), file_p]
  }.uniq
  if paths.empty?
    return ""
  else
    @total = paths.length
    return make_table_of_files(paths)
  end
end

#list_packagesObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/gonzui/webapp/markup.rb', line 287

def list_packages 
  # FIXME: sorting every time is not efficient.
  all = []
  @dbm.each_package_name {|package_name|
    all.push(package_name)
  }
  all.sort!

  package_names = all[@from, @config.max_packages_per_page]
  if package_names
    @to = @from + package_names.length
    @total = all.length
    table = make_table_of_packages(package_names)
    navi = make_navi(all.length, @from, @config.max_packages_per_page)
    return [:div, table, navi]
  else
    return ""
  end
end

#make_breadcrumbs(path) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gonzui/webapp/markup.rb', line 130

def make_breadcrumbs(path)
  parts = path.split("/")
  breadcrumbs = (1 .. parts.length).map {|i| 
    [ parts[i - 1], parts[0, i]]
  }.map {|part, parts|
    path = parts.join("/")
    uri = make_markup_uri(parts.join("/"), @search_query)
    [:a, {:href => uri}, part]
  }
  (breadcrumbs.length - 1).times {|i|
    breadcrumbs.insert(i * 2 + 1, "/")
  }
  return [:span, *breadcrumbs]
end

#make_isearch_formObject



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/gonzui/webapp/markup.rb', line 330

def make_isearch_form
  isearch = [:div, {:class => "isearch"}]
  form = [:form, {:method => "get", :action => "",
    :onsubmit => "return false;"}]
  p = [:p]
  p.push(_("Search this content: "))
  p.push([:input, {:type => "text", :name => "g", :size => "20",
             :onkeyup => "isearch(this.value);",
             :value => ""
           }])
  p.push(" ")
  form.push(p)
  isearch.push(form)
  return isearch
end

#make_not_foundObject



371
372
373
374
# File 'lib/gonzui/webapp/markup.rb', line 371

def make_not_found
  sprintf(_("No contents were found matching %s"), 
          @path)
end

#make_table(items) ⇒ Object



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
212
213
214
215
216
217
218
# File 'lib/gonzui/webapp/markup.rb', line 185

def make_table(items)
  table = [:table, {:class => "fullwidth"}]
  return table if items.empty?
  ncolumns = calc_ncolumns(items)
  width = sprintf("%d%%", 100/ncolumns)

  nrows = (items.length - 1) / ncolumns + 1
  nrows.times {|i|
    k = if (i + 1) % 2 == 0 then "even" else "odd" end
    row = [:tr, {:class => k}]
    ncolumns.times {|j|
      # item = items[j * nrows + i] # for vertical arrange
      item = items[i * ncolumns + j]
      value = if item
                name, klass, image, uri, title = item
                tr = [:tr]
                if image
                  img = [:img, {:src => image, :alt => ""}]
                  tr << [:td, [:a, {:href => uri}, img]]
                end
                a = [:a, {:class => klass, :href =>uri,:title =>title}, name]
                tr << [:td, a]
                [:table, {:class => "item"}, tr]
              else
                make_spacer
              end
      k = if j == 0 then "first" else "nonfirst" end
      td = [:td, {:width => width, :class => k}, value]
      row.push(td)
    }
    table.push(row)
  }
  return table
end

#make_table_of_files(paths) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/gonzui/webapp/markup.rb', line 233

def make_table_of_files(paths)
  items = paths.map {|path, file_p|
    basename = File.basename(path)
    klass = if file_p then "file" else "directory" end
    image = if file_p 
              make_doc_uri("text.png")
            else 
              make_doc_uri("folder.png")
            end
    uri = make_markup_uri(path, @search_query)
    title = make_title_for_path(path, file_p)
    [basename, klass, image, uri, title]
  }
  return make_table(items)
end

#make_table_of_packages(package_names) ⇒ Object



249
250
251
252
253
254
255
256
257
# File 'lib/gonzui/webapp/markup.rb', line 249

def make_table_of_packages(package_names)
  items = package_names.map {|package_name|
    package_id = @dbm.get_package_id(package_name)
    uri = make_markup_uri(package_name)
    title = ""
    [package_name, "directory", nil, uri, title]
  }
  return make_table(items)
end

#make_title_for_path(path, file_p) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/gonzui/webapp/markup.rb', line 220

def make_title_for_path(path, file_p)
  title = if file_p 
            path_id = @dbm.get_path_id(path)
            info = @dbm.(path_id)
            sprintf(_("Size: %s (%s lines)"),
                    format_bytes(info.size),
                    commify(info.nlines))
          else 
            ""
          end
  return title
end

#markup_fileObject



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/gonzui/webapp/markup.rb', line 346

def markup_file
  path = @dbm.get_path(@path_id)

  mime_type = get_mime_type(path)
  media_type = get_media_type(path)
  if media_type == "image"
    img = [:img, {:src => make_source_uri(path),
        :alt => path }]
    content = [:div, {:class => "image"}, img]
  elsif mime_type == "text/html"
    iframe = [:iframe, {:src => make_source_uri(path),
      :width => "100%", :height => "400"},
      [:a, {:href => make_source_uri(path)}, path]
    ]
    content = iframe
  else
    content = [:div]
    isearch = make_isearch_form
    content.push(isearch)
    code = markup_source
    content.push([:pre, *code])
  end
  return content
end

#markup_sourceObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/gonzui/webapp/markup.rb', line 307

def markup_source
  content = @dbm.get_content(@path_id)
  digest  = @dbm.get_digest(@path_id)
  occurrences = if @search_query.string.empty?
                  []
                else
                  search_query = @search_query.clone
                  search_query.path ||= @path
                  searcher = Searcher.new(@dbm, search_query, 
                                          @config.max_results_overall)
                  result = searcher.search
                  item = result.first
                  if item and not item.list.empty? 
                    item.list 
                  else
                    [] 
                  end
                end
  search_uri = make_search_uri_partial("")
  beautifier = TextBeautifier.new(content, digest, occurrences, search_uri)
  return beautifier.beautify
end

#set_response(html) ⇒ Object



384
385
386
387
388
389
390
391
392
393
# File 'lib/gonzui/webapp/markup.rb', line 384

def set_response(html)
  set_content_type_text_html
  body = format_html(html)
  if @request.gzip_encoding_supported?
    @response["Content-Encoding"] = "gzip"
    @response.body = gzip(body)
  else
    @response.body = body
  end
end