zenweb-autolink 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,5 @@
1
+ === 0.0.1 / 2012-06-28
2
+
3
+ * 1 major enhancement:
4
+
5
+ * First working version of autolinking pages based on their titles
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/zenweb-autolink.rb
6
+ test/test_zenweb_autolink.rb
7
+ test_site/Rakefile
8
+ test_site/_config.yml
9
+ test_site/_layouts/site.erb
10
+ test_site/page2.html.md
11
+ test_site/page3.html.autolink.md
12
+ test_site/zenweb.html.md
@@ -0,0 +1,72 @@
1
+ = zenweb-autolink
2
+
3
+ * Source :: http://github.com/tamc/zenweb-autolink
4
+
5
+ == DESCRIPTION:
6
+
7
+ A plugin for zenweb that automatically generates links between pages.
8
+
9
+ For example: If a page has a title attribute 'my page about zebweb' then
10
+ any other page that contains text like 'see my page about zenweb' would
11
+ have some of the text replaced with a link to that page:
12
+ 'see <a href='/zenweb.html'>my page about zenweb</a>'
13
+
14
+ == FEATURES/PROBLEMS:
15
+
16
+ * Only replaces text with links if the text is part of a paragraph or
17
+ list (i.e., won't create links in titles or inside other links)
18
+ * Works on any html
19
+ * But, doesn't allow you to specify when links do or don't occur
20
+
21
+ == SYNOPSIS:
22
+
23
+ See test_site directory for an example of its usage.
24
+
25
+
26
+ 1 Add `require 'zenweb-autolink'`
27
+ 2 Make sure some of your pages have title attributes (i.e., they have a
28
+ _config.yml file or config block that has title: tile at the top)
29
+ 3 Add the extension .autolink to pages that you want the autolinker to operate on
30
+ (it is best to put the extension immediately after the '.html' extension)
31
+
32
+ == REQUIREMENTS:
33
+
34
+ * Zenweb 3.0.0.b3 or later
35
+
36
+ == INSTALL:
37
+
38
+ sudo gem install zenweb-autolink
39
+
40
+ == DEVELOPERS:
41
+
42
+ After checking out the source, run:
43
+
44
+ $ rake newb
45
+
46
+ This task will install any missing dependencies, run the tests/specs,
47
+ and generate the RDoc.
48
+
49
+ == LICENSE:
50
+
51
+ (The MIT License)
52
+
53
+ Copyright (c) 2012 Tom Counsell
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining
56
+ a copy of this software and associated documentation files (the
57
+ 'Software'), to deal in the Software without restriction, including
58
+ without limitation the rights to use, copy, modify, merge, publish,
59
+ distribute, sublicense, and/or sell copies of the Software, and to
60
+ permit persons to whom the Software is furnished to do so, subject to
61
+ the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be
64
+ included in all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
67
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
69
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
70
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
71
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
72
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugin :minitest
7
+ Hoe.plugin :git
8
+
9
+ Hoe.spec 'zenweb-autolink' do
10
+ developer 'tamc', 'tamc@greenonblack.com'
11
+ self.extra_deps << ['zenweb','3.0.0.b3']
12
+ self.extra_deps << ['nokogiri', '~> 1.5.2']
13
+ end
14
+
15
+ # vim: syntax=ruby
@@ -0,0 +1,79 @@
1
+ require 'zenweb'
2
+ require 'nokogiri'
3
+
4
+ class ZenwebAutolink
5
+ VERSION = '0.0.1'
6
+ end
7
+
8
+ module Zenweb
9
+ class Site
10
+
11
+ # Returns all pages in the site that have a title
12
+ # configured.
13
+ def pages_with_titles
14
+ html_pages.select {|page| page.config["title"] }
15
+ end
16
+
17
+ # Returns all the pages in the site that have a title
18
+ # as an array of arrays, where each array's first
19
+ # element is a regular expression matching the page's
20
+ # title and the second element is that page itself.
21
+ # these are sorted so that the longest title is first
22
+ # e.g., [[/\blong title/i,page1],[/\btitle\b/i,page2]]
23
+ # FIXME: In the end may want to memoize this
24
+ def pages_with_titles_as_sorted_regular_expressions
25
+ pages_with_titles.sort_by do |page|
26
+ page.title.size
27
+ end.reverse.map do |page|
28
+ [/\b#{Regexp.escape(page.title)}\b/i, page]
29
+ end
30
+ end
31
+ end
32
+
33
+ class Page
34
+
35
+ # Autolinks any text in the page that matches the
36
+ # title of another page on the site
37
+ def render_autolink page, content
38
+ autolinked_html content
39
+ end
40
+
41
+ # Take the html and creates links to from any text
42
+ # that matches a page's title to that page. Note that
43
+ # it is reasonably intelligent and only makes the
44
+ # link if the text isn't already linked and is inside
45
+ # a span, b, p or li element.
46
+ # FIXME: Make it possible to override the elements
47
+ # in which it is acceptable to autolink
48
+ def autolinked_html(html)
49
+ nodes = Nokogiri::HTML::fragment(html)
50
+ acceptable_nodes_for_autolinking = %w{p li b em i}
51
+ nodes.traverse do |node|
52
+ next unless node.text?
53
+ next unless acceptable_nodes_for_autolinking.include?(node.parent.name)
54
+ node.replace(autolinked_html_fragment(node.text))
55
+ end
56
+ nodes.to_html
57
+ end
58
+
59
+ # Takes a fragment of text and creates links from any
60
+ # text that matches a page's title in that text. It
61
+ # matches the longest titles first.
62
+ # FIXME: Need a way of dealing with duplicate titles
63
+ def autolinked_html_fragment(text)
64
+ links = []
65
+ site.pages_with_titles_as_sorted_regular_expressions.each do |regexp,page|
66
+ next if page == self
67
+ next if page.title.length > text.length
68
+ text.gsub!(regexp) do |match|
69
+ links << "<a href='#{page.url}'>#{match}</a>"
70
+ "%!#{links.size-1}!%"
71
+ end
72
+ end
73
+ text.gsub!(/%!(\d+)!%/) do |match|
74
+ links[$1.to_i]
75
+ end
76
+ text
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,50 @@
1
+ require "test/unit"
2
+ require "zenweb-autolink"
3
+
4
+ class TestZenwebAutolink < Test::Unit::TestCase
5
+
6
+ attr_accessor :site, :page
7
+
8
+ def setup
9
+ @old_working_dir = Dir.pwd
10
+ Dir.chdir(File.join(File.dirname(__FILE__),'..','test_site'))
11
+ self.site = Zenweb::Site.new
12
+ site.scan
13
+ site.wire
14
+ self.page = site.html_pages.find { |p| p.title == 'Example Page 3' }
15
+ end
16
+
17
+ def teardown
18
+ Dir.chdir(@old_working_dir)
19
+ end
20
+
21
+ def test_pages_with_titles
22
+ assert_equal 3, site.pages_with_titles.size
23
+ end
24
+
25
+ def test_pages_with_titles_as_sorted_regular_expressions
26
+ regular_expresions = site.pages_with_titles_as_sorted_regular_expressions
27
+ assert_equal '/\bExample\\ Page\\ 2\b/i', regular_expresions.first.first.inspect
28
+ assert_kind_of Zenweb::Page, regular_expresions.first.last
29
+ assert_equal '/\bZenweb\b/i', regular_expresions.last.first.inspect
30
+ end
31
+
32
+ def test_autolinked_html_fragment
33
+ actual = page.autolinked_html_fragment("zenweb")
34
+ expected = "<a href='/zenweb.html'>zenweb</a>"
35
+ assert_equal expected, actual
36
+ end
37
+
38
+ def test_autolinked_html
39
+ actual = page.autolinked_html("<h1>zenweb</h1><p>zenweb</p>")
40
+ expected = '<h1>zenweb</h1><p><a href="/zenweb.html">zenweb</a></p>'
41
+ assert_equal expected, actual
42
+ end
43
+
44
+ def test_render_autolink
45
+ actual = page.render
46
+ expected = %r|<p>Not really much here to see except a link to <a href="/page2\.html">example page 2</a>\.</p>|
47
+ assert actual =~ expected
48
+ end
49
+
50
+ end
@@ -0,0 +1,3 @@
1
+ require 'zenweb'
2
+ require_relative '../lib/zenweb-autolink'
3
+ Rake.application.rake_require "zenweb/tasks"
@@ -0,0 +1 @@
1
+ layout: site
@@ -0,0 +1,7 @@
1
+ ---
2
+ layout: nil
3
+ ...
4
+ <html>
5
+ <head><title><%= page.title || "No title" %> </title></head>
6
+ <body><%= content %></body>
7
+ </html>
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: Example Page 2
3
+ ...
4
+
5
+ This is my Résumé.
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: Example Page 3
3
+ ...
4
+
5
+ Not really much here to see except a link to example page 2.
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: Zenweb
3
+ ...
4
+
5
+ Not really much here to see.
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zenweb-autolink
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tamc
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: zenweb
16
+ requirement: &70219613929400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0.b3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70219613929400
25
+ - !ruby/object:Gem::Dependency
26
+ name: nokogiri
27
+ requirement: &70219613928960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70219613928960
36
+ - !ruby/object:Gem::Dependency
37
+ name: minitest
38
+ requirement: &70219613928520 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70219613928520
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: &70219613928080 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.10'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70219613928080
58
+ - !ruby/object:Gem::Dependency
59
+ name: hoe
60
+ requirement: &70219613927640 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70219613927640
69
+ description: ! "A plugin for zenweb that automatically generates links between pages.\n\nFor
70
+ example: If a page has a title attribute 'my page about zebweb' then\nany other
71
+ page that contains text like 'see my page about zenweb' would\nhave some of the
72
+ text replaced with a link to that page: \n'see <a href='/zenweb.html'>my page about
73
+ zenweb</a>'"
74
+ email:
75
+ - tamc@greenonblack.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files:
79
+ - History.txt
80
+ - Manifest.txt
81
+ - README.txt
82
+ files:
83
+ - History.txt
84
+ - Manifest.txt
85
+ - README.txt
86
+ - Rakefile
87
+ - lib/zenweb-autolink.rb
88
+ - test/test_zenweb_autolink.rb
89
+ - test_site/Rakefile
90
+ - test_site/_config.yml
91
+ - test_site/_layouts/site.erb
92
+ - test_site/page2.html.md
93
+ - test_site/page3.html.autolink.md
94
+ - test_site/zenweb.html.md
95
+ - .gemtest
96
+ homepage: http://github.com/tamc/zenweb-autolink
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options:
100
+ - --main
101
+ - README.txt
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project: zenweb-autolink
118
+ rubygems_version: 1.8.10
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: A plugin for zenweb that automatically generates links between pages
122
+ test_files:
123
+ - test/test_zenweb_autolink.rb