module Giblish
put docid stuff in the giblish namespace
put the indexing in the giblish namespace
Public: Contains a number of generic utility methods.
Constants
- VERSION
Public Class Methods
# File lib/giblish.rb, line 14 def application @application ||= Giblish::Application.new end
returns raw html that displays a search box to let the user acces the text search functionality.
css - the name of the css file to use for the search result layout cgi_path - the (uri) path to a cgi script that implements the server side
functionality of searching the text
opts: :web_assets_top => string # the path to the 'web_assets' dir as seen when serving
the web server (eg www.mysite.com/blah/doc_root -> web_assets_top shall be '/blah/doc_root')
:search_assets_top => string # the path to where the 'heading.json' file is located (
as seen from the local file system on the machine that runs the search script)
# File lib/giblish/utils.rb, line 403 def generate_search_box_html(css, cgi_path, opts) # Replace the button with the below to use a text-only version of the btn # <button id="search" type="submit">Search</button> <<~SEARCH_INFO ++++ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <form class="example" action="#{cgi_path}" style="margin:20px 0px 20px 0px;max-width:790px"> Search all documents: <input id="searchphrase" type="text" placeholder="Search.." name="searchphrase"/> <button id="search" type="submit"><i class="fa fa-search"></i></button> <br> <input id="ignorecase" type="checkbox" value="true" name="ignorecase" checked/> <label for="ignorecase">Ignore Case</label> <input id="useregexp" type="checkbox" value="true" name="useregexp"/> <label for="useregexp">Use Regexp</label> <input type="hidden" name="searchassetstop" value="#{opts[:search_assets_top]}"</input> <input type="hidden" name="webassetstop" value="#{opts[:web_assets_top]}"</input> #{%(<input type="hidden" name="css" value="#{css}"</input>) unless css.nil?} </form> ++++ SEARCH_INFO end
Helper method that provides the user with a way of processing only the lines within the asciidoc header block. The user must return nil to get the next line.
ex: process_header_lines
(file_path) do |line|
if line == "Quack!" puts "Donald!" 1 else nil end
end
# File lib/giblish/utils.rb, line 322 def process_header_lines(lines) state = "before_header" lines.each do |line| case state when "before_header" then (state = "in_header" if line =~ /^[=+]^.*$/ || yield(line)) when "in_header" then (state = "done" if line =~ /^\s*$/ || yield(line)) when "done" then break end end end
Helper method that provides the user with a way of processing only the lines within the asciidoc header block. The user must return nil to get the next line.
ex: process_header_lines_from_file
(file_path) do |line|
if line == "Quack!" puts "Donald!" 1 else nil end
end
# File lib/giblish/utils.rb, line 347 def process_header_lines_from_file(path) lines = File.readlines(path) process_header_lines(lines, &Proc.new) end
Helper method to register the docid preprocessor extension with the asciidoctor engine.
# File lib/giblish/docid.rb, line 174 def register_docid_extension Asciidoctor::Extensions.register do preprocessor DocidCollector end end
Helper method to register the docid preprocessor extension with the asciidoctor engine.
# File lib/giblish/indexheadings.rb, line 244 def register_index_heading_extension Asciidoctor::Extensions.register do preprocessor IndexHeadings end end
transforms strings to valid asciidoctor id strings
# File lib/giblish/utils.rb, line 366 def to_valid_id(input_str, id_prefix = "_", id_separator = "_") id_str = input_str.strip.downcase.gsub(/[^a-z0-9]+/, id_separator) id_str = "#{id_prefix}#{id_str}" id_str.chomp(id_separator) end
See stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby Cross-platform way of finding an executable in the $PATH.
Ex
which('ruby') #=> /usr/bin/ruby
# File lib/giblish/utils.rb, line 378 def which(cmd) exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""] ENV["PATH"].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) end end nil end
runs the supplied block but redirect stderr to a string returns the string containing stderr contents
# File lib/giblish/utils.rb, line 355 def with_captured_stderr old_stderr = $stderr $stderr = StringIO.new("", "w") yield $stderr.string ensure $stderr = old_stderr end
Private Instance Methods
returns raw html that displays a search box to let the user acces the text search functionality.
css - the name of the css file to use for the search result layout cgi_path - the (uri) path to a cgi script that implements the server side
functionality of searching the text
opts: :web_assets_top => string # the path to the 'web_assets' dir as seen when serving
the web server (eg www.mysite.com/blah/doc_root -> web_assets_top shall be '/blah/doc_root')
:search_assets_top => string # the path to where the 'heading.json' file is located (
as seen from the local file system on the machine that runs the search script)
# File lib/giblish/utils.rb, line 403 def generate_search_box_html(css, cgi_path, opts) # Replace the button with the below to use a text-only version of the btn # <button id="search" type="submit">Search</button> <<~SEARCH_INFO ++++ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <form class="example" action="#{cgi_path}" style="margin:20px 0px 20px 0px;max-width:790px"> Search all documents: <input id="searchphrase" type="text" placeholder="Search.." name="searchphrase"/> <button id="search" type="submit"><i class="fa fa-search"></i></button> <br> <input id="ignorecase" type="checkbox" value="true" name="ignorecase" checked/> <label for="ignorecase">Ignore Case</label> <input id="useregexp" type="checkbox" value="true" name="useregexp"/> <label for="useregexp">Use Regexp</label> <input type="hidden" name="searchassetstop" value="#{opts[:search_assets_top]}"</input> <input type="hidden" name="webassetstop" value="#{opts[:web_assets_top]}"</input> #{%(<input type="hidden" name="css" value="#{css}"</input>) unless css.nil?} </form> ++++ SEARCH_INFO end
Helper method that provides the user with a way of processing only the lines within the asciidoc header block. The user must return nil to get the next line.
ex: process_header_lines
(file_path) do |line|
if line == "Quack!" puts "Donald!" 1 else nil end
end
# File lib/giblish/utils.rb, line 322 def process_header_lines(lines) state = "before_header" lines.each do |line| case state when "before_header" then (state = "in_header" if line =~ /^[=+]^.*$/ || yield(line)) when "in_header" then (state = "done" if line =~ /^\s*$/ || yield(line)) when "done" then break end end end
Helper method that provides the user with a way of processing only the lines within the asciidoc header block. The user must return nil to get the next line.
ex: process_header_lines_from_file
(file_path) do |line|
if line == "Quack!" puts "Donald!" 1 else nil end
end
# File lib/giblish/utils.rb, line 347 def process_header_lines_from_file(path) lines = File.readlines(path) process_header_lines(lines, &Proc.new) end
Helper method to register the docid preprocessor extension with the asciidoctor engine.
# File lib/giblish/docid.rb, line 174 def register_docid_extension Asciidoctor::Extensions.register do preprocessor DocidCollector end end
Helper method to register the docid preprocessor extension with the asciidoctor engine.
# File lib/giblish/indexheadings.rb, line 244 def register_index_heading_extension Asciidoctor::Extensions.register do preprocessor IndexHeadings end end
transforms strings to valid asciidoctor id strings
# File lib/giblish/utils.rb, line 366 def to_valid_id(input_str, id_prefix = "_", id_separator = "_") id_str = input_str.strip.downcase.gsub(/[^a-z0-9]+/, id_separator) id_str = "#{id_prefix}#{id_str}" id_str.chomp(id_separator) end
See stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby Cross-platform way of finding an executable in the $PATH.
Ex
which('ruby') #=> /usr/bin/ruby
# File lib/giblish/utils.rb, line 378 def which(cmd) exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""] ENV["PATH"].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) end end nil end
runs the supplied block but redirect stderr to a string returns the string containing stderr contents
# File lib/giblish/utils.rb, line 355 def with_captured_stderr old_stderr = $stderr $stderr = StringIO.new("", "w") yield $stderr.string ensure $stderr = old_stderr end