Merged 14009:14059 from trunk.
[rails.git] / vendor / gems / composite_primary_keys-1.1.0 / scripts / txt2js
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'redcloth'
5 require 'syntax/convertors/html'
6 require 'erb'
7 require 'active_support'
8 require File.dirname(__FILE__) + '/../lib/composite_primary_keys/version.rb'
9
10 version  = CompositePrimaryKeys::VERSION::STRING
11 download = 'http://rubyforge.org/projects/compositekeys'
12
13 class Fixnum
14   def ordinal
15     # teens
16     return 'th' if (10..19).include?(self % 100)
17     # others
18     case self % 10
19     when 1: return 'st'
20     when 2: return 'nd'
21     when 3: return 'rd'
22     else    return 'th'
23     end
24   end
25 end
26
27 class Time
28   def pretty
29     return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
30   end
31 end
32
33 def convert_syntax(syntax, source)
34   return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
35 end
36
37 if ARGV.length >= 1
38   src, template = ARGV
39   template ||= File.dirname(__FILE__) + '/../website/template.js'
40 else
41   puts("Usage: #{File.split($0).last} source.txt [template.js] > output.html")
42   exit!
43 end
44
45 template = ERB.new(File.open(template).read)
46
47 title = nil
48 body = nil
49 File.open(src) do |fsrc|
50   title_text = fsrc.readline
51   body_text = fsrc.read
52   title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
53   body = RedCloth.new(body_text)
54 end
55 stat = File.stat(src)
56 created = stat.ctime
57 modified = stat.mtime
58
59 $stdout << template.result(binding)