-
Notifications
You must be signed in to change notification settings - Fork 1
/
docs.rb
156 lines (125 loc) · 3.08 KB
/
docs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require 'rubygems'
require 'sinatra'
require 'haml'
require 'sass'
require 'indextank'
require 'topic'
# require 'rack/coderay'
# use Rack::Coderay, "//pre[@lang]>code"
require 'coderay'
require './lib/term.rb'
require 'rack/codehighlighter'
use Rack::Codehighlighter, :coderay, :markdown => true, :element => "pre>code", :pattern => /\A:::(\w+)\s*(\n|
)/i, :logging => false
configure :production do
ENV['APP_ROOT'] ||= File.dirname(__FILE__)
$:.unshift "#{ENV['APP_ROOT']}/vendor/plugins/newrelic_rpm/lib"
require 'newrelic_rpm'
end
$LOAD_PATH << File.dirname(__FILE__) + '/lib'
set :app_file, __FILE__
not_found do
erb :not_found
end
# REDIRECTS
get '/getting-started' do
redirect '/dydra'
end
get '/using-dydra' do
redirect '/overview'
end
get '/api' do
redirect '/api/sparql'
end
get '/sdk' do
redirect '/sdk/ruby'
end
#
get '/' do
cache_long
haml :index
end
get '/search' do
page = params[:page].to_i
search, prev_page, next_page = search_for(params[:q], page)
erb :search, :locals => {:search => search, :query => params[:q], :prev_page => prev_page, :next_page => next_page}
end
get '*' do
cache_long
params[:topic] = params[:splat].first[1..-1]
render_topic params[:topic]
end
helpers do
def render_topic(topic)
source = File.read(topic_file(topic))
@topic = Topic.load(topic, source)
@title = @topic.title
@content = @topic.content
@intro = @topic.intro
@toc = @topic.toc
@body = @topic.body
erb :topic
rescue Errno::ENOENT
status 404
end
def search_for(query, page = 0)
client = IndexTank::Client.new(ENV['DYDRA_INDEXTANK_URL'])
index = client.indexes('docs')
search = index.search(query, :start => page * 10, :len => 10, :fetch => 'title', :snippet => 'text')
next_page =
if search['matches'] > (page + 1) * 10
page + 1
end
prev_page =
if page > 0
page - 1
end
[search, prev_page, next_page]
end
def topic_file(topic)
#if topic.include?('/')
# topic
#else
# "#{options.root}/docs/#{topic}.txt"
#end
"#{options.root}/docs/#{topic}.txt"
end
def cache_long
response['Cache-Control'] = "public, max-age=#{60 * 60}" unless development?
end
def slugify(title)
title.downcase.gsub(/[^a-z0-9 -]/, '').gsub(/ /, '-')
end
def sections
TOC.sections
end
def next_section(current_slug, root=sections)
return sections.first if current_slug.nil?
root.each_with_index do |(slug, title, topics), i|
if current_slug == slug and i < root.length-1
return root[i+1]
elsif topics.any?
res = next_section(current_slug, topics)
return res if res
end
end
nil
end
alias_method :h, :escape_html
end
module TOC
extend self
def sections
@sections ||= []
end
# define a section
def section(name, title)
sections << [name, title, []]
yield if block_given?
end
# define a topic
def topic(name, title)
sections.last.last << [name, title, []]
end
file = File.dirname(__FILE__) + '/toc.rb'
eval File.read(file), binding, file
end