Class: JekyllTitlesFromHeadings::Generator
- Inherits:
-
Jekyll::Generator
- Object
- Jekyll::Generator
- JekyllTitlesFromHeadings::Generator
- Defined in:
- lib/jekyll-titles-from-headings/generator.rb
Constant Summary collapse
- TITLE_REGEX =
%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 =
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!\[\^[^\]]*\]!
- HOMEPAGE_REGEX =
Regex to determine if the given document is the site’s homepage
%r!^/(index.html?)?$!
Instance Attribute Summary collapse
-
#site ⇒ Object
Returns the value of attribute site.
Instance Method Summary collapse
- #generate(site) ⇒ Object
-
#initialize(site) ⇒ Generator
constructor
A new instance of Generator.
- #markdown?(document) ⇒ Boolean
- #markdown_converter ⇒ Object
- #should_add_title?(document) ⇒ Boolean
- #title?(document) ⇒ Boolean
- #title_for(document) ⇒ Object
Constructor Details
#initialize(site) ⇒ Generator
Returns a new instance of Generator.
29 30 31 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 29 def initialize(site) @site = site end |
Instance Attribute Details
#site ⇒ Object
Returns the value of attribute site.
3 4 5 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 3 def site @site end |
Instance Method Details
#generate(site) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 33 def generate(site) @site = site site.pages.each do |document| next unless should_add_title?(document) document.data["title"] = title_for(document) end end |
#markdown?(document) ⇒ Boolean
50 51 52 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 50 def markdown?(document) markdown_converter.matches(document.extname) end |
#markdown_converter ⇒ Object
54 55 56 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 54 def markdown_converter @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS) end |
#should_add_title?(document) ⇒ Boolean
42 43 44 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 42 def should_add_title?(document) markdown?(document) && !title?(document) && !homepage?(document) end |
#title?(document) ⇒ Boolean
46 47 48 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 46 def title?(document) !document.data["title"].nil? end |
#title_for(document) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 58 def title_for(document) return document.data["title"] if title?(document) matches = document.content.match(TITLE_REGEX) strip_markup(matches[1] || matches[2]) if matches rescue ArgumentError => e raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8") end |