Class: Wikitxt::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/wikitxt/cli.rb

Instance Method Summary collapse

Instance Method Details

#build(path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wikitxt/cli.rb', line 17

def build(path)
  files = Dir.glob(File.join(path, "*.txt"))
  pages = []
  files.each do |file|
    text = File.read(file)
    body = Wikitxt.to_html(text)
    title = File.basename(file, ".txt")
    pages << title
    html = ERB.new(File.read(File.join(__dir__, "views", "layout.erb"))).result_with_hash({ body: body, title: title })
    Dir.mkdir("dist") unless Dir.exist?("dist")
    File.write(File.join("dist", "#{title}.html"), html)
  end
  index = ERB.new(File.read(File.join(__dir__, "views", "index.erb"))).result_with_hash({ pages: pages })
  html = ERB.new(File.read(File.join(__dir__, "views", "layout.erb"))).result_with_hash({ body: index, title: "Index" })
  File.write(File.join("dist", "index.html"), html)
  images = Dir.glob(File.join(path, "*.{png,jpg,jpeg}"))
  FileUtils.cp(images, "dist")
end

#server(path) ⇒ Object



12
13
14
# File 'lib/wikitxt/cli.rb', line 12

def server(path)
  system("DIR=\"#{path}\" ruby #{File.join(__dir__, "server.rb")}")
end