class Preproc::IncludePreproc

Public Class Methods

from_url( src ) click to toggle source
# File lib/preproc.rb, line 45
def self.from_url( src )
  uri = URI.parse( src )
  text = Preproc.fetcher_read_utf8!( src )

  base_path = uri.path[0..uri.path.rindex('/')]  # get everything upto incl. last slash (/)
  self.new( text, base: "#{uri.scheme}://#{uri.host}:#{uri.port}#{base_path}" )
end
new( text, opts={} ) click to toggle source
# File lib/preproc.rb, line 54
def initialize( text, opts={} )
  @text = text
  @base = opts[:base]

  logger.debug("  base: #{@base}")
end

Public Instance Methods

read() click to toggle source
# File lib/preproc.rb, line 61
def read
  preproc( @text )
end

Private Instance Methods

preproc( text ) click to toggle source
# File lib/preproc.rb, line 67
def preproc( text )
  buf = ''
  text.each_line do |line|
    ### e.g. allows
    ##  @include 'test'  or
    ##  @INCLUDE "test"
    ##  note: for now we strip (allow) leading and trailing spaces (e.g. [ ]*)
    ##
    ##  todo/allow
    ##  @include( test ) - alternative syntax - why? why not?
    if line =~ /^[ ]*@include[ ]+("|')(.+?)\1[ ]*$/i
      logger.info( "  try to include '#{$2}'")
      ## buf << preproc( 'include here' )
      buf << preproc( read_include( $2 ) )
      buf << "\n"
    else
      buf << line
      buf << "\n"
    end
  end
  buf
end
read_include( name ) click to toggle source
# File lib/preproc.rb, line 90
def read_include( name )
  ## for now assume fetch via url
  ## note: assume partial e.g. add leading underscore (_)
  ##   e.g. test => _test.ini

  src = "#{@base}_#{name}.ini"
  logger.info( "  try to fetch include #{src}")

  Preproc.fetcher_read_utf8!( src )
end