class Vocco::Generator

Constants

CSS

Attributes

globs[R]
name[R]
notes[R]
out[R]
site[R]

Public Class Methods

new(opts) click to toggle source
# File lib/vocco/generator.rb, line 13
def initialize(opts)
  @globs  = Array(opts[:files]
            ).compact.map do |glob|
              glob.gsub(/(^\.\/)|(\/$)/, '')
            end
  @out    = opts[:out]
  @notes  = Array(opts[:notes]).compact
  @name   = opts[:name].capitalize
  @site   = opts[:site]
  @vim    = Array(opts[:vim]).compact

  FileUtils.mkdir_p(@out) # ensure out dir exists
end

Public Instance Methods

files() click to toggle source

array of SourceFile's mapped from the glob matches

# File lib/vocco/generator.rb, line 48
def files
  @files ||= @globs.map do |glob|
    Dir[glob]
  end.flatten.uniq.map do |path|
    SourceFile.new(path, self)
  end
end
glob_regex() click to toggle source

regex to trim the glob dir scopes off a source file path.

# File lib/vocco/generator.rb, line 38
def glob_regex
  @glob_regex ||= begin
    str = scopes.map do |scope|
      Regexp::escape(scope)
    end.join('|')
    /^(\.\/)?(#{str})(\/)?/
  end
end
run() click to toggle source
# File lib/vocco/generator.rb, line 56
def run
  run_vim; postprocess
end
Also aliased as: run!
run!()
Alias for: run
scopes() click to toggle source

the paths underneath which the @globs are globbing; the static part of the glob, or “scope” of sorts

# File lib/vocco/generator.rb, line 29
def scopes
  @scopes ||= @globs.map do |glob|
    tokens = glob.split('*')
    tokens.size > 1 ? tokens.first : nil
  end.compact
end

Private Instance Methods

hpricotify_docs() { |doc, file| ... } click to toggle source
# File lib/vocco/generator.rb, line 100
def hpricotify_docs
  files.each do |file|
    doc = File.open(file.doc_path) {|f| Hpricot(f) }
    yield(doc, file)
    File.open(file.doc_path, 'w')  {|f| f.write(doc.to_html) }
  end
end
postprocess() click to toggle source
# File lib/vocco/generator.rb, line 90
def postprocess
  hpricotify_docs do |doc, file|
    doc.at('style').after('<style type="text/css">' + CSS + '</style>')
    doc.at('title').inner_html = name + ' : ' + file.basename
    inner = doc.at('body').inner_html
    doc.at('body').swap "<body><div class='code'>#{inner}</div></body>"
    doc.at('.code').after file.render_template
  end
end
run_vim() click to toggle source
# File lib/vocco/generator.rb, line 63
def run_vim
  script = Tempfile.new('vimdocco')
  script.write(vimscript)
  script.close
  puts 'Running ' + vim_command + ':'
  system "#{vim_command} -f -S #{script.path}"
  script.delete
end
vim_command() click to toggle source
# File lib/vocco/generator.rb, line 72
def vim_command
  @vim_command ||= @vim.detect do |c|
    `which #{c}`.chomp.strip != ''
  end || abort("You must install one of #{@vim.inspect}.")
end
vimscript() click to toggle source
# File lib/vocco/generator.rb, line 78
def vimscript
  @vimscript ||= begin
    s = "let html_no_pre = 1\n"
    files.each do |file|
      s << "view! #{file.file}\n"
      s << "TOhtml\n"
      s << "w! #{file.doc_path}\n"
    end
    s << ":qall!\n"
  end
end