Module Webgen::CLI
In: lib/webgen/cli/utils.rb
lib/webgen/cli/webgui_command.rb
lib/webgen/cli/create_command.rb
lib/webgen/cli/run_command.rb
lib/webgen/cli/apply_command.rb
lib/webgen/cli.rb
Error RenderError CommandNotFoundError LoadError NodeCreationError ::Rake::TaskLib WebgenTask Node Context\n[lib/webgen/context.rb\nlib/webgen/context/nodes.rb\nlib/webgen/context/render.rb\nlib/webgen/context/tags.rb] Tree FileSystem Sitemap Feed Copy Virtual Sitemap Directory Page Fragment Template Memory Metainfo Coderay Sitemap IncludeFile BreadcrumbTrail Langbar TikZ Menu Tags Fragments Resource Website Tidy Head Less Kramdown Xmllint Blocks Helpers Configuration Comparable Language Path StandardError CmdParse::CommandParser CommandParser CmdParse::Command RunCommand CreateCommand WebguiCommand ApplyCommand WebsiteAccess Main Loggable OutputPathHelpers ExecuteCommand Link Date Relocatable Metainfo ::Kramdown::Converter::Html KramdownHtmlConverter Cache Blackboard WebsiteManager Logger Page ProxyNode Utils Scss RDoc Sass Erb RDiscount Erubis Haml Maruku Builder RedCloth AccessHash TarArchive Stacked FileSystem lib/webgen/cache.rb lib/webgen/error.rb lib/webgen/languages.rb lib/webgen/website.rb lib/webgen/blackboard.rb lib/webgen/tree.rb lib/webgen/websitemanager.rb lib/webgen/logger.rb lib/webgen/context/tags.rb lib/webgen/configuration.rb lib/webgen/path.rb lib/webgen/webgentask.rb lib/webgen/page.rb lib/webgen/node.rb lib/webgen/cli/run_command.rb lib/webgen/cli/utils.rb lib/webgen/cli/apply_command.rb lib/webgen/cli/webgui_command.rb lib/webgen/cli.rb lib/webgen/cli/create_command.rb Color CLI ClassMethods WebsiteAccess LanguageManager lib/webgen/output/filesystem.rb Output lib/webgen/common/sitemap.rb Common lib/webgen/sourcehandler/metainfo.rb lib/webgen/sourcehandler/memory.rb lib/webgen/sourcehandler/copy.rb lib/webgen/sourcehandler/directory.rb lib/webgen/sourcehandler.rb lib/webgen/sourcehandler/page.rb lib/webgen/sourcehandler/template.rb lib/webgen/sourcehandler/fragment.rb lib/webgen/sourcehandler/sitemap.rb lib/webgen/sourcehandler/virtual.rb lib/webgen/sourcehandler/feed.rb OutputPathHelpers Base SourceHandler lib/webgen/tag/coderay.rb lib/webgen/tag/relocatable.rb lib/webgen/tag/menu.rb lib/webgen/tag/langbar.rb lib/webgen/tag/executecommand.rb lib/webgen/tag/breadcrumbtrail.rb lib/webgen/tag/metainfo.rb lib/webgen/tag/link.rb lib/webgen/tag/includefile.rb lib/webgen/tag/date.rb lib/webgen/tag/tikz.rb lib/webgen/tag/sitemap.rb Base Tag lib/webgen/contentprocessor/less.rb lib/webgen/contentprocessor/scss.rb lib/webgen/contentprocessor/blocks.rb lib/webgen/contentprocessor/rdoc.rb lib/webgen/contentprocessor/sass.rb lib/webgen/contentprocessor/erb.rb lib/webgen/contentprocessor/rdiscount.rb lib/webgen/contentprocessor/tags.rb lib/webgen/contentprocessor/erubis.rb lib/webgen/contentprocessor/kramdown/html.rb lib/webgen/contentprocessor/haml.rb lib/webgen/contentprocessor/maruku.rb lib/webgen/contentprocessor/xmllint.rb lib/webgen/contentprocessor/kramdown.rb lib/webgen/contentprocessor/head.rb lib/webgen/contentprocessor/builder.rb lib/webgen/contentprocessor/tidy.rb lib/webgen/contentprocessor/redcloth.rb lib/webgen/contentprocessor/fragments.rb lib/webgen/contentprocessor.rb ContentProcessor lib/webgen/source/tararchive.rb lib/webgen/source/stacked.rb lib/webgen/source/resource.rb lib/webgen/source/filesystem.rb Source Loggable Webgen dot/m_81_0.png

Namespace for all classes that act as CLI commands.

Implementing a CLI command

Each CLI command class needs to be put into this module and has to end with Command, otherwise it is not used. A CLI command is an extension that can be invoked from the webgen command and thus needs to be derived from CmdParse::Command. For detailed information on this class and the whole cmdparse package have a look at cmdparse.rubyforge.org!

Sample CLI command

