Created
February 15, 2012 22:11
-
-
Save mysteriouspants/1839371 to your computer and use it in GitHub Desktop.
Some weird random stuffs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
# a rakefile for working with Markdown-based prose. | |
# | |
# quotes: Change dumb quotes to smart quotes, and then back again. | |
# info: Various metrics for vanity's sake. "How many words and | |
# characters are in each file, & total" and other such nonsense. | |
# (Note that this chops off the very last file, which I generally | |
# use as the 'stuff I'm about to write' scratchpad - I don't want | |
# metrics on that! | |
# | |
# This file usually mangles the stuff I generate in Daedalus Writer | |
# for iPad. I like the pretty quotes in Daedalus, but before pulling | |
# into other tools the pretty quotes need to get dumb (so that the | |
# other tools can work with them). | |
# edits per https://github.com/rubyworks/ansi/pull/8 | |
# maybe someday it'll be pulled in mainline, but for now | |
# I need to use a custom hacked-up verison of ANSI | |
require '~/Code/ansi/lib/ansi/code' | |
require '~/Code/ansi/lib/ansi/table' | |
# from http://pleac.sourceforge.net/pleac_ruby/numbers.html | |
def commify(n) | |
n.to_s =~ /([^\.]*)(\..*)?/ | |
int, dec = $1.reverse, $2 ? $2 : "" | |
while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3') | |
end | |
int.reverse + dec | |
end | |
FILES=FileList['*.md'] | |
namespace :quotes do | |
desc 'Replace all normal quotes with fancy open/close quotes' | |
task :pretty do | |
FILES.each do |markdown| | |
fcontents = File.read(markdown) | |
fcontents.gsub!(/(^| )\"/, '"' => "“", " \"" => " “") # beginning of double quote | |
fcontents.gsub!(/\"($| )/, '"' => "”", '" ' => "” ") # end of double quote | |
File.open(markdown, 'w') { |f| f.write(fcontents) } | |
end | |
end | |
desc 'Replace all fancy quotes with normal open/close quotes' | |
task :ugly do | |
FILES.each do |markdown| | |
fcontents = File.read(markdown) | |
fcontents.gsub!(/[“”]/, '"') | |
File.open(markdown, 'w') do |f| | |
f.write(fcontents) | |
end | |
end | |
end | |
end | |
namespace :info do | |
desc 'Calculate word counts and character counts' | |
task :wc do | |
FILES.sort_by! { |elem| elem.scan(/\w+/).first.to_i } | |
output = Array.new | |
output << [ 'File', 'Characters', 'Words'] | |
total_chars = 0 | |
total_words = 0 | |
FILES[0..-2].each do |markdown| | |
fcontents = File.read(markdown) | |
characters = fcontents.length | |
words = fcontents.split.length | |
output << [ markdown.ext, commify(characters), commify(words)] | |
total_chars = total_chars + characters | |
total_words = total_words + words | |
end | |
output << [ 'Total', commify(total_chars), commify(total_words)] | |
table=ANSI::Table.new(output, :align=>[:left, {(1..-1)=>:right}, {(1..-1)=>:right}]) do |cell,row,col| | |
if row == 0 or (row == output.length-1 and col == 0) | |
:bold | |
elsif col > 0 | |
:right | |
end | |
end | |
puts table | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment