Class: Middleman::Blog::Similar::Groonga

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-blog-similar-groonga.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ Groonga

Returns a new instance of Groonga.



9
10
11
12
13
14
15
16
# File 'lib/middleman-blog-similar-groonga.rb', line 9

def initialize(app, options_hash={}, &block)
  super

  app.config[:blog_similar_groonga] = self

  @db_path = Pathname.new(options.db_path)
  require 'groonga'
end

Instance Method Details

#after_configurationObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/middleman-blog-similar-groonga.rb', line 18

def after_configuration
  if @db_path.file?
    ::Groonga::Database.open @db_path.to_path
  else
    @db_path.dirname.mkpath unless @db_path.dirname.directory?
    ::Groonga::Database.create(path: @db_path.to_path)
    ::Groonga::Schema.create_table 'Articles', type: :hash do |table|
      table.short_text 'title'
      table.long_text 'body'
      # TODO: tags
    end
    ::Groonga::Schema.create_table 'Terms', type: :patricia_trie, normalizer: :NormalizerAuto, default_tokenizer: 'TokenMecab'
    ::Groonga::Schema.change_table 'Terms' do |table|
      table.index 'Articles.body'
    end
  end

  articles = ::Groonga['Articles']
  app.sitemap.resources.each do |resource|
    next unless resource.kind_of? ::Middleman::Blog::BlogArticle

    key = resource.path
    articles.add key unless articles[key]

    articles[key].title = resource.title
    articles[key].body = Nokogiri.HTML('<body>' + resource.body + '</body>').search('body').first.content
  end
end