1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
module KramdownRFC
class ParameterSet
include Kramdown::Utils::Html
attr_reader :f
def initialize(y)
raise "*** invalid parameter set #{y.inspect}" unless Hash === y
@f = y
@av = {}
end
attr :av
def [](pn)
@f.delete(pn.to_s)
end
def []=(pn, val)
@f[pn.to_s] = val
end
def default(pn, &block)
@f.fetch(pn.to_s, &block)
end
def default!(pn, value)
default(pn) {
@f[pn.to_s] = value
}
end
def has(pn)
@f[pn.to_s]
end
def has?(pn)
@f.key?(pn.to_s)
end
def escattr(str)
escape_html(str.to_s, :attribute)
end
def van(pn) # pn is a parameter name, possibly with =aliases
names = pn.to_s.split("=")
[self[names.reverse.find{|nm| has?(nm)}], names.first]
end
def attr(pn)
val, an = van(pn)
@av[an.intern] = val
%{#{an}="#{escattr(val)}"} if val # see attrtf below
end
def attrs(*pns)
pns.map{ |pn| attr(pn) if pn }.compact.join(" ")
end
def attrtf(pn) # can do an overriding false value
val, an = van(pn)
@av[an.intern] = val
%{#{an}="#{escattr(val)}"} unless val.nil?
end
def attrstf(*pns)
pns.map{ |pn| attrtf(pn) if pn }.compact.join(" ")
end
def ele(pn, attr=nil, defcontent=nil, markdown=false)
val, an = van(pn)
val ||= defcontent
val = [val] if Hash === val
Array(val).map do |val1|
a = Array(attr).dup
if Hash === val1
val1.each do |k, v|
if k == ":"
val1 = v
else
k = Kramdown::Element.attrmangle(k) || k
a.unshift(%{#{k}="#{escattr(v)}"})
end
end
end
v = val1.to_s.strip
contents =
if markdown
::Kramdown::Converter::Rfc2629::process_markdown(v)
else
escape_html(v)
end
%{<#{[an, *a.map(&:to_s)].join(" ").strip}>#{contents}</#{an}>}
end.join(" ")
end
def arr(an, converthash=true, must_have_one=false, &block)
arr = self[an] || []
arr = [arr] if Hash === arr && converthash
arr << { } if must_have_one && arr.empty?
Array(arr).each(&block)
end
def rest
@f
end
def warn_if_leftovers
if !@f.empty?
warn "*** attributes left #{@f.inspect}!"
end
end
end
end
|