How to provide template-dir to Asciidoctor.render_file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to provide template-dir to Asciidoctor.render_file

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

Reply | Threaded
Open this post in threaded view
|

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

mojavelinux
Administrator
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



--
Reply | Threaded
Open this post in threaded view
|

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

mojavelinux
Administrator
In reply to this post by gamussa
I was thinking that we should probably make ~ expansion work in the Asciidoctor API. It seems that when we build the directory to the templates, we don't use File.expand_path in the right place, which handles special path symbols like tilde. Perhaps file an issue?

-Dan


On Tue, Aug 20, 2013 at 11:12 PM, Dan Allen <[hidden email]> wrote:
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



--



--
Reply | Threaded
Open this post in threaded view
|

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

gamussa
In reply to this post by mojavelinux
Hi Dan!

Very fast help! As usual!
Everything works like a charm!
Now after I've setup (with your help) desk.js backend and guard/livereload scripts, finally I can focus on my J1 preso (written in asciidoc).
Small question, are you going this year?

Cheers,
Vik
Reply | Threaded
Open this post in threaded view
|

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

mojavelinux
Administrator
I'm excited to hear it! With that combo, your going to put on a show to remember. I can't wait to see the reactions!

As for me, I'm still not sure yet. I have a badge reserved, and I certainly want to be there, but haven't made the call yet.

-Dan


On Tue, Aug 20, 2013 at 11:39 PM, gamussa [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi Dan!

Very fast help! As usual!
Everything works like a charm!
Now after I've setup (with your help) desk.js backend and guard/livereload scripts, finally I can focus on my J1 preso (written in asciidoc).
Small question, are you going this year?

Cheers,
Vik


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-tp488p491.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--