diff options
| author | Sarah White <graphitefriction@gmail.com> | 2020-12-18 16:56:51 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-18 16:56:51 -0700 |
| commit | fffa2ed16cd1be9f7f6bb7356e1eda83d47fc972 (patch) | |
| tree | 0dda9ef8df40bbe8087b66bfadaee5cf948d1571 /docs/modules/extensions/pages/preprocessor.adoc | |
| parent | 47c5bf28ee16f598bff7f31901437c3a193ee685 (diff) | |
| parent | 717a1cbd6b7c9af192325b76d96ae5e86aeeb595 (diff) | |
merge PR #3880
resolves #3861 import Asciidoctor docs from asciidoctor.org/docs
Diffstat (limited to 'docs/modules/extensions/pages/preprocessor.adoc')
| -rw-r--r-- | docs/modules/extensions/pages/preprocessor.adoc | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/docs/modules/extensions/pages/preprocessor.adoc b/docs/modules/extensions/pages/preprocessor.adoc new file mode 100644 index 00000000..d1cc95f1 --- /dev/null +++ b/docs/modules/extensions/pages/preprocessor.adoc @@ -0,0 +1,68 @@ += Preprocessor Extension Example +:navtitle: Preprocessor + +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 + +[source,asciidoc] +---- +tags: [announcement, website] +--- += Document Title + +content + +[subs=+attributes] +.Captured front matter +.... +--- +{front-matter} +--- +.... +---- + +== FrontMatterPreprocessor + +[source,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 + +[source,ruby] +---- +Asciidoctor::Extensions.register do + preprocessor FrontMatterPreprocessor +end + +Asciidoctor.convert_file 'sample-with-front-matter.adoc', safe: :safe +---- |
