class Repobrowse::HTML

used to give each HTML page a consistent look, this is a Rack response body

Constants

ESCAPES

Attributes

buf[R]

Public Class Methods

new() click to toggle source
# File lib/repobrowse/html.rb, line 13
def initialize
  @buf = nil
end

Public Instance Methods

from_anchor(str) click to toggle source
# File lib/repobrowse/html.rb, line 52
def from_anchor(str)
  str = str.dup
  first = ''
  str.sub!(/\AZ([a-f0-9]{2})/n, '') and first = -$1.hex.chr
  str.gsub!(/::([a-f0-9]{2})/n) { $1.hex.chr }
  str.tr!(':', '/')
  "#{first}#{str}"
end
response(code, headers = {}) click to toggle source
# File lib/repobrowse/html.rb, line 61
def response(code, headers = {})
  headers['Content-Type'] ||= 'text/html; charset=UTF-8'
  [ 200, headers, self ]
end
start(title_html, desc, robots: nil) click to toggle source
# File lib/repobrowse/html.rb, line 17
  def start(title_html, desc, robots: nil)
    meta = %Q(<meta\nname=robots\ncontent="#{robots}" />) if robots
    @buf = +<<~EOF
      <!DOCTYPE html>
      <html><head><title>#{
        title_html
      }</title><style>pre{white-space:pre-wrap}</style>#{
        meta
      }</head><body><pre><b>#{
        case desc
        when String, nil
          desc
        else
          desc.driver.description.encode(xml: :text)
        end
      }</b>
    EOF
  end
to_anchor(str) click to toggle source
# File lib/repobrowse/html.rb, line 43
def to_anchor(str)
  str = str.dup
  first = ''
  # must start with alphanum
  str.sub!(/\A([^A-Ya-z])/n, '') and first = sprintf('Z%02x', $1.ord)
  str.gsub!(/([^\w\.\-])/n, ESCAPES)
  "#{first}#{str}"
end