Module: IndexHtml

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

Defined Under Namespace

Classes: CLI

Constant Summary collapse

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

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

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

Parameters:

  • link (String)

    the input link

Returns:

  • (String)

    the new string with extension drop if any



103
104
105
106
107
108
109
110
# File 'lib/index_html/index_html.rb', line 103

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

Parameters:

  • link (String)

    the input link

  • flag (Boolean) (defaults to: false)

    the boolean flag



85
86
87
88
89
90
91
# File 'lib/index_html/index_html.rb', line 85

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

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



47
48
49
50
51
52
# File 'lib/index_html/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 for a given list of files

Parameters:

  • file_list (Array<String>)

    list of input file

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

    the argument hash



8
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/index_html.rb', line 8

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

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

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

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
75
76
77
78
# File 'lib/index_html/index_html.rb', line 60

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