Module: Docter::Template::ContextMethods

Includes:
HTML
Defined in:
lib/docter/template.rb

Instance Method Summary collapse

Methods included from HTML

inner_text_from, regexp_attribute, regexp_element

Instance Method Details



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/docter/template.rb', line 36

def collect_links(content, mark = false)
  @links ||= []
  content.gsub(regexp_element('a')) do |link|
    url = $3 if link =~ regexp_attribute('href')
    text = inner_text_from(link)
    if url =~ /^\w+:/ && url != text
      unless index = @links.index(url)
        index = @links.size
        @links << [url, text]
      end
      mark ? "#{link}<sup>[#{index + 1}]</sup>" : link
    else
      link
    end
  end
end


53
54
55
56
57
58
59
60
# File 'lib/docter/template.rb', line 53

def list_links(cls = nil)
  # Remove duplicate links (same URL), sort by text and convert into DT/DD pairs.
  links = @links.select { |url, text| url =~ /^http(s?):/ }.
    inject({}) { |hash, link| hash[link.first] ||= link.last ; hash }.
    sort { |a,b| a.last.downcase <=> b.last.downcase }.
    map { |url, text| %{<dt>#{text.gsub(/^\w/) { |a| a.upcase }}</dt><dd><a href='#{url}'>#{url}</a></dd>} }
  %{<dl class='#{cls}'>#{links.join}</dl>}
end

#renumber_footnotes(html) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/docter/template.rb', line 62

def renumber_footnotes(html)
  @footnote ||= 0
  html.gsub(/<a href=['"]#fn(\d+)['"]>\1<\/a>/) {
    # Renumber footnote references starting from the last footnote number.
    fn = $1.to_i + @footnote
     %{<a href='#fn#{fn}'>#{fn}</a>}
  }.gsub(/<p id=['"]fn(\d+)['"](.*?)><sup>\1<\/sup>(.*?)<\/p>/m) {
    # Renumber footnotes the same way, and update the last footnote number.
    @footnote += 1
    %{<p id='fn#{@footnote}'#{$2}><sup>#{@footnote}</sup>#{$3}</p>}
  }
end