Class: HtmlToc::Hx
- Inherits:
-
Object
- Object
- HtmlToc::Hx
- Defined in:
- lib/html_toc.rb
Constant Summary collapse
- @@unique_id =
0
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#end_index ⇒ Object
readonly
Returns the value of attribute end_index.
-
#start_index ⇒ Object
readonly
Returns the value of attribute start_index.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #id ⇒ Object
-
#initialize(md, d) ⇒ Hx
constructor
A new instance of Hx.
- #inner_text ⇒ Object
- #l_anchor(refnum = "") ⇒ Object
Constructor Details
#initialize(md, d) ⇒ Hx
Returns a new instance of Hx.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/html_toc.rb', line 130 def initialize md, d @depth = d @text = md.to_s @start_index = md.begin(0) @end_index = md.end(0) #If the tag does not have an ID, give it one tag_id = @text.match(/\bid(\s*?)=(\s*?)(["'])(.*?)\3/) if tag_id == nil @@unique_id += 1 id = " id='_id__#{@@unique_id}'" snip = @text.index('>') #get the location of the first > @text.insert(snip, id) end end |
Instance Attribute Details
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
126 127 128 |
# File 'lib/html_toc.rb', line 126 def depth @depth end |
#end_index ⇒ Object (readonly)
Returns the value of attribute end_index.
126 127 128 |
# File 'lib/html_toc.rb', line 126 def end_index @end_index end |
#start_index ⇒ Object (readonly)
Returns the value of attribute start_index.
126 127 128 |
# File 'lib/html_toc.rb', line 126 def start_index @start_index end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
126 127 128 |
# File 'lib/html_toc.rb', line 126 def text @text end |
Instance Method Details
#id ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/html_toc.rb', line 146 def id #TODO Allow for undelimited attribute values, as in HTML5 tag_id = @text.match(/\bid(\s*?)=(\s*?)(["'])(.*?)\3/) if tag_id == nil "" else tag_id[0].to_s.match(/(["'])(.*?)\1/)[0].to_s[1..-2] end end |
#inner_text ⇒ Object
157 158 159 160 161 |
# File 'lib/html_toc.rb', line 157 def inner_text snip_start = @text.to_s.index('>') #Get the index of the first > snip_end = @text.to_s.rindex('<') #Get the index of the last < @text.to_s[(snip_start+1)..(snip_end-1)] #return everyting in between end |
#l_anchor(refnum = "") ⇒ Object
163 164 165 |
# File 'lib/html_toc.rb', line 163 def l_anchor refnum="" "<div class='__toc_level_#{@depth}'><a href='##{id}'>#{refnum}#{inner_text}</a></div>\n" end |