Skip to content

Commit

Permalink
Merge pull request #144 from DannyBen/add/base-uri
Browse files Browse the repository at this point in the history
Change how static files are served and add `base_uri` config
  • Loading branch information
DannyBen authored Feb 22, 2023
2 parents a22f6a1 + d0ce5d7 commit 0ea5daa
Show file tree
Hide file tree
Showing 39 changed files with 296 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
bundler-cache: true

- name: Run tests
run: bundle exec rspec
run: bundle exec rspec && STANDALONE=1 bundle exec rspec
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ port: 3000
# server listen address
bind: 0.0.0.0

# set a server root path
# base_uri: /docs
base_uri: ~

# enable sidebar
sidebar: true

Expand Down
7 changes: 6 additions & 1 deletion Runfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ version Madness::VERSION
import_gem 'runfile-tasks/gem'
import 'debug'

help "Generate public CSS"
help 'Run specs'
action :spec do
system 'rspec && STANDALONE=1 rspec'
end

help 'Generate public CSS'
usage 'css [--watch]'
option '--watch, -w', 'Watch for changes and regenerate'
action :css do |args|
Expand Down
2 changes: 1 addition & 1 deletion app/views/_mobile_search.slim
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
a#mobile-search href="/_search"
a#mobile-search href="#{config.base_uri}/_search"
i.icon-search
4 changes: 2 additions & 2 deletions app/views/_nav.slim
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
nav
.icon-bar
a.icon href="/" accesskey='h' class=('wide' unless nav.with_search?)
a.icon href="#{config.base_uri}/" accesskey='h' class=('wide' unless nav.with_search?)
i.icon-home

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

- if nav.caption
Expand Down
10 changes: 5 additions & 5 deletions app/views/layout.slim
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ html
title = locals[:title]
meta name="HandheldFriendly" content="true"
meta name="viewport" content="width=device-width, initial-scale=1"
link href='/css/main.css' rel='stylesheet' type='text/css'
link href='#{config.base_uri}/css/main.css' rel='stylesheet' type='text/css'
- custom_css.each do |file|
link href="/#{file}" rel='stylesheet' type='text/css'
link href="#{config.base_uri}/#{file}" rel='stylesheet' type='text/css'

- if config.copy_code
script src="/js/vendor/jquery.min.js"
script src="/js/vendor/clipboard.min.js"
script src="/js/clipboard.js"
script src="#{config.base_uri}/js/vendor/jquery.min.js"
script src="#{config.base_uri}/js/vendor/clipboard.min.js"
script src="#{config.base_uri}/js/clipboard.js"

body
== yield
2 changes: 1 addition & 1 deletion app/views/search.slim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
== slim :_nav, locals: { nav: nav } if config.sidebar

.main class=(config.sidebar ? 'with-sidebar' : 'without-sidebar')
form.search-form action="/_search" method="get"
form.search-form action="#{config.base_uri}/_search" method="get"
input.search-field type="text" name="q" value="#{params[:q]}" placeholder="Search (use quotes for exact match)" autofocus=true
input type="submit" value="Search" style='display:none'

Expand Down
18 changes: 18 additions & 0 deletions edge/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM dannyben/alpine-ruby

ENV PS1 "\n\n>> madness \W \$ "
RUN apk add --no-cache git

ARG branch=master

WORKDIR /madness
RUN git clone --branch $branch --depth 1 https://github.com/DannyBen/madness.git .
RUN gem build madness.gemspec
RUN gem install madness*.gem

WORKDIR /docs
RUN rm -rf /madness

VOLUME /docs
EXPOSE 3000
ENTRYPOINT ["madness"]
22 changes: 22 additions & 0 deletions edge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Testing Unreleased (Edge) Madness Versions

If you wish to try a version of madness straight from GitHub before it is
released, you can use one of these methods:

## Using Docker

```bash
alias madness_edge='docker run --rm -it --user $(id -u):$(id -g) --publish 3000:3000 --volume "$PWD:/docs" dannyben/madness:edge'
```

or build your own image with [this Dockerfile](Dockerfile).

## Using Ruby

```bash
git clone --depth 1 https://github.com/DannyBen/madness.git
cd madness
gem build madness.gemspec
gem install madness*.gem
cd ..
```
2 changes: 2 additions & 0 deletions edge/op.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build: docker build --build-arg=branch=${1:-master} --no-cache -t dannyben/madness:edge . && docker images |grep madness
push: docker push dannyben/madness:edge
1 change: 1 addition & 0 deletions lib/madness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
require 'requires'

requires 'madness/refinements'
requires 'madness/settings'
requires 'madness/server_helper'
requires 'madness'
8 changes: 6 additions & 2 deletions lib/madness/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def links
private

def breadcrumbs
home = OpenStruct.new({ label: 'Home', href: '/' })
home = OpenStruct.new({ label: 'Home', href: "#{config.base_uri}/" })
result = breadcrumbs_maker(path).reverse.unshift home
result.last.last = true
result
Expand All @@ -28,12 +28,16 @@ def breadcrumbs_maker(partial_path)
item = OpenStruct.new(
{
label: basename.to_label,
href: "/#{partial_path}",
href: "#{config.base_uri}/#{partial_path}",
}
)
result = [item]
result += breadcrumbs_maker parent unless parent == '.'
result
end

def config
Settings.instance
end
end
end
5 changes: 4 additions & 1 deletion lib/madness/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def label
end

def href
path_without_extension.sub(/^#{docroot}/, '').to_href
@href ||= begin
result = path_without_extension.sub(/^#{docroot}/, '').to_href
"#{config.base_uri}#{result}"
end
end

def dir?
Expand Down
19 changes: 16 additions & 3 deletions lib/madness/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ module Madness
class Server < ServerBase
using StringRefinements

get '/_search' do
if config.base_uri
not_found do
redirect "#{config.base_uri}/"
end

get config.base_uri do
redirect "#{config.base_uri}/"
end
end

get "#{config.base_uri}/_search" do
query = params[:q]
results = query ? Search.new.search(query) : false
nav = Navigation.new docroot
Expand All @@ -15,15 +25,18 @@ class Server < ServerBase
}
end

get '/*' do
get "#{config.base_uri}/*" do
path = params[:splat].first
static_file = find_static_file path

next send_file static_file if static_file

doc = Document.new path
dir = doc.dir
content = doc.content

if (doc.type == :readme) && !path.empty? && (path[-1] != '/')
redirect "#{path.to_href}/"
redirect "#{request.path_info}/"
end

nav = Navigation.new dir
Expand Down
33 changes: 13 additions & 20 deletions lib/madness/server_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,29 @@ class ServerBase < Sinatra::Application
set :root, File.expand_path('../../', __dir__)
set :environment, ENV['MADNESS_ENV'] || :production
set :server, :puma
set :static, false

# Since we cannot use any config values in the main body of the class,
# since they will be updated later, we need to set anything that relys
# on the config values just before running the server.
# The CommandLine class and the test suite should both call
# `Server.prepare` before calling Server.run!
def self.prepare
use Madness::Static, root: "#{config.path}/", urls: %w[/], cascade: true
set :bind, config.bind
set :port, config.port
class << self
include ServerHelper

set_basic_auth if config.auth
set_tempalate_locations
end

def self.set_tempalate_locations
theme = Theme.new config.theme

set :views, theme.views_path
set :public_folder, theme.public_path
end
def prepare
set :bind, config.bind
set :port, config.port
set :views, theme.views_path

def self.set_basic_auth
use Rack::Auth::Basic, config.auth_zone do |username, password|
config.auth.split(':') == [username, password]
set_basic_auth if config.auth
end
end

def self.config
Settings.instance
def set_basic_auth
use Rack::Auth::Basic, config.auth_zone do |username, password|
config.auth.split(':') == [username, password]
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/madness/server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,37 @@ def docroot
@docroot ||= File.expand_path(config.path, Dir.pwd)
end

def theme
@theme ||= Theme.new config.theme
end

def log(obj)
# :nocov:
open('madness.log', 'a') do |f|
f.puts obj.inspect
end
# :nocov:
end

# Search for static file, first in the users docroot, then in the template
# directory.
def find_static_file(path)
return nil if disallowed_static?(path)

candidates = [
"#{config.path}/#{path}",
"#{theme.public_path}/#{path}",
]

candidates.each do |candidate|
return candidate if File.file? candidate
end

nil
end

def disallowed_static?(path)
path.end_with?('.md') || path.empty? || File.basename(path).start_with?('.')
end
end
end
1 change: 1 addition & 0 deletions lib/madness/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def defaults
path: '.',
port: 3000,
bind: '0.0.0.0',
base_uri: nil,
sidebar: true,
auto_h1: true,
auto_nav: true,
Expand Down
18 changes: 0 additions & 18 deletions lib/madness/static.rb

This file was deleted.

4 changes: 4 additions & 0 deletions lib/madness/templates/madness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ port: 3000
# server listen address
bind: 0.0.0.0

# set a server root path
# base_uri: /docs
base_uri: ~

# enable sidebar
sidebar: true

Expand Down
2 changes: 1 addition & 1 deletion sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ purpose of showcasing the different features of the server.

To view it, simply run

$ madness
$ madness server

in this folder.

Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/config/show
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
path: .
port: 3000
bind: 0.0.0.0
base_uri: ~
sidebar: true
auto_h1: true
auto_nav: true
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/config/show-non-default
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
path: docs
port: 1337
bind: 4.3.2.1
base_uri: ~
sidebar: true
auto_h1: true
auto_nav: true
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/base-uri/.madness.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# .madness.yml
base_uri: /docs
1 change: 1 addition & 0 deletions spec/fixtures/base-uri/Folder/File.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Nested File #1
3 changes: 3 additions & 0 deletions spec/fixtures/base-uri/Folder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sub folder #1

Hello
5 changes: 5 additions & 0 deletions spec/fixtures/base-uri/Folder/Return of the Jedi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Luke Skywalker has returned to his home planet of Tatooine in an attempt to rescue his friend Han Solo from the clutches of the vile gangster Jabba the Hutt.

Little does Luke know that the GALACTIC EMPIRE has secretly begun construction on a new armored space station even more powerful than the first dreaded Death Star.

When completed, this ultimate weapon will spell certain doom for the small band of rebels struggling to restore freedom to the galaxy...
Binary file added spec/fixtures/base-uri/Folder/bob.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions spec/fixtures/base-uri/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a docroot fixture
1 change: 1 addition & 0 deletions spec/fixtures/base-uri/Single/File.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Nested File #2
7 changes: 7 additions & 0 deletions spec/fixtures/base-uri/The Empire Strikes Back.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
It is a dark time for the Rebellion. Although the Death Star has been destroyed, Imperial troops have driven the Rebel forces from their hidden base and pursued them across the galaxy.

Evading the dreaded Imperial Starfleet, a group of freedom fighters led by Luke Skywalker has established a new secret base on the remote ice world of Hoth.

The evil lord Darth Vader, obsessed with finding young Skywalker, has dispatched thousands of remote probes into the far reaches of space....


1 change: 1 addition & 0 deletions spec/fixtures/base-uri/css/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:root { --background-color: black; }
1 change: 0 additions & 1 deletion spec/fixtures/docroot-with-config/docs/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions spec/fixtures/docroot/.some-dotfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
with secrets
Loading

0 comments on commit 0ea5daa

Please sign in to comment.