lib/erbook/to_xhtml.rb
##
# Protects the given tags in the given input, passes
# that protected input to the given block, restores the
# given tags in the result of the block and returns it.
#
# [verbatim]
#   If true, the content of the elments having the given tags will not be
#   temporarily altered so that process nested elements can be processed.
#
Protects the given tags in the given input, passes
that protected input to the given block, restores the
given tags in the result of the block and returns it.

[verbatim]
If true, the content of the elments having the given tags will not be
temporarily altered so that process nested elements can be processed.
Protects the given tags in the given input passes that protected input to the given block restores the given tags in the result of the block and returns it . [ verbatim ] If true the content of the elments having the given tags will not be temporarily altered so that process nested elements can be processed .
def with_protected_tags input, tag_defs, verbatim 
    raise ArgumentError unless block_given?
    input = input.dup
    escapes = {}
    tag_defs.each_key do |tag|
      input.gsub! %r{(<#{tag}.*?>)(.*?)(</#{tag}>)}m do
        head, body, tail = $1, $2, $3
        body.gsub! %r/\\/, '\&\&'
        original =
          if verbatim
            body
          else
            head << CGI.escapeHTML(CGI.unescapeHTML(body)) << tail
          end
        escaped = calc_digest(original)
        escapes[escaped] = original
        escaped
      end
    end
    output = yield input
    until escapes.empty?
      escapes.each_pair do |esc, orig|
        tag = orig[/<\/(.+?)>\s*\z/, 1].to_sym
        raise ArgumentError, tag unless tag_defs.key? tag
        restore_ok =
          case tag_defs[tag]
          when :inline
            output.gsub! esc, orig
          when :block
            output.gsub!(/(<p>\s*)?#{Regexp.quote esc}/){ orig + $1.to_s }
          end
        escapes.delete esc if restore_ok
      end
    end
    output
  end
def with_protected_tags input , tag_defs , verbatim raise ArgumentError unless block_given? input = input . dup escapes = { } tag_defs . each_key do | tag | input . gsub! %r{ (< #{ tag } .*?>)(.*?)(</ #{ tag } >) }m do head , body , tail = $1 , $2 , $3 body . gsub! %r/ \\ / , '\&\&' original = if verbatim body else head << CGI . escapeHTML ( CGI . unescapeHTML ( body ) ) << tail end escaped = calc_digest ( original ) escapes [ escaped ] = original escaped end end output = yield input until escapes . empty? escapes . each_pair do | esc , orig | tag = orig [ / < \/ (.+?)> \s * \z / , 1 ] . to_sym raise ArgumentError , tag unless tag_defs . key? tag restore_ok = case tag_defs [ tag ] when :inline output . gsub! esc , orig when :block output . gsub! ( / (<p> \s *)? #{ Regexp . quote esc } / ) { orig + $1 . to_s } end escapes . delete esc if restore_ok end end output end
Protects the given tags in the given input, passes
that protected input to the given block, restores the
given tags in the result of the block and returns it.
Protects the given tags in the given input passes that protected input to the given block restores the given tags in the result of the block and returns it .
#:yields: input # protect the given tags by escaping them # XXX: when we restore protected tags later on, String.gsub! is #      removing all single backslashes for some reason... so we #      protect against this by doubling all single backslashes first # invoke the given block with the protected input # restore the protected tags by unescaping them # process inline elements normally # pull block-level elements out of paragraph tag added by Maruku
input
tag_defs
verbatim