Class: Jekyll::GenerateTags::Generator
- Inherits:
-
Object
- Object
- Jekyll::GenerateTags::Generator
- Defined in:
- lib/jekyll-generate-tags/generator.rb
Constant Summary collapse
- @@hash_key =
"cache-key"- @@cache_folder_name =
".tags-cache"- @@tag_options_name =
"tag_options.txt"- @@default_confidence =
".50"
Instance Method Summary collapse
- #create_cache_folder ⇒ Object
- #generate_tags(content, tags, confidence) ⇒ Object
-
#get_cache(key) ⇒ Object
Retrieves existing generated tags from a cache file for fast reuse.
- #get_cache_file_path(key) ⇒ Object
-
#set_cache(key, value) ⇒ Object
Stores newly generated tags in a file for fast reuse.
Instance Method Details
#create_cache_folder ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/jekyll-generate-tags/generator.rb', line 71 def create_cache_folder() cache_folder_path = File.(@@cache_folder_name) # Check if the folder exists if !Dir.exists?(cache_folder_path) # Create the folder FileUtils.mkdir_p(cache_folder_path) end end |
#generate_tags(content, tags, confidence) ⇒ Object
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 46 47 48 49 50 |
# File 'lib/jekyll-generate-tags/generator.rb', line 18 def (content, , confidence) # Do tags exist in `_config.yml`? if .nil? # Look for tags in project root = File.() if !File.exists?() # Use default tags from gem = File.(, __dir__) end = File.read() lines = .split("\n") = lines.join(",") end if confidence.nil? confidence = @@default_confidence end cache_key = content + + String(confidence) result = self.get_cache(cache_key) if result.nil? script_path = File.("generate.py", __dir__) arg1 = Shellwords.escape(content) arg2 = Shellwords.escape() arg3 = Shellwords.escape(String(confidence)) result = `python #{script_path} #{arg1} #{arg2} #{arg3}` self.set_cache(cache_key, result) end result end |
#get_cache(key) ⇒ Object
Retrieves existing generated tags from a cache file for fast reuse
62 63 64 65 66 67 68 69 |
# File 'lib/jekyll-generate-tags/generator.rb', line 62 def get_cache(key) cache_path = get_cache_file_path(key) # Retrieve value from file if File.exists?(cache_path) result = File.read(cache_path) result end end |
#get_cache_file_path(key) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/jekyll-generate-tags/generator.rb', line 80 def get_cache_file_path(key) create_cache_folder() # Create the HMAC hmac = OpenSSL::HMAC.hexdigest('sha256', @@hash_key, key) cache_path = File.("#{@@cache_folder_name}/#{hmac}.txt") cache_path end |
#set_cache(key, value) ⇒ Object
Stores newly generated tags in a file for fast reuse
53 54 55 56 57 58 59 |
# File 'lib/jekyll-generate-tags/generator.rb', line 53 def set_cache(key, value) cache_path = get_cache_file_path(key) # Store file File.open(cache_path, "w") do |file| file.puts(value) end end |