Class: Slate::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/slate.rb

Overview

The main class to rule them all

Class Method Summary collapse

Class Method Details

.convert(md_content, title, theme) ⇒ Object



61
62
63
64
65
# File 'lib/slate.rb', line 61

def self.convert(md_content, title, theme)
  raw_html = Kramdown::Document.new(md_content, input: 'GFM').to_html

  "<head>\n<title> #{title} </title>\n <link href='#{theme}.css' rel='stylesheet'>\n</head>\n<body class='markdown-body'>\n#{raw_html}\n</body>"
end

.copy_css(dest, theme) ⇒ Object



67
68
69
70
71
# File 'lib/slate.rb', line 67

def self.copy_css(dest, theme)
  FileUtils.mkdir_p(dest)
  style_path = File.join(__dir__, '..','themes',"#{theme}.css")
  FileUtils.cp(style_path, File.join(dest, "#{theme}.css"))
end

.process_directory(path, options) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/slate.rb', line 92

def self.process_directory(path, options)
  puts 'Found a directory, gotta check all .md files!'
  dir = options.output_dir || path
  copy_css(dir, options.theme)

  files = Dir.glob(File.join(path, '*.md'))

  files.each do |file|
    save_file(file, options)
  end
  puts "Done! Processed #{files.count} files."
end

.process_file(path, options) ⇒ Object



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

def self.process_file(path, options)
  puts 'Argument seems to be a single file, Parsing it'
  dest = options.output_dir || File.dirname(path)
  copy_css(dest, options.theme)
  save_file(path, options)
end

.run(argv) ⇒ Object

This method is the MAIN method… Refactored from bin/slate



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/slate.rb', line 15

def self.run(argv)
  options = Options.new(nil, "github")

  OptionParser.new do |opts|
    opts.banner = 'Usage: slate [options] <file_or_directory>'

    opts.on('-o', '--output DIR', 'Specify the output direc1tory') do |dir|
      options.output_dir = dir
    end

    opts.on('-v', '--version', 'Show current version') do |_ver|
      puts Slate::VERSION
      exit
    end

    opts.on('-h', '--help', 'View Help') do
      puts opts
      exit
    end

    opts.on('-t', '--theme THEME', 'Theme to use') do |theme|
      options.theme = theme
    end

  end.parse!(argv)

  unless THEMES.include?(options.theme)
    abort "Unknown theme '#{options.theme}'. Available themes: #{THEMES.join(', ')}"
  end

  path = argv.first
  if path.nil?
    puts 'Usage: slate [options] <file_or_directory>'
    exit
  end

  path = File.expand_path(path)
  if File.file?(path)
    process_file(path, options)
  elsif File.directory?(path)
    process_directory(path, options)
  else
    puts "Error: #{path} Is neither a file, nor a directory"
  end
end

.save_file(path, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/slate.rb', line 73

def self.save_file(path, options)
  content = File.read(path)
  filename = File.basename(path, '.*')
  dir = options.output_dir || File.dirname(path)

  FileUtils.mkdir_p(dir)
  output_path = File.join(dir, "#{filename}.html")
  final_html = convert(content, filename, options.theme)
  File.write(output_path, final_html)
  puts "Saved #{filename}.html"
end