Skip to content

Commit 528e2ee

Browse files
committed
- Change how static files are served and add base_uri config
1 parent a22f6a1 commit 528e2ee

12 files changed

Lines changed: 66 additions & 50 deletions

File tree

app/views/_mobile_search.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
a#mobile-search href="/_search"
1+
a#mobile-search href="#{config.base_uri}/_search"
22
i.icon-search

app/views/_nav.slim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
nav
22
.icon-bar
3-
a.icon href="/" accesskey='h' class=('wide' unless nav.with_search?)
3+
a.icon href="#{config.base_uri}/" accesskey='h' class=('wide' unless nav.with_search?)
44
i.icon-home
55

66
- if nav.with_search?
7-
a.icon href="/_search" accesskey='s' style='text-align:right'
7+
a.icon href="#{config.base_uri}/_search" accesskey='s' style='text-align:right'
88
i.icon-search
99

1010
- if nav.caption

app/views/layout.slim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ html
88
title = locals[:title]
99
meta name="HandheldFriendly" content="true"
1010
meta name="viewport" content="width=device-width, initial-scale=1"
11-
link href='/css/main.css' rel='stylesheet' type='text/css'
11+
link href='#{config.base_uri}/css/main.css' rel='stylesheet' type='text/css'
1212
- custom_css.each do |file|
13-
link href="/#{file}" rel='stylesheet' type='text/css'
13+
link href="#{config.base_uri}/#{file}" rel='stylesheet' type='text/css'
1414

1515
- if config.copy_code
16-
script src="/js/vendor/jquery.min.js"
17-
script src="/js/vendor/clipboard.min.js"
18-
script src="/js/clipboard.js"
16+
script src="#{config.base_uri}/js/vendor/jquery.min.js"
17+
script src="#{config.base_uri}/js/vendor/clipboard.min.js"
18+
script src="#{config.base_uri}/js/clipboard.js"
1919

2020
body
2121
== yield

lib/madness.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
require 'requires'
77

88
requires 'madness/refinements'
9+
requires 'madness/settings'
910
requires 'madness/server_helper'
1011
requires 'madness'

lib/madness/breadcrumbs.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def links
1717
private
1818

1919
def breadcrumbs
20-
home = OpenStruct.new({ label: 'Home', href: '/' })
20+
home = OpenStruct.new({ label: 'Home', href: "#{config.base_uri}/" })
2121
result = breadcrumbs_maker(path).reverse.unshift home
2222
result.last.last = true
2323
result
@@ -28,12 +28,16 @@ def breadcrumbs_maker(partial_path)
2828
item = OpenStruct.new(
2929
{
3030
label: basename.to_label,
31-
href: "/#{partial_path}",
31+
href: "#{config.base_uri}/#{partial_path}",
3232
}
3333
)
3434
result = [item]
3535
result += breadcrumbs_maker parent unless parent == '.'
3636
result
3737
end
38+
39+
def config
40+
Settings.instance
41+
end
3842
end
3943
end

lib/madness/item.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def label
1515
end
1616

1717
def href
18-
path_without_extension.sub(/^#{docroot}/, '').to_href
18+
path_without_extension.sub(/^#{docroot}/, config.base_uri).to_href
1919
end
2020

2121
def dir?

lib/madness/server.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ module Madness
55
class Server < ServerBase
66
using StringRefinements
77

8-
get '/_search' do
8+
if config.base_uri
9+
get "#{config.base_uri}" do
10+
redirect "#{config.base_uri}/"
11+
end
12+
end
13+
14+
get "#{config.base_uri}/_search" do
915
query = params[:q]
1016
results = query ? Search.new.search(query) : false
1117
nav = Navigation.new docroot
@@ -15,8 +21,11 @@ class Server < ServerBase
1521
}
1622
end
1723

18-
get '/*' do
24+
get "#{config.base_uri}/*" do
1925
path = params[:splat].first
26+
static_file = find_static_file path
27+
28+
next send_file static_file if static_file
2029

2130
doc = Document.new path
2231
dir = doc.dir

lib/madness/server_base.rb

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,26 @@ class ServerBase < Sinatra::Application
1919
# on the config values just before running the server.
2020
# The CommandLine class and the test suite should both call
2121
# `Server.prepare` before calling Server.run!
22-
def self.prepare
23-
use Madness::Static, root: "#{config.path}/", urls: %w[/], cascade: true
24-
set :bind, config.bind
25-
set :port, config.port
22+
class << self
23+
include ServerHelper
2624

27-
set_basic_auth if config.auth
28-
set_tempalate_locations
29-
end
30-
31-
def self.set_tempalate_locations
32-
theme = Theme.new config.theme
25+
def prepare
26+
set :bind, config.bind
27+
set :port, config.port
3328

34-
set :views, theme.views_path
35-
set :public_folder, theme.public_path
36-
end
29+
set_basic_auth if config.auth
30+
set_tempalate_locations
31+
end
3732

38-
def self.set_basic_auth
39-
use Rack::Auth::Basic, config.auth_zone do |username, password|
40-
config.auth.split(':') == [username, password]
33+
def set_tempalate_locations
34+
set :views, theme.views_path
4135
end
42-
end
4336

44-
def self.config
45-
Settings.instance
37+
def set_basic_auth
38+
use Rack::Auth::Basic, config.auth_zone do |username, password|
39+
config.auth.split(':') == [username, password]
40+
end
41+
end
4642
end
4743
end
4844
end

lib/madness/server_helper.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,31 @@ def docroot
1010
@docroot ||= File.expand_path(config.path, Dir.pwd)
1111
end
1212

13+
def theme
14+
@theme ||= Theme.new config.theme
15+
end
16+
1317
def log(obj)
1418
# :nocov:
1519
open('madness.log', 'a') do |f|
1620
f.puts obj.inspect
1721
end
1822
# :nocov:
1923
end
24+
25+
# Search for static file, first in the users docroot, then in the template
26+
# directory.
27+
def find_static_file(path)
28+
candidates = [
29+
"#{config.path}/#{path}",
30+
"#{theme.public_path}/#{path}",
31+
]
32+
33+
candidates.each do |candidate|
34+
return candidate if File.file? candidate
35+
end
36+
37+
nil
38+
end
2039
end
2140
end

lib/madness/settings.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def defaults
5656
path: '.',
5757
port: 3000,
5858
bind: '0.0.0.0',
59+
base_uri: nil,
5960
sidebar: true,
6061
auto_h1: true,
6162
auto_nav: true,

0 commit comments

Comments
 (0)