class HtmlToc::Hx

Attributes

depth[R]
end_index[R]
start_index[R]
text[R]

Public Class Methods

new(md, d) click to toggle source
# 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/i)
        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

Public Instance Methods

id() click to toggle source
# 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/i)

        if tag_id == nil 
                ""
        else
                tag_id[0].to_s.match(/(["'])(.*?)\1/)[0].to_s[1..-2]
        end
end
inner_text() click to toggle source
# 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="") click to toggle source
# 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