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
#collect_links(content, mark = false) ⇒ Object
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
|
#list_links(cls = nil) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/docter/template.rb', line 53
def list_links(cls = nil)
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
|
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/docter/template.rb', line 62
def (html)
@footnote ||= 0
html.gsub(/<a href=['"]#fn(\d+)['"]>\1<\/a>/) {
fn = $1.to_i + @footnote
%{<a href='#fn#{fn}'>#{fn}</a>}
}.gsub(/<p id=['"]fn(\d+)['"](.*?)><sup>\1<\/sup>(.*?)<\/p>/m) {
@footnote += 1
%{<p id='fn#{@footnote}'#{$2}><sup>#{@footnote}</sup>#{$3}</p>}
}
end
|