5
attr_reader :output, :view_path, :view, :guides_dir
7
def initialize(output = nil)
8
@guides_dir = File.join(File.dirname(__FILE__), '..')
10
@output = output || File.join(@guides_dir, "output")
13
FileUtils.rm_r(@output) if File.directory?(@output)
14
FileUtils.mkdir(@output)
17
@view_path = File.join(@guides_dir, "source")
21
guides = Dir.entries(view_path).find_all {|g| g =~ /textile$/ }
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}"
29
guides.each do |guide|
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')
38
def generate_guide(guide)
39
guide =~ /(.*?)(\.erb)?\.textile/
42
puts "Generating #{name}"
44
file = File.join(output, "#{name}.html")
45
File.open(file, 'w') do |f|
46
@view = ActionView::Base.new(view_path)
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)
54
body = File.read(File.join(view_path, guide))
55
body = set_header_section(body, @view)
56
body = set_index(body, @view)
58
result = view.render(:layout => 'layout', :text => textile(body))
64
def set_header_section(body, view)
65
new_body = body.gsub(/(.*?)endprologue\./m, '').strip
71
header = textile(header)
73
view.content_for(:page_title) { page_title }
74
view.content_for(:header_section) { header }
78
def set_index(body, view)
81
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
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]) }
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)
97
children_ul = view.content_tag(:ul, children)
99
index << view.content_tag(:li, link + children_ul)
105
view.content_for(:index_section) { index }
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)
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)
124
body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)</\1>}m) do |m|
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"
133
body.gsub(%r{<p>dirty_workaround_for_notextile_(\d+)</p>}) do |_|