Module: RightPublish::Annotation

Defined in:
lib/right_publish/annotation.rb

Class Method Summary collapse

Class Method Details

.generate_html(files, strip = 0, options = {}) ⇒ Object

Generate an HTML index file for a set of packages.

Parameters:

  • files (Array)

    list of relative file keys

  • strip (Integer) (defaults to: 0)

    number of path elements to strip from displayed filenames, default 0

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :filter (Array)

    a whitelist of String glob patterns to filter files, i.e. [“*.rpm”, “*.deb”]

  • :title (String)

    Human-readable page title

  • :scripts (Array)

    JavaScript hrefs to embed into page as <script> tags

  • :stylesheets (Array)

    Stylesheet hrefs to embed into page as <link> tags



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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