Class: HtmlToc::Hx

Inherits:
Object
  • Object
show all
Defined in:
lib/html_toc.rb

Constant Summary collapse

@@unique_id =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#depthObject (readonly)

Returns the value of attribute depth.



126
127
128
# File 'lib/html_toc.rb', line 126

def depth
  @depth
end

#end_indexObject (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_indexObject (readonly)

Returns the value of attribute start_index.



126
127
128
# File 'lib/html_toc.rb', line 126

def start_index
  @start_index
end

#textObject (readonly)

Returns the value of attribute text.



126
127
128
# File 'lib/html_toc.rb', line 126

def text
  @text
end

Instance Method Details

#idObject



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_textObject



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