Sha256: 96590c811007b7fc658ba60c5610ca9b051bbf40506bfc5ef109a17ba04d9b11

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

#!/usr/bin/env ruby
require 'dirtree'
require 'optparse'
require 'json'
require 'erb'
require 'open-uri'

templates_dir = File.join(File.dirname(__FILE__), '..', 'templates')
templates = Dir.open(templates_dir).map do |file|
  file[0...file.index('.')]
end.reject(&:empty?)

options = {
  template: 'tree'
}

OptionParser.new do |opts|
  opts.banner = 'Usage: dirtree [options]... [file]...'

  opts.on(
    '-v',
    '--version',
    'Print version'
  ) do
    puts "Dirtree version #{Dirtree::VERSION}"
    exit
  end

  opts.on(
    '-h',
    '--help',
    'Show this help text'
  ) do
    puts opts
    exit
  end

  opts.on(
    '-l',
    '--local-dependencies',
    'Use saved JavaScript libraries instead of downloading them'
  ) do
    options[:local_dependencies] = true
  end

  opts.on(
    '-oFile.html',
    '--output=File.html',
    'Specify a path to write output, if
       not specified output will be printed to STDOUT'
  ) do |value|
    options[:output] = value
  end

  opts.on(
    '-tTemplateName',
    '--template=TemplateName',
    'Specify the template name, available templates ' + templates.to_s
  ) do |value|
    options[:template] = value
  end
end.parse!

files = ARGV.empty? ? STDIN.read.lines : ARGV
files.map!(&:strip)

root = Dirtree::Node.new('/')
files.each { |file| root.insert(file.split('/')) }
template_file = File.join(templates_dir, options[:template] + '.html.erb')
template = File.read(File.expand_path(template_file))

tree = root.as_json
result = ERB.new(template).result binding

if options[:local_dependencies]
  result.gsub! %r{(<script src="(.+)"><\/script>)} do
    injection = '<script>'
    injection << open($2).read
    injection << '</script>'

    injection
  end
end

if options.key?(:output)
  File.write(options[:output], result)
else
  puts result
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dirtree-0.5.0 exe/dirtree
dirtree-0.4.0 exe/dirtree
dirtree-0.3.0 exe/dirtree