Class: Foxy::Node

Inherits:
Struct show all
Includes:
Monads
Defined in:
lib/foxy/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Monads

#and_then, #dangerously, #many, #mapy, #normally, #optionaly, #safy, #then

Instance Attribute Details

#contentObject

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



4
5
6
# File 'lib/foxy/node.rb', line 4

def content
  @content
end

#extraObject

Returns the value of attribute extra

Returns:

  • (Object)

    the current value of extra



4
5
6
# File 'lib/foxy/node.rb', line 4

def extra
  @extra
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



4
5
6
# File 'lib/foxy/node.rb', line 4

def type
  @type
end

Class Method Details

.build(closetag, singletag, tag, notag, comment, other) ⇒ Object



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
# File 'lib/foxy/node.rb', line 63

def self.build(closetag, singletag, tag, notag, comment, other)
  if tag
    tagname = RE_TAG.match(tag)[1]
    id = RE_TAG_ID.match(tag)
    id &&= id[1].gsub(/\A('|")*|('|")*\Z/, "")
    cls = RE_TAG_CLS.match(tag)
    cls = cls && cls[1].gsub(/\A('|")*|('|")*\Z/, "").split || []
    if SINGLES.include? tagname
      Node.new(:singletag, tag, [tagname, id, cls])
    else
      Node.new(:tag, tag, [tagname, id, cls])
    end
  elsif singletag
    tagname = RE_TAG.match(singletag)[1]
    id = RE_TAG_ID.match(singletag)
    id &&= id[1].gsub(/\A('|")*|('|")*\Z/, "")
    cls = RE_TAG_CLS.match(singletag)
    cls = cls && cls[1].gsub(/\A('|")*|('|")*\Z/, "").split || []
    Node.new(:singletag, singletag, [tagname, id, cls])
  elsif closetag
    closetagname = RE_CLOSETAG.match(closetag)[1]
    Node.new(:closetag, closetag, [closetagname])
  elsif notag
    Node.new(:notag, notag, nil)
  elsif comment
    Node.new(:comment, comment, nil)
  elsif other
    Node.new(:notag, other, nil)
  end
end

Instance Method Details

#attr(name) ⇒ Object



7
8
9
10
# File 'lib/foxy/node.rb', line 7

def attr(name)
  value = attr_regex(name).match(content)
  value && value[1].sub(/\A('|")?/, "").sub(/('|")?\Z/, "")
end

#attr_regex(name) ⇒ Object



59
60
61
# File 'lib/foxy/node.rb', line 59

def attr_regex(name)
  /#{name}=(("[^"]*")|('[^']*')|[^\s>]+)/mi
end

#clean(translate_table: {}, allow: ALLOW) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/foxy/node.rb', line 40

def clean(translate_table: {}, allow: ALLOW)
  if [:tag, :singletag, :closetag].include? type
    name = extra[0].downcase
    slash1 = tag? ? "" : "/"
    slash2 = (tag? && closetag?) ? "/" : ""
    allow.each do |attr_name|
      attr_value = attr(attr_name)
      slash2 = " #{attr_name}=\"#{attr_value}\"#{slash2}" if attr_value
    end
    name = translate_table.fetch(name, name)
    content = "<#{slash1}#{name}#{slash2}>"

    id = allow.include?("id") ? extra[1] : nil
    cls = allow.include?("class") ? extra[2] : []
    return Node.new(type, content, [name, id, cls])
  end
  self
end

#closetag?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/foxy/node.rb', line 16

def closetag?
  [:closetag, :singletag].include? type
end

#cls!Object



36
37
38
# File 'lib/foxy/node.rb', line 36

def cls!
  extra[2]
end

#idObject



24
25
26
# File 'lib/foxy/node.rb', line 24

def id
  tag? && id!
end

#id!Object



32
33
34
# File 'lib/foxy/node.rb', line 32

def id!
  extra[1]
end

#tag?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/foxy/node.rb', line 12

def tag?
  [:tag, :singletag].include? type
end

#tagnameObject



20
21
22
# File 'lib/foxy/node.rb', line 20

def tagname
  (tag? || closetag?) && tagname!
end

#tagname!Object



28
29
30
# File 'lib/foxy/node.rb', line 28

def tagname!
  extra[0]
end