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

Unable to modify imagesdir from Treeprocessor #547

Closed
abelsromero opened this issue Mar 31, 2017 · 7 comments
Closed

Unable to modify imagesdir from Treeprocessor #547

abelsromero opened this issue Mar 31, 2017 · 7 comments

Comments

@abelsromero
Copy link
Member

Just as an excercise, I am trying to build an extension that:

checks if an image exists
    if not, it looks for it in a list of candidates path
    if found, updates `imagesdir` attribute

But I found that setting the attribute in the block or the parent, does not update the attribute (see).
I can however update the target with the same code, so I assume the setAttr method works.

Afaik, updating the parent should be equivalent to defining the attribute in the block in the AsciiDoc document, which I did and works.

So, is this an issue or I am doing something wrong?

Other minor things:

  • There's no ImageBlock in 1.5.5 and 1.6.0 API. Shouldn't we add one to make target modification easier?
  • I get this error org.jruby.exceptions.RaiseException: (SystemCallError) Unknown error 123 - FindFirstFile with 1.6.0-alpha.3 (using jRuby 9.1.8.0)
@robertpanzer
Copy link
Member

IIRC the TreeProcessor is called after the document is loaded, so after all other block processors and attribute replacements have already taken place.

But this is from the top of my head as I am currently behind a small screen, so could be wrong.

@abelsromero
Copy link
Member Author

Could be it can't be done in an easy way?

I tried with Preprocessor, but then the document hasn't not been parsed and I have no nice access to the AST.
It seems odd to me that includes can be manipuled nicely with a IncludeProcessor, but not images.
Btw, I already discarded the Macro processor, given it does not allow to hook into Asciidoctor blocks

But this is from the top of my head as I am currently behind a small screen, so could be wrong.

Take a rest. I am just doing this for fun.

@mojavelinux
Copy link
Member

mojavelinux commented Apr 1, 2017

The following works in Ruby:

require 'asciidoctor/extensions'

Asciidoctor::Extensions.register do
  treeprocessor do
    process do |doc|
      doc.set_attr 'imagesdir', 'img'
      doc
    end
  end
end

Keep in mind that there is a difference between AbstractNode#set_attr and Document#set_attribute. The latter does not allow you to set an attribute that has been locked, which is an attribute set via the API or CLI. The AbstractNode#set_attr doesn't have that restriction. So it depends on which method AsciidoctorJ is invoking.

@abelsromero
Copy link
Member Author

Modifying the attribute (with setAttr) at document level works, but it changes the paths for all documents, which makes sense.
I guess that I should try to explore a Preprocessor to inline the something like this asciidoctor/asciidoctor-maven-plugin#291 (comment), or modify the path directly with a Postprocessor (backend dependant).

In any case, I am aware I am going against some of the principles of the tool. And my curiosity is satisfied, thank you both for enduring me.

@mojavelinux
Copy link
Member

mojavelinux commented Apr 1, 2017

Okay, I see what you're trying to do. You want to modify the path for a specific image. That's not something that's really supported at the moment. You can modify the target, but that is still combined with the document-level imagesdir.

This is a limitation of core I've hit myself. I've wanted to set the imagesdir used for a specific image. We could consider allowing imagesdir to be set as an attribute on the image node. That would require a change to the image_uri helper here: https://github.com/asciidoctor/asciidoctor/blob/7c433c0688edb09a7652c4620b460d24951ca331/lib/asciidoctor/abstract_node.rb#L330-L348

Either way, this is a change to core.

@mojavelinux
Copy link
Member

Alternatively, we could add an attribute that marks the target as resolved so it doesn't get combined with imagesdir.

@mojavelinux
Copy link
Member

mojavelinux commented Apr 1, 2017

Another solution is go through all the images in the document and set the target to a resolved value, then unset imagesdir on the document, then update the target of the image you want. That would work today.

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