Module: Mint::CommandLine
- Defined in:
- lib/mint/command_line.rb
Class Method Summary collapse
-
.config ⇒ void
Displays the sum of all active configurations, where local configurations override global ones.
-
.configure(opts, scope = :local) ⇒ void
Updates configuration options persistently in the appropriate scope, which defaults to local.
-
.create_template(name, type, scope) ⇒ String
Creates a new template directory and file at the specified scope.
-
.default_layout_content ⇒ String
Default content for layout templates.
-
.default_style_content ⇒ String
Default content for style templates.
-
.discover_files_recursively(directories) ⇒ Array
Recursively discovers Markdown files in the given directories.
-
.edit(name, type, scope) ⇒ void
Retrieve named template file (probably a built-in or installed template) and shell out that file to the user’s favorite editor.
-
.help(message) ⇒ void
Prints a help banner.
-
.install(file, name, scope = :local) ⇒ void
Install the named file as a template.
-
.parse(argv) ⇒ Hash
Parses ARGV using OptionParser.
-
.process_output_format(format_string, input_file) ⇒ String
Processes the output file format string with substitutions.
-
.publish!(files, commandline_options = {}) ⇒ void
Renders and writes to file all resources described by a document.
-
.set(key, value, scope = :local) ⇒ void
Tries to set a config option (at the specified scope) per the user’s command.
-
.templates(filter = "", scope = :local) ⇒ void
List the installed templates.
-
.uninstall(name, scope = :local) ⇒ void
Uninstall the named template.
Class Method Details
.config ⇒ void
This method returns an undefined value.
Displays the sum of all active configurations, where local configurations override global ones.
298 299 300 |
# File 'lib/mint/command_line.rb', line 298 def self.config puts YAML.dump(Mint.configuration) end |
.configure(opts, scope = :local) ⇒ void
This method returns an undefined value.
Updates configuration options persistently in the appropriate scope, which defaults to local.
277 278 279 280 281 |
# File 'lib/mint/command_line.rb', line 277 def self.configure(opts, scope=:local) config_directory = Mint.path_for_scope(scope) FileUtils.mkdir_p config_directory Helpers.update_yaml! "#{config_directory}/#{Mint::CONFIG_FILE}", opts end |
.create_template(name, type, scope) ⇒ String
Creates a new template directory and file at the specified scope
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mint/command_line.rb', line 164 def self.create_template(name, type, scope) content, ext = case type when :layout [default_layout_content, "erb"] when :style [default_style_content, "css"] else abort "Invalid template type: #{type}" end template_dir = Mint.template_path(name, scope) file_path = "#{template_dir}/#{type}.#{ext}" FileUtils.mkdir_p template_dir File.write(file_path, content) file_path end |
.default_layout_content ⇒ String
Returns default content for layout templates.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/mint/command_line.rb', line 183 def self.default_layout_content " <!DOCTYPE html>\n <html>\n <head>\n <meta charset=\"utf-8\">\n <title>Document</title>\n <% if style %>\n <link rel=\"stylesheet\" href=\"<%= style %>\">\n <% end %>\n </head>\n <body>\n <%= content %>\n </body>\n </html>\n LAYOUT_TEMPLATE\nend\n" |
.default_style_content ⇒ String
Returns default content for style templates.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/mint/command_line.rb', line 202 def self.default_style_content " body {\n font-family: -apple-system, 'Segoe UI', Roboto, sans-serif;\n line-height: 1.25;\n max-width: 960px;\n margin: 0 auto;\n padding: 2rem;\n color: #333;\n }\n\n h1, h2, h3, h4, h5, h6 {\n color: #2c3e50;\n }\n\n a {\n color: #3498db;\n text-decoration: none;\n }\n\n a:hover {\n text-decoration: underline;\n }\n\n code {\n background-color: #f8f9fa;\n padding: 0.2em 0.4em;\n border-radius: 3px;\n font-family: 'Monaco', 'Ubuntu Mono', monospace;\n }\n STYLE_TEMPLATE\nend\n" |
.discover_files_recursively(directories) ⇒ Array
Recursively discovers Markdown files in the given directories
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/mint/command_line.rb', line 306 def self.discover_files_recursively(directories) markdown_files = [] directories.each do |dir| if File.file?(dir) markdown_files << dir if dir =~ /\.(#{Mint::MARKDOWN_EXTENSIONS.join('|')})$/i elsif File.directory?(dir) Dir.glob("#{dir}/**/*.{#{Mint::MARKDOWN_EXTENSIONS.join(',')}}", File::FNM_CASEFOLD).each do |file| markdown_files << file end end end markdown_files.sort end |
.edit(name, type, scope) ⇒ void
This method returns an undefined value.
Retrieve named template file (probably a built-in or installed template) and shell out that file to the user’s favorite editor.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/mint/command_line.rb', line 242 def self.edit(name, type, scope) abort "[error] No template specified" if name.nil? || name.empty? begin file = case type when :layout Mint.lookup_layout(name) when :style Mint.lookup_style(name) else abort "[error] Invalid template type: #{type}. Use :layout or :style" end rescue Mint::TemplateNotFoundException print "Template '#{name}' does not exist. Create it? [y/N]: " response = STDIN.gets.chomp.downcase if response == 'y' || response == 'yes' file = create_template(name, type, scope) puts "Created template: #{file}" else abort "Template creation cancelled." end end editor = ENV["EDITOR"] || "vi" system "#{editor} #{file}" end |
.help(message) ⇒ void
This method returns an undefined value.
Prints a help banner
87 88 89 |
# File 'lib/mint/command_line.rb', line 87 def self.help() puts end |
.install(file, name, scope = :local) ⇒ void
This method returns an undefined value.
Install the named file as a template
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mint/command_line.rb', line 97 def self.install(file, name, scope = :local) if file.nil? raise "[error] No file specified for installation" end filename, ext = file.split "." template_name = name || filename type = Mint.css_formats.include?(ext) ? :style : :layout destination = Mint.template_path(template_name, scope) + "#{type}.#{ext}" FileUtils.mkdir_p File.dirname(destination) if File.exist? file FileUtils.cp file, destination else raise "[error] No such file: #{file}" end end |
.parse(argv) ⇒ Hash
Parses ARGV using OptionParser
14 15 16 17 18 19 20 21 22 23 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 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 |
# File 'lib/mint/command_line.rb', line 14 def self.parse(argv) = {} parser = OptionParser.new do |cli| cli. = "Usage: mint [command] files [options]" cli.on "-t", "--template TEMPLATE", "Specify the template (layout + style)" do |t| [:layout_or_style_or_template] = [:template, t] end cli.on "-l", "--layout LAYOUT", "Specify only the layout" do |l| [:layout_or_style_or_template] = [:layout, l] end cli.on "-s", "--style STYLE", "Specify only the style" do |s| [:layout_or_style_or_template] = [:style, s] end cli.on "-w", "--root ROOT", "Specify a root outside the current directory" do |r| [:root] = r end cli.on "-o", "--output-file FORMAT", "Specify the output file format with substitutions: \#{basename}, \#{original_extension}, \#{new_extension}" do |o| [:output_file] = o end cli.on "-d", "--destination DESTINATION", "Specify a destination directory, relative to the root" do |d| [:destination] = d end cli.on "--style-mode MODE", ["inline", "external", "original"], "Specify how styles are included (inline, external, original)" do |mode| [:style_mode] = mode.to_sym end cli.on "--style-destination DESTINATION", "Create stylesheet at specified directory or file path and link it" do |destination| [:style_mode] = :external [:style_destination] = destination end cli.on "-g", "--global", "Specify config changes on a global level" do [:scope] = :global end cli.on "-u", "--user", "Specify config changes on a user-wide level" do [:scope] = :user end cli.on "--local", "Specify config changes on a project-specific level" do [:scope] = :local end cli.on "-r", "--recursive", "Recursively find all Markdown files in subdirectories" do [:recursive] = true end end transient_argv = argv.dup parser.parse! transient_argv if [:style_mode] == :inline && [:style_destination] raise ArgumentError, "--style-mode inline and --style-destination cannot be used together" end = Mint..merge(destination: Dir.getwd) { argv: transient_argv, options: .merge(), help: parser.help } end |
.process_output_format(format_string, input_file) ⇒ String
Processes the output file format string with substitutions
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/mint/command_line.rb', line 145 def self.process_output_format(format_string, input_file) basename = File.basename(input_file, ".*") original_extension = File.extname(input_file)[1..-1] || "" # TODO: Remove hardcoded new_extension new_extension = "html" format_string. gsub('#{basename}', basename). gsub('#{original_extension}', original_extension). gsub('#{new_extension}', new_extension) end |
.publish!(files, commandline_options = {}) ⇒ void
This method returns an undefined value.
Renders and writes to file all resources described by a document. Specifically: it publishes a document, using the document’s accessors to determine file placement and naming, and then renders its style. This method will overwrite any existing content in a document’s destination files. The ‘render_style` option provides an easy way to stop Mint from rendering a style, even if the document’s style is not nil.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/mint/command_line.rb', line 331 def self.publish!(files, ={}) # TODO: Establish commandline defaults in one place # TODO: Use `commandline_options` everywhere instead of `options` and `doc_options` = { root: Dir.getwd }.merge(Mint.configuration_with ) if [:recursive] files = discover_files_recursively(files.empty? ? ["."] : files) end files.each_with_index do |file, idx| # Pass all files list when processing multiple files (for navigation in templates like garden) all_files = files.size > 1 ? files : nil Document.new(file, root: [:root], destination: [:destination], context: [:context], name: [:name], style_mode: [:style_mode], style_destination: [:style_destination], layout: [:layout], style: [:style], template: [:template], layout_or_style_or_template: [:layout_or_style_or_template], all_files: all_files ).publish!(:render_style => (idx == 0)) end end |
.set(key, value, scope = :local) ⇒ void
This method returns an undefined value.
Tries to set a config option (at the specified scope) per the user’s command.
290 291 292 |
# File 'lib/mint/command_line.rb', line 290 def self.set(key, value, scope = :local) configure({ key => value }, scope) end |
.templates(filter = "", scope = :local) ⇒ void
This method returns an undefined value.
List the installed templates
130 131 132 133 134 135 136 137 138 |
# File 'lib/mint/command_line.rb', line 130 def self.templates(filter = "", scope = :local) filter = filter.to_s # Convert nil to empty string Mint.templates(scope). grep(Regexp.new(filter)). sort. each do |template| puts "#{File.basename template} [#{template}]" end end |
.uninstall(name, scope = :local) ⇒ void
This method returns an undefined value.
Uninstall the named template
121 122 123 |
# File 'lib/mint/command_line.rb', line 121 def self.uninstall(name, scope = :local) FileUtils.rm_r Mint.template_path(name, scope) end |