Class: JekyllTitlesFromHeadings::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-titles-from-headings/generator.rb

Constant Summary collapse

TITLE_REGEX =

rubocop:disable Lint/InterpolationCheck

%r!
  \A\s*                   # Beginning and whitespace
    (?:                   # either
      \#{1,3}\s+(.*)      # atx-style header
      |                   # or
      (.*)\r?\n[-=]+\s*   # Setex-style header
    )$                    # end of line
!x
CONVERTER_CLASS =

rubocop:enable Lint/InterpolationCheck

Jekyll::Converters::Markdown
STRIP_MARKUP_FILTERS =
%i[
  markdownify strip_html normalize_whitespace
].freeze
EXTRA_MARKUP_REGEX =

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Generator

Returns a new instance of Generator.



35
36
37
# File 'lib/jekyll-titles-from-headings/generator.rb', line 35

def initialize(site)
  @site = site
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



5
6
7
# File 'lib/jekyll-titles-from-headings/generator.rb', line 5

def site
  @site
end

Instance Method Details

#generate(site) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jekyll-titles-from-headings/generator.rb', line 39

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) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/jekyll-titles-from-headings/generator.rb', line 62

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

#markdown_converterObject



66
67
68
# File 'lib/jekyll-titles-from-headings/generator.rb', line 66

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

#should_add_title?(document) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/jekyll-titles-from-headings/generator.rb', line 54

def should_add_title?(document)
  markdown?(document) && !title?(document)
end

#title?(document) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/jekyll-titles-from-headings/generator.rb', line 58

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

#title_for(document) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/jekyll-titles-from-headings/generator.rb', line 70

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