~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/railties/guides/rails_guides/generator.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'set'
 
2
 
 
3
module RailsGuides
 
4
  class Generator
 
5
    attr_reader :output, :view_path, :view, :guides_dir
 
6
 
 
7
    def initialize(output = nil)
 
8
      @guides_dir = File.join(File.dirname(__FILE__), '..')
 
9
 
 
10
      @output = output || File.join(@guides_dir, "output")
 
11
 
 
12
      unless ENV["ONLY"]
 
13
        FileUtils.rm_r(@output) if File.directory?(@output)
 
14
        FileUtils.mkdir(@output)
 
15
      end
 
16
 
 
17
      @view_path = File.join(@guides_dir, "source")
 
18
    end
 
19
 
 
20
    def generate
 
21
      guides = Dir.entries(view_path).find_all {|g| g =~ /textile$/ }
 
22
 
 
23
      if ENV["ONLY"]
 
24
        only = ENV["ONLY"].split(",").map{|x| x.strip }.map {|o| "#{o}.textile" }
 
25
        guides = guides.find_all {|g| only.include?(g) }
 
26
        puts "GENERATING ONLY #{guides.inspect}"
 
27
      end
 
28
 
 
29
      guides.each do |guide|
 
30
        generate_guide(guide)
 
31
      end
 
32
 
 
33
      # Copy images and css files to html directory
 
34
      FileUtils.cp_r File.join(guides_dir, 'images'), File.join(output, 'images')
 
35
      FileUtils.cp_r File.join(guides_dir, 'files'), File.join(output, 'files')
 
36
    end
 
37
 
 
38
    def generate_guide(guide)
 
39
      guide =~ /(.*?)(\.erb)?\.textile/
 
40
      name = $1
 
41
 
 
42
      puts "Generating #{name}"
 
43
 
 
44
      file = File.join(output, "#{name}.html")
 
45
      File.open(file, 'w') do |f|
 
46
        @view = ActionView::Base.new(view_path)
 
47
        @view.extend(Helpers)
 
48
 
 
49
        if guide =~ /\.erb\.textile/
 
50
          # Generate the erb pages with textile formatting - e.g. index/authors
 
51
          result = view.render(:layout => 'layout', :file => name)
 
52
          f.write textile(result)
 
53
        else
 
54
          body = File.read(File.join(view_path, guide))
 
55
          body = set_header_section(body, @view)
 
56
          body = set_index(body, @view)
 
57
 
 
58
          result = view.render(:layout => 'layout', :text => textile(body))
 
59
          f.write result
 
60
        end
 
61
      end
 
62
    end
 
63
 
 
64
    def set_header_section(body, view)
 
65
      new_body = body.gsub(/(.*?)endprologue\./m, '').strip
 
66
      header = $1
 
67
 
 
68
      header =~ /h2\.(.*)/
 
69
      page_title = $1.strip
 
70
 
 
71
      header = textile(header)
 
72
 
 
73
      view.content_for(:page_title) { page_title }
 
74
      view.content_for(:header_section) { header }
 
75
      new_body
 
76
    end
 
77
 
 
78
    def set_index(body, view)
 
79
      index = <<-INDEX
 
80
      <div id="subCol">
 
81
        <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
 
82
        <ol class="chapters">
 
83
      INDEX
 
84
 
 
85
      i = Indexer.new(body)
 
86
      i.index
 
87
 
 
88
      # Set index for 2 levels
 
89
      i.level_hash.each do |key, value|
 
90
        link = view.content_tag(:a, :href => key[:id]) { textile(key[:title]) }
 
91
 
 
92
        children = value.keys.map do |k|
 
93
          l = view.content_tag(:a, :href => k[:id]) { textile(k[:title]) }
 
94
          view.content_tag(:li, l)
 
95
        end
 
96
 
 
97
        children_ul = view.content_tag(:ul, children)
 
98
 
 
99
        index << view.content_tag(:li, link + children_ul)
 
100
      end
 
101
 
 
102
      index << '</ol>'
 
103
      index << '</div>'
 
104
 
 
105
      view.content_for(:index_section) { index }
 
106
 
 
107
      i.result
 
108
    end
 
109
 
 
110
    def textile(body)
 
111
      # If the issue with notextile is fixed just remove the wrapper.
 
112
      with_workaround_for_notextile(body) do |body|
 
113
        t = RedCloth.new(body)
 
114
        t.hard_breaks = false
 
115
        t.to_html(:notestuff, :plusplus, :code, :tip)
 
116
      end
 
117
    end
 
118
 
 
119
    # For some reason the notextile tag does not always turn off textile. See
 
120
    # LH ticket of the security guide (#7). As a temporary workaround we deal
 
121
    # with code blocks by hand.
 
122
    def with_workaround_for_notextile(body)
 
123
      code_blocks = []
 
124
      body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)</\1>}m) do |m|
 
125
        es = ERB::Util.h($2)
 
126
        css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
 
127
        code_blocks << %{<div class="code_container"><code class="#{css_class}">#{es}</code></div>}
 
128
        "\ndirty_workaround_for_notextile_#{code_blocks.size - 1}\n"
 
129
      end
 
130
      
 
131
      body = yield body
 
132
      
 
133
      body.gsub(%r{<p>dirty_workaround_for_notextile_(\d+)</p>}) do |_|
 
134
        code_blocks[$1.to_i]
 
135
      end
 
136
    end
 
137
  end
 
138
end