2011-01-21 19:41:16 -08:00
|
|
|
# vim: set nosta noet ts=4 sw=4:
|
|
|
|
|
|
|
|
|
|
require 'strscan'
|
|
|
|
|
require 'stringio'
|
|
|
|
|
|
2008-11-08 18:59:05 +00:00
|
|
|
#
|
2008-11-09 00:27:36 +00:00
|
|
|
# Chunker: A convenience library for parsing __END__ tokens consistently.
|
2008-11-08 18:59:05 +00:00
|
|
|
#
|
2008-11-09 00:27:36 +00:00
|
|
|
# == Version
|
|
|
|
|
#
|
|
|
|
|
# $Id$
|
|
|
|
|
#
|
|
|
|
|
# == Author
|
|
|
|
|
#
|
|
|
|
|
# * Mahlon E. Smith <mahlon@martini.nu>
|
|
|
|
|
#
|
|
|
|
|
# :include: LICENSE
|
2008-11-08 18:59:05 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
### Namespace for the datablock parser.
|
|
|
|
|
###
|
|
|
|
|
module Chunker
|
|
|
|
|
|
2011-01-21 19:41:16 -08:00
|
|
|
# VCS Revision
|
|
|
|
|
VCSRev = %q$Rev$
|
2008-11-08 18:59:05 +00:00
|
|
|
|
2011-01-21 19:41:16 -08:00
|
|
|
# VCS Id
|
|
|
|
|
VCSId = %q$Id$
|
2008-11-08 18:59:05 +00:00
|
|
|
|
|
|
|
|
# Package version
|
2011-01-21 19:41:16 -08:00
|
|
|
VERSION = '1.0.0'
|
2008-11-08 18:59:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
### Parser class for __END__ data blocks.
|
2008-11-09 00:27:36 +00:00
|
|
|
### Find each __TOKEN__ within the __END__, and put each into a
|
|
|
|
|
### DATA_TOKEN constant within the namespace that included us.
|
2008-11-08 18:59:05 +00:00
|
|
|
###
|
|
|
|
|
class DataParser
|
|
|
|
|
|
|
|
|
|
# The mark for a DATA block.
|
2008-11-09 00:27:36 +00:00
|
|
|
END_TOKEN = /^__END__\r?\n/
|
2008-11-08 18:59:05 +00:00
|
|
|
|
|
|
|
|
# The mark for a 'sub' block.
|
2008-11-09 00:27:36 +00:00
|
|
|
CHUNK_TOKEN = /^__([A-Z\_0-9]+)__\r?\n/
|
2008-11-08 18:59:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
### Constructor: Given a +klass+ and an +io+ to the class file,
|
|
|
|
|
### extract the data blocks and install constants.
|
|
|
|
|
###
|
|
|
|
|
def initialize( klass, io )
|
|
|
|
|
io.open if io.closed?
|
2008-11-09 00:27:36 +00:00
|
|
|
end_string = io.read.split( END_TOKEN, 2 ).last
|
2008-11-08 18:59:05 +00:00
|
|
|
|
|
|
|
|
@klass = klass
|
|
|
|
|
@scanner = StringScanner.new( end_string )
|
|
|
|
|
io.close
|
|
|
|
|
|
2011-01-21 19:41:16 -08:00
|
|
|
# put each chunk into its own constant
|
|
|
|
|
#
|
2008-11-09 00:27:36 +00:00
|
|
|
if @scanner.check_until( CHUNK_TOKEN )
|
2008-11-08 18:59:05 +00:00
|
|
|
self.extract_blocks
|
2011-01-21 19:41:16 -08:00
|
|
|
|
|
|
|
|
# no sub blocks, put the whole mess into DATA_END
|
|
|
|
|
#
|
2008-11-08 18:59:05 +00:00
|
|
|
else
|
|
|
|
|
@klass.const_set( :DATA_END, StringIO.new( end_string ) )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#########
|
|
|
|
|
protected
|
|
|
|
|
#########
|
|
|
|
|
|
|
|
|
|
### Parse the current +io+ for data blocks, set contents to
|
|
|
|
|
### IO constants in the including class.
|
|
|
|
|
###
|
|
|
|
|
def extract_blocks
|
|
|
|
|
label = nil
|
|
|
|
|
|
2008-11-09 00:27:36 +00:00
|
|
|
while @scanner.scan_until( CHUNK_TOKEN ) and ! @scanner.eos?
|
2008-11-08 18:59:05 +00:00
|
|
|
data = ''
|
|
|
|
|
|
2008-11-09 00:27:36 +00:00
|
|
|
# First pass, __END__ contents (until next token, instead
|
2008-11-08 18:59:05 +00:00
|
|
|
# of entire data block.)
|
|
|
|
|
#
|
|
|
|
|
if label.nil?
|
|
|
|
|
label = 'END'
|
|
|
|
|
data = @scanner.pre_match
|
|
|
|
|
|
|
|
|
|
@scanner.pos = self.next_position
|
|
|
|
|
else
|
|
|
|
|
label = @scanner[1]
|
|
|
|
|
|
2011-01-21 19:41:16 -08:00
|
|
|
# Pull the next token text out of the data, set up the next pass
|
|
|
|
|
#
|
2008-11-09 00:27:36 +00:00
|
|
|
if data = @scanner.scan_until( CHUNK_TOKEN )
|
2011-01-21 19:41:16 -08:00
|
|
|
data = data[ 0, data.length - @scanner[0].length ]
|
2008-11-08 18:59:05 +00:00
|
|
|
@scanner.pos = self.next_position
|
2011-01-21 19:41:16 -08:00
|
|
|
|
|
|
|
|
# No additional blocks
|
|
|
|
|
#
|
2008-11-08 18:59:05 +00:00
|
|
|
else
|
|
|
|
|
data = @scanner.rest
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Add the IO constant to the class that included me.
|
|
|
|
|
@klass.const_set( "DATA_#{label}".to_sym, StringIO.new( data ) )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Return the next scanner position for searching.
|
|
|
|
|
###
|
|
|
|
|
def next_position
|
|
|
|
|
return @scanner.pos - @scanner[0].length
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2008-11-09 00:27:36 +00:00
|
|
|
### Hook included: Find the file path for how we arrived here, and open
|
|
|
|
|
### it as an IO object. Parse the IO for data block tokens.
|
2008-11-08 18:59:05 +00:00
|
|
|
###
|
|
|
|
|
def self.included( klass )
|
|
|
|
|
# klass.instance_eval{ __FILE__ } awww, nope.
|
2008-11-09 00:27:36 +00:00
|
|
|
# __FILE__ won't work here, so we find the filename via caller().
|
2008-11-08 18:59:05 +00:00
|
|
|
io = File.open( caller(1).last.sub(/:.*?$/, ''), 'r' )
|
2008-11-09 00:27:36 +00:00
|
|
|
|
2008-11-08 18:59:05 +00:00
|
|
|
DataParser.new( klass, io )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|