Module: IndexHtml

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

Defined Under Namespace

Classes: BaseCLI, CLI, Main

Constant Summary collapse

CustomError =
Class.new(StandardError)
VERSION =
"0.0.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

Parameters:

  • file_list (Array<String>)

    if of file path

  • args (Hash<Symbol, Object>) (defaults to: {})

    list of options

Returns:

  • (Array<String>)

    list of basename of a given input file



50
51
52
53
# File 'lib/index_html.rb', line 50

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



55
56
57
58
59
60
# File 'lib/index_html.rb', line 55

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/index_html.rb', line 15

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

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

  # write the output to files
  File.open(output, "w") do |f|
    f.write(header)
    make_links(file_list, prefix: prefix).each { |i| f.write("#{' ' * indent}#{i}\n") }
    f.write(footer)
  end
end

Make links using <li> tags

Returns:

  • (Array)

    the list of valid <li> tags



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/index_html.rb', line 65

def make_links(file_list, args = {})
  original_links = file_list
  escape_uris!(file_list, args)
  file_list = basenames!(file_list, args)

  prefix = args.fetch(:prefix, "")

  result = []
  original_links.zip(file_list).each do |i,j|
    result << %Q{<li><a href="#{prefix}#{j}" target="_blank">#{i}</li>}
  end
  result
end