Sha256: 124ddaef5276d9d9aebc6c4f8042ae9abcc04cb01ecdfafc38af6d76293e9a0e

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

require 'webrick/httputils'

module YARD
  module Server
    module Commands
      # Serves static content when no other router matches a request
      class StaticFileCommand < LibraryCommand
        include WEBrick::HTTPUtils

        DefaultMimeTypes['js'] = 'text/javascript'

        # Defines the paths used to search for static assets. To define an
        # extra path, use {YARD::Server.register_static_path} rather than
        # modifying this constant directly. Also note that files in the
        # document root will always take precedence over these paths.
        STATIC_PATHS = []

        def run
          assets_template = Templates::Engine.template(:default, :fulldoc, :html)

          file = nil
          ([adapter.document_root] + STATIC_PATHS.reverse).compact.each do |path_prefix|
            file = File.join(path_prefix, path)
            break if File.exist?(file)
            file = nil
          end

          # Search in default/fulldoc/html template if nothing in static asset paths
          file ||= assets_template.find_file(path)

          if file
            ext = "." + (path[/\.(\w+)$/, 1] || "html")
            headers['Content-Type'] = mime_type(ext, DefaultMimeTypes)
            self.body = File.read(file)
            return
          end

          self.body = "Could not find: #{request.path}"
          self.status = 404
        end
      end
    end
  end
end

Version data entries

4 entries across 3 versions & 2 rubygems

Version Path
abaci-0.3.0 vendor/bundle/gems/yard-0.9.1/lib/yard/server/commands/static_file_command.rb
abaci-0.3.0 vendor/bundle/gems/yard-0.9.2/lib/yard/server/commands/static_file_command.rb
yard-0.9.2 lib/yard/server/commands/static_file_command.rb
yard-0.9.1 lib/yard/server/commands/static_file_command.rb