#-- # ============================================================================= # Copyright (c) 2006 Pau Garcia i Quiles (pgquiles@elpauer.org) # All rights reserved. # # This library may be used only as allowed by either the Ruby license (or, by # association with the Ruby license, the GPL). See the "doc" subdirectory of # the XSPF distribution for the texts of these licenses. # ----------------------------------------------------------------------------- # XSPF for Ruby website : http://www.elpauer.org/xspf # ============================================================================= #++ require 'rexml/document' require 'xml/xslt' # :main: XSPF module MetaGen #:nodoc: # define the method def self.add_method(klass, meth_name, body, meth_rdoc) code = "# #{meth_rdoc}\n def #{meth_name.downcase}; begin; #{body}; rescue NoMethodError; return nil; end; end" klass.module_eval(code) # hook to write klass + name attrib to a file if $META_RDOC open($META_RDOC, 'a+') do |f| f.puts("class #{klass}\n #{code}\n end") end end end # output in different formats def self.add_output_format(klass, format, xspf, meth_rdoc) xslt_path = "'#{File.join( File.dirname(__FILE__), %Q{xspf2#{format}.xsl} )}'" code = "# #{meth_rdoc}\n def to_#{format}; xslt = XML::XSLT.new; xslt.xml = xspf; xslt.xsl = REXML::Document.new( File.new( #{xslt_path} ) ); xslt.serve; end" klass.module_eval(code) if $META_RDOC open($META_RDOC, 'a+') do |f| f.puts("class #{klass}\n #{code}\n end") end end end end # XML Shareable Playlist Format (XSPF[http://www.xspf.org]) parser for Ruby # # When parsing, if a XSPF attribute or element is not set, the corresponding method will return _nil_. # # === Examples # ==== Parse from file # require 'xspf' # # f = File.new("playlist.xspf") # x = XSPF.new(f) # pl = XSPF::Playlist.new(x) # tl = XSPF::Tracklist.new(pl) # # puts "XML version: #{x.version}" # puts "XML encoding: #{ x.encoding}" # puts "XSPF version: #{pl.version}" # puts "Namespace: #{pl.xmlns}" # puts "Playlist title: #{pl.title}" # puts "Playlist creator: #{pl.creator}" # puts "Playlist annotation: #{pl.annotation}" # puts "Playlist info: #{pl.info}" # puts "Playlist identifier: #{pl.identifier}" # puts "Playlist attribution: #{pl.attribution}" # puts "Tracklist: #{pl.tracklist}" # tl.tracks.each do |t| # puts "Track identifier: #{t.identifier}" # puts "Track title: #{t.title}" # puts "Track creator: #{t.creator}" # puts "Track duration: #{t.duration}" # puts "Track metainformation: link=#{t.meta_rel} content=#{t.meta_content}" # end # # # Convert the XSPF document to SMIL # x.to_smil # # # Convert the XSPF document to HTML # x.to_html # # # Convert the XSPF document in a M3U playlist # x.to_m3u # # ==== Parse from string # playlist_document = <<-END_OF_PLAYLIST # # # XSPlF it up! # Mayhem & Chaos Coordinator # Just a few songs to enjoy while you XSPlF it up! # http://mayhem-chaos.net/xspf/xspf_it_up.html # http://mayhem-chaos.net/xspf/xspf_it_up/1.0 # # ihttp://mayhem-chaos.net/xspf/xspf_it_up.html # # # # http://musicbrainz.org/track/bdab6db0-2fd6-4166-a5fa-fbf2ff213793 # I Wanna Get High # Cypress Hill # 174613 # http://musicbrainz.org/mm-2.1/track/bdab6db0-2fd6-4166-a5fa-fbf2ff213793 # # # bdc846e7-6c26-4193-82a6-8d1b5a4d3429 # Smoke Two Joints # Sublime # 175466 # http://musicbrainz.org/mm-2.1/track/bdc846e7-6c26-4193-82a6-8d1b5a4d3429 # # # http://musicbrainz.org/track/7d9776f7-d428-40dc-a425-3c6e3dce4d58 # Hash Pipe # Weezer # 186533 # http://musicbrainz.org/mm-2.1/track/7d9776f7-d428-40dc-a425-3c6e3dce4d58 # # # # END_OF_PLAYLIST # # x = XSPF.new(playlist_document) # pl = XSPF::Playlist.new(x) # tl = XSPF::Tracklist.new(pl) # # puts "XML version: #{x.version}" # puts "XML encoding: #{ x.encoding}" # puts "XSPF version: #{pl.version}" # puts "Namespace: #{pl.xmlns}" # puts "Playlist title: #{pl.title}" # puts "Playlist creator: #{pl.creator}" # puts "Playlist annotation: #{pl.annotation}" # puts "Playlist info: #{pl.info}" # puts "Playlist identifier: #{pl.identifier}" # puts "Playlist attribution: #{pl.attribution}" # puts "Tracklist: #{pl.tracklist}" # tl.tracks.each do |t| # puts "Track identifier: #{t.identifier}" # puts "Track title: #{t.title}" # puts "Track creator: #{t.creator}" # puts "Track duration: #{t.duration}" # puts "Track metainformation: link=#{t.meta_rel} content=#{t.meta_content}" # end # # # Convert the XSPF document to SMIL # x.to_smil # # # Convert the XSPF document to HTML # x.to_html # # # Convert the XSPF document in a M3U playlist # x.to_m3u # class XSPF attr_reader :xspf #:stopdoc: ATTRIBUTES = %w{ version encoding } VERSION_RDOC = "Version for the XML document or _nil_ if not defined" ENCODING_RDOC = "Encoding of the XML document or _nil_ if not defined" OUTPUT_FORMATS = %w{ m3u html smil soundblox } M3U_RDOC = "Creates a .m3u playlist from the XSPF document. This method makes use of the official XSPF to M3U XSLT transformation by Lucas Gonze." HTML_RDOC = "Outputs the playlist as an HTML page. This method makes use of the official XSPF to HTML XSLT transformation by Lucas Gonze." SMIL_RDOC = "Creates a .smil playlist from the XSPF document. This method makes use of the official XSPF to SMIL XSLT transformation by Lucas Gonze." SOUNDBLOX_RDOC = "Creates a SoundBlox playlist from the XSPF document. This method makes use of the official XSPF to SoundBlox XSLT tranformation by Lucas Gonze." ATTRIBUTES.each do |attrib| MetaGen.add_method(self, attrib, "@xspf.#{attrib}", eval(attrib.upcase + '_RDOC').to_s ) end OUTPUT_FORMATS.each do |format| MetaGen.add_output_format(self, format, @xspf, eval(format.upcase + '_RDOC').to_s ) end #:startdoc: # Creates a XSPF object from a file or string def initialize(source = nil) @xspf = REXML::Document.new(source) end # The section of the XSPF document def playlist @xspf.root end end class XSPF::Playlist < XSPF attr_reader :playlist #:stopdoc: ATTRIBUTES = %w{ xmlns version } ELEMENTS = %w{ title creator annotation info location identifier image date license attribution extension } ATTRIBUTE_AND_ELEMENT = %w{ link meta } attribution_subelements = %w{ location identifier } extension_subelements = %w{ application content } XMLNS_RDOC = "The XML namespace. It must be http://xspf.org/ns/0/ for a valid XSPF document." VERSION_RDOC = "The XSPF version. It may be 0 or 1, although 1 is strongly advised." TITLE_RDOC = "A human-readable title for the playlist. xspf:playlist elements MAY contain exactly one." CREATOR_RDOC = "Human-readable name of the entity (author, authors, group, company, etc) that authored the playlist. XSPF::Playlist objects MAY contain exactly one." ANNOTATION_RDOC = "A human-readable comment on the playlist. This is character data, not HTML, and it may not contain markup. XSPF::Playlist objects elements MAY contain exactly one." INFO_RDOC = "URL of a web page to find out more about this playlist. Likely to be homepage of the author, and would be used to find out more about the author and to find more playlists by the author. XSPF::Playlist objects MAY contain exactly one." LOCATION_RDOC = "Source URL for this playlist. XSPF::Playlist objects MAY contain exactly one." IDENTIFIER_RDOC = "Canonical ID for this playlist. Likely to be a hash or other location-independent name. MUST be a legal URN. XSPF::Playlist objects MAY contain exactly one." IMAGE_RDOC = "URL of an image to display if XSPF::Playlist#image return nil. XSPF::Playlist objects MAY contain exactly one." DATE_RDOC = "Creation date (not last-modified date) of the playlist, formatted as a XML schema dateTime. XSPF::Playlist objects MAY contain exactly one." LICENSE_RDOC = "URL of a resource that describes the license under which this playlist was released. XSPF::Playlist objects MAY contain zero or one license element." ATTRIBUTION_RDOC = "An ordered list of URIs. The purpose is to satisfy licenses allowing modification but requiring attribution. If you modify such a playlist, move its XSPF::Playlist#location or XSPF::Playlist#identifier element to the top of the items in the XSPF::Playlist#attribution element. XSPF::Playlist objects MAY contain exactly one attribution element. Please note that currently XSPF for Ruby does not parse the contents of XSPF::Playlist#attribution." EXTENSION_RDOC = "The extension element allows non-XSPF XML to be included in XSPF documents without breaking XSPF validation. The purpose is to allow nested XML, which the meta and link elements do not. XSPF::Playlist objects MAY contain zero or more extension elements but currently XSPF for Ruby returns only the first one." LINK_REL_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#link_rel and XSPF::Playlist#link_content respectively. XSPF::Playlist objects MAY contain zero or more link elements, but currently XSPF for Ruby returns only the first one." LINK_CONTENT_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#link_rel and XSPF::Playlist#link_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one." META_REL_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#meta_rel and XSPF::Playlist#meta_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one." META_CONTENT_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#meta_rel and XSPF::Playlist#meta_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one." # FIXME Currently we only return the first "link" # FIXME Currently we only return the first "meta" # FIXME Currently we only return the first "extension" # TODO Parse "attribution" # TODO Parse "extension" # Returns the value of the attribute or nil if the attribute is not present ATTRIBUTES.each do |attrib| MetaGen.add_method( self, attrib, "@playlist.root.attributes['#{attrib}']", eval(attrib.upcase + '_RDOC').to_s ) end ELEMENTS.each do |element| MetaGen.add_method( self, element, "@playlist.elements['#{element}'].text", eval(element.upcase + '_RDOC').to_s ) end ATTRIBUTE_AND_ELEMENT.each do |ae| MetaGen.add_method( self, "#{ae}_content", "@playlist.elements['#{ae}'].text", eval(ae.upcase + '_CONTENT_RDOC').to_s ) MetaGen.add_method( self, "#{ae}_rel", "@playlist.elements['#{ae}'].attributes['rel']", eval(ae.upcase + '_REL_RDOC').to_s ) end #:startdoc: # Creates a XSPF::Playlist from a XSPF document def initialize(source) @playlist = source.playlist end # The section of the XSPF document def tracklist @playlist.elements['trackList'] || nil end end class XSPF::Tracklist < XSPF::Playlist attr_reader :tracklist # Creates a XSPF::Tracklist from a XSPF::Playlist def initialize(playlist) @tracklist = playlist.tracklist end # An array XSPF::Track objects, one for each track returned by XSPF::Playlist#tracklist def tracks @tracklist.elements.collect { |track| XSPF::Track.new(track) } end end class XSPF::Track attr_reader :track #:stopdoc: ELEMENTS = %w{ location identifier title creator annotation info image album trackNum duration extension } ATTRIBUTE_AND_ELEMENT = %w{ link meta } LOCATION_RDOC = "URL of resource to be rendered. Probably an audio resource, but MAY be any type of resource with a well-known duration, such as video, a SMIL document, or an XSPF document. The duration of the resource defined in this element defines the duration of rendering. XSPF::Track objects MAY contain zero or more location elements, but a user-agent MUST NOT render more than one of the named resources. Currently, XSPF for Ruby returns only the first location." IDENTIFIER_RDOC = "Canonical ID for this resource. Likely to be a hash or other location-independent name, such as a MusicBrainz identifier or isbn URN (if there existed isbn numbers for audio). MUST be a legal URN. XSPF::Track objects elements MAY contain zero or more identifier elements, but currently XSPF for Ruby returns only the first one." TITLE_RDOC = "Human-readable name of the track that authored the resource which defines the duration of track rendering. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one." CREATOR_RDOC = "Human-readable name of the entity (author, authors, group, company, etc) that authored the resource which defines the duration of track rendering. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one." ANNOTATION_RDOC = "A human-readable comment on the track. This is character data, not HTML, and it may not contain markup. XSPF::Track objects MAY contain exactly one." INFO_RDOC = "URL of a place where this resource can be bought or more info can be found." IMAGE_RDOC = "URL of an image to display for the duration of the track. XSPF::Track objects MAY contain exactly one." ALBUM_RDOC = "Human-readable name of the collection from which the resource which defines the duration of track rendering comes. For a song originally published as a part of a CD or LP, this would be the title of the original release. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one." TRACKNUM_RDOC = "Integer with value greater than zero giving the ordinal position of the media on the XSPF::Track#album. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one. It MUST be a valid XML Schema nonNegativeInteger." DURATION_RDOC = "The time to render a resource, in milliseconds. It MUST be a valid XML Schema nonNegativeInteger. This value is only a hint -- different XSPF generators will generate slightly different values. A user-agent MUST NOT use this value to determine the rendering duration, since the data will likely be low quality. XSPF::Track objects MAY contain exactly one duration element." EXTENSION_RDOC = "The extension element allows non-XSPF XML to be included in XSPF documents without breaking XSPF validation. The purpose is to allow nested XML, which the meta and link elements do not. XSPF::Track objects MAY contain zero or more extension elements, but currently XSPF for Ruby returns only the first one." LINK_REL_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#link_rel and XSPF::Track#link_content respectively. XSPF::Track objects MAY contain zero or more link elements, but currently XSPF for Ruby returns only the first one." LINK_CONTENT_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#link_rel and XSPF::Track#link_content respectively. XSPF::Track objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one." META_REL_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#meta_rel and XSPF::Track#meta_content respectively. XSPF::Track objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one." META_CONTENT_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#meta_rel and XSPF::Track#meta_content respectively. XSPF::Track objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one." ELEMENTS.each do |element| MetaGen.add_method( self, element, "@track.elements['#{element}'].text", eval(element.upcase + '_RDOC').to_s ) end ATTRIBUTE_AND_ELEMENT.each do |ae| MetaGen.add_method( self, "#{ae}_content", "@track.elements['#{ae}'].text", eval(ae.upcase + '_CONTENT_RDOC').to_s ) MetaGen.add_method( self, "#{ae}_rel", "@track.elements['#{ae}'].attributes['rel']", eval(ae.upcase + '_REL_RDOC').to_s ) end # :startdoc: # Creates a XSPF::Track object from a section of the XSPF document def initialize(tr) @track = tr end end