asciidoctor / asciidoctor-extensions-lab

A lab for testing and demonstrating Asciidoctor extensions. Please do not use this code in production. If you want to use one of these extensions in your application, create a new project, import the code, and distribute it as a RubyGem. You can then request to make it a top-level project under the Asciidoctor organization.
Other
104 stars 101 forks source link

Modify parsing options from an extension #74

Closed obilodeau closed 10 months ago

obilodeau commented 7 years ago

I tried creating an extension that would generate HTML ready to be pasted into a Wordpress post in source form (minimal HTML).

For it to work without setting the --no-header-footer in addition to the -r flag I would have liked to set the :header_footer = false option directly when the extension is registered.

I've tried several ways and it didn't work. Including subclassing and overloading initialize and trying to access the @options field and add the option to it (but the hash is frozen).

I need guidance. Here's the code with attempts commented out: https://github.com/asciidoctor/asciidoctor-extensions-lab/compare/master...obilodeau:wordpress-extension?expand=1

mojavelinux commented 7 years ago

The only extension point that can be used to control this flag is a Preprocessor. That's because the Preprocessor runs before the document is parsed.

Here's how you'd do it:

Asciidoctor::Extensions.register do
  preprocessor do
    process do |doc, reader|
      doc.instance_variable_set :@options, (doc.options.merge header_footer: true)
      nil
    end
  end
end

(Modifying the options isn't a supported operation in the public API, hence the low-level assignment).

However, if you have control over adding an extension, I'm not sure why you wouldn't just pass the header_footer option to the call to Asciidoctor.convert.

mojavelinux commented 7 years ago

Btw, you could just use the -s flag instead of --no-header-footer.

obilodeau commented 7 years ago

However, if you have control over adding an extension, I'm not sure why you wouldn't just pass the header_footer option to the call to Asciidoctor.convert.

It isn't clear how to accomplish that with an extension from the documentation. The only "convert" from the extension repository is this.

I'm wondering what is the process method's objective. If I return a string instead of nil what will happen?

obilodeau commented 7 years ago

For example, this doesn't work:

Asciidoctor::Extensions.register do
  preprocessor do
    process do |doc, reader|
      ::Asciidoctor.convert doc.attributes['docfile'], header_footer: false
    end
  end
end
mojavelinux commented 10 months ago

Sorry to have confused you. The option name is standalone (e.g., standalone: false). However, a preprocessor should never call Asciidoctor#convert on the file that is being converted. That's going to result in a mess. A preprocessor should set properties on the Document object and that's it.

I believe the answer I provided above is the correct one.