Permalink
Browse files

fixing up images and stuff

  • Loading branch information...
2 parents 6975f5e + c6ae023 commit 99656914f28a7a525ef81a1cbae8dde71fae6b14 @shiffman shiffman committed Mar 14, 2012
View
Binary file not shown.
View
@@ -1,4 +1,7 @@
source :rubygems
gem 'sinatra'
-gem 'asciidoc', :path => 'gems/asciidoc'
+gem 'asciidoc', :path => 'gems/asciidoc'
+gem 'rocco'
+gem 'mustache'
+gem 'shotgun'
View
@@ -6,9 +6,16 @@ PATH
GEM
remote: http://rubygems.org/
specs:
+ mustache (0.99.4)
rack (1.4.1)
rack-protection (1.2.0)
rack
+ redcarpet (2.1.0)
+ rocco (0.8.2)
+ mustache
+ redcarpet
+ shotgun (0.9)
+ rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
@@ -20,4 +27,7 @@ PLATFORMS
DEPENDENCIES
asciidoc!
+ mustache
+ rocco
+ shotgun
sinatra
View
@@ -1,6 +1,7 @@
require 'rubygems'
require 'bundler'
Bundler.require
+require './helpers/magicrocco'
class Application < Sinatra::Base
View
@@ -0,0 +1,121 @@
+class MagicRocco < Rocco
+ def initialize(filename, language, syntax_on=false)
+ @file = filename
+
+ # When `block` is given, it must read the contents of the file using
+ # whatever means necessary and return it as a string. With no `block`,
+ # the file is read to retrieve data.
+ @data = filename
+
+ @options = {
+ :template_file => nil,
+ :language => language || "java",
+ :comment_chars => "//",
+ :syntax_on => syntax_on
+ }
+ @options[:language] = detect_language
+ @options[:comment_chars] = generate_comment_chars
+
+ # Turn `:comment_chars` into a regex matching a series of spaces, the
+ # `:comment_chars` string, and the an optional space. We'll use that
+ # to detect single-line comments.
+ @comment_pattern = Regexp.new("^\\s*#{@options[:comment_chars][:single]}\s?")
+
+ # `parse()` the file contents stored in `@data`. Run the result through
+ # `split()` and that result through `highlight()` to generate the final
+ # section list.
+ @sections = highlight(split(parse(@data)))
+ end
+
+ def detect_language
+ @_language ||=
+ if pygmentize?
+ %x[pygmentize -N thing.#{@options[:language]}].strip.split('+').first
+ else
+ "text"
+ end
+ end
+
+ # Take the result of `split` and apply syntax highlighting to source code.
+ def highlight(blocks)
+ docs_blocks, code_blocks = blocks
+
+ # Pre-process Docblock @annotations.
+ docs_blocks = docblock(docs_blocks) if @options[:docblocks]
+
+ # We are skipping markdown formatting and passing comments as
+ # plain text. Here would be the place to apply ASCIIDOC formatting
+ docs_html = docs_blocks
+
+ # Combine all code blocks into a single big stream with section dividers and
+ # run through either `pygmentize(1)` or <http://pygments.appspot.com>
+ span, espan = '<span class="c.?">', '</span>'
+ if @options[:comment_chars][:single]
+ front = @options[:comment_chars][:single]
+ divider_input = "\n\n#{front} DIVIDER\n\n"
+ divider_output = Regexp.new(
+ [ "\\n*",
+ span,
+ Regexp.escape(CGI.escapeHTML(front)),
+ ' DIVIDER',
+ espan,
+ "\\n*"
+ ].join, Regexp::MULTILINE
+ )
+ else
+ front = @options[:comment_chars][:multi][:start]
+ back = @options[:comment_chars][:multi][:end]
+ divider_input = "\n\n#{front}\nDIVIDER\n#{back}\n\n"
+ divider_output = Regexp.new(
+ [ "\\n*",
+ span, Regexp.escape(CGI.escapeHTML(front)), espan,
+ "\\n",
+ span, "DIVIDER", espan,
+ "\\n",
+ span, Regexp.escape(CGI.escapeHTML(back)), espan,
+ "\\n*"
+ ].join, Regexp::MULTILINE
+ )
+ end
+
+ code_html =
+ if @options[:syntax_on]
+ code_stream = code_blocks.join(divider_input)
+ if pygmentize?
+ highlight_pygmentize(code_stream)
+ else
+ highlight_webservice(code_stream)
+ end
+ else
+ code_blocks.join(divider_input)
+ end
+
+ # If syntax_on is true then after pygmentize the divider_input
+ # has been altered to the form of divider_output, otherwise its
+ # still the same.
+ code_divider =
+ if @options[:syntax_on]
+ divider_output
+ else
+ divider_input
+ end
+
+ # Do some post-processing on the pygments output to split things back
+ # into sections and remove partial `<pre>` blocks.
+ code_html = code_html.
+ split(code_divider).
+ map { |code| code.sub(/\n?<div class="highlight"><pre>/m, '') }.
+ map { |code| code.sub(/\n?<\/pre><\/div>\n/m, '') }
+
+ # Wrap every line of code in spans with class "one-line"
+ code_html = code_html.
+ map { |code| code.gsub(/(?<code>.*)\n/,"<span class='one-line'>" + '\k<code>' + "</span>\n") }
+
+ # Lastly, combine the docs and code lists back into a list of two-tuples.
+ docs_html.zip(code_html)
+ end
+
+ def render(template)
+ ERB.new(template).result(binding)
+ end
+end
@@ -30,4 +30,54 @@ pre {
canvas {
border: 2px solid red;
+}
+
+/* CODE BLOCK FORMATTING
+___________________________________________________ */
+
+/* Div wrapper for code blocks */
+.code-block {
+ margin: 2em 1em;
+ overflow: auto;
+ position: relative;
+}
+
+.code-block * {
+ position: relative;
+}
+
+/* Wrapper for every pairing of code and comment.
+Used to catch the .code-comment-line inside of
+a relatively positioned parent. */
+.code-comment-pair {
+ display: block;
+ margin: 0;
+ padding: 0;
+}
+
+.code-comment-line {
+ border-top: 1px dashed #aaa;
+ display: block;
+ height: 2px;
+ overflow-y: auto;
+ width: 100%;
+}
+
+/* Div wrapper for code comments */
+.code-comment {
+ clear: right;
+ color: #777;
+ display: block;
+ float: right;
+ font-family: sans-serif;
+ width: 300px;
+ -webkit-box-sizing: border-box;
+ -moz-box-sizing: border-box;
+ box-sizing: border-box;
+}
+
+/* The code goes in here. */
+.code-block code {
+ clear: left;
+ display: block;
}
@@ -1 +1 @@
-<strong><%= element.standard_render(views) %></strong>
+<strong><%= render_children(views, filter_results) %></strong>
@@ -1,5 +1,5 @@
<ul>
<% element.children.each do |child| %>
- <li><%= child.standard_render(views) %></li>
+ <li><%= child.render_children(views, filter_results) %></li>
<% end %>
</ul>
@@ -0,0 +1,9 @@
+<div class="code-block">
+<% @sections.each do |section| %>
+ <div class="code-comment-pair">
+ <div class="code-comment-line"></div>
+ <div class="code-comment"><%= section[0] %></div>
+ <code><pre><%= section[1] %></pre></code>
+ </div>
+<% end %>
+</div>
@@ -0,0 +1,6 @@
+<div class="<%= element.attributes[:class] %>">
+ <% unless element.attributes[:title].nil? %>
+ <h2><%= element.attributes[:title] %></h2>
+ <% end %>
+ <%= element.children[0] %>
+</div>
@@ -7,10 +7,7 @@
</head>
<body>
<div id="container">
- <% if toc %>
- <%= toc %>
- <% end %>
- <%= document.element.standard_render(views) %>
+ <%= document.element.render_children(views, filter_results) %>
</div>
</body>
</html>
@@ -1 +1 @@
-<h1><%= element.standard_render(views) %></h1>
+<h1><%= render_children(views, filter_results) %></h1>
@@ -1 +1 @@
-<em><%= element.standard_render(views) %></em>
+<em><%= render_children(views, filter_results) %></em>
@@ -1,5 +1,5 @@
<ul>
<% element.children.each do |child| %>
- <li><%= child.standard_render(views) %></li>
+ <li><%= child.render_children(views, filter_results) %></li>
<% end %>
</ul>
@@ -1 +1 @@
-<p><%= element.standard_render(views) %></p>
+<p><%= render_children(views, filter_results) %></p>
@@ -1,4 +1,4 @@
<blockquote>
- <%= element.standard_render(views) %>
+ <%= render_children(views, filter_results) %>
<span><%= element.attributes[:author] %></span>
</blockquote>
@@ -1,2 +1,2 @@
-<%#= element.children[0] %>
-<pre><%= element.children[1] %></pre>
+<% template = File.read("#{File.dirname(__FILE__)}/templates/html/views/code_blocks.html.erb") %>
+<%= MagicRocco.new(element.children[1], element.children[0].downcase, true).render(template) %>
@@ -5,7 +5,7 @@
<% element.children.each do |tr| %>
<tr>
<% tr.children.each do |td| %>
- <td><%= td.standard_render(views) %></td>
+ <td><%= td.render_children(views, filter_results) %></td>
<% end %>
</tr>
<% end %>
@@ -1,2 +1,2 @@
<a name="<%= element.attributes[:href] %>"></a>
-<h2>Chapter <%= element.attributes[:count] %> <%= element.standard_render(views) %></h2>
+<h2>Chapter <%= element.attributes[:count] %> <%= render_children(views, filter_results) %></h2>
@@ -1,2 +1,2 @@
<a name="<%= element.attributes[:href] %>"></a>
-<h3><%= element.attributes[:chapter_count] %>.<%= element.attributes[:count] %> <%= element.standard_render(views) %></h3>
+<h3><%= element.attributes[:chapter_count] %>.<%= element.attributes[:count] %> <%= render_children(views, filter_results) %></h3>
@@ -1,21 +1,15 @@
<h2>Table of Contents</h2>
-<%
- def el_output(hash)
- output = "<li><a href='##{hash[:el].attributes[:href]}'>#{hash[:el].children[0]}</a></li>"
- if hash[:children].size > 0
- output += "<ul>"
- hash[:children].each do |child|
- output += el_output(child)
- end
- output += "</ul>"
- end
- output
- end
-%>
-
<ul>
- <% hierarchy.each do |el| %>
- <%= el_output(el) %>
- <% end %>
+<% filter_results[:toc].each do |hash| %>
+ <li class="chpt-title"><a href='#<%= hash[:el].attributes[:href] %>'><%= hash[:el].children[0] %></a>
+ <% if hash[:children].size > 0 %>
+ <ul>
+ <% hash[:children].each do |child| %>
+ <li class="chpt-section"><a href='#<%= child[:el].attributes[:href] %>'><%= child[:el].children[0] %></a></li>
+ <% end %>
+ </ul>
+ <% end %>
+ </li>
+<% end %>
</ul>
@@ -1 +1 @@
-<strong><%= element.standard_render(views) %></strong>
+<strong><%= render_children(views, filter_results) %></strong>
@@ -1,5 +1,5 @@
<ul>
<% element.children.each do |child| %>
- <li><%= child.standard_render(views) %></li>
+ <li><%= child.render_children(views, filter_results) %></li>
<% end %>
</ul>
@@ -6,10 +6,8 @@
</head>
<body>
<div id="container">
- <% if toc %>
- <%= toc %>
- <% end %>
- <%= document.element.standard_render(views) %>
+ <%= toc %>
+ <%= document.element.render_children(views, filter_results) %>
</div>
</body>
</html>
@@ -1 +1 @@
-<h1><%= element.standard_render(views) %></h1>
+<h1><%= render_children(views, filter_results) %></h1>
@@ -1 +1 @@
-<em><%= element.standard_render(views) %></em>
+<em><%= render_children(views, filter_results) %></em>
Oops, something went wrong. Retry.

0 comments on commit 9965691

Please sign in to comment.