Class: JekyllTitlesFromContent::Generator

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

Constant Summary collapse

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_content"
ENABLED_KEY =
"enabled"
WORDS_KEY =
"words"
COLLECTIONS_KEY =
"collections"
DOTDOTDOT_KEY =
"dotdotdot"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Generator

Returns a new instance of Generator.



23
24
25
# File 'lib/jekyll-titles-from-content/generator.rb', line 23

def initialize(site)
  @site = site
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

#generate(site) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll-titles-from-content/generator.rb', line 27

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)
  end
end

#markdown?(document) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/jekyll-titles-from-content/generator.rb', line 50

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

#markdown_converterObject



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

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

#should_add_title?(document) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/jekyll-titles-from-content/generator.rb', line 42

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

#title?(document) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/jekyll-titles-from-content/generator.rb', line 46

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

#title_for(document) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/jekyll-titles-from-content/generator.rb', line 65

def title_for(document)
  return document.data["title"] if title?(document)

  first_line = document.content.split("\n").find { |l| l unless strip_markup(l).empty? }
  return truncate(strip_markup(first_line), count, dotdotdot) unless first_line.nil?

  document.data["title"] # If we can't produce 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

#truncate(string, count, cont = "") ⇒ Object



58
59
60
61
62
63
# File 'lib/jekyll-titles-from-content/generator.rb', line 58

def truncate(string, count, cont = "")
  spl = string.split
  tr = spl.first(count).join(" ")
  tr += cont if spl.length > count
  tr
end