lib/nagoro/pipe/base.rb in manveru-nagoro-0.0.3 vs lib/nagoro/pipe/base.rb in manveru-nagoro-2009.01.29
- old
+ new
@@ -1,105 +1,57 @@
module Nagoro
module Pipe
-
- # Base is the superclass of most pipes, doing the grudge-work of
- # implementing the common interface for REXML and libxml for them as well
- # as providing all necessary defaults.
- #
- # Be aware that, depending on whether you use REXML or libxml, the specific
- # interface for them is implemented in the respective modules in
- # Nagoro::Patch
-
class Base
- attr_accessor :body, :stack
+ EMPTY_TAG = %w[ area base basefont br col frame hr
+ img input isindex link meta param ]
- EMPTY_ELEMENT = %w[ area base basefont br col frame hr
- img input isindex link meta param ]
+ def initialize(io)
+ @body, @stack = [], []
+ @scanner = Scanner.new(io, self)
+ end
- TEXT_IGNORE = [ (0..8), 11, 12, (14..31), (128..255) ]
- TEXT_PASS = [ 9, 10, 13, (32..126) ]
-
- def initialize(options = {})
- @body = []
- @stack = []
+ def result
+ @scanner.stream
+ @body.join
end
- def tag_start(tag, hash)
+ def tag_start(tag, args)
case tag
- when *EMPTY_ELEMENT
- append "<#{tag}#{hash.to_tag_params} />"
+ when *EMPTY_TAG
+ append "#{tag_with(tag, args)} />"
else
- append "<#{tag}#{hash.to_tag_params}>"
+ append "#{tag_with(tag, args)}>"
end
end
def tag_end(tag)
case tag
- when *EMPTY_ELEMENT
+ when *EMPTY_TAG
else
append "</#{tag}>"
end
end
def text(string)
- string.gsub!(/./u) do |match|
- case match.ord
- when *TEXT_IGNORE
- # ignore
- when *TEXT_PASS
- match
- else
- "&##{match.ord};" # check
- end
- end
- append string
+ append(string)
end
- def instruction(name, instruction)
- instruction.strip!
- append "<?#{name} #{instruction} ?>"
- end
-
- def comment(comment)
- append "<!--#{comment}-->"
- end
-
- def doctype(name, pub_sys, long_name, uri)
- append "<!DOCTYPE #{name} #{pub_sys} #{long_name} #{uri}>"
- end
-
- def doctype_end
- end
-
- def cdata(content)
- append "<![CDATA[#{content}]]>"
- end
-
- def xmldecl(version, encoding, standalone)
- params = {}
- params[:encoding] = encoding if encoding
- params[:standalone] = standalone if standalone
- append "<?xml #{params.to_tag_params}?>"
- end
-
def append(string)
@body << string
end
- def result
- @body.join
+ def instruction(name, instruction)
+ instruction.strip!
+ append "<?#{name} #{instruction} ?>"
end
- def preprocess
- # if @template.include?('#{')
- # @template.gsub!(/#\{((?![^\\]\})*|[^}]*)*\}/, '<?ro \1 ?>')
- # end
+ def tag_with(tag, hash)
+ "<#{tag}#{hash.map{|k,v| %( #{k}="#{v}") }.join}"
end
- def reset
- @body.clear
- @stack.clear
- self
+ def doctype(string)
+ string.strip!
+ append "<!DOCTYPE #{string}>"
end
end
end
end