From f3039744d72454e7e3b8cb1032266bcc172ce488 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 12 Sep 2021 14:17:19 +0200 Subject: Initial commit --- lib/song.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/song.rb (limited to 'lib/song.rb') diff --git a/lib/song.rb b/lib/song.rb new file mode 100644 index 0000000..4076956 --- /dev/null +++ b/lib/song.rb @@ -0,0 +1,47 @@ +require 'yaml' + +require './lib/recording' + +class Song + attr_accessor :title, :id, :lyrics, :recordings + def initialize title, id + @title = title + @id = id + @recordings = [] + end + + def self.from_yaml song_id + metadata = YAML.load(File.read(src(song_meta song_id))) + song = Song.new metadata['title'], song_id + + song.lyrics = nil + lyrics_file = src (song_lyrics song_id) + if File.file? lyrics_file + song.lyrics = File.read lyrics_file + end + + if metadata['recordings'] + metadata['recordings'].each do |recording| + if recording['type'] == 'youtube' + song.recordings.push YouTubeRecording.new(recording['link']) + elsif recording['type'] == 'bandcamp' + song.recordings.push BandcampRecording.new(recording['link']) + end + end + end + song + end + + def split_lyrics + @lyrics.split("\n\n").map { |paragraph| paragraph.split "\n" } + end + + def render_sheet_music + if File.file? (src (song_ly id)) + template = ERB.new(File.read('templates/sheet_music.html.erb'), trim_mode: '-') + template.result binding + else + "" + end + end +end -- cgit v1.2.3