Class: KramdownRFC::ParameterSet

Inherits:
Object
  • Object
show all
Includes:
Kramdown::Utils::Html
Defined in:
lib/kramdown-rfc/parameterset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(y) ⇒ ParameterSet

Returns a new instance of ParameterSet.



7
8
9
10
11
# File 'lib/kramdown-rfc/parameterset.rb', line 7

def initialize(y)
  raise "*** invalid parameter set #{y.inspect}" unless Hash === y
  @f = y
  @av = {}
end

Instance Attribute Details

#avObject (readonly)

Returns the value of attribute av.



12
13
14
# File 'lib/kramdown-rfc/parameterset.rb', line 12

def av
  @av
end

#fObject (readonly)

Returns the value of attribute f.



6
7
8
# File 'lib/kramdown-rfc/parameterset.rb', line 6

def f
  @f
end

Instance Method Details

#[](pn) ⇒ Object



13
14
15
# File 'lib/kramdown-rfc/parameterset.rb', line 13

def [](pn)
  @f.delete(pn.to_s)
end

#[]=(pn, val) ⇒ Object



16
17
18
# File 'lib/kramdown-rfc/parameterset.rb', line 16

def []=(pn, val)
  @f[pn.to_s] = val
end

#arr(an, converthash = true, must_have_one = false, &block) ⇒ Object



82
83
84
85
86
87
# File 'lib/kramdown-rfc/parameterset.rb', line 82

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

#attr(pn) ⇒ Object



40
41
42
43
44
# File 'lib/kramdown-rfc/parameterset.rb', line 40

def attr(pn)
  val, an = van(pn)
  @av[an.intern] = val
  %{#{an}="#{escattr(val)}"}    if val # see attrtf below
end

#attrs(*pns) ⇒ Object



45
46
47
# File 'lib/kramdown-rfc/parameterset.rb', line 45

def attrs(*pns)
  pns.map{ |pn| attr(pn) if pn }.compact.join(" ")
end

#attrstf(*pns) ⇒ Object



53
54
55
# File 'lib/kramdown-rfc/parameterset.rb', line 53

def attrstf(*pns)
  pns.map{ |pn| attrtf(pn) if pn }.compact.join(" ")
end

#attrtf(pn) ⇒ Object

can do an overriding false value



48
49
50
51
52
# File 'lib/kramdown-rfc/parameterset.rb', line 48

def attrtf(pn)              # can do an overriding false value
  val, an = van(pn)
  @av[an.intern] = val
  %{#{an}="#{escattr(val)}"}    unless val.nil?
end

#default(pn, &block) ⇒ Object



19
20
21
# File 'lib/kramdown-rfc/parameterset.rb', line 19

def default(pn, &block)
  @f.fetch(pn.to_s, &block)
end

#default!(pn, value) ⇒ Object



22
23
24
25
26
# File 'lib/kramdown-rfc/parameterset.rb', line 22

def default!(pn, value)
  default(pn) {
    @f[pn.to_s] = value
  }
end

#ele(pn, attr = nil, defcontent = nil, markdown = false) ⇒ Object



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
# File 'lib/kramdown-rfc/parameterset.rb', line 56

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

#escattr(str) ⇒ Object



33
34
35
# File 'lib/kramdown-rfc/parameterset.rb', line 33

def escattr(str)
  escape_html(str.to_s, :attribute)
end

#has(pn) ⇒ Object



27
28
29
# File 'lib/kramdown-rfc/parameterset.rb', line 27

def has(pn)
  @f[pn.to_s]
end

#has?(pn) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/kramdown-rfc/parameterset.rb', line 30

def has?(pn)
  @f.key?(pn.to_s)
end

#restObject



88
89
90
# File 'lib/kramdown-rfc/parameterset.rb', line 88

def rest
  @f
end

#van(pn) ⇒ Object

pn is a parameter name, possibly with =aliases



36
37
38
39
# File 'lib/kramdown-rfc/parameterset.rb', line 36

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

#warn_if_leftoversObject



91
92
93
94
95
# File 'lib/kramdown-rfc/parameterset.rb', line 91

def warn_if_leftovers
  if !@f.empty?
    warn "*** attributes left #{@f.inspect}!"
  end
end