Class: Jekyll::GitHubPagesSearch

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-github-pages-search.rb

Constant Summary collapse

VALID_KEYS =
%w(in path authors tags repo user highlight)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, opts, tokens) ⇒ GitHubPagesSearch

Returns a new instance of GitHubPagesSearch.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jekyll-github-pages-search.rb', line 24

def initialize(tag_name, opts, tokens)
  super
  @opts = opts.empty? ? [] : GitHubPagesSearch.extract_options(opts.strip.split(/\s+/).join(","))
  if @opts.empty? || (@opts[:repo].nil? && @opts[:user].nil?)
    raise ArgumentError, "You must include the option of either a :repo, or a :user"
  end
  js_file = File.read(File.join(File.dirname(__FILE__), "gh_api_search.js"))
  VALID_KEYS.each do |key|
    js_file = js_file.sub /\{\{ #{key} \}\}/, @opts[key.to_sym].nil? ? "''" : "'#{@opts[key.to_sym].join(",")}'"
  end

  @js = Uglifier.compile(js_file)
end

Class Method Details

.extract_options(input) ⇒ Object

from a string of options, construct a hash, without using ‘eval`



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jekyll-github-pages-search.rb', line 8

def self.extract_options( input )
  opts = Hash.new
  input.split(/,/).each do |entry|
    entryMap = entry.split(/:/)
    key = entryMap[0]
    next unless VALID_KEYS.include? key
    key = key.to_sym
    if opts[key].nil?
      opts[key] = []
    end
    value = entryMap[1]
    opts[key] << value.strip
  end
  opts
end

Instance Method Details

#render(context) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll-github-pages-search.rb', line 38

def render(context)
  <<-EOS
  <script type="text/javascript">
  #{@js}
  </script>
  <div id="search">
    <form action="/search" method="get">
      <input type="text" id="search-query" name="q" placeholder="Search" autocomplete="off">
    </form>
  </div>
  EOS
end