madness 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b47f7237b641f302211469708ee02f3742eb21064be7064363760a2c2aaafaa
4
- data.tar.gz: a632677ece77828952ab2297de025b72d05f22474d05bb897505318b195633d3
3
+ metadata.gz: cced29facd3dba093bbc3b25729fce834ade95ebaeb4086fdb8ad0ee179165d5
4
+ data.tar.gz: 615502287bd4ec1da6b75beddf1d57e2fcd76d71a23b728dc6921d577de7b71d
5
5
  SHA512:
6
- metadata.gz: 25c49d9d314ade07110b7d66d0f2ee3484b2815753477b1f068a14ae95aca13f22bdaffecaea85a8231c0cf9e192b4490e021f8015d6ccd1ccb9046c00518da7
7
- data.tar.gz: cb5f19654df81aa4ea1f15f05289ff4d2aff8a66f3ede34061eba108c02989b62229c52b7cfee17166f7e63bfd679168815d0c497c2263c2bcf66072bbc0ebf7
6
+ metadata.gz: b2d0b4dbf8bae41e71ec0ff9181dac4a0d4254a0219561d50161cb21b93f6abcf0acb6098d430f60c4d2874b05648c91865cf822d251b3bdbeb9894714f48c16
7
+ data.tar.gz: 78462e7922c4ea8164cab394fe9bb1e0fbb14700947c716f40ab7a7c797094cbb9c7317599c33bf1fb786a45f2a4e8c7a88614d4396a1a9aea067ee5f42eea1d
data/README.md CHANGED
@@ -125,6 +125,10 @@ port: 3000
125
125
  # server listen address
126
126
  bind: 0.0.0.0
127
127
 
128
+ # set a server root path
129
+ # base_uri: /docs
130
+ base_uri: ~
131
+
128
132
  # enable sidebar
129
133
  sidebar: true
130
134
 
@@ -1,2 +1,2 @@
1
- a#mobile-search href="/_search"
1
+ a#mobile-search href="#{config.base_uri}/_search"
2
2
  i.icon-search
data/app/views/_nav.slim CHANGED
@@ -1,10 +1,10 @@
1
1
  nav
2
2
  .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?)
4
4
  i.icon-home
5
5
 
6
6
  - 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'
8
8
  i.icon-search
9
9
 
10
10
  - if nav.caption
@@ -8,14 +8,14 @@ html
8
8
  title = locals[:title]
9
9
  meta name="HandheldFriendly" content="true"
10
10
  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'
12
12
  - 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'
14
14
 
15
15
  - 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"
19
19
 
20
20
  body
21
21
  == yield
@@ -1,7 +1,7 @@
1
1
  == slim :_nav, locals: { nav: nav } if config.sidebar
2
2
 
3
3
  .main class=(config.sidebar ? 'with-sidebar' : 'without-sidebar')
4
- form.search-form action="/_search" method="get"
4
+ form.search-form action="#{config.base_uri}/_search" method="get"
5
5
  input.search-field type="text" name="q" value="#{params[:q]}" placeholder="Search (use quotes for exact match)" autofocus=true
6
6
  input type="submit" value="Search" style='display:none'
7
7
 
@@ -17,7 +17,7 @@ module Madness
17
17
  private
18
18
 
19
19
  def breadcrumbs
20
- home = OpenStruct.new({ label: 'Home', href: '/' })
20
+ home = OpenStruct.new({ label: 'Home', href: "#{config.base_uri}/" })
21
21
  result = breadcrumbs_maker(path).reverse.unshift home
22
22
  result.last.last = true
23
23
  result
@@ -28,12 +28,16 @@ module Madness
28
28
  item = OpenStruct.new(
29
29
  {
30
30
  label: basename.to_label,
31
- href: "/#{partial_path}",
31
+ href: "#{config.base_uri}/#{partial_path}",
32
32
  }
33
33
  )
34
34
  result = [item]
35
35
  result += breadcrumbs_maker parent unless parent == '.'
36
36
  result
37
37
  end
38
+
39
+ def config
40
+ Settings.instance
41
+ end
38
42
  end
39
43
  end
data/lib/madness/item.rb CHANGED
@@ -15,7 +15,10 @@ module Madness
15
15
  end
16
16
 
17
17
  def href
