Module: Sweetie::Helper

Included in:
Bitbucket, Conversion
Defined in:
lib/sweetie/helper.rb

Instance Method Summary collapse

Instance Method Details

#check_config_and_directory_file(config, dir) ⇒ Object

Check the existence of needed files for sweetie

Parameters:

  • the (config)

    _config.yml file

  • the (dir)

    directory of the generated jekyll page



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

Parameters:

  • important (pattern)

    for nokogiri

  • the (html)

    path for the html file

  • and (ar)

    array which stores all the findings produces by nokogiri



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

Parameters:

  • is (ar)

    an array with all the found html parts



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

Parameters:

  • need (pattern)

    for nokogiri to parse the html page

  • array (array)

    to save the results

  • the (dir)

    main directory in which the by jekyll generated files are stored



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

Parameters:

  • need (pattern)

    for nokogiri to parse the html page

  • array (array)

    to save the results

  • a (page)

    single page 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, 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

Parameters:

  • is (pattern)

    a string for nokogiri after which the html pages should be parsed

  • is (ar)

    an empty Array which is used by the harvest method

  • the (dir)

    directory in which the html files are stored



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

Parameters:

  • is (file)

    the _config.yml file of the jekyll project

  • is (text)

    a multiline string which will be written in the file



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