Class: Jekyll::ReduceTitleRedundancy::Generator

Inherits:
Generator
  • Object
show all
Defined in:
lib/reduce-title-redundancy/generator.rb

Constant Summary collapse

TITLE_REGEX =
%r!
    \A\s*
        (?:
            \#\s+
            (.*)
            (?:\s+\#)?
        )
    $
!x.freeze
CONVERTER_CLASS =
Jekyll::Converters::Markdown
STRIP_MARKUP_FILTERS =
[:markdownify, :strip_html, :normalize_whitespace].freeze
EXTRA_MARKUP_REGEX =

Regex to strip extra markup still present after markdownify (footnotes at the moment).

%r!\[\^[^\]]*\]!.freeze
CONFIG_KEY =
"reduce_title_redundancy"
ENABLED_KEY =
"enabled"
STRIP_TITLE_KEY =
"strip_title"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Generator

Returns a new instance of Generator.



26
27
28
29
30
# File 'lib/reduce-title-redundancy/generator.rb', line 26

def initialize(site)
    Jekyll.logger.debug("Hello:", "There")
    @site = site
    @title_is_generated = nil
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



4
5
6
# File 'lib/reduce-title-redundancy/generator.rb', line 4

def site
  @site
end

Instance Method Details

#empty_slug?(document) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/reduce-title-redundancy/generator.rb', line 63

def empty_slug?(document)
    return ((\
        document.data["slug"].nil?\
    ) or (\
        document.data["slug"] == ""\
    ))
end

#generate(site) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/reduce-title-redundancy/generator.rb', line 32

def generate(site)
    Jekyll.logger.debug("Hello:", "There")
    @site = site
    return if disabled?
    
    documents = site.pages + site.docs_to_write
    
    documents.each do |document|
        next if document.is_a?(Jekyll::StaticFile)
        @title_is_generated = nil
        
        document.data["title"] = title_for(document) if should_add_title?(document)
        strip_title!(document) if should_strip_title?(document)
        document.data["slug"] = slug_for(document) if should_add_slug?(document)
    end
end

#markdown?(document) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/reduce-title-redundancy/generator.rb', line 75

def markdown?(document)
    return markdown_converter.matches(document.extname)
end

#markdown_converterObject



79
80
81
82
# File 'lib/reduce-title-redundancy/generator.rb', line 79

def markdown_converter
    @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS)
    return @markdown_converter
end

#production_environment?Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/reduce-title-redundancy/generator.rb', line 58

def production_environment?()
    return false
    # return (ENV['JEKYLL_ENV'] || "development") == "production"
end

#should_add_slug?(document) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/reduce-title-redundancy/generator.rb', line 54

def should_add_slug?(document)
    return ((not production_environment?()) and empty_slug?(document))
end

#should_add_title?(document) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/reduce-title-redundancy/generator.rb', line 49

def should_add_title?(document)
    @title_is_generated = markdown?(document) && !title?(document)
    return @title_is_generated
end

#slug_for(document) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/reduce-title-redundancy/generator.rb', line 95

def slug_for(document)
    return Jekyll::Utils::slugify(
        document.data["title"],
        mode: "ascii",
    )
rescue ArgumentError => e
    raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
end

#title?(document) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/reduce-title-redundancy/generator.rb', line 71

def title?(document)
    return !inferred_title?(document) && !document.data["title"].nil?
end

#title_for(document) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/reduce-title-redundancy/generator.rb', line 84

def title_for(document)
    return document.data["title"] if title?(document)
    
    matches = document.content.to_s.match(TITLE_REGEX)
    return strip_markup(matches[1] || matches[2]) if matches
    
    return document.data["title"]
rescue ArgumentError => e
    raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
end