Skip to content

Instantly share code, notes, and snippets.

@NuckChorris
Created January 4, 2013 04:55
Show Gist options
  • Select an option

  • Save NuckChorris/4450029 to your computer and use it in GitHub Desktop.

Select an option

Save NuckChorris/4450029 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'wikicloth'
class Wikebooks < WikiCloth::Parser
def initialize (opts)
@root = URI.parse opts[:wiki]
@url = @root.clone
@url.query = "&action=raw&title=#{URI.encode opts[:article]}"
@imgurl = @root.clone
@imgurl.query = 'title=Special:FilePath'
@conn = Net::HTTP.new @url.host, @url.port
req = Net::HTTP::Get.new @url.request_uri
res = @conn.request req
opts[:data] = res.body
super(opts)
end
def to_html (opts = {})
if opts[:noedit] != false
opts[:noedit] = true
end
super(opts)
end
external_link do |url, text|
"<a href=\"#{url}\">#{text || url}</a>"
end
link_for_resource do |thing, page|
url = @imgurl.clone
url.query << "&file=#{URI.escape page}"
req = Net::HTTP::Get.new url.request_uri
res = @conn.request req
puts "RESOURCE [#{res.code} #{res.message}] #{page}"
puts " ==> #{res['location']}"
"<img src=\"img/#{page}\" class=\"\" />"
end
link_for do |page, text|
text.nil? ? page : text
end
end
Wikebooks.new(
:wiki => "http://www.baka-tsuki.org/project/index.php",
:article => "Sword Art Online:Volume 1 Chapter 3"
).to_html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment