app.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. require 'bundler/setup'
  2. Bundler.require :app
  3. class App < Sinatra::Application
  4. Bundler.require environment
  5. require 'sinatra/cookies'
  6. Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'
  7. configure do
  8. set :sentry_dsn, ENV['SENTRY_DSN']
  9. set :protection, except: [:frame_options, :xss_header]
  10. set :root, Pathname.new(File.expand_path('../..', __FILE__))
  11. set :sprockets, Sprockets::Environment.new(root)
  12. set :assets_prefix, 'assets'
  13. set :assets_path, -> { File.join(public_folder, assets_prefix) }
  14. set :assets_manifest_path, -> { File.join(assets_path, 'manifest.json') }
  15. set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)
  16. require 'yajl/json_gem'
  17. set :docs_prefix, 'docs'
  18. set :docs_host, -> { File.join('', docs_prefix) }
  19. set :docs_path, -> { File.join(public_folder, docs_prefix) }
  20. set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
  21. set :docs, -> { Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc| [doc['slug'], doc] }] }
  22. set :default_docs, %w(css dom dom_events html http javascript)
  23. set :news_path, -> { File.join(root, assets_prefix, 'javascripts', 'news.json') }
  24. set :news, -> { JSON.parse(File.read(news_path)) }
  25. Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
  26. sprockets.append_path(path)
  27. end
  28. Sprockets::Helpers.configure do |config|
  29. config.environment = sprockets
  30. config.prefix = "/#{assets_prefix}"
  31. config.public_path = public_folder
  32. config.protocol = :relative
  33. end
  34. end
  35. configure :test, :development do
  36. require 'active_support/per_thread_registry'
  37. require 'active_support/cache'
  38. sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets')
  39. end
  40. configure :development do
  41. register Sinatra::Reloader
  42. use BetterErrors::Middleware
  43. BetterErrors.application_root = File.expand_path('..', __FILE__)
  44. BetterErrors.editor = :sublime
  45. end
  46. configure :production do
  47. set :static, false
  48. set :docs_host, '//docs.devdocs.io'
  49. use Rack::ConditionalGet
  50. use Rack::ETag
  51. use Rack::Deflater
  52. use Rack::Static,
  53. root: 'public',
  54. urls: %w(/assets /docs/ /images /favicon.ico /robots.txt /opensearch.xml /manifest.webapp),
  55. header_rules: [
  56. [:all, {'Cache-Control' => 'no-cache, max-age=0'}],
  57. ['/assets', {'Cache-Control' => 'public, max-age=604800'}],
  58. ['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
  59. ['/images', {'Cache-Control' => 'public, max-age=86400'}] ]
  60. sprockets.js_compressor = Uglifier.new output: { beautify: true, indent_level: 0 }
  61. sprockets.css_compressor = :sass
  62. Sprockets::Helpers.configure do |config|
  63. config.digest = true
  64. config.asset_host = 'cdn.devdocs.io'
  65. config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
  66. end
  67. end
  68. configure :test do
  69. set :docs_manifest_path, -> { File.join(root, 'test', 'files', 'docs.json') }
  70. end
  71. helpers do
  72. include Sinatra::Cookies
  73. include Sprockets::Helpers
  74. def browser
  75. @browser ||= Browser.new ua: request.user_agent
  76. end
  77. def unsupported_browser?
  78. browser.ie? && %w(6 7 8 9).include?(browser.version)
  79. end
  80. def docs
  81. @docs ||= begin
  82. cookie = cookies[:docs]
  83. docs = if cookie.nil? || cookie.empty?
  84. settings.default_docs
  85. else
  86. cookie.split('/')
  87. end
  88. end
  89. end
  90. def doc_index_urls
  91. docs.inject [] do |result, slug|
  92. if doc = settings.docs[slug]
  93. result << File.join('', settings.docs_prefix, doc['index_path']) + "?#{doc['mtime']}"
  94. end
  95. result
  96. end
  97. end
  98. def doc_index_page?
  99. @doc && request.path == "/#{@doc['slug']}/"
  100. end
  101. def query_string_for_redirection
  102. request.query_string.empty? ? nil : "?#{request.query_string}"
  103. end
  104. def main_stylesheet_path
  105. stylesheet_paths[dark_theme? ? :dark : :default]
  106. end
  107. def alternate_stylesheet_path
  108. stylesheet_paths[dark_theme? ? :default : :dark]
  109. end
  110. def stylesheet_paths
  111. @stylesheet_paths ||= {
  112. default: stylesheet_path('application'),
  113. dark: stylesheet_path('application-dark')
  114. }
  115. end
  116. def app_size
  117. @app_size ||= cookies[:size].nil? ? '18rem' : "#{cookies[:size]}px"
  118. end
  119. def app_layout
  120. cookies[:layout]
  121. end
  122. def app_theme
  123. @app_theme ||= cookies[:dark].nil? ? 'default' : 'dark'
  124. end
  125. def dark_theme?
  126. app_theme == 'dark'
  127. end
  128. def redirect_via_js(path) # courtesy of HTML5 App Cache
  129. response.set_cookie :initial_path, value: path, expires: Time.now + 15, path: '/'
  130. redirect '/', 302
  131. end
  132. def supports_js_redirection?
  133. browser.modern? && !cookies.empty?
  134. end
  135. end
  136. before do
  137. halt erb :unsupported if unsupported_browser?
  138. end
  139. get '/manifest.appcache' do
  140. content_type 'text/cache-manifest'
  141. expires 0, :'no-cache'
  142. erb :manifest
  143. end
  144. get '/' do
  145. return redirect '/' unless request.query_string.empty? # courtesy of HTML5 App Cache
  146. erb :index
  147. end
  148. %w(offline about news help).each do |page|
  149. get "/#{page}" do
  150. if supports_js_redirection?
  151. redirect_via_js "/#{page}"
  152. else
  153. redirect "/#/#{page}", 302
  154. end
  155. end
  156. end
  157. get '/search' do
  158. redirect "/#q=#{params[:q]}"
  159. end
  160. get '/ping' do
  161. 200
  162. end
  163. %w(docs.json application.js application.css).each do |asset|
  164. class_eval <<-CODE, __FILE__, __LINE__ + 1
  165. get '/#{asset}' do
  166. redirect asset_path('#{asset}', protocol: 'http')
  167. end
  168. CODE
  169. end
  170. {
  171. '/s/maxcdn' => 'https://www.maxcdn.com/?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  172. '/s/shopify' => 'https://www.shopify.com/careers?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  173. '/s/jetbrains' => 'https://www.jetbrains.com/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  174. '/s/jetbrains/ruby' => 'https://www.jetbrains.com/ruby/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  175. '/s/jetbrains/python' => 'https://www.jetbrains.com/pycharm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  176. '/s/jetbrains/c' => 'https://www.jetbrains.com/clion/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  177. '/s/jetbrains/web' => 'https://www.jetbrains.com/webstorm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  178. '/s/code-school' => 'http://www.codeschool.com/?utm_campaign=devdocs&utm_content=homepage&utm_source=devdocs&utm_medium=sponsorship',
  179. '/s/tw' => 'https://twitter.com/intent/tweet?url=http%3A%2F%2Fdevdocs.io&via=DevDocs&text=All-in-one%2C%20offline%20API%20documentation%20browser%3A',
  180. '/s/fb' => 'https://twitter.com/intent/tweet?url=http%3A%2F%2Fdevdocs.io&via=DevDocs&text=All-in-one%2C%20offline%20API%20documentation%20browser%3A',
  181. '/s/re' => 'http://www.reddit.com/submit?url=http%3A%2F%2Fdevdocs.io&title=All-in-one%2C%20offline%20API%20documentation%20browser&resubmit=true'
  182. }.each do |path, url|
  183. class_eval <<-CODE, __FILE__, __LINE__ + 1
  184. get '#{path}' do
  185. redirect '#{url}'
  186. end
  187. CODE
  188. end
  189. get %r{\A/feed(?:\.atom)?\z} do
  190. content_type 'application/atom+xml'
  191. settings.news_feed
  192. end
  193. get %r{\A/(\w+)(\-[\w\-]+)?(/.*)?\z} do |doc, type, rest|
  194. return 404 unless @doc = settings.docs[doc]
  195. if rest.nil?
  196. redirect "/#{doc}#{type}/#{query_string_for_redirection}"
  197. elsif rest.length > 1 && rest.end_with?('/')
  198. redirect "/#{doc}#{type}#{rest[0...-1]}#{query_string_for_redirection}"
  199. elsif docs.include?(doc) && supports_js_redirection?
  200. redirect_via_js(request.path)
  201. else
  202. erb :other
  203. end
  204. end
  205. not_found do
  206. send_file File.join(settings.public_folder, '404.html'), status: status
  207. end
  208. error do
  209. send_file File.join(settings.public_folder, '500.html'), status: status
  210. end
  211. configure do
  212. require 'rss'
  213. feed = RSS::Maker.make('atom') do |maker|
  214. maker.channel.id = 'tag:devdocs.io,2014:/feed'
  215. maker.channel.title = 'DevDocs'
  216. maker.channel.author = 'DevDocs'
  217. maker.channel.updated = "#{settings.news.first.first}T14:00:00Z"
  218. maker.channel.links.new_link do |link|
  219. link.rel = 'self'
  220. link.href = 'http://devdocs.io/feed.atom'
  221. link.type = 'application/atom+xml'
  222. end
  223. maker.channel.links.new_link do |link|
  224. link.rel = 'alternate'
  225. link.href = 'http://devdocs.io/'
  226. link.type = 'text/html'
  227. end
  228. news.each_with_index do |news, i|
  229. maker.items.new_item do |item|
  230. item.id = "tag:devdocs.io,2014:News/#{settings.news.length - i}"
  231. item.title = news[1].split("\n").first.gsub(/<\/?[^>]*>/, '')
  232. item.description do |desc|
  233. desc.content = news[1..-1].join.gsub("\n", '<br>').gsub('href="/', 'href="http://devdocs.io/')
  234. desc.type = 'html'
  235. end
  236. item.updated = "#{news.first}T14:00:00Z"
  237. item.published = "#{news.first}T14:00:00Z"
  238. item.links.new_link do |link|
  239. link.rel = 'alternate'
  240. link.href = 'http://devdocs.io/'
  241. link.type = 'text/html'
  242. end
  243. end
  244. end
  245. end
  246. set :news_feed, feed.to_s
  247. end
  248. end