app.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. # frozen_string_literal: true
  2. require 'bundler/setup'
  3. Bundler.require :app
  4. class App < Sinatra::Application
  5. Bundler.require environment
  6. require 'sinatra/cookies'
  7. require 'tilt/erubi'
  8. require 'active_support/notifications'
  9. Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'
  10. configure do
  11. set :sentry_dsn, ENV['SENTRY_DSN']
  12. set :protection, except: [:frame_options, :xss_header]
  13. set :root, Pathname.new(File.expand_path('../..', __FILE__))
  14. set :sprockets, Sprockets::Environment.new(root)
  15. set :cdn_origin, ''
  16. set :assets_prefix, 'assets'
  17. set :assets_path, File.join(public_folder, assets_prefix)
  18. set :assets_manifest_path, File.join(assets_path, 'manifest.json')
  19. set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)
  20. require 'yajl/json_gem'
  21. set :docs_prefix, 'docs'
  22. set :docs_origin, File.join('', docs_prefix)
  23. set :docs_path, File.join(public_folder, docs_prefix)
  24. set :docs_manifest_path, File.join(docs_path, 'docs.json')
  25. set :default_docs, %w(css dom dom_events html http javascript)
  26. set :news_path, File.join(root, assets_prefix, 'javascripts', 'news.json')
  27. set :csp, false
  28. Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
  29. sprockets.append_path(path)
  30. end
  31. Sprockets::Helpers.configure do |config|
  32. config.environment = sprockets
  33. config.prefix = "/#{assets_prefix}"
  34. config.public_path = public_folder
  35. config.protocol = :relative
  36. end
  37. end
  38. configure :test, :development do
  39. require 'active_support/per_thread_registry'
  40. require 'active_support/cache'
  41. sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets', environment.to_s)
  42. end
  43. configure :development do
  44. register Sinatra::Reloader
  45. use BetterErrors::Middleware
  46. BetterErrors.application_root = File.expand_path('..', __FILE__)
  47. BetterErrors.editor = :sublime
  48. set :csp, "default-src 'self' *; script-src 'self' 'nonce-devdocs' *; font-src 'none'; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
  49. end
  50. configure :production do
  51. set :static, false
  52. set :cdn_origin, 'https://cdn.devdocs.io'
  53. set :docs_origin, '//docs.devdocs.io'
  54. set :csp, "default-src 'self' *; script-src 'self' 'nonce-devdocs' http://cdn.devdocs.io https://cdn.devdocs.io https://www.google-analytics.com https://secure.gaug.es http://*.jquery.com https://*.jquery.com; font-src 'none'; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
  55. use Rack::ConditionalGet
  56. use Rack::ETag
  57. use Rack::Deflater
  58. use Rack::Static,
  59. root: 'public',
  60. urls: %w(/assets /docs/ /images /favicon.ico /robots.txt /opensearch.xml /manifest.webapp /mathml.css),
  61. header_rules: [
  62. [:all, {'Cache-Control' => 'no-cache, max-age=0'}],
  63. ['/assets', {'Cache-Control' => 'public, max-age=604800'}],
  64. ['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
  65. ['/mathml.css', {'Cache-Control' => 'public, max-age=604800'}],
  66. ['/images', {'Cache-Control' => 'public, max-age=86400'}] ]
  67. sprockets.js_compressor = Uglifier.new output: { beautify: true, indent_level: 0 }
  68. sprockets.css_compressor = :sass
  69. Sprockets::Helpers.configure do |config|
  70. config.digest = true
  71. config.asset_host = 'cdn.devdocs.io'
  72. config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
  73. end
  74. end
  75. configure :test do
  76. set :docs_manifest_path, File.join(root, 'test', 'files', 'docs.json')
  77. end
  78. def self.parse_docs
  79. Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
  80. doc['full_name'] = doc['name'].dup
  81. doc['full_name'] << " #{doc['version']}" if doc['version']
  82. doc['slug_without_version'] = doc['slug'].split('~').first
  83. [doc['slug'], doc]
  84. }]
  85. end
  86. def self.parse_news
  87. JSON.parse(File.read(news_path))
  88. end
  89. configure :development, :test do
  90. set :docs, -> { parse_docs }
  91. set :news, -> { parse_news }
  92. end
  93. configure :production do
  94. set :docs, parse_docs
  95. set :news, parse_news
  96. end
  97. helpers do
  98. include Sinatra::Cookies
  99. include Sprockets::Helpers
  100. def memoized_cookies
  101. @memoized_cookies ||= cookies.to_hash
  102. end
  103. def canonical_origin
  104. "http://#{request.host_with_port}"
  105. end
  106. def browser
  107. @browser ||= Browser.new(request.user_agent)
  108. end
  109. UNSUPPORTED_IE_VERSIONS = %w(6 7 8 9).freeze
  110. def unsupported_browser?
  111. browser.ie? && UNSUPPORTED_IE_VERSIONS.include?(browser.version)
  112. end
  113. def docs
  114. @docs ||= begin
  115. cookie = memoized_cookies['docs']
  116. if cookie.nil?
  117. settings.default_docs
  118. else
  119. cookie.split('/')
  120. end
  121. end
  122. end
  123. def find_doc(slug)
  124. settings.docs[slug] || begin
  125. settings.docs.each do |_, doc|
  126. return doc if doc['slug_without_version'] == slug
  127. end
  128. nil
  129. end
  130. end
  131. def user_has_docs?(slug)
  132. docs.include?(slug) || begin
  133. slug = "#{slug}~"
  134. docs.any? { |_slug| _slug.start_with?(slug) }
  135. end
  136. end
  137. def doc_index_urls
  138. docs.each_with_object [] do |slug, result|
  139. if doc = settings.docs[slug]
  140. result << File.join('', settings.docs_prefix, slug, 'index.json') + "?#{doc['mtime']}"
  141. end
  142. end
  143. end
  144. def doc_index_page?
  145. @doc && (request.path == "/#{@doc['slug']}/" || request.path == "/#{@doc['slug_without_version']}/")
  146. end
  147. def query_string_for_redirection
  148. request.query_string.empty? ? nil : "?#{request.query_string}"
  149. end
  150. def manifest_asset_urls
  151. @@manifest_asset_urls ||= [
  152. javascript_path('application', asset_host: false),
  153. stylesheet_path('application'),
  154. stylesheet_path('application-dark'),
  155. image_path('icons.png'),
  156. image_path('icons@2x.png'),
  157. image_path('docs-1.png'),
  158. image_path('docs-1@2x.png'),
  159. image_path('docs-2.png'),
  160. image_path('docs-2@2x.png'),
  161. asset_path('docs.js')
  162. ]
  163. end
  164. def main_stylesheet_path
  165. stylesheet_paths[dark_theme? ? :dark : :default]
  166. end
  167. def alternate_stylesheet_path
  168. stylesheet_paths[dark_theme? ? :default : :dark]
  169. end
  170. def stylesheet_paths
  171. @@stylesheet_paths ||= {
  172. default: stylesheet_path('application'),
  173. dark: stylesheet_path('application-dark')
  174. }
  175. end
  176. def app_size
  177. @app_size ||= memoized_cookies['size'].nil? ? '20rem' : "#{memoized_cookies['size']}px"
  178. end
  179. def app_layout
  180. memoized_cookies['layout']
  181. end
  182. def app_theme
  183. @app_theme ||= memoized_cookies['dark'].nil? ? 'default' : 'dark'
  184. end
  185. def dark_theme?
  186. app_theme == 'dark'
  187. end
  188. def redirect_via_js(path) # courtesy of HTML5 App Cache
  189. response.set_cookie :initial_path, value: path, expires: Time.now + 15, path: '/'
  190. redirect '/', 302
  191. end
  192. def supports_js_redirection?
  193. browser.modern? && !memoized_cookies.empty?
  194. end
  195. end
  196. before do
  197. halt erb :unsupported if unsupported_browser?
  198. end
  199. OUT_HOST = 'out.devdocs.io'.freeze
  200. before do
  201. if request.host == OUT_HOST && !request.path.start_with?('/s/')
  202. query_string = "?#{request.query_string}" unless request.query_string.empty?
  203. redirect "http://devdocs.io#{request.path}#{query_string}", 302
  204. end
  205. end
  206. get '/manifest.appcache' do
  207. content_type 'text/cache-manifest'
  208. expires 0, :'no-cache'
  209. erb :manifest
  210. end
  211. get '/' do
  212. return redirect "/#q=#{params[:q]}" if params[:q]
  213. return redirect '/' unless request.query_string.empty? # courtesy of HTML5 App Cache
  214. response.headers['Content-Security-Policy'] = settings.csp if settings.csp
  215. erb :index
  216. end
  217. %w(settings offline about news help).each do |page|
  218. get "/#{page}" do
  219. if supports_js_redirection?
  220. redirect_via_js "/#{page}"
  221. else
  222. redirect "/#/#{page}", 302
  223. end
  224. end
  225. end
  226. get '/search' do
  227. redirect "/#q=#{params[:q]}"
  228. end
  229. get '/ping' do
  230. 200
  231. end
  232. %w(docs.json application.js application.css).each do |asset|
  233. class_eval <<-CODE, __FILE__, __LINE__ + 1
  234. get '/#{asset}' do
  235. redirect asset_path('#{asset}', protocol: 'http')
  236. end
  237. CODE
  238. end
  239. {
  240. '/s/maxcdn' => 'https://www.maxcdn.com/?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  241. '/s/shopify' => 'https://www.shopify.com/careers?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  242. '/s/jetbrains' => 'https://www.jetbrains.com/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  243. '/s/jetbrains/ruby' => 'https://www.jetbrains.com/ruby/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  244. '/s/jetbrains/python' => 'https://www.jetbrains.com/pycharm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  245. '/s/jetbrains/c' => 'https://www.jetbrains.com/clion/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  246. '/s/jetbrains/web' => 'https://www.jetbrains.com/webstorm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  247. '/s/code-school' => 'http://www.codeschool.com/?utm_campaign=devdocs&utm_content=homepage&utm_source=devdocs&utm_medium=sponsorship',
  248. '/s/tw' => 'https://twitter.com/intent/tweet?url=http%3A%2F%2Fdevdocs.io&via=DevDocs&text=All-in-one%20API%20documentation%20browser%20with%20offline%20mode%20and%20instant%20search%3A',
  249. '/s/fb' => 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdevdocs.io',
  250. '/s/re' => 'https://www.reddit.com/submit?url=http%3A%2F%2Fdevdocs.io&title=All-in-one%20API%20documentation%20browser%20with%20offline%20mode%20and%20instant%20search&resubmit=true'
  251. }.each do |path, url|
  252. class_eval <<-CODE, __FILE__, __LINE__ + 1
  253. get '#{path}' do
  254. redirect '#{url}'
  255. end
  256. CODE
  257. end
  258. %w(/maxcdn /maxcdn/).each do |path|
  259. class_eval <<-CODE, __FILE__, __LINE__ + 1
  260. get '#{path}' do
  261. 410
  262. end
  263. CODE
  264. end
  265. {
  266. '/tips' => '/help',
  267. '/css-data-types/' => '/css-values-units/',
  268. '/css-at-rules/' => '/?q=css%20%40',
  269. '/dom/window/setinterval' => '/dom/windoworworkerglobalscope/setinterval',
  270. '/html/article' => '/html/element/article',
  271. '/html-html5/' => 'html-elements/',
  272. '/html-standard/' => 'html-elements/',
  273. '/http-status-codes/' => '/http-status/',
  274. '/ruby/bignum' => '/ruby~2.3/bignum',
  275. '/ruby/fixnum' => '/ruby~2.3/fixnum',
  276. }.each do |path, url|
  277. class_eval <<-CODE, __FILE__, __LINE__ + 1
  278. get '#{path}' do
  279. redirect '#{url}', 301
  280. end
  281. CODE
  282. end
  283. get %r{/feed(?:\.atom)?} do
  284. content_type 'application/atom+xml'
  285. settings.news_feed
  286. end
  287. DOC_REDIRECTS = {
  288. 'iojs' => 'node',
  289. 'node_lts' => 'node~6_lts',
  290. 'node~4.2_lts' => 'node~4_lts',
  291. 'yii1' => 'yii~1.1',
  292. 'python2' => 'python~2.7',
  293. 'xpath' => 'xslt_xpath',
  294. 'angular~2.0_typescript' => 'angular~2_typescript',
  295. 'angular~1.5' => 'angularjs~1.5',
  296. 'angular~1.4' => 'angularjs~1.4',
  297. 'angular~1.3' => 'angularjs~1.3',
  298. 'angular~1.2' => 'angularjs~1.2',
  299. 'codeigniter~3.0' => 'codeigniter~3',
  300. 'webpack~2' => 'webpack'
  301. }
  302. get %r{/([\w~\.%]+)(\-[\w\-]+)?(/.*)?} do |doc, type, rest|
  303. doc.sub! '%7E', '~'
  304. if DOC_REDIRECTS.key?(doc)
  305. return redirect "/#{DOC_REDIRECTS[doc]}#{type}#{rest}", 301
  306. end
  307. if rest && doc == 'angular' && rest.start_with?('/ng')
  308. return redirect "/angularjs/api#{rest}", 301
  309. end
  310. if rest && doc == 'dom'
  311. if rest.start_with?('/windowtimers')
  312. return redirect "/dom#{rest.sub('windowtimers', 'windoworworkerglobalscope')}", 301
  313. end
  314. if rest.start_with?('/window/url.')
  315. return redirect "/dom#{rest.sub('window/url.', 'url/')}", 301
  316. end
  317. if rest.start_with?('/window.')
  318. return redirect "/dom#{rest.sub('window.', 'window/')}", 301
  319. end
  320. if rest.start_with?('/element.')
  321. return redirect "/dom#{rest.sub('element.', 'element/')}", 301
  322. end
  323. if rest.start_with?('/event.')
  324. return redirect "/dom#{rest.sub('event.', 'event/')}", 301
  325. end
  326. if rest.start_with?('/document.')
  327. return redirect "/dom#{rest.sub('document.', 'document/')}", 301
  328. end
  329. end
  330. return 404 unless @doc = find_doc(doc)
  331. if rest.nil?
  332. redirect "/#{doc}#{type}/#{query_string_for_redirection}"
  333. elsif rest.length > 1 && rest.end_with?('/')
  334. redirect "/#{doc}#{type}#{rest[0...-1]}#{query_string_for_redirection}"
  335. elsif user_has_docs?(doc) && supports_js_redirection?
  336. redirect_via_js(request.path)
  337. else
  338. response.headers['Content-Security-Policy'] = settings.csp if settings.csp
  339. erb :other
  340. end
  341. end
  342. not_found do
  343. send_file File.join(settings.public_folder, '404.html'), status: status
  344. end
  345. error do
  346. send_file File.join(settings.public_folder, '500.html'), status: status
  347. end
  348. configure do
  349. require 'rss'
  350. feed = RSS::Maker.make('atom') do |maker|
  351. maker.channel.id = 'tag:devdocs.io,2014:/feed'
  352. maker.channel.title = 'DevDocs'
  353. maker.channel.author = 'DevDocs'
  354. maker.channel.updated = "#{settings.news.first.first}T14:00:00Z"
  355. maker.channel.links.new_link do |link|
  356. link.rel = 'self'
  357. link.href = 'http://devdocs.io/feed.atom'
  358. link.type = 'application/atom+xml'
  359. end
  360. maker.channel.links.new_link do |link|
  361. link.rel = 'alternate'
  362. link.href = 'http://devdocs.io/'
  363. link.type = 'text/html'
  364. end
  365. news.each_with_index do |news, i|
  366. maker.items.new_item do |item|
  367. item.id = "tag:devdocs.io,2014:News/#{settings.news.length - i}"
  368. item.title = news[1].split("\n").first.gsub(/<\/?[^>]*>/, '')
  369. item.description do |desc|
  370. desc.content = news[1..-1].join.gsub("\n", '<br>').gsub('href="/', 'href="http://devdocs.io/')
  371. desc.type = 'html'
  372. end
  373. item.updated = "#{news.first}T14:00:00Z"
  374. item.published = "#{news.first}T14:00:00Z"
  375. item.links.new_link do |link|
  376. link.rel = 'alternate'
  377. link.href = 'http://devdocs.io/'
  378. link.type = 'text/html'
  379. end
  380. end
  381. end
  382. end
  383. set :news_feed, feed.to_s
  384. end
  385. end