Module: Sweetie::Helper
- Included in:
- Bitbucket, Conversion
- Defined in:
- lib/sweetie/helper.rb
Instance Method Summary collapse
-
#check_config_and_directory_file(config, dir) ⇒ Object
Check the existence of needed files for sweetie.
-
#harvest(pattern, html, ar) ⇒ Object
Traverse each html page and gather information about the specified html element.
-
#output_count(ar) ⇒ Object
Count the elements.
-
#perform_global_search(pattern, array, dir) ⇒ Object
Traverse the dir after the pattern and return the number of occurences in the pages.
-
#perform_search_for_single_page(pattern, array, page) ⇒ Object
Traverse the page after the pattern and return the number of occurences on it.
-
#traverse(pattern, ar, dir) ⇒ Object
Traverse the jekyll directory and get the information about a specific pattern.
-
#write_config(file, text) ⇒ Object
Write in the file the text.
Instance Method Details
#check_config_and_directory_file(config, dir) ⇒ Object
Check the existence of needed files for sweetie
76 77 78 79 80 |
# File 'lib/sweetie/helper.rb', line 76 def check_config_and_directory_file(config, dir) if !File.exist? config or !Dir.exist? dir raise "Can't find the _config.yml or the _site directory! Please create these files it!" end end |
#harvest(pattern, html, ar) ⇒ Object
Traverse each html page and gather information about the specified html element
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sweetie/helper.rb', line 40 def harvest(pattern, html, ar) file = File.open(html) doc = Nokogiri::HTML(file) doc.xpath(pattern).each do |node| if pattern == "//a" ar << node.text elsif pattern == "//img" and ar.include?(node.to_s) elsif pattern == "//img" ar << node.to_s elsif pattern == "//html" ar << node else end end ar end |
#output_count(ar) ⇒ Object
Count the elements
59 60 61 |
# File 'lib/sweetie/helper.rb', line 59 def output_count(ar) ar.uniq.count # remove duplicates with uniq end |
#perform_global_search(pattern, array, dir) ⇒ Object
Traverse the dir after the pattern and return the number of occurences in the pages
18 19 20 21 |
# File 'lib/sweetie/helper.rb', line 18 def perform_global_search(pattern, array, dir) traverse(pattern, array, dir) output_count(array) end |
#perform_search_for_single_page(pattern, array, 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, array, page) harvest(pattern, page, array) output_count(array) end |
#traverse(pattern, ar, dir) ⇒ Object
Traverse the jekyll directory and get the information about a specific pattern
27 28 29 30 31 32 33 34 |
# File 'lib/sweetie/helper.rb', line 27 def traverse(pattern, ar, dir) Dir.glob(dir+"/**/*") do |file| next if file == '.' or file == '..' or file.include?("html~") if file.match(/(.*).html/) harvest(pattern, file, ar) end end end |
#write_config(file, text) ⇒ Object
Write in the file the text
66 67 68 69 70 71 |
# File 'lib/sweetie/helper.rb', line 66 def write_config(file, text) File.open(file, 'w') do |file| file.puts text file.close end end |