Skip to content

Instantly share code, notes, and snippets.

@l3thal
l3thal / flatten
Last active September 17, 2015 18:18
flatten an array of arbitrarily nested integers
def flatten(list, result=[])
list.each do |item|
if item.is_a? Integer
result << item
else
result += flatten(item)
end
end
res
end

Keybase proof

I hereby claim:

  • I am l3thal on github.
  • I am l3thal (https://keybase.io/l3thal) on keybase.
  • I have a public key whose fingerprint is 5E04 F2F0 E0A1 E915 25DB 6052 BEDD DFF2 5540 86B5

To claim this, I am signing this object:

#!/bin/bash
# rename doc files with spaces
find . -depth -name '*.doc' | while IFS= read -r f ; do mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr ' ' _)" 2>/dev/null; done
# convert doc files to docx
for i in `find ./ -name "*.doc" 2>/dev/null`
do
dir=$(dirname ${i})"/";
libreoffice --headless --convert-to docx:"MS Word 2007 XML" --outdir $dir $i;
done
@l3thal
l3thal / gist:6836116
Created October 5, 2013 02:56
docx unique word count
#!/usr/bin/ruby
require 'zip'
require 'nokogiri'
class Docx
def self.word_count(file, zip=Zip::ZipFile.open(file))
Nokogiri::XML.parse(zip.find_entry("word/document.xml").get_input_stream).text.split(" ").uniq.length
end
end
@l3thal
l3thal / gist:6835672
Last active December 24, 2015 17:29
word count from pptx
#!/usr/bin/ruby
require 'zip'
require 'nokogiri'
#Zip::ZipFile.open("sample.pptx").each{ |entry| puts Nokogiri::XML.parse(zip.find_entry(entry.to_s).get_input_stream).text.split(' ').uniq.length if entry.to_s.match(/ppt\/slides\/slide[0-9]+\.xml$/) }
class Pptx
def self.word_count(file, zip=Zip::ZipFile.open(file), count=nil)