mediawiki-gateway 0.1.3 → 0.1.4

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.
data/doc/created.rid CHANGED
@@ -1 +1 @@
1
- Fri, 15 Oct 2010 22:06:45 +1100
1
+ Tue, 02 Nov 2010 18:10:30 +0100
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:42:56 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Sun Sep 12 16:38:12 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 15 22:06:38 +1100 2010</td>
59
+ <td>Tue Nov 02 18:04:41 +0100 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Thu Aug 05 18:02:41 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:30:10 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:30:41 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:30:47 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:29:55 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:30:57 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Thu Sep 30 20:40:04 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:31:07 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Oct 01 18:31:14 +1000 2010</td>
59
+ <td>Fri Oct 29 19:22:19 +0200 2010</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -54,16 +54,38 @@ module MediaWiki
54
54
  # Render a MediaWiki page as HTML
55
55
  #
56
56
  # [page_title] Page title to fetch
57
+ # [options] Hash of additional options
57
58
  #
59
+ # Options:
60
+ # * [linkbase] supply a String to prefix all internal (relative) links with. '/wiki/' is assumed to be the base of a relative link
61
+ # * [noeditsections] strips all edit-links if set to +true+
62
+ # * [noimages] strips all +img+ tags from the rendered text if set to +true+
63
+ #
58
64
  # Returns nil if the page does not exist
59
- def render(page_title)
65
+ def render(page_title, options = {})
60
66
  form_data = {'action' => 'parse', 'page' => page_title}
67
+
68
+ valid_options = %w(linkbase noeditsections noimages)
69
+ # Check options
70
+ options.keys.each{|opt| raise ArgumentError.new("Unknown option '#{opt}'") unless valid_options.include?(opt.to_s)}
71
+
72
+ rendered = nil
61
73
  parsed = make_api_request(form_data).elements["parse"]
62
74
  if parsed.attributes["revid"] != '0'
63
- return parsed.elements["text"].text.gsub(/<!--(.|\s)*?-->/, '')
64
- else
65
- nil
75
+ rendered = parsed.elements["text"].text.gsub(/<!--(.|\s)*?-->/, '')
76
+ # OPTIMIZE: unifiy the keys in +options+ like symbolize_keys! but w/o
77
+ if options["linkbase"] or options[:linkbase]
78
+ linkbase = options["linkbase"] || options[:linkbase]
79
+ rendered = rendered.gsub(/\shref="\/wiki\/(.*)"/, ' href="' + linkbase + '/wiki/\1"')
80
+ end
81
+ if options["noeditsections"] or options[:noeditsections]
82
+ rendered = rendered.gsub(/<span class="editsection">\[.+\]<\/span>/, '')
83
+ end
84
+ if options["noimages"] or options[:noimages]
85
+ rendered = rendered.gsub(/<img.*\/>/, '')
86
+ end
66
87
  end
88
+ rendered
67
89
  end
68
90
 
69
91
  # Create a new page, or overwrite an existing one
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mediawiki-gateway}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jani Patokallio"]
12
- s.date = %q{2010-10-24}
12
+ s.date = %q{2010-11-04}
13
13
  s.description = %q{}
14
14
  s.email = %q{jpatokal@iki.fi}
