I was wondering if one can make a static wiki deployed on a private site, using middleman and the private gists from github (Muhahaha) and after a bit of investigation I manage to do it using two helpers in config.rb
file with minimum effort.
I added into the gemfile
redcapet
s gem:
gem "middleman", "~>3.2.2"
Ran install
on the project folder with command :
$ bundle install
Uncommented helpers in config.rb
and added markdown
method:
# Methods defined in the helpers block are available in templates
helpers do
# Initialize a redcarpet XHTML renderer and renders the input text
def markdown(text)
renderer = Redcarpet::Render::XHTML.new
markdown = Redcarpet::Markdown.new(renderer)
return markdown.render(text)
end
end
markdown
method can be called in a erb
template with <%= markdown("#This is an H1") %>
.
In addition to this method in the helpers
I added another method that will get the text from a particular URL provided in the input, the content is passed further on to the markdown
helper.
require 'open-uri'
# ...
# Methods defined in the helpers block are available in templates
helpers do
# Initialize a redcarpet XHTML renderer and renders the input text
# ...
def markdownFromUrl(url)
file = open(url)
contents = file.read
return markdown(contents);
end
end
markdownFromUrl
method can be called in a erb
template with <%= markdownFromUrl("https://gist.githubusercontent.com/tiberiucorbu/67df6c13d61c0fb3a34a/raw/middleman-markdown_from_text.md") %>
.
As for the gist markdown files there is a little trick to get a particular or the latest revision.
To get a particular revision construct the URLs like this :
https://gist.githubusercontent.com/<username>/<gist_id>/raw/<revision>/<filename>
Otherwise if you want the latest revision omit the <revision>
part :
https://gist.githubusercontent.com/<username>/<gist_id>/raw/<filename>
Here is a list of things considered but not implemented:
- Check memory leaks : Shame on me - I have no idea if opening an url requires a close as well in Ruby like in Java;
- Optimize to use minimum resources : Just by reviewing the code here
markdown
method can save a little initializations ofredcarpet
renderer instances with a lazy load pattern. - These methods are not "monkey proofed" and they lack any validation or error handling whatsoever, just by expecting that they are not going to be exposed to "monkeys" - Is that something that you can really relay on ?