Class: Jekyll::CurseWords

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-curse.rb

Constant Summary collapse

@@not_allowed_extensions =
[
  '.scss',
  '.less',
  '.xml',
  '.js',
  '.coffee'
]
@@replace_chars =
['*', '$', '%', '!', '#', '&']

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll-curse.rb', line 15

def generate(site)
  # Load up the yaml file containing all the curse words
  load_words

  # Remove curse words in all pages
  site.pages.each do |page|
    not_allowed = page_not_allowed(page.name)

    page.content = remove_curse_word(page.content) if !not_allowed
  end

  # Remove curse words in all pots
  site.posts.each do |post|
    not_allowed = page_not_allowed(post.name)

    post.content = remove_curse_word(post.content) if !not_allowed
  end
end

#load_wordsObject

Private: Loads the YAML file with the curse words



35
36
37
# File 'lib/jekyll-curse.rb', line 35

def load_words
  @@curse_words = YAML::load_file(File.join(__dir__, 'curse-words.yml'))['words']
end

#mask_curse_word(word) ⇒ Object

Private: Masks the curse word found

word - Found curse word

Returns the masked curse word



74
75
76
# File 'lib/jekyll-curse.rb', line 74

def mask_curse_word(word)
  "#{word[0]}#{replace_letters(word[1..-2])}#{word[-1]}"
end

#page_not_allowed(name) ⇒ Object

Private: Checks if the page should be checked based on not allowed extensions

name - The name of the page

Returns true/false on whether the page is allowed



44
45
46
47
48
# File 'lib/jekyll-curse.rb', line 44

def page_not_allowed(name)
  @@not_allowed_extensions.any? do |word|
    name.include?(word)
  end
end

#remove_curse_word(content) ⇒ Object

Private: Removes any of the curse words from the content

content - The page content

Returns the content minus any curse words



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jekyll-curse.rb', line 55

def remove_curse_word(content)
  # Search for any of the curse words
  @@curse_words.each do |word|
    regex = /#{word}/i
    if found = content.match(regex)
      # Mask the curse word
      content = content.gsub!("#{found}", mask_curse_word(found.to_s))
      puts content
    end
  end

  content
end

#replace_letters(letters) ⇒ Object

Private: Replace letters with random characters

letter - Letters to replace

Returns the letters replace with chars



83
84
85
86
87
88
89
# File 'lib/jekyll-curse.rb', line 83

def replace_letters(letters)
  l = letters.split('').map do |letter|
    @@replace_chars.sample
  end

  l.join('')
end