lib/media_wiki/gateway.rb in mediawiki-gateway-0.1.3 vs lib/media_wiki/gateway.rb in mediawiki-gateway-0.1.4
- old
+ new
@@ -52,19 +52,41 @@
end
# Render a MediaWiki page as HTML
#
# [page_title] Page title to fetch
+ # [options] Hash of additional options
#
+ # Options:
+ # * [linkbase] supply a String to prefix all internal (relative) links with. '/wiki/' is assumed to be the base of a relative link
+ # * [noeditsections] strips all edit-links if set to +true+
+ # * [noimages] strips all +img+ tags from the rendered text if set to +true+
+ #
# Returns nil if the page does not exist
- def render(page_title)
+ def render(page_title, options = {})
form_data = {'action' => 'parse', 'page' => page_title}
+
+ valid_options = %w(linkbase noeditsections noimages)
+ # Check options
+ options.keys.each{|opt| raise ArgumentError.new("Unknown option '#{opt}'") unless valid_options.include?(opt.to_s)}
+
+ rendered = nil
parsed = make_api_request(form_data).elements["parse"]
if parsed.attributes["revid"] != '0'
- return parsed.elements["text"].text.gsub(/<!--(.|\s)*?-->/, '')
- else
- nil
+ rendered = parsed.elements["text"].text.gsub(/<!--(.|\s)*?-->/, '')
+ # OPTIMIZE: unifiy the keys in +options+ like symbolize_keys! but w/o
+ if options["linkbase"] or options[:linkbase]
+ linkbase = options["linkbase"] || options[:linkbase]
+ rendered = rendered.gsub(/\shref="\/wiki\/(.*)"/, ' href="' + linkbase + '/wiki/\1"')
+ end
+ if options["noeditsections"] or options[:noeditsections]
+ rendered = rendered.gsub(/<span class="editsection">\[.+\]<\/span>/, '')
+ end
+ if options["noimages"] or options[:noimages]
+ rendered = rendered.gsub(/<img.*\/>/, '')
+ end
end
+ rendered
end
# Create a new page, or overwrite an existing one
#
# [title] Page title to create or overwrite, string