Updated website to reflect proper git repo path
[couchobject.git] / script / txt2html
blobdd9a40cb71e56ecb38751e1ff01756b8cba5364f
1 #!/usr/bin/env ruby
3 require 'rubygems'
4 begin
5 require 'newgem'
6 rescue LoadError
7 puts "\n\nGenerating the website requires the newgem RubyGem"
8 puts "Install: gem install newgem\n\n"
9 exit(1)
10 end
11 require 'redcloth'
12 require 'syntax/convertors/html'
13 require 'erb'
14 require File.dirname(__FILE__) + '/../lib/couch_object/version.rb'
16 version = CouchObject::VERSION::STRING
17 download = 'http://rubyforge.org/projects/couch_object'
19 class Fixnum
20 def ordinal
21 # teens
22 return 'th' if (10..19).include?(self % 100)
23 # others
24 case self % 10
25 when 1: return 'st'
26 when 2: return 'nd'
27 when 3: return 'rd'
28 else return 'th'
29 end
30 end
31 end
33 class Time
34 def pretty
35 return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
36 end
37 end
39 def convert_syntax(syntax, source)
40 return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
41 end
43 if ARGV.length >= 1
44 src, template = ARGV
45 template ||= File.join(File.dirname(__FILE__), '/../website/template.rhtml')
47 else
48 puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
49 exit!
50 end
52 template = ERB.new(File.open(template).read)
54 title = nil
55 body = nil
56 File.open(src) do |fsrc|
57 title_text = fsrc.readline
58 body_text = fsrc.read
59 syntax_items = []
60 body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
61 ident = syntax_items.length
62 element, syntax, source = $1, $2, $3
63 syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
64 "syntax-temp-#{ident}"
66 title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
67 body = RedCloth.new(body_text).to_html
68 body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
69 end
70 stat = File.stat(src)
71 created = stat.ctime
72 modified = stat.mtime
74 $stdout << template.result(binding)