public
Description: Ruby on Rails TextMate bundle [Learn it with PeepCode - http://peepcode.com/products/textmate-for-rails-2]
Homepage: http://groups.google.com/group/rubyonrails-textmate
Clone URL: git://github.com/drnic/ruby-on-rails-tmbundle.git
Search Repo:
Click here to lend your support to: ruby-on-rails-tmbundle and make a donation at www.pledgie.com !
100755 98 lines (85 sloc) 2.509 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env ruby
 
require 'erb'
require 'rubygems'
begin
  require 'newgem'
rescue LoadError
  puts "\n\nGenerating the website requires the newgem RubyGem"
  puts "Install: gem install newgem\n\n"
  exit(1)
end
require 'redcloth'
require 'syntax/convertors/html'
require 'erb'
ENV['rakefile_just_config'] = "true"
load File.dirname(__FILE__) + '/../Rakefile'
 
version = APP_VERSION
download = 'http://railsbundle.com/dist/JavaScript Unit Testing.tar.gz'
 
class Fixnum
  def ordinal
    # teens
    return 'th' if (10..19).include?(self % 100)
    # others
    case self % 10
    when 1: return 'st'
    when 2: return 'nd'
    when 3: return 'rd'
    else return 'th'
    end
  end
end
 
class Time
  def pretty
    return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
  end
end
 
def convert_syntax(syntax, source)
  return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
end
 
if ARGV.length >= 1
  src, template = ARGV
  template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
 
else
  puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
  exit!
end
 
template = ERB.new(File.open(template).read)
 
title = nil
body = nil
File.open(src) do |fsrc|
  title_text = fsrc.readline
  body_text = fsrc.read
  syntax_items = []
  body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
    ident = syntax_items.length
    element, syntax, source = $1, $2, $3
    syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
    "syntax-temp-#{ident}"
  }
  
  # a few extra supported markup commands specifically for documenting tm bundles
  [
    ['COMMAND', '&#x2318;'],
    ['TAB', '&#x21E5;'],
    ['SHIFT', '&#x21E7;'],
    ['OPTION', '&#x2325;'],
    ['CONTROL', '&#x2303;'],
    ['BACKSPACE', '&#x232B;'],
    ['DELETE', '&#x2326;'],
    ['RETURN', '&#x21A9;'],
    ['ENTER', '&#x2305;'],
    ['ESCAPE', '&#x238B;'],
    ['DOWN', '&#x2193;'],
    ['UP', '&#x2191;'],
    ['LEFT', '&#x2190;'],
    ['RIGHT', '&#x2192;']
  ].map do |regexp, replacement|
    body_text.gsub!(Regexp.new(regexp), replacement)
  end
  
  title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
  body = RedCloth.new(body_text).to_html
  body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
end
stat = File.stat(src)
created = stat.ctime
modified = stat.mtime
 
$stdout << template.result(binding)