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 1 commit
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
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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be better in StringRefinements#to_label?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. You are right

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change in this file? Seems to also be related to why the CI tests fail?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I don't know why the output of ls -R #{theme_dir} is different on my system. I'm going to recover the previous content and comment that test in local to be able to check the other ones.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I will change the test to be more tolerant after we merge this.

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