html-pipeline-wiki-link 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.2
4
+
5
+ * Added support for supplying a base URL.
6
+ * Added support for replacing and collapsing spaces in wiki links.
7
+ * Added support for replacing spaces with a configurable string.
8
+
3
9
  ## 0.0.1 - *Initial Version*
4
10
 
5
11
  * Built support for basic wiki links and wiki links with descriptions.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- html-pipeline-wiki-link (0.0.1)
4
+ html-pipeline-wiki-link (0.0.2)
5
5
  html-pipeline (>= 0.0.8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,4 @@
1
- [![Build Status](https://travis-ci.org/lifted-studios/html-pipeline-cite.png?branch=master)](https://travis-ci.org/lifted-studios/html-pipeline-cite)
2
-
3
- # HTML::Pipeline WikiLink Gem
1
+ # HTML::Pipeline WikiLink Gem [![Build Status](https://travis-ci.org/lifted-studios/html-pipeline-wiki-link.png?branch=master)](https://travis-ci.org/lifted-studios/html-pipeline-wiki-link)
4
2
 
5
3
  An HTML::Pipeline filter for handling WikiMedia-style wiki links.
6
4
 
@@ -26,7 +24,7 @@ $ gem install html-pipeline-wiki-link
26
24
 
27
25
  ## Usage
28
26
 
29
- This library is designed as an extension of the [HTML::Pipeline]() system for creating pipelines of text conversions. It can work on its own or in conjunction with other filters descended from `HTML::Pipeline::Filter`.
27
+ This library is designed as an extension of the [HTML::Pipeline](https://github.com/jch/html-pipeline) system for creating pipelines of text conversions. It can work on its own or in conjunction with other filters descended from `HTML::Pipeline::Filter`.
30
28
 
31
29
  To use this filter on its own:
32
30
 
@@ -50,17 +48,21 @@ This is some **Markdown** with a [[Link]] in it!
50
48
  CODE
51
49
  ```
52
50
 
51
+ <!--
53
52
  ## Troubleshooting
53
+ -->
54
54
 
55
55
  ## Development
56
56
 
57
- To see what has changed in recent versions of Lifted Wiki, see the [CHANGELOG](https://github.com/lifted-studios/html-pipeline-wiki-link/blob/master/CHANGELOG.md).
57
+ To see what has changed in recent versions of Lifted Wiki, see the [CHANGELOG](CHANGELOG.md).
58
58
 
59
59
  ## Core Team Members
60
60
 
61
61
  * [Lee Dohm](https://github.com/lee-dohm/)
62
62
 
63
+ <!--
63
64
  ## Resources
65
+ -->
64
66
 
65
67
  <!-- ### Other questions
66
68
 
@@ -69,6 +71,6 @@ Feel free to chat with the Lifted Wiki core team (and many other users) on IRC i
69
71
 
70
72
  ## Copyright
71
73
 
72
- Copyright © 2013 Lee Dohm, Lifted Studios. See [LICENSE](https://github.com/lifted-studios/html-pipeline-wiki-link/blob/master/LICENSE.md) for details.
74
+ Copyright © 2013 Lee Dohm, Lifted Studios. See [LICENSE](LICENSE.md) for details.
73
75
 
74
76
  Project is a member of the [OSS Manifesto](http://ossmanifesto.com/).
@@ -6,7 +6,7 @@ module HTML
6
6
  class Pipeline
7
7
  module WikiLink
8
8
  # Version number for the Lifted Wiki gem.
9
- VERSION = '0.0.1'
9
+ VERSION = '0.0.2'
10
10
  end
11
11
  end
12
12
  end
@@ -8,12 +8,54 @@ module HTML
8
8
  class Pipeline
9
9
  # An `HTML::Pipeline` filter class that detects wiki-style links and converts them to HTML links.
10
10
  class WikiLinkFilter < Filter
11
+ # Initializes a new instance of the `WikiLinkFilter` class.
12
+ #
13
+ # @param doc Document to filter.
14
+ # @param context Parameters for the filter.
15
+ # @param result Results extracted from the filter.
16
+ def initialize(doc, context = nil, result = nil)
17
+ super(doc, context, result)
18
+
19
+ @base_url = '/'
20
+ @space_replacement = '_'
21
+
22
+ if context
23
+ @base_url = context[:base_url] if context[:base_url]
24
+ @space_replacement = context[:space_replacement] if context[:space_replacement]
25
+ end
26
+ end
27
+
11
28
  # Performs the translation and returns the updated text.
12
29
  #
13
30
  # @return [String] Updated text with translated wiki links.
14
31
  def call
15
- text = html.gsub(/\[\[(.*)\|(.*)\]\]/, '<a href="/\1">\2</a>')
16
- text.gsub(/\[\[(.*)\]\]/, '<a href="/\1">\1</a>')
32
+ html.gsub(/\[\[([^|]*)(\|(.*))?\]\]/) do
33
+ link = $1
34
+ desc = $3 ? $3 : $1
35
+
36
+ link = convert_whitespace(link)
37
+ desc = collapse_whitespace(desc)
38
+
39
+ "<a href=\"#{@base_url}#{link}\">#{desc}</a>"
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ # Collapses multiple whitespace characters into a single space.
46
+ #
47
+ # @param text Text within which to collapse whitespace.
48
+ # @return Text with collapsed whitespace.
49
+ def collapse_whitespace(text)
50
+ text.gsub(/\s+/, ' ')
51
+ end
52
+
53
+ # Converts spaces to underscores in the given text.
54
+ #
55
+ # @param text Text within which to replace spaces.
56
+ # @return Text with spaces replaced with underscores.
57
+ def convert_whitespace(text)
58
+ text.gsub(/\s+/, @space_replacement)
17
59
  end
18
60
  end
19
61
  end
@@ -14,9 +14,10 @@ describe HTML::Pipeline::WikiLinkFilter do
14
14
  # Creates a new `WikiLinkFilter` object.
15
15
  #
16
16
  # @param text Text for the filter to parse.
17
+ # @param context Context object to pass to the constructor.
17
18
  # @return [HTML::Pipeline::WikiLinkFilter] Newly created filter object.
18
- def new_filter(text)
19
- HTML::Pipeline::WikiLinkFilter.new(text)
19
+ def new_filter(text, context = {})
20
+ HTML::Pipeline::WikiLinkFilter.new(text, context)
20
21
  end
21
22
 
22
23
  it 'can be instantiated' do
@@ -40,4 +41,44 @@ describe HTML::Pipeline::WikiLinkFilter do
40
41
 
41
42
  text.must_equal '<a href="/Link">Description</a>'
42
43
  end
44
+
45
+ it 'converts a wiki link with spaces to use underscores in the href' do
46
+ filter = new_filter('[[A Link With Spaces]]')
47
+
48
+ text = filter.call
49
+
50
+ text.must_equal '<a href="/A_Link_With_Spaces">A Link With Spaces</a>'
51
+ end
52
+
53
+ it 'converts a wiki link with spaces in the link to use underscores in the href' do
54
+ filter = new_filter('[[A Link With Spaces|A Description With Spaces]]')
55
+
56
+ text = filter.call
57
+
58
+ text.must_equal '<a href="/A_Link_With_Spaces">A Description With Spaces</a>'
59
+ end
60
+
61
+ it 'converts a link with multiple spaces into one underscore and coalesces them in the description' do
62
+ filter = new_filter('[[Many Spaces]]')
63
+
64
+ text = filter.call
65
+
66
+ text.must_equal '<a href="/Many_Spaces">Many Spaces</a>'
67
+ end
68
+
69
+ it 'coalesces spaces in both link and description' do
70
+ filter = new_filter('[[Many Spaces|Many Spaces]]')
71
+
72
+ text = filter.call
73
+
74
+ text.must_equal '<a href="/Many_Spaces">Many Spaces</a>'
75
+ end
76
+
77
+ it 'replaces spaces with other characters when given a :space_replacement parameter' do
78
+ filter = new_filter('[[A Link With Spaces]]', :space_replacement => '*')
79
+
80
+ text = filter.call
81
+
82
+ text.must_equal '<a href="/A*Link*With*Spaces">A Link With Spaces</a>'
83
+ end
43
84
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-pipeline-wiki-link
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-01 00:00:00.000000000 Z
12
+ date: 2013-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: html-pipeline