Class: Textile2Html

Inherits:
Object
  • Object
show all
Defined in:
lib/textile2html.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :layout => File.expand_path("./textile2html_layout.html.erb", File.dirname(__FILE__)),
  :src_dir => File.expand_path("."),
  :dest_dir => File.expand_path("."),
  :noop => false,
  :verbose => false,
}
EXTENSIONS =
{
  :textile => %w[textile],
  :html_template => %w[html.erb],
  :css_template => %w[css.erb],
  :text => %w[html css],
  :image => %w[png jpg gif icon],
}
EXTENSION_PATTERNS =
EXTENSIONS.inject({}) do |dest, (t, exts)|
  key = Regexp.union( exts.map{|ext| /\.#{Regexp.escape(ext)}\Z/i} )
  dest[key] = t
  dest
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextile2Html

Returns a new instance of Textile2Html.



17
18
19
# File 'lib/textile2html.rb', line 17

def initialize
  @options = DEFAULT_OPTIONS.dup
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/textile2html.rb', line 16

def options
  @options
end

Instance Method Details

#binding_for_yieldObject



21
22
23
# File 'lib/textile2html.rb', line 21

def binding_for_yield
  binding
end

#build_page_outlines(links) ⇒ Object



37
38
39
40
# File 'lib/textile2html.rb', line 37

def build_page_outlines(links)
  src = links.map{|(depth, desc)| "#" * depth << ' "' << desc << '":#' << desc }.join("\n")
  RedCloth.new(src).to_html
end

#executeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/textile2html.rb', line 55

def execute
  @layout_erb = ERB.new(File.read(options[:layout]))
  @src_dir = File.expand_path(options[:src_dir])
  src_files = Dir["#{@src_dir}/**/*.*"]
  src_files.each do |src_file|
    EXTENSION_PATTERNS.each do |pattern, ext_type|
      if src_file =~ pattern
        @src_path = src_file
        @rel_path = src_file.sub(/^#{Regexp.escape(@src_dir)}\//, '')
        @rel_path_depth = @rel_path.split(/\//).length - 1
        puts "processing: #{src_file}"
        send("process_#{ext_type}", src_file)
        break
      end
    end
  end
end

#pickup_headers(body) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/textile2html.rb', line 25

def pickup_headers(body)
  links = []
  result = body.gsub(/(<h[1-6]>)(.*?)<\/h[1-6]>/) do |*args|
    # puts "args: #{args.inspect}"
    tag, desc = $1, $2
    depth = tag.gsub(/\D/, '').to_i
    links << [depth, desc]
    "<h#{depth}><a name=\"#{desc}\">#{desc}</a></h#{depth}>"
  end
  return result, links
end

#process_css_template(src_file) ⇒ Object



97
98
99
100
# File 'lib/textile2html.rb', line 97

def process_css_template(src_file)
  body = ERB.new(File.read(src_file)).result
  write_text(src_file, body, [/\.erb$/i, ''])
end

#process_html_template(src_file) ⇒ Object



90
91
92
93
94
95
# File 'lib/textile2html.rb', line 90

def process_html_template(src_file)
  html_body = ERB.new(File.read(src_file)).result
  b = binding_for_yield{|*args| html_body}
  html = @layout_erb.result(b)
  write_text(src_file, html, [/\.erb$/i, ''])
end

#process_image(src_file) ⇒ Object



106
107
108
# File 'lib/textile2html.rb', line 106

def process_image(src_file)
  copy(src_file)
end

#process_text(src_file) ⇒ Object



102
103
104
# File 'lib/textile2html.rb', line 102

def process_text(src_file)
  copy(src_file)
end

#process_textile(src_file) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/textile2html.rb', line 73

def process_textile(src_file)
  src = ERB.new(File.read(src_file)).result
  html_body = RedCloth.new(src).to_html
  html_body, links = pickup_headers(html_body)
  b = binding_for_yield do|*args|
    arg = args.first
    case arg
    when nil then html_body
    when :page_outline then build_page_outlines(links)
    else
      nil
    end
  end
  html = @layout_erb.result(b)
  write_text(src_file, html, [/\.textile$/, '.html'])
end