Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to pass options to sub-template processor? #192

Closed
sunaku opened this issue Oct 11, 2011 · 8 comments
Closed

how to pass options to sub-template processor? #192

sunaku opened this issue Oct 11, 2011 · 8 comments

Comments

@sunaku
Copy link

sunaku commented Oct 11, 2011

Hello,

I use markdown: sub-templates inside my Slim templates. I was able to make Slim use Redcarpet for markdown processing via the Tilt API, as shown below. However, I don't know how to set default rendering options for Redcarpet, which is hidden somewhere in the Slim => Tilt pipeline. In particular, I want to enable GitHub-style code blocks and also use Albino/Pygments for syntax highlighting of code blocks.

I tried RTFM and browsing the Tilt & Temple source code, but no luck. So I ask you now for suggestions.

require 'slim'
Tilt.prefer Tilt::RedcarpetTemplate
# TODO: how to set rendering options on Redcarpet?
Slim::Template.new("bogus_file.md"){ "markdown:\n  ```ruby\n  puts \#{who}\n  ```\n" }.render(Object.new, :who => 123)

The above code yields:

"<p><code>ruby\nputs 123\n</code></p>\n"

But eventually (after I set my rendering options), I want to get something like this:

"<p><pre><code lang=\"ruby\">\n<strong>puts</strong> <span style="color: red">123</span>\n</code></pre></p>\n"

I'm using ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux] with slim (1.0.3), tilt (1.3.3), and redcarpet (1.17.2).

Thanks for your consideration.

@minad
Copy link
Member

minad commented Oct 11, 2011

@sunaku: Good point! There is currently no syntax to set options for embedded engines. Which option do you need to set? Do you have a syntax proposal?

@sunaku
Copy link
Author

sunaku commented Oct 11, 2011

I want to set the following Redcarpet options:

        :no_intra_emphasis - do not parse emphasis inside of words.
            Strings such as `foo_bar_baz` will not generate `<em>`
            tags.

        :tables - parse tables, PHP-Markdown style

        :fenced_code_blocks - parse fenced code blocks, PHP-Markdown
            style .Blocks delimited with 3 or more `~` or backticks
            will be considered as code, without the need to be
            indented. An optional language name may be added at the
            end of the opening fence for the code block

        :autolink - parse links even when they are not enclosed in
            `<>` characters. Autolinks for the http, https and ftp
            protocols will be automatically detected. Email addresses
            are also handled, and http links without protocol, but
            starting with `www.`

        :space_after_headers - A space is always required between the
            hash at the beginning of a header and its name, e.g.
            `#this is my header` would not be a valid header.

I think the best place to set these would be when we tell Tilt our preferences:

Tilt.prefer Tilt::RedcarpetTemplate, my_options_hash

Thanks.

@sunaku
Copy link
Author

sunaku commented Oct 11, 2011

I found a workaround to the issue. In fact, perhaps this is how Tilt is meant to be used:

>> require 'slim'
=> true
>> Tilt.prefer Class.new(Tilt::RedcarpetTemplate){ def options; {:fenced_code_blocks => true, :superscript => true, :smartypants => true, :no_intra_emphasis => true}; end }, 'markdown'
=> ["markdown"]
>> Slim::Template.new("bogus_file.slim"){ "markdown:\n  hello_world_this_is -- 2^(nd)\n  ```ruby\n  puts \#{who}; def foo; end\n  ```\n" }.render(Object.new, :who => 123)
=> "<p>hello_world_this_is &ndash; 2<sup>nd</sup>\n<code>ruby\nputs 123; def foo; end\n</code></p>\n"

This is using Redcarpet 2.0.0b5, by the way.

@judofyr
Copy link
Member

judofyr commented Oct 12, 2011

Tilt's template engine lookup is separate from template initialization, so this is probably something that Slim should provide.

Camping solves this problem by having a per-app options-hash where you can set it:

Camping.goes :Foo
Foo.set :markdown, { :fenced_code_blocks => true }

Sinatra does the same.

@minad
Copy link
Member

minad commented Oct 12, 2011

Fixed in master.

Slim::EmbeddedEngine.set_default_options :markdown => {:fenced_code_blocks => true}

This needs to be documented.

@judofyr: Any ideas how to make such things on a per app-basis, not on a per-ruby-instance basis? This is a Temple thing.

@minad minad closed this as completed Oct 12, 2011
@sunaku
Copy link
Author

sunaku commented Jan 29, 2013

Sorry for the late reply @minad but that snippet doesn't work:

## ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-linux]
>> require 'slim'
true
>> Slim::VERSION
"1.3.6"
>> Slim::EmbeddedEngine.set_default_options :markdown => {:fenced_code_blocks => true}
{:markdown=>{:fenced_code_blocks=>true}}
>> Slim::Template.new("bogus_file.slim"){ "markdown:\n  hello_world_this_is -- 2^(nd)\n  ```ruby\n  puts \#{who}; def foo; end\n  ```\n" }.render(Object.new, :who => 123)
"<p>hello<em>world</em>this_is -- 2^(nd)\n<code>ruby\nputs 123; def foo; end\n</code></p>\n"

Could you please tell me how to make it work with Slim 1.3.6? 😅

@sunaku
Copy link
Author

sunaku commented Jan 31, 2013

Never mind. I found the answer in issue #245. 😤

Tilt.prefer Tilt::RedcarpetTemplate::Redcarpet2, 'markdown'

By adding the above line, we ensure that Slim uses Redcarpet (for which the :fenced_code_blocks => true option is meant) like this:

>> require 'slim'
true
>> Slim::EmbeddedEngine.set_default_options :markdown => {:fenced_code_blocks => true}
{:markdown=>{:fenced_code_blocks=>true}}

## the problem (notice the <p><code> in the rendered output):

>> Slim::Template.new("bogus_file.slim"){ "markdown:\n  hello_world_this_is -- 2^(nd)\n\n  ```\n  puts \#{who}; def foo; end\n  ```\n" }.render(Object.new, :who => 123)
"<p>hello<em>world</em>this_is -- 2^(nd)</p>\n\n<p><code>\nputs 123; def foo; end\n</code></p>\n"

## the solution (notice the <p>\n\n<pre><code> in the rendered output):

>> Tilt.prefer Tilt::RedcarpetTemplate::Redcarpet2, 'markdown'
["markdown"]
>> Slim::Template.new("bogus_file.slim"){ "markdown:\n  hello_world_this_is -- 2^(nd)\n\n  ```\n  puts \#{who}; def foo; end\n  ```\n" }.render(Object.new, :who => 123)
"<p>hello<em>world</em>this_is -- 2^(nd)</p>\n\n<pre><code>puts 123; def foo; end\n</code></pre>\n"

Cheers!

@sunaku
Copy link
Author

sunaku commented Oct 12, 2013

I learned recently that Redcarpet has_two_ sets of options. See my blog post for details and an updated solution. 🍰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants