Module: Giblish

Defined in:
lib/giblish/utils.rb,
lib/giblish.rb,
lib/giblish/core.rb,
lib/giblish/docid.rb,
lib/giblish/gititf.rb,
lib/giblish/docinfo.rb,
lib/giblish/version.rb,
lib/giblish/buildgraph.rb,
lib/giblish/buildindex.rb,
lib/giblish/application.rb,
lib/giblish/docconverter.rb,
lib/giblish/indexheadings.rb

Overview

Public: Contains a number of generic utility methods.

Defined Under Namespace

Classes: Application, BasicIndexBuilder, DocConverter, DocInfo, DocidCollector, FileTreeConverter, GitItf, GitRepoConverter, GitRepoIndexBuilder, GitSummaryIndexBuilder, GraphBuilderGraphviz, HtmlConverter, IndexHeadings, PathManager, PdfConverter, SimpleIndexBuilder

Constant Summary collapse

VERSION =
"0.4.0".freeze

Class Method Summary collapse

Class Method Details

.applicationObject



14
15
16
# File 'lib/giblish.rb', line 14

def application
  @application ||= Giblish::Application.new
end

.process_header_lines(lines) ⇒ Object

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



179
180
181
182
183
184
185
186
187
188
# File 'lib/giblish/utils.rb', line 179

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

.process_header_lines_from_file(path) ⇒ Object

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



204
205
206
207
# File 'lib/giblish/utils.rb', line 204

def process_header_lines_from_file(path)
  lines = File.readlines(path)
  process_header_lines(lines,&Proc.new)
end

.register_docid_extensionObject

Helper method to register the docid preprocessor extension with the asciidoctor engine.



182
183
184
185
186
# File 'lib/giblish/docid.rb', line 182

def register_docid_extension
  Asciidoctor::Extensions.register do
    preprocessor DocidCollector
  end
end

.register_index_heading_extensionObject

Helper method to register the docid preprocessor extension with the asciidoctor engine.



157
158
159
160
161
# File 'lib/giblish/indexheadings.rb', line 157

def register_index_heading_extension
  Asciidoctor::Extensions.register do
    preprocessor IndexHeadings
  end
end

.to_valid_id(input_str) ⇒ Object

transforms strings to valid asciidoctor id strings



223
224
225
226
227
# File 'lib/giblish/utils.rb', line 223

def to_valid_id(input_str)
  id_str = "_#{input_str.strip.downcase}"
  id_str.gsub!(%r{[^a-z0-9]+},"_")
  id_str.chomp('_')
end

.which(cmd) ⇒ Object

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


235
236
237
238
239
240
241
242
243
244
# File 'lib/giblish/utils.rb', line 235

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  return nil
end

.with_captured_stderrObject

runs the supplied block but redirect stderr to a string returns the string containing stderr contents



212
213
214
215
216
217
218
219
# File 'lib/giblish/utils.rb', line 212

def with_captured_stderr
  old_stderr = $stderr
  $stderr = StringIO.new("", "w")
  yield
  $stderr.string
ensure
  $stderr = old_stderr
end