Top Level Namespace

Defined Under Namespace

Modules: Giblish Classes: CmdLineParser, Giblog, GrepDocTree, PathTree, SearchDocTree

Instance Method Summary collapse

Instance Method Details

#cgi_main(cgi) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/giblish-search.rb', line 309

def cgi_main cgi
  # retrieve the form data supplied by user
  input_data = {
      search_phrase: cgi["searchphrase"],
      ignorecase: cgi.has_key?("ignorecase"),
      useregexp: cgi.has_key?("useregexp"),
      doc_root_abs: Pathname.new(cgi["topdir"]),
      referer_rel_top: Pathname.new("/#{cgi["reltop"]}"),
      referer: cgi.referer,
      uri_path: URI(cgi.referer).path,
      client_css: cgi["css"],
      search_top: nil,
      styles_top: nil
  }

  # fixup paths depending on git branch or not
  #
  # search_assets is an absolute path
  # styles_top is a relative path
  #
  # if the source was rendered from a git branch, the paths
  # search_assets = <index_dir>/../search_assets/<branch_name>/
  # styles_dir = ../web_assets/css
  #
  # and if not, the path is
  # search_assets = <index_dir>/search_assets
  # styles_dir = ./web_assets/css
  #
  # The styles dir shall be a relative path
  if input_data[:doc_root_abs].join("./search_assets").exist?
    # this is not from a git branch
    input_data[:search_top] = input_data[:doc_root_abs].join("./search_assets")
    # input_data[:styles_top] = Pathname.new(input_data[:uri_path]).join("./web_assets/css")
    input_data[:styles_top] = Pathname.new(input_data[:referer_rel_top]).join("web_assets/css")
    input_data[:gitbranch] = false
  elsif input_data[:doc_root_abs].join("../search_assets").exist?
    # this is from a git branch
    input_data[:search_top] = input_data[:doc_root_abs].join("../search_assets").join(input_data[:doc_root_abs].basename)
    input_data[:styles_top] = Pathname.new(input_data[:referer_rel_top]).join("../web_assets/css")
    input_data[:gitbranch] = true
  else
    raise ScriptError, "Could not find search_assets dir!"
  end

  # use a relative stylesheet (same as the index page was rendered with)
  adoc_options =  {
      "data-uri" => 1,
      "linkcss" => 1,
      "stylesdir" => input_data[:styles_top].to_s,
      "stylesheet" => input_data[:client_css],
      "copycss!" => 1
  }

  # search the docs and render html
  sdt = SearchDocTree.new(input_data)
  docstr = sdt.search

  # send the result back to the client
  print Asciidoctor.convert docstr, header_footer: true, attributes: adoc_options
end

#hello_worldObject



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/giblish-search.rb', line 285

def hello_world
  require "pp"

  # init a new cgi 'connection'
  cgi = CGI.new
  print cgi.header
  print "<br>"
  print "Useful cgi parameters and variables."
  print "<br>"
  print cgi.public_methods(false).sort
  print "<br>"
  print "<br>"
  print "referer: #{cgi.referer}<br>"
  print "path: #{URI(cgi.referer).path}<br>"
  print "host: #{cgi.host}<br>"
  print "client_sent_topdir: #{cgi["topdir"]}<br>"
  print "<br>"
  print "client_sent_reldir: #{cgi["reltop"]}<br>"
  print "<br>"
  print "ENV: "
  pp ENV
  print "<br>"
end

#init_web_server(web_root) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/giblish-search.rb', line 266

def init_web_server web_root
  require 'webrick'

  root = File.expand_path web_root
  puts "Trying to start a WEBrick instance at port 8000 serving files from #{web_root}..."

  server = WEBrick::HTTPServer.new(
      :Port => 8000,
      :DocumentRoot => root,
      :Logger => WEBrick::Log.new("webrick.log",WEBrick::Log::DEBUG)
  )

  puts "WEBrick instance now listening to localhost:8000"

  trap 'INT' do server.shutdown end

  server.start
end