Skip to content

Instantly share code, notes, and snippets.

@rupl
Forked from chriseppstein/word_count.rb
Last active December 16, 2015 16:40
Show Gist options
  • Save rupl/5464755 to your computer and use it in GitHub Desktop.
Save rupl/5464755 to your computer and use it in GitHub Desktop.
Word counter for writing articles. Allows you to count lines of code at a different rate than regular words.
#!/usr/bin/env ruby
require 'rubygems'
require 'rdiscount'
require 'nokogiri'
words = 0
limit = 2700
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
if words < limit
puts "You need to add #{limit - words} words"
else
puts "You need to cut #{words - limit} words"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment