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

Instance Method Details

#check_directory_and_config_file(dir = '', config = '') ⇒ Object

Check the existence of given directory and config

Parameters:

  • dir (String) (defaults to: '')

    The directory of the files.

  • config (String) (defaults to: '')

    The path to the config file.



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

Parameters:

  • pattern (String)

    The xpath regular for Nokogiri expressions after which the HTML is grabbed

  • html (String)

    An array to save the results

  • arr (Array)

    An array which stores all the findings produces by nokogiri

Returns:

  • (Array)

    The number of found results



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

Parameters:

  • arr (Array)

    An array with all the found html parts.

Returns:

  • (Integer)

    The number of uniq results in the given array.



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

Parameters:

  • pattern (String)

    The xpath regular for Nokogiri expressions after which the HTML is grabbed.

  • arr (Array)

    array to save the results.

  • dir (String)

    The main directory in which the search should be performed.

Returns:

  • (Integer)

    The number of found results.



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

Parameters:

  • pattern (String)

    need for nokogiri to parse the html page

  • arr (Array)

    array to save the results

  • page (String)

    The path to a file which will be taken for the search



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

Parameters:

  • pattern (String)

    The xpath regular for Nokogiri expressions after which the HTML is grabbed.

  • arr (Array)

    array to save the results.

  • dir (String)

    The directory in which the html files are stored.



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

Parameters:

  • file (String)

    The path to the config file

  • text (String)

    Is a multiline string of text

Returns:

  • (nil)


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