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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/docter/template.rb', line 16

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


32
33
34
35
36
37
38
# File 'lib/docter/template.rb', line 32

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

#renumber_footnotes(html) ⇒ Object



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

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