asciidoctor / asciidoctor-epub3

:blue_book: Asciidoctor EPUB3 is a set of Asciidoctor extensions for converting AsciiDoc to EPUB3
https://asciidoctor.org
MIT License
216 stars 68 forks source link

Remote image url failed to load. #415

Closed rshiva closed 5 months ago

rshiva commented 2 years ago

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

slonopotamus commented 2 years ago

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?

rshiva commented 2 years ago

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 😄

slonopotamus commented 2 years ago

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

slonopotamus commented 2 years ago

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.

mojavelinux commented 2 years ago

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 commented 2 years ago

-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.

slonopotamus commented 2 years ago

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

rshiva commented 2 years ago

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

slonopotamus commented 5 months ago

asciidoctor-epub3 no longer support producing MOBI/KF8, see https://github.com/asciidoctor/asciidoctor-epub3/pull/458.