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

Class Method Summary collapse

Class Method Details

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

Transform the list of full path to list of base name



46
47
48
49
# File 'lib/index_html.rb', line 46

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

.drop_extension(link) ⇒ String

Drop string after the last ‘.’ dot string if any

Examples:

drop_extension("some_file")                  == "some_file"
drop_extension("some_file.txt")              == "some_file"
drop_extension("/path/to/some_file")         == "/path/to/some_file"
drop_extension("/path/to/some_file.txt")     == "/path/to/some_file"
drop_extension("/path/to/some_file.txt.pdf") == "/path/to/some_file.txt"


107
108
109
110
111
112
113
114
# File 'lib/index_html.rb', line 107

def drop_extension(link)
  dot_index = link.rindex(".")
  if dot_index
    link[0..(dot_index - 1)]
  else
    link
  end
end

.drop_last_ext(link, flag = false) ⇒ Object

Wrapper method to call the drop_extension if applicable



89
90
91
92
93
94
95
# File 'lib/index_html.rb', line 89

def drop_last_ext(link, flag = false)
  if flag
    drop_extension(link)
  else
    link
  end
end

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



51
52
53
54
55
56
# File 'lib/index_html.rb', line 51

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 for a given list of files



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

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

Make links using <li> tags



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/index_html.rb', line 64

def make_links(file_list, args)
  prefix   = args.fetch(:prefix, ".")
  drop_ext = args.fetch(:drop_ext, false)
  result = []
  file_list.each do |file|
    path = File.absolute_path(file).gsub(Dir.pwd, "")
    if prefix
      # link =  %Q(<li><a href="#{prefix}#{drop_ext ? drop_extension(path): path}" target='_blank'>#{prefix}#{path}</li>)
      link =  %Q(<li><a href="#{prefix}#{path}" target='_blank'>#{prefix}#{drop_last_ext(path, drop_ext)}</li>)
    else
      # add "." in front of the link and drop the last extension
      # link =  %Q(<li><a href=".#{drop_ext ? drop_extension(path) : path}" target='_blank'>#{path.gsub(/^\//, "")}</li>)
      # at this point path always start with "/"
      link =  %Q(<li><a href=".#{path}" target='_blank'>#{drop_last_ext(path.gsub(/^\//, ""), drop_ext)}</li>)
    end
    result << link
  end
  result
end