class JekyllTitlesFromHeadings::Generator

Constants

COLLECTIONS_KEY
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/jekyll-titles-from-headings/generator.rb, line 31
def initialize(site)
  @site = site
end

Public Instance Methods

generate(site) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 35
def generate(site)
  @site = site
  return if disabled?

  documents = site.pages
  documents = site.pages + site.docs_to_write if collections?

  documents.each do |document|
    next unless should_add_title?(document)
    next if document.is_a?(Jekyll::StaticFile)

    document.data["title"] = title_for(document)
    strip_title!(document) if strip_title?(document)
  end
end
markdown?(document) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 59
def markdown?(document)
  markdown_converter.matches(document.extname)
end
markdown_converter() click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 63
def markdown_converter
  @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS)
end
should_add_title?(document) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 51
def should_add_title?(document)
  markdown?(document) && !title?(document)
end
title?(document) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 55
def title?(document)
  !inferred_title?(document) && !document.data["title"].nil?
end
title_for(document) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 67
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

  document.data["title"] # If we cant match a title, we use the inferred one.
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/jekyll-titles-from-headings/generator.rb, line 108
def collections?
  option(COLLECTIONS_KEY) == true
end
disabled?() click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 90
def disabled?
  option(ENABLED_KEY) == false
end
filters() click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 129
def filters
  @filters ||= JekyllTitlesFromHeadings::Filters.new(site)
end
inferred_title?(document) click to toggle source

Documents (posts and collection items) have their title inferred from the filename. We want to override these titles, because they were not excplicitly set.

# File lib/jekyll-titles-from-headings/generator.rb, line 114
def inferred_title?(document)
  document.is_a?(Jekyll::Document)
end
option(key) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 86
def option(key)
  site.config[CONFIG_KEY] && site.config[CONFIG_KEY][key]
end
strip_markup(string) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 80
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/jekyll-titles-from-headings/generator.rb, line 118
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?(document) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 94
def strip_title?(document)
  if document.data.key?(STRIP_TITLE_KEY)
    document.data[STRIP_TITLE_KEY] == true
  else
    option(STRIP_TITLE_KEY) == true
  end
end
strip_title_excerpt!(document) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 125
def strip_title_excerpt!(document)
  document.data["excerpt"] = Jekyll::Excerpt.new(document)
end
strip_title_excerpt?(document) click to toggle source
# File lib/jekyll-titles-from-headings/generator.rb, line 102
def strip_title_excerpt?(document)
  document.is_a?(Jekyll::Document) &&
    document.collection.label == "posts" &&
    document.generate_excerpt?
end