Class: AsciiDocFrontMatterLoader

Inherits:
Bridgetown::FrontMatter::Loaders::Base
  • Object
show all
Defined in:
lib/asciidoc_front_matter_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.header?(file) ⇒ Boolean

Tell Bridgetown to intercept .adoc and .asciidoc files before standard parsing

Returns:

  • (Boolean)


9
10
11
# File 'lib/asciidoc_front_matter_loader.rb', line 9

def self.header?(file)
  File.extname(file) =~ /^\.(adoc|asciidoc)$/i
end

.register!Object



4
5
6
# File 'lib/asciidoc_front_matter_loader.rb', line 4

def self.register!
  Bridgetown::FrontMatter::Loaders.register(self)
end

Instance Method Details

#read(file_contents, file_path: "") ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/asciidoc_front_matter_loader.rb', line 13

def read(file_contents, file_path: "")
  return nil unless self.class.header?(file_path)

  # Use Asciidoctor to parse only the header for performance
  doc = Asciidoctor.load(file_contents, parse_header_only: true)
  
  # Map native AsciiDoc attributes into Bridgetown's data structure
  front_matter = {
    "title"                      => doc.doctitle,
    "description"                => doc.attributes["description"],
    "image"                      => doc.attributes["image"],
    "author"                     => doc.attributes["author"],
    "lang"                       => doc.attributes["lang"],
    "categories"                 => doc.attributes["categories"],
    "category"                   => doc.attributes["category"],
    "date"                       => doc.attributes["date"],
    "id"                         => doc.attributes["id"],
    "layout"                     => doc.attributes["layout"] || "post",
    "paginate"                   => doc.attributes["paginate"],
    "permalink"                  => doc.attributes["permalink"],
    "published"                  => doc.attributes["published"],
    "sitemap_change_frequency"   => doc.attributes["sitemap_change_frequency"],
    "sitemap_priority"           => doc.attributes["sitemap_priority"],
    "slug"                       => doc.attributes["slug"],
    "tag"                        => doc.attributes["tag"],
    "tags"                       => doc.attributes["tags"],
    "template_engine"            => doc.attributes["template_engine"],
    "willamette"                 => doc.attributes["willamette"],
    "willamette.article_classes" => doc.attributes["willamette.article_classes"]
  }.compact

  Bridgetown::FrontMatter::Loaders::Result.new(
    content: file_contents,
    front_matter: front_matter,
    line_count: file_contents.lines.size
  )
end