class Juli::Macro::Tag

Register tags of this document for tag-search.

See 'doc/tag(macro).txt' for the detail how to use it. Here is the implementation document.

Tag-DB ER-chart

tag <—>> tag_page <<—> page

FILES

JURI_REPO/.juli/tag.sdbm

tag DB

JURI_REPO/.juli/page.sdbm

page DB

JURI_REPO/.juli/tag_page.sdbm

tag-page intersection DB

Constants

NO_TAG
SEPARATOR

Attributes

page_db[RW]
tag_db[RW]
tag_page_db[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Juli::Macro::Base::new
# File lib/juli/macro/tag.rb, line 27
def initialize
  super

  repo_dir      = File.join(juli_repo, Juli::REPO)
  @tag_db       = SDBM.open(File.join(repo_dir, 'tag.sdbm'))
  @page_db      = SDBM.open(File.join(repo_dir, 'page.sdbm'))
  @tag_page_db  = SDBM.open(File.join(repo_dir, 'tag_page.sdbm'))
end

Public Instance Methods

after_root(file, root) click to toggle source

follow-up process to register 'no-tag' if there is no tag in the file.

# File lib/juli/macro/tag.rb, line 58
def after_root(file, root)
  key = sprintf("%s%s%s", @wikiname, SEPARATOR, NO_TAG)
  if @tag_exists
    @tag_page_db.delete(key)
  else
    @tag_page_db[key] = '1'
  end
end
delete_page(file) click to toggle source

delete entry from DB

# File lib/juli/macro/tag.rb, line 96
def delete_page(file)
  wikiname = Juli::Util::to_wikiname(file)
  @page_db.delete(wikiname)

  tag_on_the_file = {}
  for tag, val in @tag_db.keys do
    if @tag_page_db[tag_page_key(tag, wikiname)]
      tag_on_the_file[tag] = 1
    end
  end

  # -1 on tag
  for tag in tag_on_the_file.keys do
    @tag_db[tag]  = ((@tag_db[tag] || '1').to_i - 1).to_s
  end
end
dump() click to toggle source

print DB info; debugging purpose. How to use:

$ test/console
> Dir.chdir('../test/repo')
> include Juli::Util
> t = Juli::Macro::Tag.new
> t.dump
# File lib/juli/macro/tag.rb, line 120
def dump
  for db in %w(tag_db page_db tag_page_db) do
    printf("%s\n", db)
    for key, val in instance_variable_get('@' + db) do
      printf("  %s\t%s\n", key, val)
    end
    print "\n"
  end
end
max_tag_weight() click to toggle source
# File lib/juli/macro/tag.rb, line 85
def max_tag_weight
  @tag_db.values.map{|v| v.to_i}.max || 1
end
on_root(file, root, visitor = nil) click to toggle source

register page

# File lib/juli/macro/tag.rb, line 37
def on_root(file, root, visitor = nil)
  @wikiname           = Juli::Util::to_wikiname(file)
  @page_db[@wikiname] = '1'
  @tag_exists         = false
end
pages(key) click to toggle source

return pages associated with key

# File lib/juli/macro/tag.rb, line 75
def pages(key)
  result = []
  for tag_page, val in @tag_page_db do
    if to_utf8(tag_page) =~ /^(.*)#{SEPARATOR}#{key}$/
      result << $1
    end
  end
  result
end
run(*args) click to toggle source
# File lib/juli/macro/tag.rb, line 43
def run(*args)
  for tag in args do
    @tag_exists   = true

    # +1 on tag
    @tag_db[tag]  = ((@tag_db[tag] || '0').to_i + 1).to_s
    if @wikiname
      @tag_page_db[tag_page_key(tag, @wikiname)] = '1'
    end
  end
  ''
end
tag_weight_ratio(key) click to toggle source

return 0..10 in tag weight v.s. max-weight

# File lib/juli/macro/tag.rb, line 90
def tag_weight_ratio(key)
  v = (@tag_db[key] || '0').to_i
  (v * 10 / max_tag_weight).to_i
end
to_utf8(v) click to toggle source

value in sdbm in ruby 1.9 looks not to support encoding (in other words, always set to ASCII-8BIT) so that enforce to set it to UTF-8.

# File lib/juli/macro/tag.rb, line 70
def to_utf8(v)
  v.force_encoding(Encoding::UTF_8)
end

Private Instance Methods

tag_page_key(tag, wikiname) click to toggle source
# File lib/juli/macro/tag.rb, line 131
def tag_page_key(tag, wikiname)
  sprintf("%s%s%s", wikiname, SEPARATOR, tag)
end