Re: How to provide template-dir to Asciidoctor.render_file

Posted by mojavelinux on
URL: https://discuss.asciidoctor.org/How-to-provide-template-dir-to-Asciidoctor-render-file-tp488p489.html

Hey Victor!

Here you go. This snippet should get you going:

```ruby
require 'asciidoctor'

source_file = ARGV.first

Asciidoctor.render_file(source_file,
  :in_place => true,
  :safe => :safe,
  :template_dir => "#{ENV['HOME']}/projects/asciidoctor/asciidoctor-backends",
  :template_engine => 'haml',
  :backend => 'deckjs',
  :attributes => %w(source-highlighter=highlight.js)
)
```

I'm grabbing the source file from the first argument to my Ruby script, but of course you can plug m[0] back in there.

I'll point out a few things to help you understand what's needed fixing.

:template_dir :: The template directory should be specified as an option argument, a hash symbol adjacent to other options like :safe, :backend and :attributes. The directory can be a relative path to from the working directory of the script or an absolute path. The ~ in the path is not expanded, so you need to use the Ruby environment variable syntax #{ENV['HOME']} to resolve it explicitly. Note that symbols in Ruby can't have a hyphen (without quoting it) so when we are doing options, we always use underscore.

Notice that I left the "haml/deckjs" off of the template directory path. I split it out into the :template_engine and :backend options. From that, Asciidoctor will resolve the final directory path to the template directory. This makes it possible to switch between engines and backends without fiddling with the path.

:template_engine :: This option is nothing more than a hint to Asciidoctor to tell it how to build the full path to the templates.

:backend :: This serves as a hint to Asciidoctor to tell it how to build the full path to the templates, plus it makes the parser and renderers aware of what output is being targeted (in this case an HTML 5 dialect).

:attributes :: Attributes can now be specified as a String (in the same form as name=value pairs are used on the commandline) or as an Array of name=value pairs. The %w() quoting automatically creates an Array from a space-separated list. It's my preferred way of defining attributes.

:safe :: The safe-mode can be specified as a symbol. This makes it clearer and more future proof than using the internal numeric value.

Let me know if you still have trouble making that work, or if you have other questions. Btw, I recommend that you use Asciidoctor 0.1.4.preview.3, which you can install from rubygems.org using:

 gem install asciidoctor --pre

Good luck!

-Dan


On Tue, Aug 20, 2013 at 10:33 PM, gamussa [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi team,

How I can provide template_dir in render_file method?
This snippet doesn't seem to work.

Asciidoctor.render_file(m[0],
                            :in_place => true,
                            :safe =>10,
                            'template-dir' => '~/projects/asciidoctor-backends/haml/deckjs',
                            #:to_file=>'slides.html',
                            :attributes => {
                                'source-highlighter' => 'highlightjs'
})
  }


Please advice...




If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/How-to-provide-template-dir-to-Asciidoctor-render-file-tp488.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | http://google.com/profiles/dan.j.allen