Module: IndexHtml

Defined in:
lib/index_html.rb,
lib/index_html/cli.rb,
lib/index_html/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

CustomError =
Class.new(StandardError)
VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.basenames!(file_list, args = {}) ⇒ Array<String>

Transform the list of full path to list of base name



42
43
44
45
# File 'lib/index_html.rb', line 42

def basenames!(file_list, args = {})
  file_list.map! { |file| File.basename(file) } if args.fetch(:basename, false)
  file_list
end

.escape_uris!(file_list, args = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/index_html.rb', line 47

def escape_uris!(file_list, args = {})
  if args.fetch(:encoded, false)
    file_list.map! { |file| URI.escape(file, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) }
  end
  file_list
end

.htmlify(file_list, args = {}) ⇒ Object

Create html links to list of files



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/index_html.rb', line 9

def htmlify(file_list, args = {})
  header = <<-END.gsub(/^\s+\|/, "")
    |<html>
    |<title>File Listing</title>
    |<header>File List</header>
    |  <body>
    |    <ol>
    END

  footer = <<-END.gsub(/^\s+\|/, "")
    |    </ol>
    |  </body>
    |</html>
    END

  prefix = args.fetch(:prefix, ".")
  indent = args.fetch(:indent, 6)
  output = args.fetch(:output, "index.html")

  File.open(output, "w") do |f|
    f.write(header)
    links = make_links file_list, prefix: prefix, base_dir: args[:base_dir]
    links.each { |i| f.write("#{" " * indent}#{i}\n") }
    f.write(footer)
  end
end

Make links using <li> tags



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/index_html.rb', line 60

def make_links(file_list, args)
  prefix = args.fetch(:prefix, ".")
  result = []

  file_list.each do |i|
    path = File.absolute_path(i).gsub(Dir.pwd, "")
    if prefix
      link =  %Q(<li><a href="#{prefix}#{path}" target='_blank'>#{prefix}#{path}</li>)
    else
      link =  %Q(<li><a href=".#{path}" target='_blank'>#{path.gsub(/^\//, "")}</li>)
    end
    result << link
  end
  result
end