Sha256: c03e3d9c8bfc0c94a4f062e1073a8941dbbef0a05c96b45e690b560473ee156e

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# encoding: utf-8
require 'rack'

# this is the rack application for ehg
class EasyHtmlGenerator::RackDispatcher
  EHG_URL   = 'https://github.com/creative-workflow/easy-html-generator'
  LIST_BODY = "<!DOCTYPE html><html><head>
  <style>*{ font-size: 20px;
            font-family: Verdana, 'Lucida Sans Unicode', sans-serif;}
         a,a:hover{text-decoration: none; color: #818181;}
  </style></head>
  <body>
    {CONTENT}
    <a target='_blank' href='#{EHG_URL}'>ehc on Github</a></body></html>"

  def initialize(app)
    @app = app
  end

  def call(env)
    request = Rack::Request.new(env)
    case request.path_info
    when '/'
      create_directory_listening_index_file
    else
      EasyHtmlGenerator.dispatch(request)
    end
    @app.call(env)
  end

  def create_directory_listening_index_file
    content = ''
    EasyHtmlGenerator.projects.each do |_name, project|
      content += "<h3>#{project.name}</h3>"
      project.list_dir.each do |file|
        content += build_list_dir_row(file)
      end
    end
    content = LIST_BODY.sub('{CONTENT}', "<ul>#{content}</ul>")

    File.write("#{EasyHtmlGenerator::DIST_PATH}/index.html", content)
  end

  def build_list_dir_row(file)
    f = File.path(file)
    f_name = File.basename(f)
    f_path = File.dirname(f)
    "<li><a href='#{f_path}/#{f_name}'><b>#{f_path}</b>/#{f_name}</a></li>"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy_html_generator-1.0.3 lib/easy_html_generator/rack_dispatcher.rb