Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added colorization to the webby output and simplified the output mess…

…ages. When building a site, the standard logging goobers no longer appear. The action being taken for the file is colorized if the "facets" gem is installed.
  • Loading branch information...
commit cdb8e98d5c037ffdc1e7679ae231df59495de93c 1 parent 3a72af0
@TwP authored
View
3  History.txt
@@ -1,8 +1,9 @@
== 0.9.3 / 2008-10-
-* 1 minor enhancement
+* 2 minor enhancements
- Added a "wikiwords" filter to process [[text]] as links to other pages
(thanks to Paul)
+ - Added colorization to the output text (facets gem required)
* 1 bug fix
- Fixed a bug on Windows where line endings were messing with meta-data
View
1  Manifest.txt
@@ -134,6 +134,7 @@ lib/webby/helpers/tag_helper.rb
lib/webby/helpers/tex_img_helper.rb
lib/webby/helpers/ultraviolet_helper.rb
lib/webby/helpers/url_helper.rb
+lib/webby/journal.rb
lib/webby/link_validator.rb
lib/webby/renderer.rb
lib/webby/resources.rb
View
33 bin/webby
@@ -1,5 +1,38 @@
#!/usr/bin/env ruby
+require 'rubygems'
+require 'logging'
+
+::Logging.configure {
+ logger('Webby') {
+ level :info
+ appenders 'stdout'
+ }
+
+ logger('Webby::Journal') {
+ level :info
+ additive false
+ appenders 'journal'
+ }
+
+ appender('stdout') {
+ type 'Stdout'
+ layout {
+ type 'Pattern'
+ pattern "[%d] %5l: %m\n"
+ date_pattern "%H:%M:%S"
+ }
+ }
+
+ appender('journal') {
+ type 'Stdout'
+ layout {
+ type 'Pattern'
+ pattern "%m\n"
+ }
+ }
+}
+
require File.expand_path(
File.join(File.dirname(__FILE__), %w[.. lib webby]))
View
33 bin/webby-gen
@@ -1,5 +1,38 @@
#!/usr/bin/env ruby
+require 'rubygems'
+require 'logging'
+
+::Logging.configure {
+ logger('Webby') {
+ level :info
+ appenders 'stdout'
+ }
+
+ logger('Webby::Journal') {
+ level :info
+ additive false
+ appenders 'journal'
+ }
+
+ appender('stdout') {
+ type 'Stdout'
+ layout {
+ type 'Pattern'
+ pattern "[%d] %5l: %m\n"
+ date_pattern "%H:%M:%S"
+ }
+ }
+
+ appender('journal') {
+ type 'Stdout'
+ layout {
+ type 'Pattern'
+ pattern "%m\n"
+ }
+ }
+}
+
require File.expand_path(
File.join(File.dirname(__FILE__), %w[.. lib webby]))
View
8 lib/webby.rb
@@ -7,14 +7,6 @@
require 'ostruct'
require 'date'
-# Configure Webby to log to STDOUT at the 'info' level
-Logging::Logger['Webby'].level = :info
-Logging::Logger['Webby'].add_appenders(Logging::Appender.stdout)
-Logging::Appender.stdout.layout = Logging::Layouts::Pattern.new(
- :pattern => "[%d] %5l: %m\n", # [date] LEVEL: message
- :date_pattern => "%H:%M:%S" # date == HH:MM:SS
-)
-
module Webby
# :stopdoc:
View
67 lib/webby/apps/generator.rb
@@ -1,5 +1,6 @@
require 'fileutils'
require 'optparse'
+require 'forwardable'
module Webby::Apps
@@ -9,6 +10,7 @@ module Webby::Apps
#
class Generator
+ extend Forwardable
# Create a new Generator instance and run the +webby+ application given the
# command line _args_.
@@ -22,10 +24,34 @@ def self.run( args )
# Initialize a new generator object.
#
- def initialize
+ def initialize( output = $stdout, input = $stdin )
@options = {}
@site = @template = nil
- @stdout, @stdin = $stdout, $stdin
+ @output, @input = output, input
+ @journal = journal
+ end
+
+ def_delegators :@journal,
+ :exists, :create, :force, :skip, :identical
+
+ # Writes the given objects to the output destination. Each object is
+ # followed by a newline unless the object is a string with a newline
+ # already at the end.
+ #
+ def puts( *args )
+ @output.puts(*args)
+ end
+
+ # Writes the given objects to the output destination.
+ #
+ def print( *args )
+ @output.print(*args)
+ end
+
+ # Reads a line text frim the input source.
+ #
+ def gets
+ @input.gets
end
# Run the generator executing the commands specified by the user on the
@@ -60,18 +86,18 @@ def parse( args )
opts.on('-t', '--templates', 'list available templates') {
ary = templates.map {|t| ::File.basename(t)}
ary.delete 'webby'
- @stdout.puts "\nAvailable Templates"
- @stdout.puts " #{ary.join(', ')}"
- @stdout.puts
+ puts "\nAvailable Templates"
+ puts " #{ary.join(', ')}"
+ puts
exit
}
opts.separator ''
opts.separator 'common options:'
- opts.on( '-h', '--help', 'show this message' ) {@stdout.puts opts; exit}
+ opts.on( '-h', '--help', 'show this message' ) {puts opts; exit}
opts.on( '--version', 'show version' ) do
- @stdout.puts "Webby #{::Webby::VERSION}"
+ puts "Webby #{::Webby::VERSION}"
exit
end
@@ -87,13 +113,13 @@ def parse( args )
# exit if comand line args are missing
if site.nil? or tmpl.nil?
- @stdout.puts opts
+ puts opts
exit 1
end
templates.each {|t| self.template = t if t =~ %r/\/#{tmpl}$/}
if template.nil?
- @stdout.puts opts
+ puts opts
abort "Could not find template '#{tmpl}'"
end
@@ -205,25 +231,13 @@ def win_line_endings( src, dst )
end
end
- %w[exists create force skip identical].each do |m|
- class_eval "def #{m}( msg ) message('#{m}', msg); end"
- end
-
- # Print the given message and message type to stdout.
- #
- def message( type, msg )
- msg = msg.sub(%r/#{site}\/?/, '')
- return if msg.empty?
- @stdout.puts "%13s %s" % [type, msg]
- end
-
# Prints an abort message to the screen and then exits the Ruby
# interpreter. A non-zero return code is used to indicate an error.
#
def abort( msg )
- @stdout.puts "\nAborting!"
- @stdout.puts " #{msg}"
- @stdout.puts
+ puts "\nAborting!"
+ puts " #{msg}"
+ puts
exit 1
end
@@ -265,9 +279,8 @@ def identical?( src, dst )
#
def force_file_collision?( dst )
dst = dst.sub(%r/#{site}\/?/, '')
- @stdout.print "overwrite #{dst}? [(Y)es (n)o (q)uit] "
- @stdout.flush
- case @stdin.gets
+ print "overwrite #{dst}? [(Y)es (n)o (q)uit] "
+ case gets
when %r/q/i then abort 'user asked to quit'
when %r/n/i then :skip
when %r/y/i then :force
View
13 lib/webby/auto_builder.rb
@@ -21,13 +21,15 @@ def self.run
self.new.run
end
+ attr_reader :logger
+
# call-seq:
# AutoBuilder.new
#
# Create a new AutoBuilder class.
#
def initialize
- @log = Logging::Logger[self]
+ @logger = Logging::Logger[self]
@builder = Builder.new
@builder.load_files
@@ -53,15 +55,16 @@ def update( *events )
return if ary.empty?
ary.each do |evt|
- @log.debug "changed #{evt.path}"
+ logger.debug "changed #{evt.path}"
next unless test ?f, evt.path
next if evt.path =~ ::Webby.exclude
Resources.new evt.path
end
- @builder.run :load_files => false
+ logger.info 'running the build'
+ @builder.run :load_files => false, :verbose => false
rescue => err
- @log.error err
+ logger.error err
end
# call-seq:
@@ -71,7 +74,7 @@ def update( *events )
# Ctrl-C to stop the watcher thread.
#
def run
- @log.info 'starting autobuild (Ctrl-C to stop)'
+ logger.info 'starting autobuild (Ctrl-C to stop)'
Signal.trap('INT') {@watcher.stop}
View
18 lib/webby/builder.rb
@@ -82,7 +82,6 @@ def new_page_info
[args.page, args.title, args.dir]
end
-
private
# Returns the binding in the scope of the Builder class object.
@@ -98,7 +97,7 @@ def scope() binding end
# layout directories.
#
def initialize
- @log = Logging::Logger[self]
+ @logger = Logging::Logger[self]
end
# call-seq:
@@ -125,24 +124,25 @@ def initialize
#
def run( opts = {} )
opts[:load_files] = true unless opts.has_key?(:load_files)
+ verbose = opts.getopt(:verbose, true)
unless test(?d, output_dir)
- @log.info "creating #{output_dir}"
+ journal.create output_dir
FileUtils.mkdir output_dir
end
load_files if opts[:load_files]
Resources.pages.each do |page|
- next unless page.dirty? or opts[:rebuild]
-
- @log.info "creating #{page.destination}"
-
- # make sure the directory exists
- FileUtils.mkdir_p ::File.dirname(page.destination)
+ unless page.dirty? or opts[:rebuild]
+ journal.identical(page.destination) if verbose
+ next
+ end
# copy the resource to the output directory if it is static
if page.instance_of? Resources::Static
+ FileUtils.mkdir_p ::File.dirname(page.destination)
+ journal.create_or_update(page)
FileUtils.cp page.path, page.destination
FileUtils.chmod 0644, page.destination
View
5 lib/webby/core_ext/kernel.rb
@@ -16,6 +16,11 @@ def cmd_available?( *args )
STDERR.reopen io.last
$stdout, $stderr = STDOUT, STDERR
end
+
+ def journal
+ @journal ||= ::Webby::Journal.new
+ end
+
end # module Kernel
# EOF
View
91 lib/webby/journal.rb
@@ -0,0 +1,91 @@
+
+begin require 'facets/ansicode'; rescue LoadError; end
+
+module Webby
+
+# The Journal class is used to output simple messages regarding the creation
+# and updating of files when webby applications are run. The output messages
+# will be color coded if the "facets" gem is installed.
+#
+class Journal
+
+ attr_reader :logger, :colorize
+
+ # Create a new journal
+ #
+ def initialize
+ @logger = ::Logging::Logger[self]
+ @colorize = defined?(::ANSICode) and ENV.has_key?('TERM')
+ end
+
+ # Output a message of the given _type_ using the option _color_ code. The
+ # available codes are as follows:
+ #
+ # * black
+ # * red
+ # * green
+ # * yellow
+ # * blue
+ # * magenta
+ # * cyan
+ # * white
+ #
+ # The color is specified as a string or a symbol.
+ #
+ def typed_message( type, msg, color = nil )
+ type = type.to_s.rjust(13)
+ type = ::ANSICode.send(color, type) unless color.nil?
+ logger.info "#{type} #{msg.to_s}"
+ end
+
+ # Output a "create" message or an "update" message depending on whether
+ # the given _page_ already has a generated output file or not.
+ #
+ def create_or_update( page )
+ if test(?e, page.destination)
+ update(page.destination)
+ else
+ create(page.destination)
+ end
+ end
+
+ # Output a create message.
+ #
+ def create( msg )
+ typed_message('create', msg, (colorize ? :green : nil))
+ end
+
+ # Output an update message.
+ #
+ def update( msg )
+ typed_message('update', msg, (colorize ? :yellow : nil))
+ end
+
+ # Output a force message.
+ #
+ def force( msg )
+ typed_message('force', msg, (colorize ? :red : nil))
+ end
+
+ # Output a skip message.
+ #
+ def skip( msg )
+ typed_message('skip', msg, (colorize ? :yellow : nil))
+ end
+
+ # Output an exists message.
+ #
+ def exists( msg )
+ typed_message('exists', msg, (colorize ? :cyan : nil))
+ end
+
+ # Output an identical message.
+ #
+ def identical( msg )
+ typed_message('identical', msg, (colorize ? :cyan : nil))
+ end
+
+end # class Journal
+end # module Webby
+
+# EOF
View
15 lib/webby/renderer.rb
@@ -36,6 +36,9 @@ def self.write( page )
renderer = self.new(page)
loop {
+ FileUtils.mkdir_p ::File.dirname(page.destination)
+ journal.create_or_update(page)
+
::File.open(page.destination, 'w') do |fd|
fd.write(renderer._layout_page)
end
@@ -43,6 +46,8 @@ def self.write( page )
}
end
+ attr_reader :logger
+
# call-seq:
# Renderer.new( page )
#
@@ -64,7 +69,7 @@ def initialize( page )
@_bindings = []
@_content_for = {}
- @log = Logging::Logger[self]
+ @logger = Logging::Logger[self]
end
# call-seq:
@@ -227,11 +232,11 @@ def _layout_page
@content
rescue ::Webby::Error => err
- @log.error "while rendering page '#{@page.path}'"
- @log.error err.message
+ logger.error "while rendering page '#{@page.path}'"
+ logger.error err.message
rescue => err
- @log.error "while rendering page '#{@page.path}'"
- @log.fatal err
+ logger.error "while rendering page '#{@page.path}'"
+ logger.fatal err
exit 1
ensure
@content = nil
View
7 lib/webby/tasks/growl.rake
@@ -2,12 +2,13 @@
unless WINDOWS
task :growl do
- Logging::Logger['Webby'].add_appenders(Logging::Appenders::Growl.new(
- "Webby",
+ growl = Logging::Appenders::Growl.new('growl',
:layout => Logging::Layouts::Pattern.new(:pattern => "%5l - Webby\000%m"),
:coalesce => true,
:separator => "\000"
- ))
+ )
+ Logging::Logger['Webby'].add_appenders(growl)
+ Logging::Logger['Webby::Journal'].add_appenders(growl)
end
end # unless WINDOWS
View
5 spec/webby/apps/generator_spec.rb
@@ -55,8 +55,9 @@
before :each do
@strio = StringIO.new
- @generator = Webby::Apps::Generator.new
- @generator.instance_variable_set(:@stdout, @strio)
+ ::Logging::Logger['Webby::Journal'].appenders =
+ ::Logging::Appenders::IO.new('test', @strio)
+ @generator = Webby::Apps::Generator.new(@strio)
class << @strio
def to_s
View
7 tasks/manifest.rake
@@ -1,4 +1,3 @@
-# $Id$
require 'find'
@@ -15,9 +14,9 @@ namespace :manifest do
lines.map! do |line|
case line
when %r/^(-{3}|\+{3})/; nil
- when %r/^@/; Console::ANSICode.blue line
- when %r/^\+/; Console::ANSICode.green line
- when %r/^\-/; Console::ANSICode.red line
+ when %r/^@/; ANSICode.blue line
+ when %r/^\+/; ANSICode.green line
+ when %r/^\-/; ANSICode.red line
else line end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.