Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow request md files with or without the .md extension #66

Merged
merged 4 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/madness/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def set_base_attributes
if File.directory? base
@title = File.basename(path).to_label unless path.empty?
set_base_attributes_for_directory
elsif File.exist? "#{base}.md"
elsif md_file?
@file = md_filename
@title = File.basename(base).to_label
@file = "#{base}.md"
@dir = File.dirname file
@type = :file
end
Expand Down Expand Up @@ -138,6 +138,14 @@ def syntax_highlight(html)
CodeRay.scan(code, lang).html opts
end
end

def md_file?
File.exist?("#{base}.md") || (File.exist?(base) && File.extname(base) == '.md')
end

def md_filename
File.extname(base) == '.md' ? base : "#{base}.md"
end
end
end

2 changes: 1 addition & 1 deletion lib/madness/refinements/string_refinements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def to_slug
# just removes any numbers followed by a dot at the beginning of the
# string, in order to allow "The Invisible Sorting Hand".
def to_label
gsub(/^\d+\.\s+/, '')
gsub(/^\d+\.\s+/, '').gsub(/\.md$/, '')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/madness/server_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ServerBase < Sinatra::Application
# The CommandLine class and the test suite should both call
# `Server.prepare` before calling Server.run!
def self.prepare
use Rack::TryStatic, root: "#{config.path}/", :urls => %w[/]
use Madness::Static, root: "#{config.path}/", :urls => %w[/]
set :bind, config.bind
set :port, config.port

Expand Down
22 changes: 22 additions & 0 deletions lib/madness/static.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Madness

# The Madness::Static middleware delegates requests to Rack::TryStatic middleware
# trying to match a static file when the request doesn't finish with ".md"

class Static

def initialize(app, options)
@app = app
@static = ::Rack::TryStatic.new(app, options)
end

def call(env)
orig_path = env['PATH_INFO']
if orig_path.end_with? ".md"
@app.call(env)
else
@static.call(env)
end
end
end
end
30 changes: 29 additions & 1 deletion spec/madness/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,35 @@
describe '#title' do
it 'returns proper title' do
expect(subject.title).to eq 'File'
end
end
end
end

context "with a file.md" do
subject { described_class.new "Folder/File.md" }

describe '#type' do
it 'returns :file' do
expect(subject.type).to eq :file
end
end

describe '#file' do
it 'returns full path to file' do
expect(subject.file).to end_with 'Folder/File.md'
end
end

describe '#dir' do
it 'returns full path to directory' do
expect(subject.dir).to end_with "#{config.path}/Folder"
end
end

describe '#title' do
it 'returns proper title' do
expect(subject.title).to eq 'File'
end
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/madness/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@
end
end

context "with a nested file with .md extension" do
it "works" do
get '/Folder/File.md'
expect(last_response).to be_ok
expect(last_response.body).to have_tag 'h1', text: "Nested File #1"
end

it "shows breadcrumbs" do
get '/Folder/File.md'
expect(last_response.body).to have_tag '.breadcrumbs' do
with_tag 'a', text: 'Home'
with_tag 'a', text: 'Folder'
with_tag 'span', text: 'File'
end
end
end

context "in an empty folder" do
it "shows index" do
get '/Empty%20Folder'
Expand Down