Class: Statikaj::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



10
11
12
# File 'lib/statikaj/cli.rb', line 10

def self.source_root
  File.expand_path('../../..', __FILE__)
end

Instance Method Details

#articleObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/statikaj/cli.rb', line 85

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

  article = {'title' => title, 'date' => Time.now.strftime("%d/%m/%Y"), 'author' => 'User', 'category' => 'category'}.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

#buildObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/statikaj/cli.rb', line 35

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

  config = {}
  config[:url] = options[:url].split("/").join("/")

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

  articles.each do |article|
    categories[article.category] ||= []
    categories[article.category] << article

    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

    create_file article_file, content, force: options[:force]
  end

  render = Render.new(source, page: 'index', articles: articles.reverse)
  content = render.page {}
  create_file destination.join("index.html"), content, force: true

  render = Render.new(source, page: 'index', articles: articles.reverse, type: :atom)
  atom_content = render.page do |page|
    page.url = config[:url]
  end
  create_file destination.join("feed.atom"), atom_content, force: true

  unless options[:"no-category"]
    empty_directory destination.join("category")
    categories.each do |key, _articles|
      key = "No Category" if key.nil?
      slug = key.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')

      render = Render.new(source, page: 'category', url: slug, articles: _articles.reverse)
      content = render.page{|page| page.category = key }
      create_file destination.join("category/#{slug}"), content, force: true
    end
  end
end

#new(name) ⇒ Object



15
16
17
# File 'lib/statikaj/cli.rb', line 15

def new(name)
  directory('templates', name, verbose: true)
end