Class: Foresite::Cli
- Inherits:
-
Thor
- Object
- Thor
- Foresite::Cli
- Defined in:
- lib/foresite/cli.rb
Overview
Cli class.
For CLI functionality to generate static content.
Class Method Summary collapse
-
.exit_on_failure? ⇒ Boolean
Ensure that failures exit with a status of zero.
Instance Method Summary collapse
Class Method Details
.exit_on_failure? ⇒ Boolean
Ensure that failures exit with a status of zero.
13 14 15 |
# File 'lib/foresite/cli.rb', line 13 def self.exit_on_failure? true end |
Instance Method Details
#build ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/foresite/cli.rb', line 84 def build unless Foresite.subdirectories_exist? warn("Missing subdirectories, try running `foresite init`") exit(1) end # Wipe all output files. Dir.glob(File.join(Foresite.get_path_to_out, "*.html")).each { File.delete(_1) } File.delete(Foresite.get_path_to_index_file) if File.exist?(Foresite.get_path_to_index_file) File.delete(Foresite.get_path_to_feed_file) if File.exist?(Foresite.get_path_to_feed_file) markdown_paths = Dir.glob(File.join(Foresite.get_path_to_md, "*.md")) if markdown_paths.count == 0 warn("No markdown files, try running `foresite touch`") exit(1) end links = markdown_paths.map do |markdown_path| markdown_content = File.read(markdown_path) title = markdown_content.split("\n").first { |line| /^# [a-z]/i =~ line }.gsub(/^#/, "").strip filename_markdown = File.basename(markdown_path) html_path = Foresite.get_path_to_out_file(filename_markdown.gsub(/\.md$/, ".html")) File.write(html_path, Foresite.render_wrapped(title, markdown_content)) $stdout.puts("Created #{Foresite.relative_path(html_path)}") # Extract date. match_data = /\d{4}-\d{2}-\d{2}/.match(filename_markdown) date_ymd = match_data[0] date_822 = DateTime.strptime(date_ymd, "%F").strftime("%a, %d %b %Y %H:%M:%S %z") { date_ymd: date_ymd, date_822: date_822, href: Foresite.relative_path(html_path), title: title } end # Generate index file. index_html_path = Foresite.get_path_to_index_file File.write(index_html_path, Foresite.render_wrapped_index(links)) $stdout.puts("Created #{Foresite.relative_path(index_html_path)}") feed_xml_path = Foresite.get_path_to_feed_file File.write(feed_xml_path, Foresite.render_feed(links, links.last[:date_822])) $stdout.puts("Created #{Foresite.relative_path(feed_xml_path)}") end |
#init ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/foresite/cli.rb', line 31 def init unless Foresite.root_exists? warn("Nonexistent directory #{Foresite.get_path_to_root}") exit(1) end unless Foresite.root_writable? warn("Cannot write to directory #{Foresite.get_path_to_root}") exit(1) end Foresite.touch_directories.map { $stdout.puts(_1) } Foresite.copy_templates.map { $stdout.puts(_1) } end |
#touch(title) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/foresite/cli.rb', line 58 def touch(title) unless Foresite.subdirectories_exist? warn("Missing subdirectories, try running `foresite init`") exit(1) end date_ymd = Time.now.strftime("%F") slug = title.downcase.gsub(/[^a-z]/i, " ").strip.gsub(/ +/, "-") path = Foresite.get_path_to_md_file("#{date_ymd}-#{slug}.md") if File.exist?(path) $stdout.puts("File #{Foresite.relative_path(path)} already exists") else File.write(path, Foresite.render_post(title, date_ymd)) $stdout.puts("Created #{Foresite.relative_path(path)}") end end |
#version ⇒ Object
18 19 20 |
# File 'lib/foresite/cli.rb', line 18 def version $stdout.puts(Foresite::VERSION) end |
#watch ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/foresite/cli.rb', line 141 def watch dirs_to_watch = [ Foresite::DIRNAME_MARKDOWN, Foresite::DIRNAME_ERB ] $stdout.puts("Watching #{dirs_to_watch.map { "./#{_1}" }.join(", ")} for changes... (Ctrl+C to exit)") Filewatcher.new(dirs_to_watch).watch do |changes| changes.each do |filename, event| $stdout.puts("#{File.basename(filename)} #{event}") end build end end |