Find inline images with Asciidoctor API

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Find inline images with Asciidoctor API

yashi
How can I find all referenced images in an asciidoc file with Asciidoctor API?
I can find block images `image::file_name[]` with `find_by(context: :image)` however I can't find inline images.

I'm attaching  a test code a.rb I came up with.

require 'asciidoctor'

input = <<-EOS
= Title

== Chapter 1

image::foo.png[]

=== Section 1.1

image::bar.png[]

This is a paragraph with inline image image:baz.png[].
EOS

doc = Asciidoctor.load input
doc.find_by(context: :image).each do |e|
  pp e.attributes["target"]
end

Running the code yields:
    "foo.png"
    "bar.png"
But the last inline `image:baz.png[]` is not found. What / where can I change to find baz.png?

Reply | Threaded
Open this post in threaded view
|

Re: Find inline images with Asciidoctor API

mojavelinux
Administrator
Asciidoctor does not parse inline markup during load. (see https://github.com/asciidoctor/asciidoctor/issues/61). Therefore, you can't visit inline images as nodes. And find_by only returns blocks anyway.

However, it's still possible to pull them into the document model. First, what you need to do is set the catalog_assets option to true.

doc = Asciidoctor.load input, safe: :safe, catalog_assets: true

Then, all the images will be available in the catalog.

images = doc.catalog[:images]

That gives you access to the original image target. As long as the imagesdir attribute stays the same throughout the document, you'll know how to find the image.

Unfortunately, what you don't know is where the image came from. It's just a list of images. But if you cross-check it against block images, you can figure out which ones are inline images.

Cheers,

-Dan

On Fri, Mar 9, 2018 at 2:44 AM, yashi [via Asciidoctor :: Discussion] <[hidden email]> wrote:
How can I find all referenced images in an asciidoc file with Asciidoctor API?
I can find block images `image::file_name[]` with `find_by(context: :image)` however I can't find inline images.

I'm attaching  a test code a.rb I came up with.

require 'asciidoctor'

input = <<-EOS
= Title

== Chapter 1

image::foo.png[]

=== Section 1.1

image::bar.png[]

This is a paragraph with inline image image:baz.png[].
EOS

doc = Asciidoctor.load input
doc.find_by(context: :image).each do |e|
  pp e.attributes["target"]
end

Running the code yields:
    "foo.png"
    "bar.png"
But the last inline `image:baz.png[]` is not found. What / where can I change to find baz.png?




If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Find-inline-images-with-Asciidoctor-API-tp6210.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux