Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
serve example docs
  • Loading branch information
svenfuchs committed May 22, 2019
1 parent 9fec86e commit 5256f22
Show file tree
Hide file tree
Showing 138 changed files with 7,705 additions and 29 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -6,6 +6,7 @@ group :web do
gem 'puma', '~> 3.12.0'
gem 'rack', '~> 2.0.6'
gem 'rack-cors', '~> 1.0.2'
gem 'rack-markdown'
gem 'rack-ssl-enforcer', '~> 0.2.9'
gem 'sentry-raven', '~> 2.9.0'
gem 'travis-config', '~> 1.1.3'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Expand Up @@ -28,6 +28,7 @@ GEM
method_source (0.8.2)
mize (0.3.5)
protocol
multi_json (1.13.1)
multipart-post (2.0.0)
neatjson (0.8.4)
oj (3.7.12)
Expand All @@ -39,12 +40,19 @@ GEM
method_source (~> 0.8.1)
slop (~> 3.4)
puma (3.12.0)
pygments.rb (1.2.1)
multi_json (>= 1.0.0)
rack (2.0.6)
rack-cors (1.0.2)
rack-markdown (0.0.3)
pygments.rb
rack
redcarpet
rack-ssl-enforcer (0.2.9)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rake (12.3.1)
redcarpet (3.4.0)
regstry (1.0.2)
rspec (3.7.0)
rspec-core (~> 3.7.0)
Expand Down Expand Up @@ -86,6 +94,7 @@ DEPENDENCIES
puma (~> 3.12.0)
rack (~> 2.0.6)
rack-cors (~> 1.0.2)
rack-markdown
rack-ssl-enforcer (~> 0.2.9)
rack-test
rake
Expand Down
17 changes: 10 additions & 7 deletions config.ru
Expand Up @@ -3,7 +3,13 @@ require 'rack/cors'
require 'rack/ssl-enforcer'
require 'travis/yml/web'

if Travis::Yaml::Web::Env.staging?
env = Travis::Yml::Web::Env

if env.production?
use Rack::SslEnforcer
use Travis::Yml::Web::BasicAuth

elsif env.staging?
use Rack::Cors do
allow do
origins '*'
Expand All @@ -12,15 +18,12 @@ if Travis::Yaml::Web::Env.staging?
end
end

use Rack::SslEnforcer if Travis::Yaml::Web::Env.production?
use Travis::Yaml::Web::BasicAuth unless Travis::Yaml::Web::Env.test?

if ENV['SENTRY_DSN']
if dsn = ENV['SENTRY_DSN']
Raven.configure do |config|
config.dsn = ENV['SENTRY_DSN']
config.dsn = dsn
end
use Raven::Rack
end

run Travis::Yaml::Web
run Travis::Yml::Web

49 changes: 34 additions & 15 deletions lib/travis/yml/docs.rb
@@ -1,4 +1,5 @@
require 'erb'
require 'fileutils'

module Travis
module Yml
Expand Down Expand Up @@ -85,39 +86,57 @@ def yaml(obj)
yml = yml.sub("'on'", 'on')
yml.strip
end

def write
FileUtils.mkdir_p(dir)
File.write(path, render)
end

def path
@path ||= "#{dir}/#{node.id}.md"
end

def dir
"public/docs/#{node.namespace}"
end
end

extend self
extend Helper::Obj
class Index < Page
def render
node.map do |node|
"* [#{node.title}](/v1/docs/#{node.namespace}/#{node.id})"
end.join("\n")
end

def write
pages.map do |name, page|
File.write(path(name), page)
def path
"#{dir}/index.md"
end

def dir
'public/docs/'
end
File.write(path(:index), index)
end

def path(name)
"public/#{name}.md"
extend self
extend Helper::Obj

def write
pages.each(&:write)
index.write
end

def pages
nodes.map do |node|
[[node.namespace, node.id].join('/'), Page.new(node).render]
end.to_h
nodes.map { |node| Page.new(node) }
end

def index
nodes.map do |node|
"* [#{node.title}](/#{node.namespace}/#{node.id}]"
end.join("\n")
Index.new(nodes)
end

def nodes
Schema.schema
nodes = Schema::Type::Node.exports.values.map(&:values).flatten
nodes = nodes.reject(&:internal?)
# nodes = [Schema::Def::Stages.new.node.lookup]
nodes = sort(nodes)
nodes
end
Expand Down
15 changes: 10 additions & 5 deletions lib/travis/yml/web/router.rb
Expand Up @@ -7,15 +7,20 @@ def initialize(map = {})

def call(env)
req = Rack::Request.new(env)
env_path = req.path_info.chomp(?/)
env_path = ?/ if env_path.empty?
path = req.path_info.chomp(?/)
path = ?/ if path.empty?

@map.each do |path, app|
next if env_path.nil? || path.nil?
return app.call(env) if env_path == path
@map.each do |pattern, app|
next if path.nil? || pattern.nil?
return app.call(env) if match?(pattern, path)
end

[404, {}, []]
end

def match?(pattern, path)
return pattern == path unless pattern.include?('/*')
path.start_with?(pattern.sub('/*', ''))
end
end
end
8 changes: 6 additions & 2 deletions lib/travis/yml/web/v1.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
require 'travis/yml/web/router'
require 'travis/yml/web/v1/css'
require 'travis/yml/web/v1/docs'
require 'travis/yml/web/v1/expand'
require 'travis/yml/web/v1/home'
require 'travis/yml/web/v1/parse'
Expand All @@ -14,8 +16,10 @@ def call(env)

def router
@router ||= Router.new(
'/' => V1::Home.new,
'/parse' => V1::Parse.new,
'/' => V1::Home.new,
'/css/*' => V1::Css.new,
'/docs/*' => V1::Docs.new,
'/parse' => V1::Parse.new,
'/expand' => V1::Expand.new
)
end
Expand Down
42 changes: 42 additions & 0 deletions lib/travis/yml/web/v1/css.rb
@@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'oj'
require 'rack/markdown'
require 'travis/yml/web/route'

module Travis::Yml::Web
module V1
class Css
include Route

def get(env)
req = Rack::Request.new(env)
path = req.path_info.chomp(?/)
exists?(path) ? ok(path) : not_found
end

def ok(path)
[200, headers, [read(path)]]
end

def not_found
[404, headers, ['Not found']]
end

def exists?(path)
File.exists?(file(path))
end

def read(path)
File.read(file(path))
end

def file(path)
"./public#{path.sub('..', '')}"
end

def headers
{ 'Content-Type' => 'text/css' }
end
end
end
end
90 changes: 90 additions & 0 deletions lib/travis/yml/web/v1/docs.rb
@@ -0,0 +1,90 @@
# frozen_string_literal: true
require 'oj'
require 'rack/markdown'
require 'travis/yml/web/route'

Rack::Markdown::CSS.class_eval do
def external
hrefs = %w(/v1/css/docs/github.css /v1/css/docs/syntax.css)
links = hrefs.map { |h| "<link rel='stylesheet' type='text/css' href='#{h}' />" }
links.join
end
end

Rack::Markdown::HTML.class_eval do
def body
<<-HTML
<style>
li {
margin: 0;
}
#outer {
display: flex;
}
#index {
flex: 200px;
padding: 30px;
background-color: white;
}
#index ul {
margin: 0;
padding: 0;
}
#index li {
list-style-type: none;
}
#inner {
border: 0;
}
</style>
<div id='outer'>
<div id='index'>
#{index}
</div>
<div id='inner'>
#{rendered}
</div>
</div>
HTML
end
end

Rack::Markdown.class_eval do
def index
Rack::Markdown::Renderer.render(File.read('public/docs/index.md'))
end
end

module Travis::Yml::Web
module V1
class Docs
include Route

def get(env)
req = Rack::Request.new(env)
path = req.path_info.chomp(?/)
exists?(path) ? ok(path) : not_found
end

def ok(path)
Rack::Markdown.new(file(path)).call(nil)
end

def not_found
[404, headers, ['Not found']]
end

def exists?(path)
File.exists?(file(path))
end

def file(path)
"./public#{path.sub('..', '')}.md"
end

def headers
{ 'Content-Type' => 'text/html' }
end
end
end
end

0 comments on commit 5256f22

Please sign in to comment.