asciidoctor.org
asciidoctor.org copied to clipboard
FrontMatterPreprocessor example -- problem runing it
It's no doubt just me, but I'll be the test case for the unsophisticated user:-) I tried following FrontMatterPreprocessor
example (section 82.1 of the User Manual. Here is the first try, with the
error in the comments.
TRY #1
require 'asciidoctor'
require 'asciidoctor/extensions'
# ERROR: te.rb:5:in `block in <main>': uninitialized constant FrontMatterPreprocessor (NameError)
Asciidoctor::Extensions.register do |document|
preprocessor FrontMatterPreprocessor
end
Asciidoctor.render_file 'sample-with-front-matter.ad', :safe => :safe, :in_place => true
Of course! How could ruby know where FrontMatterPreprocessor
is?
@jxxcarlson.noob_rating += 1
So I tried the below: But this time it doesn't know about preprocessor
.
I tried a few more variants, all unsuccessful.
@jxxcarlson.noob_rating += 1
I must be missing some really simple but really important line of code.
*TRY #2#
# file te2.rb
require 'asciidoctor'
require 'asciidoctor/extensions'
# ERROR A: te.rb:5:in `block in <main>': uninitialized constant FrontMatterPreprocessor (NameError)
# ERROR B: te2.rb:31:in `block in <main>': undefined method `preprocessor' for main:Object (NoMethodError)
class FrontMatterPreprocessor < Asciidoctor::Extensions::Preprocessor
def process reader, 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
Asciidoctor::Extensions.register do |document|
preprocessor FrontMatterPreprocessor
end
Asciidoctor.render_file 'sample-with-front-matter.ad', :safe => :safe, :in_place => true
Looking through the tests (e.g, asciidoctor / test / extensions_test.rb
, line 227), it seems
that one has to do something like this:
registry = Asciidoctor::Extensions::Registry.new
Asciidoctor::Extensions.register do |document|
registry.preprocessor FrontMatterPreprocessor
end
in place of the bare
Asciidoctor::Extensions.register do |document|
preprocessor FrontMatterPreprocessor
end
The above runs, but the extensions are not invoked. Here is what works:
Extensions.register do
preprocessor TeXPreprocessor
postprocessor EntToUni
end
Whew!!
I apologize for the documentation being out of date. It was taken from the 0.1.4 release post and not really updated after we change the API in 1.5.0. I'll update the examples so they work.
When in doubt, for now, consult the examples in the extensions lab.
I've updated the section. See if that gets you going.
Keep in mind that you don't have to run Asciidoctor via the API to load the extensions. You can use the -r
flag with the asciidoctor
command to require the file that contains the extension register code. The examples in the docs don't reflect that yet.
Thanks so much! In the meantime I found a workable-for-now solution and have the basics of the toolchain working — just pushed it to my asciidoctor-latex. It can be tested with the file asciidoctor-latex / tests / env_test.rb using
$ ruby env_test.rb —html
or
$ ruby env_test.rb —tex
The source file or the tests is samples/env.adoc.
For the fist test, there is work to do with templates, but the html backend now handles the new environment block. For the latter, I’ve set it up for my paths. I will try to put a better solution so other people can more easily test it. Still finding my way around. But I am amazed by Asciidoctors power and versatility. Very good magic at work!
On Nov 12, 2014, at 4:50 PM, Dan Allen [email protected] wrote:
I've updated the section. See if that gets you going.
Keep in mind that you don't have to run Asciidoctor via the API to load the extensions. You can use the -r flag with the asciidoctor command to require the file that contains the extension register code. The examples in the docs don't reflect that yet.
— Reply to this email directly or view it on GitHub.
Excellent! I'll give it a try asap.
@mojavelinux said:
I've updated the section. See if that gets you going.
Keep in mind that you don't have to run Asciidoctor via the API to load the extensions. You can use the -r flag with the asciidoctor command to require the file that contains the extension register code. The examples in the docs don't reflect that yet.
http://asciidoctor.org/docs/user-manual/#preprocessor-example is where the extra little bit of info about the -r
flag should go, right? Or should it be applied at the chapter level as a general piece of guidance?
I think the -r flag should go at the chapter level (or at the start of the examples section) since it applies to all extension examples. There is now more details about how to do this in the extensions-lab repo. See https://github.com/asciidoctor/asciidoctor-extensions-lab#using-an-extension. I'd say we could include most of that content into the user manual.