Skip to content

Instantly share code, notes, and snippets.

@chriseppstein
Created October 16, 2010 15:57
Show Gist options
  • Save chriseppstein/629946 to your computer and use it in GitHub Desktop.
Save chriseppstein/629946 to your computer and use it in GitHub Desktop.
counts words for an article I'm writing
#!/usr/bin/env ruby
require 'rdiscount'
require 'nokogiri'
words = 0
content = $stdin.read
html = RDiscount.new(content).to_html
doc = Nokogiri::HTML(html)
codes = doc.css("pre code")
codes.each do |code|
words += code.inner_html.split("\n").size * 15
code.parent.remove
end
words += doc.inner_text.split(/\s+/).size
puts words
puts "You need to cut #{words - 2700} words"
@SachaG
Copy link

SachaG commented Mar 6, 2013

Thanks for the gist! Just a small note: only works for me if I use content = ARGF.read instead of content = $stdin.read

@rupl
Copy link

rupl commented Apr 26, 2013

Yes thanks a ton for this! I got a minor error when I ran it too.

wc.rb:2:in require': no such file to load -- rdiscount (LoadError)`

I'm not a ruby dev so maybe it's obvious but I had to add the following line above the other required gems and now it works perfect:

require 'rubygems'

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