Class: Juli::Macro::Tag
Overview
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
-
tag DB value counts number of wikipages
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
Constant Summary collapse
- SEPARATOR =
'_, _'
- NO_TAG =
'_no_tag_'
Instance Attribute Summary collapse
-
#page_db ⇒ Object
Returns the value of attribute page_db.
-
#tag_db ⇒ Object
Returns the value of attribute tag_db.
-
#tag_page_db ⇒ Object
Returns the value of attribute tag_page_db.
Instance Method Summary collapse
-
#after_root(file, root) ⇒ Object
follow-up process to register ‘no-tag’ if there is no tag in the file.
-
#delete_page(file) ⇒ Object
delete entry from DB.
-
#dump ⇒ Object
print DB info; debugging purpose.
-
#initialize ⇒ Tag
constructor
A new instance of Tag.
- #max_tag_weight ⇒ Object
-
#on_root(file, root, visitor = nil) ⇒ Object
register page.
-
#pages(key) ⇒ Object
return pages associated with key.
- #run(*args) ⇒ Object
-
#tag_weight_ratio(key) ⇒ Object
return 0..10 in tag weight v.s.
-
#to_utf8(v) ⇒ Object
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.
Methods inherited from Base
conf_template, #set_conf_default
Methods included from Util
#camelize, conf, find_template, in_filename, juli_repo, mkdir, out_filename, str_limit, str_trim, to_wikiname, underscore, usage, visitor, visitor_list
Constructor Details
#initialize ⇒ Tag
Returns a new instance of Tag.
27 28 29 30 31 32 33 34 |
# 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 |
Instance Attribute Details
#page_db ⇒ Object
Returns the value of attribute page_db.
25 26 27 |
# File 'lib/juli/macro/tag.rb', line 25 def page_db @page_db end |
#tag_db ⇒ Object
Returns the value of attribute tag_db.
25 26 27 |
# File 'lib/juli/macro/tag.rb', line 25 def tag_db @tag_db end |
#tag_page_db ⇒ Object
Returns the value of attribute tag_page_db.
25 26 27 |
# File 'lib/juli/macro/tag.rb', line 25 def tag_page_db @tag_page_db end |
Instance Method Details
#after_root(file, root) ⇒ Object
follow-up process to register ‘no-tag’ if there is no tag in the file.
58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
delete entry from DB
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# 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 ⇒ Object
print DB info; debugging purpose. How to use:
$ test/console
> Dir.chdir('../test/repo')
> include Juli::Util
> t = Juli::Macro::Tag.new
> t.dump
120 121 122 123 124 125 126 127 128 |
# 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 ⇒ Object
85 86 87 |
# 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) ⇒ Object
register page
37 38 39 40 41 |
# 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) ⇒ Object
return pages associated with key
75 76 77 78 79 80 81 82 83 |
# 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) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
return 0..10 in tag weight v.s. max-weight
90 91 92 93 |
# 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) ⇒ Object
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.
70 71 72 |
# File 'lib/juli/macro/tag.rb', line 70 def to_utf8(v) v.force_encoding(Encoding::UTF_8) end |