[kramdown-users] --template file: Get stuff from the kramdown document

125 views
Skip to first unread message

Dave Hennings

unread,
Aug 8, 2013, 2:24:40 PM8/8/13
to kramdow...@rubyforge.org
Hi there! Is it possible to access, in a template file (used with the --template option), the contents of the first `h1` (if any) of the kramdown document? I'd like that to be the contents of the `<title>` element. (I only know about `<%= @body %>`, and I hardly know any Ruby at all.)

`kramdown foo.kramdown --template mytemplate.html`, mytemplate.html:

~~~
<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <title><%= ??? %></title>
    <link rel="stylesheet" href="style.css"/>
</head>
<body>
    <%= @body %>
</body>
</html>
~~~

Thomas Leitner

unread,
Aug 9, 2013, 4:41:08 AM8/9/13
to kramdow...@rubyforge.org
Hi,

On 2013-08-08 20:24 +0200 Dave Hennings wrote:
> Hi there! Is it possible to access, in a template file (used with the
> --template option), the contents of the first `h1` (if any) of the
> kramdown document? I'd like that to be the contents of the `<title>`
> element. (I only know about `<%= @body %>`, and I hardly know any
> Ruby at all.)

no, this is information is currently not exposed. I have written a new
item on my TODO list :)

Best regards,
Thomas
_______________________________________________
kramdown-users mailing list
kramdow...@rubyforge.org
http://rubyforge.org/mailman/listinfo/kramdown-users

Dave Hennings

unread,
Aug 12, 2013, 11:55:57 AM8/12/13
to kramdow...@rubyforge.org
I just noticed that the default HTML template does this. Putting the following in your own, custom template is a bit wordy, though, isn't it?

~~~
<%
extend ::Kramdown::Utils::Html
title = ''
h = @converter.root.children.find {|c| c.type == :header}
if h
  collector = lambda {|c| c.children.collect {|cc| cc.type == :text ? escape_html(cc.value, :text) : collector.call(cc)}.join('')}
  title = collector.call(h)
end
%>
<title><%= title %></title>
~~~

By the way, is writing your own templates documented somewhere? Perhaps at least `@body` should be mentioned in `kramdown --help`?

Also, wouldn't it be cool if you could pass arbitrary data to templates? Perhaps a `--data` option which is a hash. Then you perhaps could access this info via `@data.myvar` or even `@myvar` in the templates. That would be useful for me at least:

1. I could specify the language of the document: `kramdown --template foo --data {'lang'=>'en-US' }` (`<html lang="<%=@lang%>"`)
2. In some projects, I have given each kramdown file their own, almost identical template file. Instead I could use _one_ template file and use the `data` option in the kramdown files themselves: `{::options data=…}`

Thoughts?

Matt Neuburg

unread,
Aug 12, 2013, 12:39:54 PM8/12/13
to kramdow...@rubyforge.org

On Aug 12, 2013, at 8:55 AM, Dave Hennings <dave.hennin...@gmail.com> wrote:

> Also, wouldn't it be cool if you could pass arbitrary data to templates

Of course at this point you are describing a full-on template-based web page/site builder framework such as webgen (or my own RubyFrontier). It might be simpler to adopt one. It's really no responsibility of kramdown to do what a surrounding framework can do far more generally and powerfully. m.

--
matt neuburg, phd = ma...@tidbits.com, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 6! http://shop.oreilly.com/product/0636920029717.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com

Shawn Van Ittersum

unread,
Aug 12, 2013, 1:00:14 PM8/12/13
to kramdow...@rubyforge.org
Also consider Middleman (middlemanapp.com).

Shawn

Dave Hennings

unread,
Aug 12, 2013, 2:56:37 PM8/12/13
to kramdow...@rubyforge.org
Yeah, I gues you're right. Thanks.


2013/8/12 Shawn Van Ittersum <svical...@gmail.com>
Reply all
Reply to author
Forward
0 new messages