Permalink
Browse files

+ Importing a website template from cod project

  • Loading branch information...
kschiess committed May 24, 2012
1 parent 0c65250 commit 8edc6117b6b3cd99c4c90b60d4c1e83fd66efb38
View
@@ -0,0 +1,8 @@
+source "https://rubygems.org"
+
+gem "middleman"
+gem 'RedCloth'
+
+gem 'cod'
+gem 'case'
+gem 'text-highlight'
View
@@ -0,0 +1,114 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ RedCloth (4.2.9)
+ activesupport (3.2.2)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ addressable (2.2.7)
+ case (0.5.2.1)
+ chunky_png (1.2.5)
+ cod (0.4.4)
+ coffee-script (2.2.0)
+ coffee-script-source
+ execjs
+ coffee-script-source (1.2.0)
+ compass (0.12.1)
+ chunky_png (~> 1.2)
+ fssm (>= 0.2.7)
+ sass (~> 3.1)
+ daemons (1.1.8)
+ em-websocket (0.3.6)
+ addressable (>= 2.1.1)
+ eventmachine (>= 0.12.9)
+ eventmachine (0.12.10)
+ execjs (1.2.13)
+ multi_json (~> 1.0)
+ fssm (0.2.8.1)
+ guard (0.6.3)
+ thor (~> 0.14.6)
+ guard-livereload (0.3.1)
+ em-websocket (>= 0.2.0)
+ guard (>= 0.4.0)
+ multi_json (~> 1.0.3)
+ haml (3.1.4)
+ hike (1.2.1)
+ http_router (0.10.2)
+ rack (>= 1.0.0)
+ url_mount (~> 0.2.1)
+ i18n (0.6.0)
+ maruku (0.6.0)
+ syntax (>= 1.0.0)
+ middleman (2.0.15.1)
+ coffee-script (~> 2.2.0)
+ compass (~> 0.11)
+ execjs (~> 1.2.7)
+ guard (~> 0.6.2)
+ haml (~> 3.1.0)
+ maruku (~> 0.6.0)
+ middleman-livereload (~> 0.2.0)
+ padrino-core (~> 0.10.5)
+ padrino-helpers (~> 0.10.5)
+ rack (~> 1.3.5)
+ rack-test (~> 0.6.1)
+ sass (>= 3.1.7)
+ sinatra (~> 1.3.1)
+ slim (~> 1.0.2)
+ sprockets (~> 2.0.3)
+ thin (~> 1.2.11)
+ thor (~> 0.14.0)
+ tilt (~> 1.3.1)
+ uglifier (~> 1.0.0)
+ middleman-livereload (0.2.1)
+ guard-livereload (~> 0.3.1)
+ multi_json (1.0.4)
+ padrino-core (0.10.6)
+ activesupport (~> 3.2.0)
+ http_router (~> 0.10.2)
+ sinatra (~> 1.3.1)
+ thor (~> 0.14.3)
+ tilt (~> 1.3.0)
+ padrino-helpers (0.10.6)
+ i18n (~> 0.6)
+ padrino-core (= 0.10.6)
+ rack (1.3.6)
+ rack-protection (1.2.0)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ sass (3.1.15)
+ sinatra (1.3.2)
+ rack (~> 1.3, >= 1.3.6)
+ rack-protection (~> 1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ slim (1.0.4)
+ temple (~> 0.3.4)
+ tilt (~> 1.3.2)
+ sprockets (2.0.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ syntax (1.0.0)
+ temple (0.3.5)
+ text-highlight (1.0.2)
+ thin (1.2.11)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
+ thor (0.14.6)
+ tilt (1.3.3)
+ uglifier (1.0.4)
+ execjs (>= 0.3.0)
+ multi_json (>= 1.0.2)
+ url_mount (0.2.1)
+ rack
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ RedCloth
+ case
+ cod
+ middleman
+ text-highlight
View
@@ -0,0 +1,9 @@
+Parslets website is built with middleman. Just do a 'bundle install' in this
+directory and you should be good to go. 'middleman server' will give you
+a development server.
+
+Once you want to publish the website to github, you need to set the gh-pages
+branch to the build directory below this website directory. The script
+publish-gh-pages.sh will do this using a little black git magic. (Panic!)
+
+
View
@@ -0,0 +1,21 @@
+require 'tilt'
+require 'RedCloth'
+
+class NBRedClothTemplate < Tilt::RedClothTemplate
+ include Middleman::CoreExtensions::FrontMatter::YamlAware
+
+ def prepare
+ super
+ @engine.hard_breaks = false
+ end
+end
+Tilt.register NBRedClothTemplate, 'textile'
+Tilt.prefer NBRedClothTemplate
+
+Slim::Engine.set_default_options :pretty => true
+
+set :textile, :layout_engine => :slim
+
+configure :build do
+ set :http_prefix, "/cod/"
+end
View
@@ -0,0 +1,8 @@
+#!/usr/bin/env ruby
+
+require 'bundler/setup'
+
+$:.unshift File.expand_path(File.dirname(__FILE__) + "/lib")
+require 'example_runner'
+
+ExampleRunner.new.run(ARGV)
View
@@ -0,0 +1,88 @@
+class Document
+ def initialize(name)
+ @name = name
+ @state = :outside
+ @line_count = 1
+ end
+
+ def process
+ @target = Tempfile.new('erdoc')
+
+ File.open(@name, 'r') do |original|
+ while line = original.gets
+ consume(line.chomp)
+ end
+ end
+
+ @target.close(false)
+ FileUtils.mv(@target.path, @name)
+ end
+
+ def consume(line)
+ @line_count += 1
+
+ a = lambda { |*args| Case::Array[*args] }
+ any = Case::Any
+
+ case [@state, line]
+ when a[:outside, /<pre class="sh_ruby"><code title="(.*)">/]
+ @target.puts(line)
+ @state = :inside
+ extract_title(line)
+ when a[:inside, %r(</code></pre>)]
+ @state = :outside
+ run_example
+
+ when a[:outside, /<pre class="output">/]
+ @target.puts(line)
+ @state = :inside_old_output
+ when a[:inside_old_output, %r(</pre>)]
+ @target.puts(@last_output) if @last_output
+ @state = :outside
+
+ when a[:inside, any]
+ @example << line
+ else
+ # do nothing
+ end
+
+ # Write lines outside a code block directly to target.
+ @target.puts(line) if @state == :outside
+ end
+
+ def run_example
+ String.highlighter = Text::ANSIHighlighter.new
+ print " Running #@example... "
+
+ if @example.skip?
+ puts "Skipped, no inspection points."
+ return
+ end
+
+ # Prevent Ruby from buffering the script for too long. After a fork,
+ # Ruby buffers become a problem.
+ @target.flush
+
+ unless @example.run
+ puts "error".red
+ @example.output[:err].lines.each { |line|
+ print " " + line.magenta }
+ else
+ puts 'ok.'.green
+
+ # Stores last output for an eventual <pre class="output"> that might
+ # come somewhere.
+ @last_output = @example.output[:out]
+ end
+
+ @example.check_expectations
+
+ @target.puts @example.produce_modified_code
+ @example = nil
+ end
+ def extract_title(line)
+ if md=line.match(/title="(.*)"/)
+ @example = Example.new(md[1], @name, @line_count)
+ end
+ end
+end
View
@@ -0,0 +1,117 @@
+require 'tempfile'
+require 'cod'
+
+require 'site'
+require 'fail_site'
+
+class Example
+ def initialize(title, file, line)
+ @title = title
+ @file, @line = file, line
+
+ @lines = []
+
+ @sites = {}
+ @site_by_line = {}
+ end
+
+ def to_s
+ "'#@title'"
+ end
+
+ def <<(line)
+ @lines << line
+ end
+
+ attr_reader :output
+
+ def skip?
+ !@lines.grep(/# =>/)
+ end
+
+ def run
+ # Create a tempfile per output
+ tempfiles = [:err, :out].inject({}) { |h, name|
+ h[name] = Tempfile.new(name.to_s); h }
+
+ # Where code results are communicated.
+ $instrumentation = Cod.pipe
+
+ code = produce_example_code
+ pid = fork do
+ redirect_streams(tempfiles)
+ # puts example_code
+ eval(code, nil, @file, @line-2)
+ end
+ Process.wait(pid)
+
+ # Read these tempfiles.
+ @output = tempfiles.inject({}) { |h, (name, io)|
+ io.rewind
+ h[name] = io.read;
+ io.close
+ h }
+
+ loop do
+ site_id, probe_value = $instrumentation.get rescue break
+ fail "No such site #{site_id}." unless @sites.has_key?(site_id)
+
+ @sites[site_id].store probe_value
+ end
+
+ $instrumentation.close; $instrumentation = nil
+
+ return $?.success?
+ end
+
+ def redirect_streams(io_hash)
+ {
+ out: $stdout,
+ err: $stderr
+ }.each do |name, io|
+ io.reopen(io_hash[name])
+ end
+ end
+
+ def produce_example_code
+ root = File.expand_path(File.dirname(__FILE__))
+
+ '' <<
+ "$:.unshift #{root.inspect}\n" <<
+ "load 'prelude.rb'\n" <<
+ instrument(@lines).join("\n") <<
+ "\nload 'postscriptum.rb'\n"
+ end
+ def instrument(code)
+ code.map { |line|
+ md = line.match(/(?<pre>.*)# (?<type>=>|raises) (?<expectation>.*)/)
+ next line unless md
+
+ if md[:type] == 'raises'
+ site = FailSite.new(line, md[:pre], md[:expectation].strip)
+ else
+ site = Site.new(line, md[:pre], md[:expectation].strip)
+ end
+
+ add_site site
+ site.to_instrumented_line }
+ end
+ def add_site(site)
+ @sites[site.id] = site
+ @site_by_line[site.original_line] = site
+ end
+
+ def produce_modified_code
+ @lines.map { |line|
+ site = @site_by_line[line]
+ next line unless site
+
+ site.format_documentation_line }
+ end
+
+ def check_expectations
+ @sites.each do |_, site|
+ site.check
+ end
+ end
+end
@@ -0,0 +1,19 @@
+
+require 'case'
+require 'text/highlight'
+
+require 'document'
+require 'example'
+
+class ExampleRunner
+ def run(args)
+ Dir[File.join(args.last, '*.textile')].each do |name|
+ puts name.white
+ Document.new(name).process
+ end
+ end
+end
+
+if $0 == __FILE__
+ ExampleRunner.new.run(ARGV)
+end
Oops, something went wrong.

0 comments on commit 8edc611

Please sign in to comment.