Sha256: 28f2c1b45840d9fd432b8e27d239f4f4eafabc834118064b05495528f6382f40

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module YARD
  module Server
    module Commands
      # Include this module to get access to {#static_template_file?}
      # and {favicon?} helpers.
      module StaticFileHelpers
        include Server::HTTPUtils

        # Serves an empty favicon.
        # @raise [FinishRequest] finalizes an empty body if the path matches
        #   /favicon.ico so browsers don't complain.
        def favicon?
          return unless request.path == '/favicon.ico'
          headers['Content-Type'] = 'image/png'
          self.status = 200
          self.body = ''
          raise FinishRequest
        end

        # Attempts to route a path to a static template file.
        #
        # @raise [FinishRequest] if a file was found and served
        # @return [void]
        def static_template_file?
          # this const was defined in StaticFileCommand originally
          default_mime_types = StaticFileCommand::DefaultMimeTypes

          file = find_file(adapter, path)

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

        module_function

        def find_file(adapter, url)
          # this const was defined in StaticFileCommand originally
          static_paths = StaticFileCommand::STATIC_PATHS

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

          # Search in default/fulldoc/html template if nothing in static asset paths
          assets_template = Templates::Engine.template(:default, :fulldoc, :html)
          file || assets_template.find_file(url)
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
kinetic_sdk-5.0.29 gems/yard-0.9.36/lib/yard/server/commands/static_file_helpers.rb
kinetic_sdk-5.0.28 gems/yard-0.9.36/lib/yard/server/commands/static_file_helpers.rb
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/server/commands/static_file_helpers.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/server/commands/static_file_helpers.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/server/commands/static_file_helpers.rb
yard-0.9.37 lib/yard/server/commands/static_file_helpers.rb
kinetic_sdk-5.0.27 gems/yard-0.9.36/lib/yard/server/commands/static_file_helpers.rb
kinetic_sdk-5.0.26 gems/yard-0.9.36/lib/yard/server/commands/static_file_helpers.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/server/commands/static_file_helpers.rb
yard-0.9.36 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.35 lib/yard/server/commands/static_file_helpers.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/server/commands/static_file_helpers.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/yard-0.9.34/lib/yard/server/commands/static_file_helpers.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/server/commands/static_file_helpers.rb
yard-0.9.34 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.33 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.32 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.31 lib/yard/server/commands/static_file_helpers.rb