Class: Statikaj::CLI

Inherits:
Thor show all
Includes:
Thor::Actions
Defined in:
lib/statikaj/cli.rb

Instance Method Summary collapse

Instance Method Details

#articleObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/statikaj/cli.rb', line 57

def article
  title = ask('Title: ')
  slug = title.empty?? nil : title.strip.slugize

  article = {'title' => title, 'date' => Time.now.strftime("%d/%m/%Y"), 'author' => 'User'}.to_yaml
  article << "\n"
  article << "Once upon a time...\n\n"
  path = "src/articles/#{Time.now.strftime("%Y-%m-%d")}#{'-' + slug if slug}.md"
  unless File.exist? path
    begin
      File.open(path, "w") do |file|
        file.write article
      end
      say "An article was created for you at #{path}.", :green
    rescue
      say "Impossible to create #{path}, make sure you are in project root", :red
    end
  else
    say "I can't create the article, #{path} already exists.", :red
  end
end

#build(source = "./src", destination = "./public") ⇒ Object



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
# File 'lib/statikaj/cli.rb', line 24

def build(source = "./src", destination = "./public")
  source = Pathname.new source
  destination   = Pathname.new destination

  articles_files = Dir[source.join('articles/*.md')].sort_by {|entry| File.basename(entry) }
  articles = articles_files.map{|f| Article.new(f) }

  articles.each do |article|
    say "Saving: #{article.slug}", :green
    article_file = destination.join("#{article.slug}").to_s

    render = Render.new(source, article: article)
    content = render.article do |page|
      page.title = article.title
      page.description = article.summary
    end

    #unless File.exists?(article_file)
    file = File.new(article_file, "w+")
    file.puts content
    file.close
    #end
  end

  say "Creating index.html", :green
  render = Render.new(source, page: 'index', articles: articles.reverse)
  content = render.page {}
  file = File.new(destination.join("index.html"), "w+")
  file.puts content
  file.close
end

#new(name) ⇒ Object



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

def new(name)
  directory('templates', name)
end