class Jekyll::ReduceTitleRedundancy::Generator

Constants

CONFIG_KEY
CONVERTER_CLASS
ENABLED_KEY
EXTRA_MARKUP_REGEX

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

STRIP_MARKUP_FILTERS
STRIP_TITLE_KEY
TITLE_REGEX

Attributes

site[RW]

Public Class Methods

new(site) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 26
def initialize(site)
    Jekyll.logger.debug("Hello:", "There")
    @site = site
    @title_is_generated = nil
end

Public Instance Methods

empty_slug?(document) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 75
def markdown?(document)
    return markdown_converter.matches(document.extname)
end
markdown_converter() click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 71
def title?(document)
    return !inferred_title?(document) && !document.data["title"].nil?
end
title_for(document) click to toggle source
# 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

Private Instance Methods

collections?() click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 136
def collections?
    return option(COLLECTIONS_KEY) == true
end
disabled?() click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 116
def disabled?
    return option(ENABLED_KEY) == false
end
filters() click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 155
def filters
    @filters ||= ReduceTitleRedundancy::Filters.new(site)
    return @filters
end
inferred_title?(document) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 140
def inferred_title?(document)
    return document.is_a?(Jekyll::Document)
end
option(key) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 112
def option(key)
    return site.config[CONFIG_KEY] && site.config[CONFIG_KEY][key]
end
should_strip_title?(document) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 120
def should_strip_title?(document)
    return false if not @title_is_generated
    
    if document.data.key?(STRIP_TITLE_KEY)
        return document.data[STRIP_TITLE_KEY] == true
    else
        return option(STRIP_TITLE_KEY) == true
    end
end
strip_markup(string) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 106
def strip_markup(string)
    STRIP_MARKUP_FILTERS.reduce(string) do |memo, method|
        filters.public_send(method, memo)
    end.gsub(EXTRA_MARKUP_REGEX, "")
end
strip_title!(document) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 144
def strip_title!(document)
    if document.content
        document.content = document.content.gsub(TITLE_REGEX, "").strip
        strip_title_excerpt!(document) if strip_title_excerpt?(document)
    end
end
strip_title_excerpt!(document) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 151
def strip_title_excerpt!(document)
    document.data["excerpt"] = Jekyll::Excerpt.new(document) if document.generate_excerpt?
end
strip_title_excerpt?(document) click to toggle source
# File lib/reduce-title-redundancy/generator.rb, line 130
def strip_title_excerpt?(document)
    return document.is_a?(Jekyll::Document) &&
        document.collection.label == "posts" &&
        document.generate_excerpt?
end