Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Refactor processing, make script standalone with initial env, add command-line argument
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
language = 'en'
article = ARGV[0] ||
print 'What do you need to know? : '
URI::encode gets.chomp
request_url = "http://#{language}{article}&format=json&prop=text&section=0"
open(request_url) do |file|
puts JSON.parse(['parse']['text'].first[1]
.gsub(/<\/?[^>]+>/, '') # strip tags
.gsub(/[[:space:]]+/, ' ') # strip whitespace
.gsub(/&#[0-9]+;/,'') # strip encoded
.gsub(/\[[0-9]+\]/,'') # strip referencing
Copy link

torgeir commented Feb 25, 2014

You vim'er ought to parse it to markdown and

ask-wikipedia github | vim -c "set ft=markdown" -

Copy link

mahemoff commented Feb 25, 2014

I would only want this for the command-line, not to open in Vim. Also Wiki syntax isn't really MD. It could be interesting if you color-formatted it for command-line output, not sure how though.

Copy link

schmerg commented Feb 25, 2014

Strip tags with regexp?? The horror ("the center cannot hold" etc :)

If you did it with Node you could pull in Caja and detag it properly and safely... just saying...

Copy link

mahemoff commented Feb 25, 2014

@schmerg The original arose in the Ruby G+ community, but Node ports welcome. I agree DOM munging would work nicely and could be achieved in a Ruby context with Nokogiri.

Copy link

tawrahim commented Feb 26, 2014

@mahemoff -- how about port as a ruby gem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment