Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional statements are not working when I use preprocessor extensions #64

Open
JmyL opened this issue Apr 6, 2016 · 2 comments
Open

Comments

@JmyL
Copy link

JmyL commented Apr 6, 2016

Conditional statements are not working when I use 'mathematical-treeprocessor' extension.
The "hello world" string doesn't apear when using 'preprossor extension'.

:enable-string:

ifdef::enable-string[]
hello world!
endif::[]

Conditional statements are working well when I delete mathematical-preprocessor code on 'mathematical-treeprocessor.rb'.

Extensions.register do
  # if @document.basebackend? 'html'
  //inline_macro MathematicalInlineMacro
  treeprocessor MathematicalTreeprocessor
end

I've tested several other preprocessors, it's same on other preprocessors make same problem too.

Conditional statements which looks for CLI attributes works well.
Only conditional satements which looks for DOCUMENT attributes are not working.

@mojavelinux
Copy link
Member

Unfortunately, this is one of the limitations of a preprocessor. A preprocessor does not see the structure of the AsciiDoc document in any way, aside from the preprocessor directives. As far as the preprocessor is concerned, it's just handling lines of text, some of which may contain preprocessor directives.

If you want the preprocessor to know about the attributes defined in the document header, you first need to parse the document up to the end of the document header, push those attributes into the document object, then allow the preprocessor to proceed.

I've done something similar in the AsciiDoc coalescer script. See https://github.com/asciidoctor/asciidoctor-extensions-lab/blob/master/scripts/asciidoc-coalescer.rb#L46-L53

What we may want to consider is adding an option to the Preprocessor that tells it to parse the document header in order to extract the attributes, then run on the remaining lines of code. That change would need to happen in core.

@stephenramm
Copy link

stephenramm commented Jul 12, 2017

There is a fix. Instead of returning a new Reader from your preprocessor, return a new PreprocessorReader initialised with your processed raw lines, thus:

class myPreprocessor <Asciidoctor::Extensions::Preprocessor
  def process document, reader
    Asciidoctor::PreprocessorReader.new document, reader.lines.map {|line|
      # do your stuff here..
      line
     }
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants