Permalink
Browse files

Advanced code templating to handle comments pointing to multiple lines

  • Loading branch information...
1 parent 927e41b commit 93b7a7aa87e28a53d3d657b4b20d1c2702bb943e @sklise sklise committed Aug 2, 2012
Showing with 32 additions and 5 deletions.
  1. +31 −2 helpers/codepostprocessing.rb
  2. +1 −3 views/code.erb
@@ -1,9 +1,38 @@
def formatCodeCommentPairs(code, comment)
# No comment, skip to code
if comment.length == 0
- return "<code><pre>#{parseCode(code)}</pre></code>"
+ response = "<div class='code-comment-pair no-comment'>"
+ response += "<code><pre>#{parseCode(code)}</pre></code>"
+ response += "</div>"
+ response
else
- return "<div class='code-comment'>#{comment}</div><code><pre>#{parseCode(code)}</pre></code>"
+ response = "<div class='code-comment-pair #{stretch_or_no_comment(comment)}'>"
+ response += look_for_multiline(comment)
+ response += "<code><pre>#{parseCode(code)}</pre></code>"
+ response += "</div>"
+ response
+ end
+end
+
+def stretch_or_no_comment(comment)
+ if comment.match(/\[full\]/)
+ "stretch"
+ elsif comment.gsub(/\[end\]/,'').length == 0
+ "no-comment"
+ else
+ ""
+ end
+end
+
+def look_for_multiline(comment)
+ clean_comment = comment.gsub(/\[end\]/,'').gsub(/\[full\] /,'')
+
+ stretch = comment.match(/\[full\]/) ? "stretch" : ""
+
+ if clean_comment.length == 0
+ ""
+ else
+ "<div class='code-comment #{stretch}'>#{clean_comment}</div>"
end
end
View
@@ -1,7 +1,5 @@
<div class="code-block">
<% @sections.each do |section| %>
- <div class="code-comment-pair <%= (section[0].length > 0) ? "" : "no-comment" %>">
- <%= formatCodeCommentPairs(section[1],section[0]) %>
- </div>
+ <%= formatCodeCommentPairs(section[1],section[0]) %>
<% end %>
</div>

0 comments on commit 93b7a7a

Please sign in to comment.