Module: Sweetie::Helper
- Included in:
- Bitbucket, BitbucketStatiHelper, Conversion
- Defined in:
- lib/sweetie/helper.rb
Overview
The Helper module which can get the stati of the project including number of html pages, images, and links
Instance Method Summary collapse
-
#check_directory_and_config_file(dir = '', config = '') ⇒ Object
Check the existence of given directory and config.
-
#harvest(pattern, html, arr) ⇒ Array
Traverse each html page and gather information about the specified html element.
-
#output_count(arr) ⇒ Integer
Count the elements.
-
#perform_global_search(pattern, arr, dir) ⇒ Integer
Traverse the dir after the pattern and return the number of occurences in the pages.
-
#perform_search_for_single_page(pattern, arr, page) ⇒ Object
Traverse the page after the pattern and return the number of occurences on it.
-
#traverse(pattern, arr, dir) ⇒ Object
Traverse the jekyll directory and get the information about a specific pattern.
-
#write_config(file, text) ⇒ nil
Write in the file the text.
Instance Method Details
#check_directory_and_config_file(dir = '', config = '') ⇒ Object
Check the existence of given directory and config
83 84 85 86 87 |
# File 'lib/sweetie/helper.rb', line 83 def check_directory_and_config_file(dir = '', config = '') if !Dir.exist?(dir) || !File.exist?(config) raise "Can't find the _config.yml or the _site directory! Please create these files it!" end end |
#harvest(pattern, html, arr) ⇒ Array
Traverse each html page and gather information about the specified html element
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sweetie/helper.rb', line 20 def harvest(pattern, html, arr) file = File.open(html) doc = Nokogiri::HTML(file) doc.xpath(pattern).each do |node| if pattern == '//a' arr << node.text elsif pattern == '//img' && arr.include?(node.to_s) elsif pattern == '//img' arr << node.to_s elsif pattern == '//html' arr << node end end arr end |
#output_count(arr) ⇒ Integer
Count the elements
40 41 42 |
# File 'lib/sweetie/helper.rb', line 40 def output_count(arr) arr.uniq.count # remove duplicates with uniq end |
#perform_global_search(pattern, arr, dir) ⇒ Integer
Traverse the dir after the pattern and return the number of occurences in the pages
50 51 52 53 |
# File 'lib/sweetie/helper.rb', line 50 def perform_global_search(pattern, arr, dir) traverse(pattern, arr, dir) output_count(arr) end |
#perform_search_for_single_page(pattern, arr, page) ⇒ Object
Traverse the page after the pattern and return the number of occurences on it
9 10 11 12 |
# File 'lib/sweetie/helper.rb', line 9 def perform_search_for_single_page(pattern, arr, page) harvest(pattern, page, arr) output_count(arr) end |
#traverse(pattern, arr, dir) ⇒ Object
Traverse the jekyll directory and get the information about a specific pattern
60 61 62 63 64 65 |
# File 'lib/sweetie/helper.rb', line 60 def traverse(pattern, arr, dir) Dir.glob(dir + '/**/*') do |file| next if file == '.' || file == '..' || file.include?('html~') harvest(pattern, file, arr) if file =~ /(.*).html/ end end |
#write_config(file, text) ⇒ nil
Write in the file the text
72 73 74 75 76 77 |
# File 'lib/sweetie/helper.rb', line 72 def write_config(file, text) File.open(file, 'w') do |file| file.puts text file.close end end |