Changing text content in a TreeProcessor extension

Posted by MattBlissett on
URL: https://discuss.asciidoctor.org/Changing-text-content-in-a-TreeProcessor-extension-tp8115.html

Overall aim: I'd like to change the style of crossreferences to an anchor in the glossary, to get something like this (glossary links are more discreet than other links).  I'd prefer not to change the <<word>> syntax, so document authors don't need to learn another syntax and can still use other tools (without my extension) to preview the document.

Using a TreeProcessor seemed the best way to find the glossary links -- they should be descendants of the block with the id "glossary".

    document.blocks.each do |block|
      if block.id == "glossary" then
        buildGlossary block
      end
    end

How, though, am I supposed to change the actual text content?

Even retrieving the text seems complicated.  I came up with removing the substitutions, reading .text (maybe replacing it) and restoring the substitutions:

    if defined? block.text then
      s = []
      s.replace(block.subs)
      s.map{|ss| block.remove_sub(ss)}
      text = block.text
      block.text = changeGlossaryRefs(block.text)
      s.map{|ss| block.subs.append(ss)}
    end

Is there a better approach?  

The rough work is here: https://github.com/gbif/gbif-asciidoctor-toolkit/blob/glossary-references/gbif-extensions/lib/glossary.rb#L24