Sha256: 17d2c2d4ddc986c8795bc06f65b903cb3101cb93b9625f39a0ab05cffa682d96

Contents?: true

Size: 1.93 KB

Versions: 16

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true
require 'webrick/httputils'

module YARD
  module Server
    module Commands
      # Include this module to get access to {#static_template_file?}
      # and {favicon?} helpers.
      module StaticFileHelpers
        include WEBrick::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)
            p file
            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

16 entries across 15 versions & 4 rubygems

Version Path
yard-0.9.19 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.16 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.15 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.14 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.13 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.12 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.11 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.10 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.9 lib/yard/server/commands/static_file_helpers.rb
etude_for_ruby-0.1.4 vendor/bundle/ruby/2.4.0/gems/yard-0.9.8/lib/yard/server/commands/static_file_helpers.rb
etude_for_ruby-0.1.4 vendor/bundle/ruby/2.2.0/gems/yard-0.9.8/lib/yard/server/commands/static_file_helpers.rb
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/yard-0.9.8/lib/yard/server/commands/static_file_helpers.rb
yard-0.9.8 lib/yard/server/commands/static_file_helpers.rb
abaci-0.3.0 vendor/bundle/gems/yard-0.9.7/lib/yard/server/commands/static_file_helpers.rb
yard-0.9.7 lib/yard/server/commands/static_file_helpers.rb
yard-0.9.6 lib/yard/server/commands/static_file_helpers.rb