basic image tags

This commit is contained in:
John Bintz 2011-03-14 12:35:29 -04:00
parent 179d017f37
commit 3aede05d1b
4 changed files with 115 additions and 32 deletions

View File

@ -15,6 +15,24 @@ module AVM
creator.add_to_document(document) creator.add_to_document(document)
document.get_refs do |refs|
[ :title, :description ].each do |field|
refs[:dublin_core].add_child(%{<dc:#{field}>#{alt_li_tag(send(field))}</dc:#{field}>})
end
refs[:photoshop].add_child(%{<photoshop:Headline>#{headline}</photoshop:Headline>})
{
'Distance.Notes' => distance_notes,
'ReferenceURL' => reference_url,
'Credit' => credit,
'Date' => string_date,
'ID' => id,
}.each do |tag, value|
refs[:avm].add_child(%{<avm:#{tag}>#{value}</avm:#{tag}>}) if value
end
end
document.doc document.doc
end end
@ -27,7 +45,12 @@ module AVM
end end
def date def date
Time.parse(@options[:date]) (Time.parse(@options[:date]) rescue nil)
end
def string_date
return nil if !date
date.strftime('%Y-%m-%d')
end end
def distance def distance
@ -45,6 +68,11 @@ module AVM
def method_missing(method) def method_missing(method)
@options[method] @options[method]
end end
private
def alt_li_tag(text)
%{<rdf:Alt><rdf:li xml:lang="x-default">#{text}</rdf:li></rdf:Alt>}
end
end end
end end

View File

@ -2,6 +2,12 @@ require 'nokogiri'
module AVM module AVM
class XMP class XMP
PREFIXES = {
'dc' => 'Dublin Core',
'Iptc4xmpCore' => 'IPTC',
'Photoshop' => 'Photoshop'
}
attr_reader :doc attr_reader :doc
def initialize(doc = nil) def initialize(doc = nil)
@ -11,7 +17,7 @@ module AVM
end end
def get_refs def get_refs
yield Hash[[ :dublin_core, :iptc ].collect { |key| [ key, send(key) ] }] yield Hash[[ :dublin_core, :iptc, :photoshop, :avm ].collect { |key| [ key, send(key) ] }]
end end
def self.from_string(string) def self.from_string(string)
@ -36,12 +42,7 @@ module AVM
doc.search('//rdf:Description').each do |description| doc.search('//rdf:Description').each do |description|
if first_child = description.first_element_child if first_child = description.first_element_child
if first_child.namespace if first_child.namespace
case first_child.namespace.prefix description['about'] = PREFIXES[first_child.namespace.prefix]
when 'dc'
description['about'] = 'Dublin Core'
when 'Iptc4xmpCore'
description['about'] = 'IPTC'
end
end end
end end
end end
@ -55,6 +56,14 @@ module AVM
at_rdf_description "IPTC" at_rdf_description "IPTC"
end end
def avm
at_rdf_description "AVM"
end
def photoshop
at_rdf_description "Photoshop"
end
def at_rdf_description(about) def at_rdf_description(about)
@doc.at_xpath(%{//rdf:Description[@about="#{about}"]}) @doc.at_xpath(%{//rdf:Description[@about="#{about}"]})
end end
@ -63,12 +72,10 @@ module AVM
Nokogiri::XML(<<-XML) Nokogiri::XML(<<-XML)
<x:xmpmeta xmlns:x="adobe:ns:meta/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <x:xmpmeta xmlns:x="adobe:ns:meta/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:RDF> <rdf:RDF>
<rdf:Description about="Dublin Core"> <rdf:Description about="Dublin Core" />
<rdf:Description about="IPTC" />
</rdf:Description> <rdf:Description about="Photoshop" />
<rdf:Description about="IPTC"> <rdf:Description about="AVM" />
</rdf:Description>
</rdf:RDF> </rdf:RDF>
</x:xmpmeta> </x:xmpmeta>
XML XML

View File

@ -7,9 +7,20 @@ describe AVM::Image do
subject { image } subject { image }
describe '#initialize' do let(:title) { 'My title' }
it { should be_a_kind_of(AVM::Image) } let(:headline) { 'Headline' }
let(:description) { 'Description' }
let(:distance_notes) { 'Distance Notes' }
let(:reference_url) { 'Reference URL' }
let(:credit) { 'Credit' }
let(:date) { '2010-01-01' }
let(:id) { 'ID' }
let(:type) { 'Obvservation' }
let(:image_quality) { 'Good' }
let(:redshift) { 'Redshift' }
let(:light_years) { 'Light years' }
def self.with_all_options
let(:options) { { let(:options) { {
:title => title, :title => title,
:headline => headline, :headline => headline,
@ -24,19 +35,12 @@ describe AVM::Image do
:redshift => redshift, :redshift => redshift,
:light_years => light_years :light_years => light_years
} } } }
end
let(:title) { 'My title' } describe '#initialize' do
let(:headline) { 'Headline' } with_all_options
let(:description) { 'Description' }
let(:distance_notes) { 'Distance Notes' } it { should be_a_kind_of(AVM::Image) }
let(:reference_url) { 'Reference URL' }
let(:credit) { 'Credit' }
let(:date) { '2010-01-01' }
let(:id) { 'ID' }
let(:type) { 'Obvservation' }
let(:image_quality) { 'Good' }
let(:redshift) { 'Redshift' }
let(:light_years) { 'Light years' }
its(:creator) { should be_a_kind_of(AVM::Creator) } its(:creator) { should be_a_kind_of(AVM::Creator) }
its(:title) { should == title } its(:title) { should == title }
@ -58,9 +62,50 @@ describe AVM::Image do
describe '#to_xml' do describe '#to_xml' do
let(:xml) { image.to_xml } let(:xml) { image.to_xml }
let(:dublin_core) { xml.at_xpath('//rdf:Description[@about="Dublin Core"]') }
let(:photoshop) { xml.at_xpath('//rdf:Description[@about="Photoshop"]') }
let(:avm) { xml.at_xpath('//rdf:Description[@about="AVM"]') }
context 'nothing in it' do context 'nothing in it' do
subject { xml.at_xpath('//rdf:RDF').should_not be_nil } it "should have basic tags" do
subject { xml.search('//rdf:RDF/rdf:Description').should be_empty } xml.at_xpath('//rdf:RDF').should_not be_nil
xml.search('//rdf:RDF/rdf:Description').should_not be_empty
avm.at_xpath('./avm:Date').should be_nil
end
end
context 'with basics' do
with_all_options
it "should have the image info tags" do
dublin_core.at_xpath('./dc:title/rdf:Alt/rdf:li').text.should == title
photoshop.at_xpath('./photoshop:Headline').text.should == headline
dublin_core.at_xpath('./dc:description/rdf:Alt/rdf:li').text.should == description
avm.at_xpath('./avm:Distance.Notes').text.should == distance_notes
avm.at_xpath('./avm:ReferenceURL').text.should == reference_url
avm.at_xpath('./avm:Credit').text.should == credit
avm.at_xpath('./avm:Date').text.should == date
avm.at_xpath('./avm:ID').text.should == id
end
context "distance" do
context "no distances" do
end
context "redshift only" do
end
context "light years only" do
end
context "redshift and light years" do
end
end
end end
end end
end end

View File

@ -11,11 +11,14 @@ describe AVM::XMP do
xmp.get_refs do |refs| xmp.get_refs do |refs|
refs[:dublin_core] << "<rdf:addedToDublinCore />" refs[:dublin_core] << "<rdf:addedToDublinCore />"
refs[:iptc] << "<rdf:addedToIPTC />" refs[:iptc] << "<rdf:addedToIPTC />"
refs[:photoshop] << '<rdf:addedToPhotoshop />'
end end
} }
specify { xmp.doc.at_xpath('//rdf:Description[@about="Dublin Core"]//rdf:addedToDublinCore').should_not be_nil } it "should have gotten the refs correctly" do
specify { xmp.doc.at_xpath('//rdf:Description[@about="IPTC"]//rdf:addedToIPTC').should_not be_nil } xmp.doc.at_xpath('//rdf:Description[@about="Dublin Core"]//rdf:addedToDublinCore').should_not be_nil
xmp.doc.at_xpath('//rdf:Description[@about="IPTC"]//rdf:addedToIPTC').should_not be_nil
end
end end
describe '.from_string' do describe '.from_string' do