You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

69 lines
1.4 KiB

////
Included in:
- user-manual: Extensions: Preprocessor example
////
Purpose::
Skim off front matter from the top of the document that gets used by site generators like Jekyll and Awestruct.
.sample-with-front-matter.adoc
```
---
tags: [announcement, website]
---
= Document Title
content
[subs="attributes,specialcharacters"]
.Captured front matter
....
---
{front-matter}
---
....
```
.FrontMatterPreprocessor
```ruby
require 'asciidoctor'
require 'asciidoctor/extensions'
class FrontMatterPreprocessor < Asciidoctor::Extensions::Preprocessor
def process document, reader
lines = reader.lines # get raw lines
return reader if lines.empty?
front_matter = []
if lines.first.chomp == '---'
original_lines = lines.dup
lines.shift
while !lines.empty? && lines.first.chomp != '---'
front_matter << lines.shift
end
if (first = lines.first).nil? || first.chomp != '---'
lines = original_lines
else
lines.shift
document.attributes['front-matter'] = front_matter.join.chomp
# advance the reader by the number of lines taken
(front_matter.length + 2).times { reader.advance }
end
end
reader
end
end
```
.Usage
```ruby
Asciidoctor::Extensions.register do
preprocessor FrontMatterPreprocessor
end
Asciidoctor.convert_file 'sample-with-front-matter.adoc', :safe => :safe
```