Created
February 17, 2013 09:31
-
-
Save biesnecker/4970767 to your computer and use it in GitHub Desktop.
Wiki-like automatic links for Middleman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
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