Sha256: 3dee8315c046d136130cdb68ffe2c777bc0c1e536b1031f88d59e0c690076616

Contents?: true

Size: 1.51 KB

Versions: 22

Compression:

Stored size: 1.51 KB

Contents

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

$: << File.join(File.dirname(__FILE__))

require 'uri'
require 'net/http'
require 'json'
require 'utils'


module Wp2txt

  def post_request(uri_string, data={})
    data = data.map{ |k, v| "#{k}=#{v}" }.join("&")
    uri = URI.parse(uri_string)
    uri.path = "/" if uri.path.empty?
    http = Net::HTTP.new(uri.host)
    return http.post(uri.path, data).body
  end

  def expand_template(uri, template, page)
    text = URI.escape(template)
    title = URI.escape(page)
    data = {"action" => "expandtemplates",
            "format" => "json",
            "text"   => text,
            "title"  => title}
    jsn = post_request(uri, data)
    hash = JSON.parse(jsn)
    begin
      result = hash["expandtemplates"]["*"]
      result = special_chr(result)
      return chrref_to_utf(result).gsub("{{", "&#123;&#123;").gsub("}}", "&#125;&#125;")
    rescue => e      
      puts "ERROR!"
      p e
      exit
      template
    end
  end

  def parse_wikitext(uri, wikitext, page)
    text = URI.escape(wikitext)
    title = URI.escape(page)
    data = {"action" => "parse",
            "format" => "json",
            "text"   => text,
            "title"  => title}
    jsn = post_request(uri, data)
    hash = JSON.parse(jsn)
    begin
      result = hash["parse"]["text"]["*"]
      result = special_chr(result)
      return chrref_to_utf(result).gsub("[[", "&#91;&#91;").gsub("]]", "&#93;&#93;")
    rescue => e      
      puts "ERROR!"
      p e
      exit
      template
    end
  end
  
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
wp2txt-0.9.5.1 lib/wp2txt/mw_api.rb
wp2txt-0.9.5 lib/wp2txt/mw_api.rb
wp2txt-0.9.4 lib/wp2txt/mw_api.rb
wp2txt-0.9.3 lib/wp2txt/mw_api.rb
wp2txt-0.9.2 lib/wp2txt/mw_api.rb
wp2txt-0.9.1 lib/wp2txt/mw_api.rb
wp2txt-0.8.0 lib/wp2txt/mw_api.rb
wp2txt-0.7.8 lib/wp2txt/mw_api.rb
wp2txt-0.7.7 lib/wp2txt/mw_api.rb
wp2txt-0.7.6 lib/wp2txt/mw_api.rb
wp2txt-0.7.5 lib/wp2txt/mw_api.rb
wp2txt-0.7.0 lib/wp2txt/mw_api.rb
wp2txt-0.6.1 lib/wp2txt/mw_api.rb
wp2txt-0.6.0 lib/wp2txt/mw_api.rb
wp2txt-0.5.4 lib/wp2txt/mw_api.rb
wp2txt-0.5.3 lib/wp2txt/mw_api.rb
wp2txt-0.5.2 lib/wp2txt/mw_api.rb
wp2txt-0.5.1 lib/wp2txt/mw_api.rb
wp2txt-0.5.02 lib/wp2txt/mw_api.rb
wp2txt-0.5.0 lib/wp2txt/mw_api.rb