nanoc-cli 4.11.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nanoc
4
+ module CLI
5
+ # A command runner subclass for Nanoc commands that adds Nanoc-specific
6
+ # convenience methods and error handling.
7
+ #
8
+ # @api private
9
+ class CommandRunner < ::Cri::CommandRunner
10
+ # @see http://rubydoc.info/gems/cri/Cri/CommandRunner#call-instance_method
11
+ #
12
+ # @return [void]
13
+ def call
14
+ Nanoc::CLI::ErrorHandler.handle_while do
15
+ run
16
+ end
17
+ end
18
+
19
+ # @return [Boolean] true if the current working directory is a Nanoc site
20
+ # directory, false otherwise
21
+ def in_site_dir?
22
+ Nanoc::Core::SiteLoader.cwd_is_nanoc_site?
23
+ end
24
+
25
+ def self.find_site_dir
26
+ start_here = Dir.pwd
27
+
28
+ here = start_here
29
+ until Nanoc::Core::SiteLoader.cwd_is_nanoc_site?
30
+ Dir.chdir('..')
31
+ return nil if Dir.pwd == here
32
+
33
+ here = Dir.pwd
34
+ end
35
+ here
36
+ ensure
37
+ Dir.chdir(start_here)
38
+ end
39
+
40
+ def self.enter_site_dir
41
+ dir = find_site_dir
42
+ if dir.nil?
43
+ raise ::Nanoc::Core::TrivialError, 'The current working directory, nor any of its parents, seems to be a Nanoc site.'
44
+ end
45
+
46
+ return if Dir.getwd == dir
47
+
48
+ $stderr.puts "Using Nanoc site in #{dir}"
49
+ Dir.chdir(dir)
50
+ end
51
+
52
+ # Asserts that the current working directory contains a site and loads the site into memory.
53
+ #
54
+ # @return [void]
55
+ def load_site
56
+ self.class.enter_site_dir
57
+
58
+ $stderr.print 'Loading site… '
59
+ $stderr.flush
60
+ site = Nanoc::Core::SiteLoader.new.new_from_cwd
61
+
62
+ $stderr.puts 'done'
63
+ site
64
+ end
65
+
66
+ # @return [Boolean] true if debug output is enabled, false if not
67
+ #
68
+ # @see Nanoc::CLI.debug?
69
+ def debug?
70
+ Nanoc::CLI.debug?
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ usage 'compile [options]'
4
+ summary 'compile items of this site'
5
+ description <<~EOS
6
+ Compile all items of the current site.
7
+ EOS
8
+ no_params
9
+
10
+ flag nil, :diff, 'generate diff'
11
+ if Nanoc::Core::Feature.enabled?(Nanoc::Core::Feature::LIVE_CMD)
12
+ flag :w, :watch, 'watch for changes and recompile when needed'
13
+ end
14
+
15
+ module Nanoc::CLI::Commands
16
+ class Compile < ::Nanoc::CLI::CommandRunner
17
+ attr_accessor :listener_classes
18
+
19
+ def run
20
+ self.class.enter_site_dir
21
+
22
+ if options[:watch]
23
+ run_repeat
24
+ else
25
+ run_once
26
+ end
27
+ end
28
+
29
+ def run_repeat
30
+ require 'nanoc/live'
31
+ Nanoc::Live::LiveRecompiler.new(command_runner: self).run
32
+ end
33
+
34
+ def run_once
35
+ time_before = Time.now
36
+
37
+ @site = load_site
38
+
39
+ puts 'Compiling site…'
40
+ compiler = Nanoc::Core::Compiler.new_for(@site)
41
+ listener = Nanoc::CLI::CompileListeners::Aggregate.new(
42
+ command_runner: self,
43
+ site: @site,
44
+ compiler: compiler,
45
+ )
46
+ listener.run_while do
47
+ compiler.run_until_end
48
+ end
49
+
50
+ time_after = Time.now
51
+ puts
52
+ puts "Site compiled in #{format('%.2f', time_after - time_before)}s."
53
+ end
54
+ end
55
+ end
56
+
57
+ runner Nanoc::CLI::Commands::Compile
@@ -0,0 +1,257 @@
1
+ # frozen_string_literal: true
2
+
3
+ usage 'create-site [options] path'
4
+ aliases :create_site, :cs
5
+ summary 'create a site'
6
+ description 'Create a new site at the given path. The site will use the `filesystem` data source.'
7
+ flag nil, :force, 'force creation of new site'
8
+ param :path
9
+
10
+ module Nanoc::CLI::Commands
11
+ class CreateSite < ::Nanoc::CLI::CommandRunner
12
+ class << self
13
+ protected
14
+
15
+ # Converts the given array to YAML format
16
+ def array_to_yaml(array)
17
+ '[ ' + array.map { |s| "'" + s + "'" }.join(', ') + ' ]'
18
+ end
19
+ end
20
+
21
+ DEFAULT_CONFIG = <<~EOS unless defined? DEFAULT_CONFIG
22
+ # A list of file extensions that Nanoc will consider to be textual rather than
23
+ # binary. If an item with an extension not in this list is found, the file
24
+ # will be considered as binary.
25
+ text_extensions: #{array_to_yaml(Nanoc::Core::Configuration::DEFAULT_CONFIG[:text_extensions])}
26
+
27
+ prune:
28
+ auto_prune: true
29
+
30
+ data_sources:
31
+ - type: filesystem
32
+ encoding: utf-8
33
+ EOS
34
+
35
+ DEFAULT_RULES = <<~EOS unless defined? DEFAULT_RULES
36
+ #!/usr/bin/env ruby
37
+
38
+ compile '/index.html' do
39
+ layout '/default.*'
40
+ write '/index.html'
41
+ end
42
+
43
+ compile '/**/*.html' do
44
+ layout '/default.*'
45
+ write item.identifier.without_ext + '/index.html'
46
+ end
47
+
48
+ # This is an example rule that matches Markdown (.md) files, and filters them
49
+ # using the :kramdown filter. It is commented out by default, because kramdown
50
+ # is not bundled with Nanoc or Ruby.
51
+ #
52
+ #compile '/**/*.md' do
53
+ # filter :kramdown
54
+ # layout '/default.*'
55
+ # write item.identifier.without_ext + '/index.html'
56
+ #end
57
+
58
+ compile '/**/*' do
59
+ write item.identifier.to_s
60
+ end
61
+
62
+ layout '/**/*', :erb
63
+ EOS
64
+
65
+ DEFAULT_ITEM = <<~EOS unless defined? DEFAULT_ITEM
66
+ ---
67
+ title: Home
68
+ ---
69
+
70
+ <h1>A Brand New Nanoc Site</h1>
71
+
72
+ <p>You’ve just created a new Nanoc site. The page you are looking at right now is the home page for your site. To get started, consider replacing this default homepage with your own customized homepage. Some pointers on how to do so:</p>
73
+
74
+ <ul>
75
+ <li><p><strong>Change this page’s content</strong> by editing the “index.html” file in the “content” directory. This is the actual page content, and therefore doesn’t include the header, sidebar or style information (those are part of the layout).</p></li>
76
+ <li><p><strong>Change the layout</strong>, which is the “default.html” file in the “layouts” directory, and create something unique (and hopefully less bland).</p></li>
77
+ </ul>
78
+
79
+ <p>If you need any help with customizing your Nanoc web site, be sure to check out the documentation (see sidebar), and be sure to subscribe to the discussion group (also see sidebar). Enjoy!</p>
80
+ EOS
81
+
82
+ DEFAULT_STYLESHEET = <<~EOS unless defined? DEFAULT_STYLESHEET
83
+ * {
84
+ margin: 0;
85
+ padding: 0;
86
+
87
+ font-family: Georgia, Palatino, serif;
88
+ }
89
+
90
+ body {
91
+ background: #fff;
92
+ }
93
+
94
+ a {
95
+ text-decoration: none;
96
+ }
97
+
98
+ a:link,
99
+ a:visited {
100
+ color: #f30;
101
+ }
102
+
103
+ a:hover {
104
+ color: #f90;
105
+ }
106
+
107
+ #main {
108
+ position: absolute;
109
+
110
+ top: 40px;
111
+ left: 280px;
112
+
113
+ width: 500px;
114
+ }
115
+
116
+ #main h1 {
117
+ font-size: 40px;
118
+ font-weight: normal;
119
+
120
+ line-height: 40px;
121
+
122
+ letter-spacing: -1px;
123
+ }
124
+
125
+ #main p {
126
+ margin: 20px 0;
127
+
128
+ font-size: 15px;
129
+
130
+ line-height: 20px;
131
+ }
132
+
133
+ #main ul, #main ol {
134
+ margin: 20px;
135
+ }
136
+
137
+ #main li {
138
+ font-size: 15px;
139
+
140
+ line-height: 20px;
141
+ }
142
+
143
+ #main ul li {
144
+ list-style-type: square;
145
+ }
146
+
147
+ #sidebar {
148
+ position: absolute;
149
+
150
+ top: 40px;
151
+ left: 20px;
152
+ width: 200px;
153
+
154
+ padding: 20px 20px 0 0;
155
+
156
+ border-right: 1px solid #ccc;
157
+
158
+ text-align: right;
159
+ }
160
+
161
+ #sidebar h2 {
162
+ text-transform: uppercase;
163
+
164
+ font-size: 13px;
165
+
166
+ color: #333;
167
+
168
+ letter-spacing: 1px;
169
+
170
+ line-height: 20px;
171
+ }
172
+
173
+ #sidebar ul {
174
+ list-style-type: none;
175
+
176
+ margin: 20px 0;
177
+ }
178
+
179
+ #sidebar li {
180
+ font-size: 14px;
181
+
182
+ line-height: 20px;
183
+ }
184
+ EOS
185
+
186
+ DEFAULT_LAYOUT = <<~EOS unless defined? DEFAULT_LAYOUT
187
+ <!DOCTYPE HTML>
188
+ <html lang="en">
189
+ <head>
190
+ <meta charset="utf-8">
191
+ <title>A Brand New Nanoc Site - <%= @item[:title] %></title>
192
+ <link rel="stylesheet" href="/stylesheet.css">
193
+
194
+ <!-- you don't need to keep this, but it's cool for stats! -->
195
+ <meta name="generator" content="Nanoc <%= Nanoc::VERSION %>">
196
+ </head>
197
+ <body>
198
+ <div id="main">
199
+ <%= yield %>
200
+ </div>
201
+ <div id="sidebar">
202
+ <h2>Documentation</h2>
203
+ <ul>
204
+ <li><a href="https://nanoc.ws/doc/">Documentation</a></li>
205
+ <li><a href="https://nanoc.ws/doc/tutorial/">Tutorial</a></li>
206
+ </ul>
207
+ <h2>Community</h2>
208
+ <ul>
209
+ <li><a href="http://groups.google.com/group/nanoc/">Discussion group</a></li>
210
+ <li><a href="https://gitter.im/nanoc/nanoc">Gitter channel</a></li>
211
+ <li><a href="https://nanoc.ws/contributing/">Contributing</a></li>
212
+ </ul>
213
+ </div>
214
+ </body>
215
+ </html>
216
+ EOS
217
+
218
+ def run
219
+ path = arguments[:path]
220
+
221
+ # Check whether site exists
222
+ if File.exist?(path) && (!File.directory?(path) || !(Dir.entries(path) - %w[. ..]).empty?) && !options[:force]
223
+ raise(
224
+ Nanoc::Core::TrivialError,
225
+ "The site was not created because '#{path}' already exists. " \
226
+ 'Re-run the command using --force to create the site anyway.',
227
+ )
228
+ end
229
+
230
+ # Build entire site
231
+ FileUtils.mkdir_p(path)
232
+ FileUtils.cd(File.join(path)) do
233
+ FileUtils.mkdir_p('content')
234
+ FileUtils.mkdir_p('layouts')
235
+ FileUtils.mkdir_p('lib')
236
+ FileUtils.mkdir_p('output')
237
+
238
+ write('nanoc.yaml', DEFAULT_CONFIG)
239
+ write('Rules', DEFAULT_RULES)
240
+ write('content/index.html', DEFAULT_ITEM)
241
+ write('content/stylesheet.css', DEFAULT_STYLESHEET)
242
+ write('layouts/default.html', DEFAULT_LAYOUT)
243
+ end
244
+
245
+ puts "Created a blank Nanoc site at '#{path}'. Enjoy!"
246
+ end
247
+
248
+ private
249
+
250
+ def write(filename, content)
251
+ File.write(filename, content)
252
+ Nanoc::CLI::Logger.instance.file(:high, :create, filename)
253
+ end
254
+ end
255
+ end
256
+
257
+ runner Nanoc::CLI::Commands::CreateSite
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ usage 'nanoc command [options] [arguments]'
4
+ summary 'Nanoc, a static site compiler written in Ruby'
5
+ default_subcommand 'compile'
6
+
7
+ opt :l, :color, 'enable color' do
8
+ $stdout.remove_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors)
9
+ $stderr.remove_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors)
10
+ end
11
+
12
+ opt :d, :debug, 'enable debugging' do
13
+ Nanoc::CLI.debug = true
14
+ end
15
+
16
+ opt :e, :env, 'set environment', argument: :required do |value|
17
+ ENV.store('NANOC_ENV', value)
18
+ end
19
+
20
+ opt :h, :help, 'show the help message and quit' do |_value, cmd|
21
+ puts cmd.help
22
+ exit 0
23
+ end
24
+
25
+ opt :C, :'no-color', 'disable color' do
26
+ $stdout.add_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors)
27
+ $stderr.add_stream_cleaner(Nanoc::CLI::StreamCleaners::ANSIColors)
28
+ end
29
+
30
+ opt :V, :verbose, 'make output more detailed', multiple: true do |val|
31
+ Nanoc::CLI::Logger.instance.level = :low
32
+ Nanoc::CLI.verbosity = val.size
33
+ end
34
+
35
+ opt :v, :version, 'show version information and quit' do
36
+ puts Nanoc::Core.version_information
37
+ exit 0
38
+ end
39
+
40
+ opt :w, :warn, 'enable warnings' do
41
+ $-w = true
42
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ usage 'prune'
4
+ summary 'remove files not managed by Nanoc from the output directory'
5
+ description <<~EOS
6
+ Find all files in the output directory that do not correspond to an item
7
+ managed by Nanoc and remove them. Since this is a hazardous operation, an
8
+ additional `--yes` flag is needed as confirmation.
9
+
10
+ Also see the `auto_prune` configuration option in `nanoc.yaml` (`config.yaml`
11
+ for older Nanoc sites), which will automatically prune after compilation.
12
+ EOS
13
+ no_params
14
+
15
+ flag :y, :yes, 'confirm deletion'
16
+ flag :n, :'dry-run', 'print files to be deleted instead of actually deleting them'
17
+
18
+ module Nanoc::CLI::Commands
19
+ class Prune < ::Nanoc::CLI::CommandRunner
20
+ def run
21
+ @site = load_site
22
+ res = Nanoc::Core::Compiler.new_for(@site).run_until_reps_built
23
+ reps = res.fetch(:reps)
24
+
25
+ if options.key?(:yes)
26
+ Nanoc::Core::Pruner.new(@site.config, reps, exclude: prune_config_exclude).run
27
+ elsif options.key?(:'dry-run')
28
+ Nanoc::Core::Pruner.new(@site.config, reps, exclude: prune_config_exclude, dry_run: true).run
29
+ else
30
+ $stderr.puts 'WARNING: Since the prune command is a destructive command, it requires an additional --yes flag in order to work.'
31
+ $stderr.puts
32
+ $stderr.puts 'Please ensure that the output directory does not contain any files (such as images or stylesheets) that are necessary but are not managed by Nanoc. If you want to get a list of all files that would be removed, pass --dry-run.'
33
+ exit 1
34
+ end
35
+ end
36
+
37
+ protected
38
+
39
+ def prune_config
40
+ @site.config[:prune] || {}
41
+ end
42
+
43
+ def prune_config_exclude
44
+ prune_config[:exclude] || {}
45
+ end
46
+ end
47
+ end
48
+
49
+ runner Nanoc::CLI::Commands::Prune