Hoof
: A basic pastebin type thing
Snip
ruby script to convert ABC tunes from thesession.org to lilypond files
#!/usr/bin/env ruby require 'optparse' require 'open-uri' require 'rexml/document' require 'iconv' def sanitize( string ) #replace non alpha-numerics with _ string = string.gsub(/[^A-z0-9_]/,'_') return string.downcase end def error( string ) puts "**** ERROR ****" puts string exit end option = {} OptionParser.new do |opts| opts.banner = "Usage: sessionconvert [options] [thesession.org_URL]" opts.on("-b", "--banjo", "add banjo tabulature") do |v| option[:banjo] = v end opts.on("-l", "--lilypond", "compile with lilypond") do |v| option[:lilypond] = v end end.parse! url = ARGV[0] # was a url supplied? unless url error "you must supply a thesession.org url" end begin text = open(url).read rescue error "Failed to read #{url}" end begin #watch out for sneaky ic = Iconv.new('UTF-8//IGNORE', 'UTF-8') valid_string = ic.iconv(text) doc = REXML::Document.new valid_string rescue error "Failed to parse #{url}" end root = doc.root p = root.elements["body/div[@id='abc']/div[@class='box']/p"].to_s abc = p.gsub(/<.*>/,'') #get the title m = /T: (?
.*)/.match( abc ) title = m['title'] #get the key m = /K: (?
.*)/.match( abc ) key = m['key'] #sanitize the title, we will use this a bunch sanitized_title = sanitize(title) #what will the filenames be? filename_abc = sanitized_title+".abc" f = open(filename_abc,'w') f.write(abc) f.close begin `abc2ly #{filename_abc}` rescue error "abc2ly is not installed" end #what will the lilypond file be? filename_ly = sanitized_title+".ly" if option[:banjo] #what is the key we are transposing from t_key = key[0].downcase banjer_bit = "\\transpose #{t_key} g, \\context TabStaff = \"banjer\"{ \\set TabStaff.stringTunings = #banjo-open-g-tuning \\set Staff.instrumentName = #\"Banjo\" \\context TabVoice = \"banjer\" { \\tabFullNotation \\stemDown \\voicedefault } }" text = File::read(filename_ly) #substitute the banjer stuff text = text.gsub(/<<.*>>/m, "<<\n #{banjer_bit}\n\t>>") #make a new lilypond file filename_ly = sanitized_title+"-banjo.ly" #write the new text to file f = open(filename_ly,'w') f.write(text) f.close end #did the user request compiling begin `lilypond #{filename_ly}` if option[:lilypond] rescue error "lilypond is not installed" end