15
15
  s.extra_rdoc_files = [
@@ -80,11 +80,14 @@ Gem::Specification.new do |s|
80
80
 
81
81
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
82
82
  s.add_runtime_dependency(%q<rest-client>, [">= 0"])
83
+ s.add_development_dependency(%q<sham_rack>, [">= 0"])
83
84
  else
84
85
  s.add_dependency(%q<rest-client>, [">= 0"])
86
+ s.add_dependency(%q<sham_rack>, [">= 0"])
85
87
  end
86
88
  else
87
89
  s.add_dependency(%q<rest-client>, [">= 0"])
90
+ s.add_dependency(%q<sham_rack>, [">= 0"])
88
91
  end
89
92
  end
90
93
 
@@ -32,6 +32,7 @@ module FakeMediaWiki
32
32
  @pages.add_namespace(100, "Book")
33
33
  @pages.add('Book:Italy', 'Introduction')
34
34
  @pages.add_namespace(200, "Sandbox")
35
+ @pages.add('Foopage', 'Content')
35
36
 
36
37
  @extensions = { 'FooExtension' => 'r1', 'BarExtension' => 'r2' }
37
38
 
@@ -144,7 +145,15 @@ module FakeMediaWiki
144
145
  page = @pages.get(params[:page])
145
146
  api_response do |_|
146
147
  _.parse({ :revid => page ? page[:pageid] : 0}) do
147
- _.text("Sample <B>HTML</B> content")
148
+ if params[:page] == "Foopage"
149
+ _.text('Sample <B>HTML</B> content.' \
150
+ '<img width="150" height="150" class="thumbimage" src="http://upload.wikimedia.org/foo/Ruby_logo.svg" alt="Ruby logo.svg"/>' \
151
+ '<span class="editsection">[<a title="Edit section: Nomenclature" href="/w/index.php?title=Seat_of_local_government&amp;action=edit&amp;section=1">edit</a>]</span>' \
152
+ '<a title="Interpreted language" href="/wiki/Interpreted_language">interpreted language</a>'
153
+ )
154
+ else
155
+ _.text('Sample <B>HTML</B> content.')
156
+ end
148
157
  end
149
158
  end
150
159
  end
data/spec/gateway_spec.rb CHANGED
@@ -1,5 +1,12 @@
1
- require 'active_support'
2
- require 'rr'
1
+ require 'active_support/version'
2
+ if ActiveSupport::VERSION::MAJOR >= 3
3
+ # :nodoc: Rails 3: #to_xml is defined in ActiveModel::Serializers::Xml
4
+ require 'active_model'
5
+ Hash.send(:include, ActiveModel::Serializers::Xml)
6
+ else
7
+ # :nodoc: Rails 2.3.x: Hash#to_xml is defined in active_support
8
+ require 'active_support'
9
+ end
3
10
  require 'sham_rack'
4
11
 
5
12
  require 'media_wiki'
@@ -206,15 +213,54 @@ describe MediaWiki::Gateway do
206
213
  describe "#render" do
207
214
 
208
215
  describe "for an existing wiki page" do
209
-
216
+
210
217
  before do
211
218
  @pages = @gateway.render('Main Page')
212
219
  end
213
220
 
214
- it "should return the page content" do
215
- expected = "Sample <B>HTML</B> content"
221
+ it "should return the page content" do
222
+ expected = 'Sample <B>HTML</B> content.'
216
223
  @pages.to_s.should == expected
217
224
  end
225
+
226
+ it "should raise an ArgumentError on illegal options" do
227
+ lambda do
228
+ @gateway.render("Main Page", :doesnotexist => :at_all)
229
+ end.should raise_error(ArgumentError)
230
+ end
231
+
232
+ describe "with option" do
233
+
234
+ it "should strip img tags" do
235
+ @pages = @gateway.render('Foopage', :noimages => true)
236
+
237
+ expected = 'Sample <B>HTML</B> content.'\
238
+ '<span class="editsection">[<a title="Edit section: Nomenclature" href="/w/index.php?title=Seat_of_local_government&amp;action=edit&amp;section=1">edit</a>]</span>'\
239
+ '<a title="Interpreted language" href="/wiki/Interpreted_language">interpreted language</a>'
240
+ @pages.to_s.should == expected
241
+ end
242
+
243
+ it "should strip edit sections" do
244
+ @pages = @gateway.render('Foopage', :noeditsections => true)
245
+
246
+ expected = 'Sample <B>HTML</B> content.' \
247
+ '<img width="150" height="150" class="thumbimage" src="http://upload.wikimedia.org/foo/Ruby_logo.svg" alt="Ruby logo.svg"/>' \
248
+ '<a title="Interpreted language" href="/wiki/Interpreted_language">interpreted language</a>'
249
+ @pages.to_s.should == expected
250
+ end
251
+
252
+ it "should make all links absolute" do
253
+ @pages = @gateway.render('Foopage', :linkbase => "http://en.wikipedia.org")
254
+
255
+ expected = 'Sample <B>HTML</B> content.' \
256
+ '<img width="150" height="150" class="thumbimage" src="http://upload.wikimedia.org/foo/Ruby_logo.svg" alt="Ruby logo.svg"/>' \
257
+ '<span class="editsection">[<a title="Edit section: Nomenclature" href="/w/index.php?title=Seat_of_local_government&amp;action=edit&amp;section=1">edit</a>]</span>'\
258
+ '<a title="Interpreted language" href="http://en.wikipedia.org/wiki/Interpreted_language">interpreted language</a>'
259
+ @pages.to_s.should == expected
260
+ end
261
+
262
+ end
263
+
218
264
  end
219
265
 
220
266
  describe "for a missing wiki page" do
@@ -246,7 +292,7 @@ describe MediaWiki::Gateway do
246
292
  it "should create the page" do
247
293
  expected = <<-XML
248
294
  <api>
249
- <edit new='' result='Success' pageid='5' title='A New Page' oldrevid='0' newrevid='5'/>
295
+ <edit new='' result='Success' pageid='6' title='A New Page' oldrevid='0' newrevid='6'/>
250
296
  </api>
251
297
  XML
252
298
  Hash.from_xml(@page.to_s).should == Hash.from_xml(expected)
@@ -269,7 +315,7 @@ describe MediaWiki::Gateway do
269
315
  it "should overwrite the existing page" do
270
316
  expected = <<-XML
271
317
  <api>
272
- <edit result='Success' pageid='5' title='Main Page' oldrevid='1' newrevid='5'/>
318
+ <edit result='Success' pageid='6' title='Main Page' oldrevid='1' newrevid='6'/>
273
319
  </api>
274
320
  XML
275
321
  Hash.from_xml(@new_page.to_s).should == Hash.from_xml(expected)
@@ -435,7 +481,7 @@ describe MediaWiki::Gateway do
435
481
  end
436
482
 
437
483
  it "should list all pages" do
438
- @list.sort.should == [ "Book:Italy", "Level/Level/Index", "Main 2", "Main Page" ]
484
+ @list.sort.should == [ "Book:Italy", "Foopage", "Level/Level/Index", "Main 2", "Main Page" ]
439
485
  end
440
486
 
441
487
  end
@@ -544,7 +590,7 @@ describe MediaWiki::Gateway do
544
590
  end
545
591
 
546
592
  it "should return an HTML string" do
547
- @response.should == "Sample <B>HTML</B> content"
593
+ @response.should == 'Sample <B>HTML</B> content.'
548
594
  end
549
595
 
550
596
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mediawiki-gateway
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jani Patokallio
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-24 00:00:00 +11:00
18
+ date: 2010-11-04 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,20 @@ dependencies:
32
32
  version: "0"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: sham_rack
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
35
49
  description: ""
36
50
  email: jpatokal@iki.fi
37
51
  executables: []