class StaticSearch::IndexBuilder

Public Instance Methods

build(pages_path, options = {}) click to toggle source
# File lib/static_search/index_builder.rb, line 8
def build(pages_path, options = {})
  Dir["#{pages_path}/**/*"].each do |fname|
    unless File.directory? fname
      erb      = parse_file fname
      filename = parse_title fname
      text     = parse_content(erb, options[:keep_tags] || {})
      save_content(text, filename)
    end
  end
  puts "-- Completed" if production?
end
parse_content(erb, options = {}) click to toggle source
# File lib/static_search/index_builder.rb, line 33
def parse_content (erb, options = {})
  html = Nokogiri.HTML erb.result
  html.css('script').remove
  if options[:keep_tags]
    html.css("body")
  else
    html.css("body")
        .text
        .gsub("\n"," ")
        .gsub(/(\s{2,})/," ")
        .strip
  end
end
parse_file(fname) click to toggle source
# File lib/static_search/index_builder.rb, line 28
def parse_file(fname)
  file = File.read(fname)
  ERB.new file, nil, "%"
end
parse_title(fname) click to toggle source
# File lib/static_search/index_builder.rb, line 47
def parse_title(fname)
  filename = fname.split("/pages/").last
  if filename.match(/\./)
    return filename.split(".").first
  else
    return filename
  end
end
save_content(text, title) click to toggle source
# File lib/static_search/index_builder.rb, line 20
def save_content (text, title)
  puts "Indexing #{title} page" if production?
  static_content = StaticContent.find_or_initialize_by(title: title)
  static_content.update content: text
  return static_content
end

Private Instance Methods

production?() click to toggle source
# File lib/static_search/index_builder.rb, line 58
def production?
  defined?(Rails) || defined?(Rake)
end