Class: Zenweb::Page
- Inherits:
-
Object
- Object
- Zenweb::Page
- Defined in:
- lib/zenweb-autolink.rb
Instance Method Summary collapse
-
#autolinked_html(html) ⇒ Object
Take the html and creates links to from any text that matches a page’s title to that page.
-
#autolinked_html_fragment(text) ⇒ Object
Takes a fragment of text and creates links from any text that matches a page’s title in that text.
-
#render_autolink(page, content) ⇒ Object
Autolinks any text in the page that matches the title of another page on the site.
Instance Method Details
#autolinked_html(html) ⇒ Object
Take the html and creates links to from any text that matches a page’s title to that page. Note that it is reasonably intelligent and only makes the link if the text isn’t already linked and is inside a span, b, p or li element. FIXME: Make it possible to override the elements in which it is acceptable to autolink
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/zenweb-autolink.rb', line 48 def autolinked_html(html) nodes = Nokogiri::HTML::fragment(html) acceptable_nodes_for_autolinking = %w{p li b em i} nodes.traverse do |node| next unless node.text? next unless acceptable_nodes_for_autolinking.include?(node.parent.name) node.replace(autolinked_html_fragment(node.text)) end nodes.to_html end |
#autolinked_html_fragment(text) ⇒ Object
Takes a fragment of text and creates links from any text that matches a page’s title in that text. It matches the longest titles first. FIXME: Need a way of dealing with duplicate titles
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/zenweb-autolink.rb', line 63 def autolinked_html_fragment(text) links = [] site.pages_with_titles_as_sorted_regular_expressions.each do |regexp,page| next if page == self next if page.title.length > text.length text.gsub!(regexp) do |match| links << "<a href='#{page.url}'>#{match}</a>" "%!#{links.size-1}!%" end end text.gsub!(/%!(\d+)!%/) do |match| links[$1.to_i] end text end |
#render_autolink(page, content) ⇒ Object
Autolinks any text in the page that matches the title of another page on the site
37 38 39 |
# File 'lib/zenweb-autolink.rb', line 37 def render_autolink page, content autolinked_html content end |