docs.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. require 'bundler/setup'
  2. Bundler.require :default, :docs
  3. require 'active_support'
  4. require 'active_support/core_ext'
  5. I18n.enforce_available_locales = true
  6. module Docs
  7. require 'docs/core/autoload_helper'
  8. extend AutoloadHelper
  9. mattr_reader :root_path
  10. @@root_path = File.expand_path '..', __FILE__
  11. autoload :URL, 'docs/core/url'
  12. autoload_all 'docs/core'
  13. autoload_all 'docs/filters/core', 'filter'
  14. autoload_all 'docs/scrapers'
  15. autoload_all 'docs/storage'
  16. autoload_all 'docs/subscribers'
  17. mattr_accessor :store_class
  18. self.store_class = FileStore
  19. mattr_accessor :store_path
  20. self.store_path = File.expand_path '../public/docs', @@root_path
  21. mattr_accessor :rescue_errors
  22. self.rescue_errors = false
  23. class DocNotFound < NameError; end
  24. class SetupError < StandardError; end
  25. def self.all
  26. Dir["#{root_path}/docs/scrapers/**/*.rb"].
  27. map { |file| File.basename(file, '.rb') }.
  28. map { |name| const_get(name.camelize) }.
  29. sort { |a, b| a.name.casecmp(b.name) }.
  30. reject(&:abstract)
  31. end
  32. def self.all_versions
  33. all.flat_map(&:versions)
  34. end
  35. def self.defaults
  36. %w(css dom html http javascript).map(&method(:find))
  37. end
  38. def self.installed
  39. Dir["#{store_path}/**/index.json"].
  40. map { |file| file[%r{/([^/]*)/index\.json\z}, 1] }.
  41. sort!.
  42. map { |path| all_versions.find { |doc| doc.path == path } }.
  43. compact
  44. end
  45. def self.find(name, version = nil)
  46. const = name.camelize
  47. doc = const_get(const)
  48. if version.present?
  49. doc = doc.versions.find { |klass| klass.version == version || klass.version_slug == version }
  50. raise DocNotFound.new(%(could not find version "#{version}" for doc "#{name}"), name) unless doc
  51. elsif version != false
  52. doc = doc.versions.first
  53. end
  54. doc
  55. rescue NameError => error
  56. if error.name.to_s == const
  57. raise DocNotFound.new(%(could not find doc "#{name}"), name)
  58. else
  59. raise error
  60. end
  61. end
  62. def self.find_by_slug(slug, version = nil)
  63. doc = all.find { |klass| klass.slug == slug }
  64. unless doc
  65. raise DocNotFound.new(%(could not find doc with "#{slug}"), slug)
  66. end
  67. if version.present?
  68. version = doc.versions.find { |klass| klass.version == version || klass.version_slug == version }
  69. raise DocNotFound.new(%(could not find version "#{version}" for doc "#{doc.name}"), doc.name) unless version
  70. doc = version
  71. end
  72. doc
  73. end
  74. def self.generate_page(name, version, page_id)
  75. find(name, version).store_page(store, page_id)
  76. end
  77. def self.generate(doc, version = nil)
  78. doc = find(doc, version) unless doc.respond_to?(:store_pages)
  79. doc.store_pages(store)
  80. end
  81. def self.generate_manifest
  82. Manifest.new(store, all_versions).store
  83. end
  84. def self.store
  85. store_class.new(store_path)
  86. end
  87. def self.aliases
  88. {
  89. 'angular' => 'ng',
  90. 'angular.js' => 'ng',
  91. 'backbone' => 'bb',
  92. 'cpp' => 'c++',
  93. 'coffeescript' => 'cs',
  94. 'crystal' => 'cr',
  95. 'elixir' => 'ex',
  96. 'javascript' => 'js',
  97. 'julia' => 'jl',
  98. 'jquery' => '$',
  99. 'knockout' => 'ko',
  100. 'kubernetes' => 'k8s',
  101. 'less' => 'ls',
  102. 'lodash' => '_',
  103. 'love' => 'löve',
  104. 'marionette' => 'mn',
  105. 'markdown' => 'md',
  106. 'matplotlib' => 'mpl',
  107. 'modernizr' => 'mdr',
  108. 'moment' => 'mt',
  109. 'openjdk' => 'java',
  110. 'nginx' => 'ngx',
  111. 'numpy' => 'np',
  112. 'pandas' => 'pd',
  113. 'postgresql' => 'pg',
  114. 'python' => 'py',
  115. 'rails' => 'ror',
  116. 'ruby' => 'rb',
  117. 'rust' => 'rs',
  118. 'sass' => 'scss',
  119. 'tensorflow' => 'tf',
  120. 'typescript' => 'ts',
  121. 'underscore.js' => '_',
  122. }
  123. end
  124. extend Instrumentable
  125. def self.install_report(*names)
  126. names.each do |name|
  127. const_get("#{name}_subscriber".camelize).subscribe_to(self)
  128. end
  129. end
  130. end