]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/composite_primary_keys-2.2.2/scripts/txt2html
Allow the format to be selected for note API responses
[rails.git] / vendor / gems / composite_primary_keys-2.2.2 / scripts / txt2html
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'redcloth'
5 require 'syntax/convertors/html'
6 require 'erb'
7 require File.dirname(__FILE__) + '/../lib/composite_primary_keys/version.rb'
8
9 version  = CompositePrimaryKeys::VERSION::STRING
10 download = 'http://rubyforge.org/projects/compositekeys'
11
12 class Fixnum
13   def ordinal
14     # teens
15     return 'th' if (10..19).include?(self % 100)
16     # others
17     case self % 10
18     when 1: return 'st'
19     when 2: return 'nd'
20     when 3: return 'rd'
21     else    return 'th'
22     end
23   end
24 end
25
26 class Time
27   def pretty
28     return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
29   end
30 end
31
32 def convert_syntax(syntax, source)
33   return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
34 end
35
36 if ARGV.length >= 1
37   src, template = ARGV
38   template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
39   
40 else
41   puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > 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   syntax_items = []
53   body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
54     ident = syntax_items.length
55     element, syntax, source = $1, $2, $3
56     syntax_items << "<#{element} class=\"syntax\">#{convert_syntax(syntax, source)}</#{element}>"
57     "syntax-temp-#{ident}"
58   }
59   title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
60   body = RedCloth.new(body_text).to_html
61   body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
62 end
63 stat = File.stat(src)
64 created = stat.ctime
65 modified = stat.mtime
66
67 $stdout << template.result(binding)