Skip to content

Instantly share code, notes, and snippets.

@biesnecker
Created February 17, 2013 09:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save biesnecker/4970767 to your computer and use it in GitHub Desktop.
Save biesnecker/4970767 to your computer and use it in GitHub Desktop.
Wiki-like automatic links for Middleman
# add this to your config.rb
ready do
wikitargets = Hash.new
wikiwords = Hash.new
sitemap.resources.select {|p| p.ext == ".html" }.each do |p|
unless p.data['wikitag'].nil?
wikitargets[p.data['wikitag']] = p.url
end
File.open(p.source_file).each_line do |line|
re = /\<\%\=\W?wiki\(.*?,\W?[\'\"](.*?)[\'\"]\)\W?\%\>/
line.scan(re).map(&:first).each do |match|
if wikiwords[match].nil?
wikiwords[match] = 1
else
wikiwords[match] += 1
end
end
end
end
Middleman::Application.set(:wikitargets, wikitargets)
Middleman::Application.set(:wikiwords, wikiwords)
end
helpers do
def wiki(text, tag)
wikitargets = Middleman::Application.defaults[:wikitargets]
if wikitargets[tag].nil?
text
else
target = wikitargets[tag]
"<a href=\"#{target}\">#{text}</a>"
end
end
end
<%
targets = Middleman::Application.defaults[:wikitargets]
words = Middleman::Application.defaults[:wikiwords]
words_sorted = words
.sort_by { |word, count| count }
.select {|w| targets.assoc(w[0]).nil? }
.reverse!
%>
<h4>Unlinked Words</h4>
<table class="table table-bordered">
<% words_sorted.each do |word| %>
<tr>
<td><%= word[0] %></td><td><%= word[1] %></td>
</tr>
<% end %>
</table>
<h4>Available Targets</h4>
<ul>
<% targets.each do |key, value| %>
<li><%= key %><br /><small><%= value %></small></li>
<% end %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment