Skip to content

Commit

Permalink
Allow request md files with or without the .md extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Peña Pita committed Aug 7, 2018
1 parent 48571ee commit 1848ba5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/madness/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_breadcrumbs
def breadcrumbs_maker(partial_path)
parent, basename = File.split partial_path
item = OpenStruct.new({
label: basename.to_label,
label: File.basename(basename, '.md').to_label,
href: "/#{partial_path}" }
)
result = [item]
Expand Down
6 changes: 3 additions & 3 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"
@title = File.basename(base).to_label
@file = "#{base}.md"
elsif File.exist?("#{base}.md") || (File.exist?(base) && File.extname(base) == '.md')
@file = File.extname(base) == '.md' ? base : "#{base}.md"
@title = File.basename(@file, '.md').to_label
@dir = File.dirname file
@type = :file
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
9 changes: 4 additions & 5 deletions spec/fixtures/cli/theme-ls
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
tmp/theme-test:
public
styles
views
Expand All @@ -22,26 +21,26 @@ fontello.woff2
tmp/theme-test/styles:
_anchor.scss
_breadcrumbs.scss
_coderay.scss
_code.scss
_coderay.scss
_general.scss
_icons.scss
_image.scss
_keyboard.scss
_line.scss
_list.scss
main.scss
_manifest.scss
_mixins.scss
_nav.scss
_search.scss
_table.scss
_typography.scss
main.scss

tmp/theme-test/views:
_breadcrumbs.slim
document.slim
_index_nav.slim
layout.slim
_nav.slim
document.slim
layout.slim
search.slim
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

0 comments on commit 1848ba5

Please sign in to comment.