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.1"

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>)

    input file list

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

    list of options

Returns:

  • (Array<String>)

    list of basename of a given input file



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 = "    |<html>\n    |<title>File Listing</title>\n    |<header>File List</header>\n    |  <body>\n    |    <ol>\n    END\n\n  footer = <<-END.gsub(/^\\s+\\|/, \"\")\n    |    </ol>\n    |  </body>\n    |</html>\n    END\n\n  prefix = args.fetch(:prefix, \".\")\n  indent = args.fetch(:indent, 6)\n  output = args.fetch(:output, \"index.html\")\n\n  File.open(output, \"w\") do |f|\n    f.write(header)\n    links = make_links file_list, prefix: prefix, base_dir: args[:base_dir]\n    links.each { |i| f.write(\"\#{\" \" * indent}\#{i}\\n\") }\n    f.write(footer)\n  end\nend\n".gsub(/^\s+\|/, "")

Make links using <li> tags

Parameters:

  • file_list (Array<String>)

    the input file list

  • args (Hash<Symbol,Object>)

    the option hash

Returns:

  • (Array<String>)

    the list of valid <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