Class: Nwiki::Frontend::App

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

Constant Summary collapse

TEMPLATE =
-> (wiki, page_title, html) {
  template_file = Pathname.new(__FILE__).parent.join("template.html.erb")
  erb = ERB.new(template_file.read)
  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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nwiki/frontend/app.rb', line 47

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|svg)/
        }
      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



71
72
73
# File 'lib/nwiki/frontend/app.rb', line 71

def call env
  @builder.call env
end