aboutsummaryrefslogtreecommitdiff
path: root/doc/api/ruby/create_ruby_examples.rb
blob: b396a7e12b11142145cda631e81e37c2b35ad2bb (plain)
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
#!/usr/bin/env ruby
# vim: set sw=4 sts=4 et tw=80 :

require 'rubygems'
require 'syntax/convertors/html'

convertor = Syntax::Convertors::HTML.for_syntax('ruby')

srcdir = ARGV.shift

topuri = '../../'
toplinks = File.read(srcdir + '/../toplinks.html.part.in')
header = File.read(srcdir + '/../../header.html.part.in')
footer = File.read(srcdir + '/../../footer.html.part.in')
css = '<link rel="stylesheet" href="ruby_syntax.css" type="text/css" />'
header.gsub!('###TOPLINKS###', toplinks)
header.gsub!('###TOPURI###', topuri)
header.gsub!("</head>", "#{css}</head>")

ARGV.each do |example_file|
    html = convertor.convert(File.read(srcdir + '/' + example_file), false)
    File.open('ruby/' + example_file.gsub(/rb$/,'html'), 'w') do |output|
        output.write header
        output.write "<h1>#{example_file}</h1>"

        #Grab examples description
        html.scan(/=begin description(.*?)=end/m) {|desc| output.puts "<p>#{desc[0]}</p>"}

        #enclose each line of an =begin block in a comment span
        html.gsub!(/=begin description.*?=end/m) do |match|
            match.gsub("\n", "</span>\n<span class=\"comment\">")
        end
        output.write '<pre>';
        i = 0
        html.each_line do |line|
            i += 1
            output.print '<span class="lineno">' + i.to_s.rjust(5,'0') + '</span> <span class="ruby_code">' + line + '</span>'
        end
        output.write '</pre>'
        output.write footer
    end
end