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+(.*)(?:\s+\#{1,3})? # atx-style header | # or (.*)\r?\n[-=]+\s* # Setex-style header )$ # end of line !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 =
"titles_from_headings"
- ENABLED_KEY =
"enabled"
- STRIP_TITLE_KEY =
"strip_title"
- COLLECTIONS_KEY =
"collections"
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.
31 32 33 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 31 def initialize(site) @site = site end |
Instance Attribute Details
#site ⇒ Object
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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Boolean
59 60 61 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 59 def markdown?(document) markdown_converter.matches(document.extname) end |
#markdown_converter ⇒ Object
63 64 65 |
# 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) ⇒ Boolean
51 52 53 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 51 def should_add_title?(document) markdown?(document) && !title?(document) end |
#title?(document) ⇒ Boolean
55 56 57 |
# File 'lib/jekyll-titles-from-headings/generator.rb', line 55 def title?(document) !inferred_title?(document) && !document.data["title"].nil? end |
#title_for(document) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# 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 |