middleman / middleman-asciidoc

:beginner: AsciiDoc support for Middleman 4. (In Middleman 3, AsciiDoc support is provided by a core extension).
https://middlemanapp.com
Other
27 stars 12 forks source link

Set :to_dir option on document when loading during conversion phase #78

Closed mojavelinux closed 6 years ago

mojavelinux commented 6 years ago

When loading the document during the conversion/rendering phase (i.e., in the Tilt template), set the :to_dir option to the location where the file will be written (build_dir + dirname of destination_path).

opts[:to_dir] = ::File.expand_path \
    (::File.dirname ctx.current_page.destination_path),
    ctx.app.config[:build_dir]

Setting the :to_dir option on the document allows extensions (such as Asciidoctor Diagram) to know where the file will be output so it can write additional assets to that directory (or relative to it).

An alternate strategy is to add the outdir and outfile attributes to the attributes Hash. The benefit of this approach is that it also gives the content access to this information. We could also consider taking both approaches.

mojavelinux commented 6 years ago

The correct path calculation is as follows:

opts[:to_dir] = ::File.dirname ::File.join \
    (ctx.app.root_path.join ctx.app.config[:build_dir].to_s),
    ctx.current_page.destination_path
mojavelinux commented 6 years ago

I decided to go ahead and also pass the outfile and outdir attributes to the AsciiDoc processor. The value of the outdir attribute will match the value of the :to_dir option.