Class: Nwiki::Frontend::App

Inherits:
Object
  • Object
show all
Defined in:
lib/nwiki/frontend/app.rb

Constant Summary collapse

TEMPLATE =
-> (wiki, page_title, html) {
  erb = ERB.new "<!DOCTYPE HTML>\n<html>\n<head>\n  <title><%= page_title %><%= wiki.title %></title>\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"ATOM Feed\" href=\"/articles.xml\">\n  <link rel=\"stylesheet\" href=\"//netdna.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css\">\n  <link rel=\"stylesheet\" href=\"//netdna.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css\">\n</head>\n<body>\n  <div class=\"container\">\n    <div class=\"row\">\n<div class=\"col-md-12\"><h1><a href=\"/articles/\"><%= wiki.title %></a></h1></div>\n    </div>\n    <div class=\"row\">\n<div class=\"col-md-12\"><h2\"><small><%= wiki.subtitle %></small></h2></div>\n    </div>\n    <div class=\"row\">\n<div class=\"col-md-12\">\n  <%= html %>\n</div>\n    </div>\n  </div>\n  <script src=\"//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>\n  <script src=\"//netdna.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js\"></script>\n</body>\n</html>\n"
  erb.result(binding).force_encoding("UTF-8")
}
FILE_CONVERTER =
-> (wiki, template, file, env) {
  path = Rack::Utils.unescape(env["PATH_INFO"])
  return file unless Nwiki::Utils.orgfile?(path)
  page_title = Nwiki::Utils.page_title(path)
  file.force_encoding("UTF-8")
  html = Orgmode::Parser.new(file, offset: 1).to_html
  template.call(wiki, page_title, html)
}.curry
DIRECTORY_CONVERTER =
-> (wiki, template, file, env) {
  path = Rack::Utils.unescape(env["PATH_INFO"])
  if path == '/'
    page_title = Nwiki::Utils.page_title(path)
    html = wiki.find_directory("/").to_html
    template.call(wiki, page_title, html)
  else
    page_title = Nwiki::Utils.page_title(path)
    list = dirs.
      each { |d| d.force_encoding("UTF-8") }.
      map  { |e| Nwiki::Utils.strip_org(e) }.
      map  { |e| %Q!<li><a href="#{e.gsub('#', '%23')}">#{e}</a></li>! }
    html = "<ul><li><a href=\"../\">../</a></li>#{list.join}</ul>"
    template.call(wiki, page_title, html)
  end
}.curry

Instance Method Summary collapse

Constructor Details

#initialize(git_repo_path) ⇒ App

Returns a new instance of App.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nwiki/frontend/app.rb', line 74

def initialize git_repo_path
  wiki = Core::Wiki.new git_repo_path

  @builder = Rack::Builder.new {
    map '/' do
      run Top.new git_repo_path
    end
    map '/articles.xml' do
      run Feed.new git_repo_path, articles_path: '/articles'
    end
    map '/articles' do
      use Rack::Rewrite do
        rewrite %r{^(.*)$}, '$1.org', if: -> (env) {
          path = Rack::Utils.unescape(env["PATH_INFO"])
          path !~ /\/$/ && File.extname(path) !~ /(png|jpg|gif)/
        }
      end
      run Rack::Git::File.new git_repo_path,
        file_converter: FILE_CONVERTER.call(wiki, TEMPLATE),
        directory_converter: DIRECTORY_CONVERTER.call(wiki, TEMPLATE)
    end
  }
end

Instance Method Details

#call(env) ⇒ Object



98
99
100
# File 'lib/nwiki/frontend/app.rb', line 98

def call env
  @builder.call env
end