asciidoctor / asciidoctor-gradle-plugin

A Gradle plugin that uses Asciidoctor via JRuby to process AsciiDoc source files within the project.
https://asciidoctor.github.io/asciidoctor-gradle-plugin/
Apache License 2.0
285 stars 120 forks source link

Use of custom converters #683

Closed davbader closed 1 year ago

davbader commented 1 year ago

Some Background and description

I want to write an asciidoc document which includes mathematical expressions.

I use STEM blocks to write using latex syntax:

File: index.adoc:

= Hello World
:stem: latexmath

Inline math stem:[\alpha + \beta] followed by a block equation:

[stem]
++++
\alpha + \beta = \gamma
++++

Now I want to generate HTML output and later publish it to confluence. When generating HTML, asciidoctor will generate code with the default mathjax deliiters, \[, \], \(, \).

However our Confluence MathJax plugin does not support the delimiters above. Thus, I followed this tutorial to write a custom converter that changes the delimiters.

File: my-html5-converter.rb

Asciidoctor::Converter.for 'html5'
# => Asciidoctor::Converter::Html5Converter

class MyHtml5Converter < (Asciidoctor::Converter.for 'html5')
    register_for 'html5'

    def initialize backend, opts = {}
        Asciidoctor::BLOCK_MATH_DELIMITERS[:latexmath] = ['(mathjax-block(', ')mathjax-block)']
        Asciidoctor::INLINE_MATH_DELIMITERS[:latexmath] = ['(mathjax-inline(', ')mathjax-inline)']
        QUOTE_TAGS[:latexmath] = ['(mathjax-inline(', ')mathjax-inline)']
        super(backend, opts)
    end
end

I can now generate the HTML site using asciidoctor with:

asciidoctor -r ./my-html5-converter.rb index.adoc

Delimiters are changed and everything renders correctly.

Actual Problem Solution

The actual question is: How to a run the above example using asciidoctor-gradle-plugin?

The following build.gradle works:

plugins {
    id "org.asciidoctor.jvm.convert" version "3.3.2"
}

repositories {
    mavenCentral()
}

asciidoctor {
  sourceDir  file('docs')
  sources {
    include 'index.adoc'
  }
  outputDir  file('build/docs')

  asciidoctorj {
    modules {
        diagram.use() 
        diagram.version '2.2.11' 
    }
    requires './my-html5-converter.rb'
  }

}

Edited: I tested my custom converter with inline stem, which did not work in the original implentation. Thus, I did not see any result and thought the converter was not called at all.

The problem was the user, not the plugin.

davbader commented 1 year ago

Closed, because everything does work as expected.