Create a link "see blog post source on GitHub".

48 views
Skip to first unread message

Jérémie Bresson

unread,
Dec 26, 2015, 1:42:22 PM12/26/15
to JBake Users

I am doing some experiments with JBake (used in a maven Build). I am really happy with my first results. My blog isn’t published online yet, but I am playing with the jbake-sample project (see what I have done on my fork: https://github.com/jmini/jbake-sample)

 

I would like to propose a “View post source on GitHub” link on each post page. For that I need to know the relative path within the repository (relative to the root of the repository).

 

I was’nt able to figure out all the possibility of the content variable: ${content.xxxxx}. To reverse engineer I have use the keys built in function of Freemarker and I have replaced the content of post.ftl with this:


<#include "header.ftl">

<#include "menu.ftl">

<ul>
<#assign keys = content?keys>
<#list keys as key>
<li>${key}</li>
</#list>
</ul>

<#include "footer.ftl">


For this page:

http://jmini.github.io/jbake-sample/blog/2013/third-post.html


${content.uri} is for the output document:
blog/2013/third-post.html

 

${content.file} is interesting, but it correspond to the file on my machine: (on windows):
C:\******\jbake-sample\src\main\jbake\content\blog\2013\third-post.adoc

 

I think I could replace all backslashes with slashes, but I wonder how I can eliminate the path to the repository ( C:\******\jbake-sample in this example, that I should replace with https://github.com/jmini/jbake-sample/blob/master/).

 

Thank you for your help.

Jérémie Bresson

unread,
Dec 27, 2015, 2:08:43 AM12/27/15
to JBake Users
My current solution (or work arround) is to use Groovy templates with this bloc:

<%
def urlsrc = content.file
def i = urlsrc.indexOf('jbake-sample')
urlsrc
= "https://github.com/jmini/jbake-sample/tree/master" + urlsrc.substring(i + 12).replaceAll('\\\\','/')
%>

and this usage:
<a href="${urlsrc}">Post source on GitHub</a>

If someone knows a better solution...

Jonathan Bullock

unread,
Jan 13, 2016, 9:33:54 AM1/13/16
to Jérémie Bresson, JBake Users
Hi Jérémie,

An alternative would be to define the source file in the header metadata like so:

sourcefile=blog/2013/third-post.adoc

And use this in the templates. But it's a very manual process compared to your solution.

Would be quite easy to add another value to the content map in JBake that referred to the source file with just the relative path?

Regards,
Jon

--
You received this message because you are subscribed to the Google Groups "JBake Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jbake-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jérémie Bresson

unread,
Jan 15, 2016, 2:51:18 AM1/15/16
to JBake Users, jebr...@gmail.com
Hi John,

Thanks for your reply. As you figured out, I want something automatic because I fear copy-paste errors...

I think the new variable should be called something with uri: sourceuri, srcuri, src-uri, src.uri...
This would be consistent with the current uri variable (that correspond to the output uri).

If you think this could be integrated in the core, I would be happy to contribute it.

Jérémie

Jonathan Bullock

unread,
Jan 17, 2016, 11:32:52 AM1/17/16
to Jérémie Bresson, JBake Users
Hi Jérémie,

Unfortunately sourceuri is already being used to track the content in the OrientDB, but its not a problem to change this if need be.


Jon
Reply all
Reply to author
Forward
0 new messages