Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Pull Disqus comments into <noscript> for AT users / bots

  • Loading branch information...
commit d429efec1e464135466a53b2d9e4ad048f540324 1 parent 4829901
runpaint runpaint authored
38 Rakefile
@@ -16,7 +16,7 @@ PDF = "#{WEB_OUT}/vim-recipes.pdf"
16 16 CLOBBER.include('output','deb')
17 17 TEMPLATES_DIR = 'templates/'
18 18 TEMPLATE_WRAPPER = 'page.html'
19   -TEMPLATE_NO_WRAP = [TEMPLATE_WRAPPER, 'atom.atom']
  19 +TEMPLATE_NO_WRAP = [TEMPLATE_WRAPPER, 'atom.atom', 'comment.html']
20 20
21 21 def template(filename,hash)
22 22 content = ERB.new(File.open(File.join(TEMPLATES_DIR,filename)).read).
@@ -107,6 +107,12 @@ end
107 107 desc "Generate the HTML version"
108 108 task :html => SOURCE_HTML do |t|
109 109 toc = make_toc()
  110 + require 'open-uri'
  111 + require 'json'
  112 + api_key = File.read(File.expand_path("~/disqus-api-key")).chomp
  113 + disqus = JSON.parse(open(
  114 + "http://disqus.com/api/get_forum_posts/?api_version=1.1&user_api_key=#{api_key}&forum_id=127380&limit=99999"
  115 + ).read)['message']
110 116 toc.each_with_index do |entry,idx|
111 117 next if entry[:type] == :subsection
112 118 source = File.open(entry[:file]).read
@@ -153,10 +159,36 @@ task :html => SOURCE_HTML do |t|
153 159 |e| e[:type] == :recipe}.first
154 160 prv = idx == 0 ? toc[-1] : toc[0..(idx - 1)].select{
155 161 |e| e[:type] == :recipe}[-1]
  162 + url = "http://vim.runpaint.org/#{entry[:section_id]}/#{entry[:id]}/"
  163 + comments = disqus.select do |m|
  164 + m['thread']['url'] == url
  165 + end.map do |m|
  166 + author = m.key?('author') ? m['author'] : m['anonymous_author']
  167 + name = %w{display_name username name}.
  168 + select{|k| author.key?(k) && author[k] != ''}.
  169 + map{|k| author[k]}.first
  170 + url = if author.key?('url') && author['url'] != ''
  171 + author['url']
  172 + elsif author.key?('username')
  173 + "http://disqus.com/#{author['username']}"
  174 + else
  175 + '#'
  176 + end
  177 + text = m['message'].gsub(/</,"&lt;").
  178 + gsub(/>/,"&gt;").
  179 + gsub(/&lt;(\/?(a|b|i|h\d|code|pre|blockquote))&gt;/i,'<\1>').
  180 + gsub(/\r\n\r/,'<p>')
  181 + avatar = author.key?('avatar') ? author['avatar']['small'] : "http://media.disqus.com/images/noavatar32.png"
  182 + template('comment.html', {
  183 + :comment => text, :url => url,
  184 + :author => name, :avatar => avatar, :date => m['created_at']
  185 + })
  186 + end
  187 + comments = comments.empty? ? '' : "<h2>Comments</h2>#{comments.join}"
156 188 page = template('recipe.html',
157 189 {:body => doc.to_s, :title => entry[:title], :id => entry[:id],
158 190 :section_id => entry[:section_id], :section => entry[:section_name],
159   - :next_e => nxt, :prev_e => prv})
  191 + :next_e => nxt, :prev_e => prv, :comments => comments})
160 192 #FIXME: Use :target_path instead:
161 193 path = "#{WEB_OUT}/#{entry[:section_id]}/#{entry[:id]}/index.html"
162 194 mkdir_p File.dirname(path)
@@ -203,7 +235,7 @@ end
203 235
204 236 desc "Upload the website"
205 237 task :upload => [:clobber, :www, :gzip, :sitemap] do
206   - sh "rsync --delete -vaz #{WEB_OUT}/ vim.runpaint.org:/home/public/"
  238 + sh "rsync --delete -vaz #{WEB_OUT}/ vr:/home/public/"
207 239 Rake::Task['sitemap_notify'].invoke
208 240 sh 'git push'
209 241 end
9 templates/comment.html
... ... @@ -0,0 +1,9 @@
  1 +<article>
  2 +<p><%= comment %></p>
  3 + <footer>
  4 + <p><a href="<%= url %>"><img src="<%= avatar %>" height=32 width=32
  5 + alt=avatar></a> <time pubdate datetime="<%= date %>"><%= date %></time> by
  6 + <a href="<%= url %>"><%= author %></a></p>
  7 + </footer>
  8 +</article>
  9 +
5 templates/recipe.html
@@ -16,7 +16,10 @@
16 16 src="http://disqus.com/forums/vimrecipes/embed.js">
17 17 </script>
18 18 <noscript>
19   - <a href="http://vimrecipes.disqus.com/?url=ref">View the comment thread.</a>
  19 + <div class=comments><%= comments %>
  20 + <p><a href="http://vimrecipes.disqus.com/?url=ref">View the comment
  21 + thread on Disqus</a>
  22 + </div>
20 23 </noscript>
21 24 <div id="footnotes"></div>
22 25
5 templates/web.css
@@ -72,3 +72,8 @@ p {margin: 1em 0}
72 72 text-size: larger;
73 73 font-weight: bold;
74 74 }
  75 +div.comments > h2 { text-align: center }
  76 +div.comments img { float: left; margin-right: 2%; border: none; }
  77 +div.comments article { display: block; clear: both; margin-left: 5%; }
  78 +div.comments article > p { border-left: 1px dashed black; padding-left: 1% }
  79 +div.comments > p:last-child { text-align: center }

0 comments on commit d429efe

Please sign in to comment.
Something went wrong with that request. Please try again.