Re: Changing text content in a TreeProcessor extension

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

I got interested in glossaries yesterday…

I’m struggling to understand what you are doing now and what you want to be different.  Could you show the asciidoc for everything involved in the simplest possible example and indicate how you want the generated html to be different from how it is?

Thanks!
David Jencks

On Jul 28, 2020, at 10:38 AM, MattBlissett [via Asciidoctor :: Discussion] <[hidden email]> wrote:

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


If you reply to this email, your message will be added to the discussion below:
https://discuss.asciidoctor.org/Changing-text-content-in-a-TreeProcessor-extension-tp8115.html
To start a new topic under Asciidoctor :: Discussion, [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML