Class: ActionSite::Site

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/action_site/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_dir, out_dir) ⇒ Site

Returns a new instance of Site.



27
28
29
30
31
32
33
34
# File 'lib/action_site/site.rb', line 27

def initialize(in_dir, out_dir)
  @context = Context.new
  @generator = HtmlGenerator.new(@context, in_dir, generators)
  @in_dir, @out_dir = in_dir, out_dir
  puts "\nGENERATING #{File.basename(in_dir).upcase}\n\n"
  rm_rf out_dir
  mkdir_p out_dir
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



25
26
27
# File 'lib/action_site/site.rb', line 25

def context
  @context
end

Instance Method Details

#excluded?(file) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/action_site/site.rb', line 76

def excluded?(file)
  if File.directory?(file)
    EXCLUDED_DIRECTORIES.include?(File.basename(file))
  elsif File.file?(file)
    EXCLUDED_DIRECTORIES.include?(File.basename(File.dirname(file)))
  end
end

#generateObject



41
42
43
44
# File 'lib/action_site/site.rb', line 41

def generate
  Dir[@in_dir + "/**/*"].map {|path| path.sub(@in_dir, '') }.
                         each {|path| refresh_page(path) }
end

#generatorsObject

add / remove / change generators to change the behavior of the html generation



37
38
39
# File 'lib/action_site/site.rb', line 37

def generators
  @generators ||= DEFAULT_GENERATORS.dup
end

#refresh_page(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/action_site/site.rb', line 46

def refresh_page(path)
  in_file, out_file = File.join(@in_dir, path), File.join(@out_dir, path)

  if !File.exist?(in_file)
    # is there another version that does?
    return unless in_file.extension == "html" && (in_file = Dir[in_file.split_filename[0] + ".*"].first)
  end
      
  if excluded?(in_file)
    # nothing
  
  elsif File.symlink?(in_file)
    ensure_directory_exists(out_file)
    cp in_file, out_file rescue nil # maybe the links don't exist here

  elsif File.directory?(in_file)
    ensure_directory_exists(out_file)

  elsif resource?(in_file)
    ensure_directory_exists(out_file)
    ln_sf File.expand_path(in_file), File.expand_path(out_file)

  else 
    ensure_directory_exists(out_file)
    out_file = out_file.gsub(/\..+$/, '.html')
    generate_page(in_file, out_file)
    puts "   #{in_file} => #{out_file}"
  end
end

#resource?(file) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/action_site/site.rb', line 84

def resource?(file)
  RESOURCE_EXTENSIONS.include?(file.extension)
end

#serve(port = 3000) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/action_site/site.rb', line 88

def serve(port = 3000)
  app = proc do |env|
    path = env["PATH_INFO"]
    file = File.join(@out_dir, path)
    if File.directory?(file)
      path = env["PATH_INFO"] = File.join(path, "index.html")
    end

    begin
      yield path if block_given?
      refresh_page(path)
    rescue Exception
      return [500, {"Content-Type" => "text/plain"}, "Error in generation :\n\n#{$!.to_s}\n#{$!.backtrace.join("\n")}"]
    end

    Rack::File.new(@out_dir).call(env)
  end

  Thin::Server.start('0.0.0.0', port) do
    use Rack::CommonLogger
    run app
  end
end