Here is a sample CLI command extension which could be put, for example, into the ext/init.rb of a webgen website:

  require 'webgen/cli'

  class Webgen::CLI::SampleCommand < CmdParse::Command

    def initialize
      super('sample', false)
      self.short_desc = "This sample command just outputs its parameters"
      self.description = Webgen::CLI::Utils.format("Uses the global verbosity level and outputs additional " +
        "information when the level is set to verbose!")
      @username = nil
      self.options = CmdParse::OptionParserWrapper.new do |opts|
        opts.separator "Options:"
        opts.on('-u', '--user USER', String,
          'Specify an additional user name to output') {|username| @username = username}
      end
    end

    def execute(args)
      if args.length == 0
        raise OptionParser::MissingArgument.new('ARG1 [ARG2 ...]')
      else
        puts "Command line arguments:"
        args.each {|arg| puts arg}
        if commandparser.verbosity == :verbose
          puts "Yeah, some additional information is always cool!"
        end
        puts "The entered username: #{@username}" if @username
      end
    end

  end

Note the use of Webgen::CLI::Utils.format in the initialize method so that the long text gets wrapped correctly! The Utils class provides some other useful methods, too!

For information about which attributes are available on the webgen command parser instance have a look at Webgen::CLI::CommandParser!

Methods

Classes and Modules

Module Webgen::CLI::Color
Class Webgen::CLI::ApplyCommand
Class Webgen::CLI::CommandParser
Class Webgen::CLI::CreateCommand
Class Webgen::CLI::RunCommand
Class Webgen::CLI::Utils
Class Webgen::CLI::WebguiCommand

Public Class methods

Return an array of lines which represents the text in content formatted sothat no line is longer than width characters. The indent parameter specifies the amount of spaces prepended to each line. If first_line_indented is true, then the first line is indented.

[Source]

    # File lib/webgen/cli/utils.rb, line 45
45:     def self.format(content, indent = 0, first_line_indented = false, width = DEFAULT_WIDTH)
46:       content = (content || '').dup
47:       length = width - indent
48: 
49:       paragraphs = content.split(/\n\n/)
50:       if (0..1) === paragraphs.length
51:         pattern = /^(.{0,#{length}})[ \n]/m
52:         lines = []
53:         while content.length > length
54:           if content =~ pattern
55:             str = $1
56:             len = $&.length
57:           else
58:             str = content[0, length]
59:             len = length
60:           end
61:           lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + str.gsub(/\n/, ' ')
62:           content.slice!(0, len)
63:         end
64:         lines << (lines.empty? && !first_line_indented ? '' : ' '*indent) + content.gsub(/\n/, ' ') unless content.strip.empty?
65:         lines
66:       else
67:         ((format(paragraphs.shift, indent, first_line_indented, width) << '') +
68:          paragraphs.collect {|p| format(p, indent, true, width) << '' }).flatten[0..-2]
69:       end
70:     end

Creates a listing of the key-value pairs of hash under a section called name.

[Source]

    # File lib/webgen/cli/utils.rb, line 85
85:     def self.hash_output(name, hash)
86:       ljust = 20
87:       puts section('Name', ljust) + "#{lred(name)}"
88: 
89:       hash.sort_by {|k,v| k.to_s }.each do |name, value|
90:         next unless value.respond_to?(:to_str)
91:         desc = format(value.to_str, ljust)
92:         puts section(name.to_s.capitalize, ljust) + desc.shift
93:         desc.each {|line| puts line}
94:       end
95:       puts
96:     end

Return a headline with the given text and amount of indent.

[Source]

    # File lib/webgen/cli/utils.rb, line 73
73:     def self.headline(text, indent = 2)
74:       ' '*indent + "#{bold(text)}"
75:     end

Tries to match name to a unique bundle name of the WebsiteManager wm. If this can not be done, it is checked whether name is actually a valid bundle URL and if so, the URL source is added to the bundles of wm.

Returns the correct bundle name or raises an error.

[Source]

     # File lib/webgen/cli/utils.rb, line 103
103:     def self.match_bundle_name(wm, name)
104:       matches = wm.bundles.keys.select {|k| k =~ /#{Regexp.escape(name)}/}
105:       if matches.size > 1
106:         raise ArgumentError.new("#{name} matches more than one bundle: #{matches.join(", ")}")
107:       elsif matches.size == 0
108:         begin
109:           source = Webgen::Source::TarArchive.new(name)
110:           wm.add_source(source, 'custom-URL-source')
111:           name = 'custom-URL-source'
112:         rescue
113:           raise ArgumentError.new("#{name} is neither a valid bundle name nor a valid URL")
114:         end
115:       else
116:         name = matches.first
117:       end
118:       name
119:     end

Used for dynamically formatting the text (setting color, bold face, …).

[Source]

    # File lib/webgen/cli/utils.rb, line 34
34:     def self.method_missing(id, text = nil)
35:       if USE_ANSI_COLORS && Color.respond_to?(id)
36:         Color.send(id, text.to_s)
37:       else
38:         text.to_s
39:       end
40:     end

Return a section header with the given text formatted in the given color and indented according to indent. The whole text is also left justified to the column specified with ljustlength.

[Source]

    # File lib/webgen/cli/utils.rb, line 80
80:     def self.section(text, ljustlength = 0, indent = 4, color = :green)
81:       ' '*indent + "#{send(color, text)}".ljust(ljustlength - indent + send(color).length)
82:     end

[Validate]