asciidoctor-epub3 icon indicating copy to clipboard operation
asciidoctor-epub3 copied to clipboard

Remote image url failed to load.

Open rshiva opened this issue 2 years ago • 8 comments

Asciidoctors, I'm unable to generate/view the image from a remote URL while generating the Mobi. Whereas it works for asciidoctor when converting adoc to HTML

Code look something like this

image::https://upload.wikimedia.org/wikipedia/commons/3/35/Tux.svg[Tux,250,350]

You can find image:https://upload.wikimedia.org/wikipedia/commons/3/35/Tux.svg[Linux,25,35] everywhere these days.
asciidoctor-epub3 -D output -a ebook-format=kf8 chapter1.adoc --trace

asciidoctor-epub3 -D output -a ebook-format=kf8 chapter1.adoc --trace
asciidoctor: WARNING: Warning(svgparser):W25001: SVG Parser could not find the referenced file  /var/folders/lk/4m5qlr251kn__x7p13nsbhcc0000gn/T/mobi-o0n9Ff/EPUB/https:/upload.wikimedia.org/wikipedia/commons/3/35/Tux.svg
asciidoctor: WARNING: Warning(prcgen):W14010: media file not found  /var/folders/lk/4m5qlr251kn__x7p13nsbhcc0000gn/T/mobi-o0n9Ff/EPUB/https:/upload.wikimedia.org/wikipedia/commons/3/35/Tux.svg
asciidoctor: ERROR: Error(prcgen):E21018: Enhanced Mobi building failure, while parsing content in the file. Content: <You can find image::> in file: /var/folders/lk/4m5qlr251kn__x7p13nsbhcc0000gn/T/mobi-o0n9Ff/EPUB/preamble.xhtml line: 24
asciidoctor: ERROR: KindleGen failed to write MOBI to /Users/shiva/workspace/kindle-book-generator/first-book/new-book/output/chapter1.mobi

Am I missing something? Please guide I'm totally new to Asciidoctor. This https://github.com/asciidoctor/asciidoctor-epub3/issues/333 issue says remote URL issue is resolved

rshiva avatar Mar 31 '22 19:03 rshiva

I'm not sure Mobi supports remote images at all. In #333, asciidoctor-epub3 was fixed to pass URLs to EPUB. Mobi is created from EPUB using KindleGen and looks like the latter doesn't support remote URLs. We could add some hacks like "if we are producing Mobi, download remote resource and use that". The problem is that is isn't very clear when to download. Should we do it on each and every conversion? Or we should cache file? But when to re-download it?

slonopotamus avatar Apr 01 '22 15:04 slonopotamus

Thanks for the quick reply. Is it possible, whenever a URL is provided we download the images from the URL and put in a temporary folder or cache then read the images from there to create the Mobi and delete the cache after converting. I think it would make sense to download the images on every conversion. @slonopotamus Thanks for your contribution 😄

rshiva avatar Apr 01 '22 16:04 rshiva

We should not reinvent the wheel and just implement this logic: https://docs.asciidoctor.org/asciidoc/latest/directives/include-uri/

slonopotamus avatar Apr 03 '22 18:04 slonopotamus

Hmm... Looks like we already can do that:

asciidoctor-epub3 -D output -a ebook-format=kf8 -a data-uri -a allow-uri-read chapter1.adoc

@rshiva does this work for you?

Note that you cannot use SVG images because Mobi doesn't support that.

slonopotamus avatar Apr 03 '22 20:04 slonopotamus

The logic to fetch a remote image, and to even cache it, is already provided by Asciidoctor core. See this method: https://github.com/asciidoctor/asciidoctor/blob/main/lib/asciidoctor/abstract_node.rb#L322-L334

The downside of that method is that it returns the image as a data URI instead of as a File reference. However, you can find a different implementation in Asciidoctor PDF that returns a temporary file path: https://github.com/asciidoctor/asciidoctor-pdf/blob/main/lib/asciidoctor/pdf/converter.rb#L4240-L4259

Suffice to say, this is a solved problem in Asciidoctor. It's just a matter of reusing the code which is already available.

mojavelinux avatar Apr 03 '22 20:04 mojavelinux

-a data-uri

Indeed, this is one approach that leverages the logic I linked to in Asciidoctor core. If you prefer to have it add the image files to the manifest, then you would need the code from Asciidoctor PDF.

mojavelinux avatar Apr 03 '22 20:04 mojavelinux

Yep, there are some downsides using -a data-uri, but it might be Good Enough as a quick-and-dirty workaround.

slonopotamus avatar Apr 03 '22 20:04 slonopotamus

Hmm... Looks like we already can do that:

asciidoctor-epub3 -D output -a ebook-format=kf8 -a data-uri -a allow-uri-read chapter1.adoc

@rshiva does this work for you?

Note that you cannot use SVG images because Mobi doesn't support that.

Thanks @mojavelinux and @slonopotamus this worked

rshiva avatar Apr 04 '22 19:04 rshiva