Class: Sweetie::Conversion

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/sweetie/conversion.rb

Overview

The class which can get the stati of the project including number of html pages, images, and links

Instance Method Summary collapse

Methods included from Helper

#check_directory_and_config_file, #harvest, #output_count, #perform_global_search, #perform_search_for_single_page, #traverse, #write_config

Constructor Details

#initialize(dir = '_site', config = '_config.yml') ⇒ nil

A basic initialize method.

Parameters:

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

    The directoy in which should search after links, images and number of HTML pages

  • config (String) (defaults to: '_config.yml')

    The name of the config file in which the results should be written



14
15
16
17
# File 'lib/sweetie/conversion.rb', line 14

def initialize(dir = '_site', config = '_config.yml')
  @dir = dir
  @config = config
end

Instance Method Details

#build_timeString

Create the actual build time.

Returns:

  • (String)

    in the date format mm-dd-yyyy



106
107
108
109
# File 'lib/sweetie/conversion.rb', line 106

def build_time
  time = Time.now
  "#{time.month}-#{time.day}-#{time.year}"
end

#count_all_html_pages(dir) ⇒ Integer

Counts all html pages for the given directory.

Parameters:

  • dir (String)

    The path of the directory

Returns:

  • (Integer)

    The number of unique html pages



83
84
85
# File 'lib/sweetie/conversion.rb', line 83

def count_all_html_pages(dir)
  perform_global_search('//html', [], dir)
end

#count_all_images(dir) ⇒ Integer

Counts all the images for the given directory.

Parameters:

  • dir (String)

    The path of the directory

Returns:

  • (Integer)

    The number of all images for the given dir



99
100
101
# File 'lib/sweetie/conversion.rb', line 99

def count_all_images(dir)
  perform_global_search('//img', [], dir)
end

Counts all the links for the given directory.

Parameters:

  • dir (String)

    The path of the directory

Returns:

  • (Integer)

    The number of unique links for the given dir



91
92
93
# File 'lib/sweetie/conversion.rb', line 91

def count_all_links(dir)
  perform_global_search('//a', [], dir)
end

#count_images_of_one_page(page) ⇒ Integer

Count the images of one html page.

Parameters:

  • page (String)

    The path of a html page

Returns:

  • (Integer)

    The number of unique images for the given page



75
76
77
# File 'lib/sweetie/conversion.rb', line 75

def count_images_of_one_page(page)
  perform_search_for_single_page('//img', [], page)
end

Counts the link of on html page.

Parameters:

  • page (String)

    The path of a html page

Returns:

  • (Integer)

    The number of unique links for the given page



67
68
69
# File 'lib/sweetie/conversion.rb', line 67

def count_link_of_one_page(page)
  perform_search_for_single_page('//a', [], page)
end

#create_statinil

It saves the gathered information about the build-date, the links, the images, and the number of html-pages in the jekyll project.

Returns:

  • (nil)


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
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sweetie/conversion.rb', line 23

def create_stati
  check_directory_and_config_file(@dir, @config)

  file = File.open(@config)
  text = ''
  if File.extname(file) =~ /.rb/
    while line = file.gets
      text << if line =~ /set :build,/
                "set :build, '#{build_time}'\n"
              elsif line =~ /set :htmlpages,/
                "set :htmlpages, #{count_all_html_pages(@dir)}\n"
              elsif line =~ /set :images,/
                "set :images, #{count_all_images(@dir)}\n"
              elsif line =~ /set :links,/
                "set :links, #{count_all_links(@dir)}\n"
              else
                line
              end
    end
  else
    while line = file.gets
      text << if line =~ /build:/
                "build: '#{build_time}'\n"
              elsif line =~ /htmlpages:/
                "htmlpages: #{count_all_html_pages(@dir)}\n"
              elsif line =~ /images:/
                "images: #{count_all_images(@dir)}\n"
              elsif line =~ /links:/
                "links: #{count_all_links(@dir)}\n"
              else
                line
              end
    end
  end

  file.close

  write_config(file, text)
end