18
- path_without_extension.sub(/^#{docroot}/, '').to_href
18
+ @href ||= begin
19
+ result = path_without_extension.sub(/^#{docroot}/, '').to_href
20
+ "#{config.base_uri}#{result}"
21
+ end
19
22
  end
20
23
 
21
24
  def dir?
@@ -5,7 +5,17 @@ module Madness
5
5
  class Server < ServerBase
6
6
  using StringRefinements
7
7
 
8
- get '/_search' do
8
+ if config.base_uri
9
+ not_found do
10
+ redirect "#{config.base_uri}/"
11
+ end
12
+
13
+ get config.base_uri do
14
+ redirect "#{config.base_uri}/"
15
+ end
16
+ end
17
+
18
+ get "#{config.base_uri}/_search" do
9
19
  query = params[:q]
10
20
  results = query ? Search.new.search(query) : false
11
21
  nav = Navigation.new docroot
@@ -15,15 +25,18 @@ module Madness
15
25
  }
16
26
  end
17
27
 
18
- get '/*' do
28
+ get "#{config.base_uri}/*" do
19
29
  path = params[:splat].first
30
+ static_file = find_static_file path
31
+
32
+ next send_file static_file if static_file
20
33
 
21
34
  doc = Document.new path
22
35
  dir = doc.dir
23
36
  content = doc.content
24
37
 
25
38
  if (doc.type == :readme) && !path.empty? && (path[-1] != '/')
26
- redirect "#{path.to_href}/"
39
+ redirect "#{request.path_info}/"
27
40
  end
28
41
 
29
42
  nav = Navigation.new dir
@@ -13,36 +13,29 @@ module Madness
13
13
  set :root, File.expand_path('../../', __dir__)
14
14
  set :environment, ENV['MADNESS_ENV'] || :production
15
15
  set :server, :puma
16
+ set :static, false
16
17
 
17
18
  # Since we cannot use any config values in the main body of the class,
18
19
  # since they will be updated later, we need to set anything that relys
19
20
  # on the config values just before running the server.
20
21
  # The CommandLine class and the test suite should both call
21
22
  # `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
23
+ class << self
24
+ include ServerHelper
26
25
 
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
33
-
34
- set :views, theme.views_path
35
- set :public_folder, theme.public_path
36
- end
26
+ def prepare
27
+ set :bind, config.bind
28
+ set :port, config.port
29
+ set :views, theme.views_path
37
30
 
38
- def self.set_basic_auth
39
- use Rack::Auth::Basic, config.auth_zone do |username, password|
40
- config.auth.split(':') == [username, password]
31
+ set_basic_auth if config.auth
41
32
  end
42
- end
43
33
 
44
- def self.config
45
- Settings.instance
34
+ def set_basic_auth
35
+ use Rack::Auth::Basic, config.auth_zone do |username, password|
36
+ config.auth.split(':') == [username, password]
37
+ end
38
+ end
46
39
  end
47
40
  end
48
41
  end
@@ -10,6 +10,10 @@ module Madness
10
10
  @docroot ||= File.expand_path(config.path, Dir.pwd)
11
11
  end
12
12
 
13
+ def theme
14
+ @theme ||= Theme.new config.theme
15
+ end
16
+
13
17
  def log(obj)
14
18
  # :nocov:
15
19
  open('madness.log', 'a') do |f|
@@ -17,5 +21,26 @@ module Madness
17
21
  end
18
22
  # :nocov:
19
23
  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
+ return nil if disallowed_static?(path)
29
+
30
+ candidates = [
31
+ "#{config.path}/#{path}",
32
+ "#{theme.public_path}/#{path}",
33
+ ]
34
+
35
+ candidates.each do |candidate|
36
+ return candidate if File.file? candidate
37
+ end
38
+
39
+ nil
40
+ end
41
+
42
+ def disallowed_static?(path)
43
+ path.end_with?('.md') || path.empty? || File.basename(path).start_with?('.')
44
+ end
20
45
  end
21
46
  end
@@ -56,6 +56,7 @@ module Madness
56
56
  path: '.',
57
57
  port: 3000,
58
58
  bind: '0.0.0.0',
59
+ base_uri: nil,
59
60
  sidebar: true,
60
61
  auto_h1: true,
61
62
  auto_nav: true,
@@ -11,6 +11,10 @@ port: 3000
11
11
  # server listen address
12
12
  bind: 0.0.0.0
13
13
 
14
+ # set a server root path
15
+ # base_uri: /docs
16
+ base_uri: ~
17
+
14
18
  # enable sidebar
15
19
  sidebar: true
16
20
 
@@ -1,3 +1,3 @@
1
1
  module Madness
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/lib/madness.rb CHANGED
@@ -6,5 +6,6 @@ end
6
6
  require 'requires'
7
7
 
8
8
  requires 'madness/refinements'
9
+ requires 'madness/settings'
9
10
  requires 'madness/server_helper'
10
11
  requires 'madness'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: madness
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-17 00:00:00.000000000 Z
11
+ date: 2023-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -266,7 +266,6 @@ files:
266
266
  - lib/madness/server_base.rb
267
267
  - lib/madness/server_helper.rb
268
268
  - lib/madness/settings.rb
269
- - lib/madness/static.rb
270
269
  - lib/madness/table_of_contents.rb
271
270
  - lib/madness/templates/madness.yml
272
271
  - lib/madness/theme.rb
@@ -1,18 +0,0 @@
1
- module Madness
2
- # The Madness::Static middleware delegates requests to
3
- # Rack::Static middleware unless the request URI ends with .md
4
- class Static
5
- def initialize(app, options)
6
- @app = app
7
- @static = Rack::Static.new app, options
8
- end
9
-
10
- def call(env)
11
- if env['PATH_INFO'].end_with? '.md'
12
- @app.call env
13
- else
14
- @static.call env
15
- end
16
- end
17
- end
18
- end