Skip to content

Commit

Permalink
Merge pull request #11 from DannyBen/redirect
Browse files Browse the repository at this point in the history
Redirect when a folder contains only a single file
  • Loading branch information
DannyBen committed Jun 12, 2016
2 parents d1ecd2d + 5666ff0 commit c47134d
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Gemfile.lock
.sass-cache
.bundle
.byebug_history
madness.log

*.sublime-*
Binary file added app/public/favicon.ico
Binary file not shown.
2 changes: 0 additions & 2 deletions app/views/document.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
== debug

== slim :_nav, locals: { nav: nav }

main
Expand Down
3 changes: 1 addition & 2 deletions app/views/layout.slim
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
doctype html

head
title Sinatra Baseline
/ link href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' rel='stylesheet' type='text/css'
title = locals[:title]
link href='/css/main.css' rel='stylesheet' type='text/css'

body
Expand Down
2 changes: 1 addition & 1 deletion lib/madness.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'singleton'
require 'yaml'

require 'slim'
require 'rdiscount'
require 'coderay'
require 'yaml'
require 'sass'
require 'sinatra/base'
require "sinatra/reloader"
Expand Down
23 changes: 14 additions & 9 deletions lib/madness/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,27 @@ def content!
end
end

def title
if file =~ /README.md/
result = File.basename File.dirname(file)
else
result = File.basename(file,'.md')
end
result.tr '-', ' '
end

private

def markdown_to_html(html)
html = RDiscount.new(File.read file).to_html
html = syntax_highlight(html) if config.highlighter
html = prepend_h1(html, file) if config.autoh1
html = prepend_h1(html) if config.autoh1
html
end

def prepend_h1(html, filename)
def prepend_h1(html)
unless html[0..3] == "<h1>"
if filename =~ /README.md/
h1 = File.basename File.dirname(filename)
else
h1 = File.basename(filename,'.md')
end

html = "<h1>#{h1}</h1>\n#{html}"
html = "<h1>#{title}</h1>\n#{html}"
end
html
end
Expand Down
2 changes: 1 addition & 1 deletion lib/madness/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(dir)
def link(item, type)
OpenStruct.new({
label: File.basename(item).tr('-', ' '),
href: item.sub(/^#{docroot}/, ''),
href: URI.escape(item.sub(/^#{docroot}/, '')),
type: type
})
end
Expand Down
10 changes: 6 additions & 4 deletions lib/madness/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Madness
class Server < ServerBase
get '/*' do
path = params[:splat].first
debug = nil

doc = Document.new path
dir = doc.dir
Expand All @@ -12,14 +11,17 @@ class Server < ServerBase
nav = Navigation.new(dir)
breadcrumbs = Breadcrumbs.new(path).links

if nav.links.count == 1 and doc.type == :empty
redirect to(nav.links.first.href)
end

slim :document, locals: {
content: content,
type: doc.type,
title: doc.title,
nav: nav,
breadcrumbs: breadcrumbs,
debug: debug
breadcrumbs: breadcrumbs
}
end

end
end
8 changes: 8 additions & 0 deletions lib/madness/server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ def config
def docroot
@docroot ||= File.expand_path(config.path, Dir.pwd)
end

def log(obj)
#:nocov:
open('madness.log', 'a') { |f|
f.puts obj.inspect
}
#:nocov:
end
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/docroot/Redirect/The only file here.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

This is the only file in this folder, therefore when browsing to this
folder, we want to see the file directly, instead of the index.
9 changes: 9 additions & 0 deletions spec/madness/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
end
end

context "in a folder with a single file" do
it "redirects to the file" do
get '/Redirect'
expect(last_response).to be_redirect
follow_redirect!
expect(last_request.url).to match /Redirect\/The%20only%20file%20here/
end
end

describe "sass plugin" do
let(:css) { 'app/public/css/main.css' }

Expand Down

0 comments on commit c47134d

Please sign in to comment.