module RightPublish::Annotation

Private Class Methods

recursive_ul(index, b) click to toggle source
# File lib/right_publish/annotation.rb, line 79
def self.recursive_ul(index, b)
  b.ul(:class => 'dirList') {
    index.keys.sort.each do |k|
      v = index[k]

      case v
      when Hash
        b.li(:class => 'dir') {
          b.text! k
          recursive_ul(v, b)
        }
      else
        base = File.basename(v)
        ext  = File.extname(v)[1..-1]
        b.li(:class => ext) {
          b.a(base, :href => v)
        }
      end
    end
  }
end

Public Instance Methods

generate_html(files, strip=0, options={}) click to toggle source

Generate an HTML index file for a set of packages. @param [Array] files list of relative file keys @param [Integer] strip number of path elements to strip from displayed filenames, default 0 @option options [Array] :filter a whitelist of String glob patterns to filter files, i.e. [“*.rpm”, “*.deb”] @option options [String] :title Human-readable page title @option options [Array] :scripts JavaScript hrefs to embed into page as <script> tags @option options [Array] :stylesheets Stylesheet hrefs to embed into page as <link> tags

# File lib/right_publish/annotation.rb, line 19
def generate_html(files, strip=0, options={})
  title       = options[:title]
  filter      = options[:filter] || []
  scripts     = options[:scripts] || []
  stylesheets = options[:stylesheets] || []

  unless filter.nil? || filter.empty?
    filtered  = files.select { |f| filter.any? { |ff| File.fnmatch(ff, f.key) } }
  end
  segmented = filtered.map { |f| f.key.split('/') }

  index     = {}
  segmented.each do |segments|
    stripped = segments[strip..-1]

    map = index
    stripped.each_with_index do |seg, idx|
      if idx == stripped.size - 1
        map[seg] = "/#{segments.join('/')}"
      else
        map[seg] ||= {}
        map      = map[seg]
      end
    end
  end

  b = Builder::XmlMarkup.new(:indent => 2)

  # Declare us as an HTML5 document
  b.declare!(:DOCTYPE, :HTML)

  b.html {
    b.head {
      # Ensure that browsers get a valid content type and charset, even if the Web server
      # is misconfigured. This is necessary for our doc to be considered valid HTML5.
      b.meta(:'http-equiv'=>'Content-Type', :content=>'text/html; charset=UTF-8')

      if title
        b.title(title)
      end

      stylesheets.each do |stylesheet|
        b.link(:rel => 'stylesheet', :type => 'text/css', :href => stylesheet)
      end

      scripts.each do |script|
        b.script(:src => script, :type => 'text/javascript')
      end
    }
    b.body {
      if title
        b.h1(title, :class => 'title')
      end
      recursive_ul(index, b)
    }
  }
end