alt text http://img146.imageshack.us/img146/6882/clocal.png
(原始歌词已经被审查,请单击此处查看它们)
不管怎样,我的问题是如何在我的应用程序中对这些注释进行建模。现在,我正在以以下格式将歌词和注释存储为一个大的HTML块:
<div class="lyrics">
With the goons I spy
<a href="#note1">Stay in tune with ma</a>
<a href="#note2">She like damn
This the realest since 'Kumbaya'</a>
Kumbayay Killa Cam my lord
</div>
<div class="annotations">
<div id="note1">
"Ma" refers to ladies, generally, and specifically also the woman singing the hook; "Stay in tune" is a musical metaphor: he literally stays in tune with the singer and also in the sense that he has game.
</div>
<div id="note2">
Kumbaya is a campfire singalong.
</div>
</div>
然后使用此方法处理它以输出:
class Song < ActiveRecord::Base
include ActionView::Helpers
def annotated_lyrics
lyrics = read_attribute('annotated_lyrics')
return if lyrics.blank?
require 'hpricot'
doc = Hpricot lyrics
doc.at('.lyrics').inner_html = doc.at('.lyrics').inner_html.strip
doc.search("a[@href^='#note']").set('class', 'tooltip').each do |t|
t.inner_html = t.inner_html.strip
end
doc.search("div[@id^='note']").set('class', 'annotation').each do |a|
a.inner_html = auto_link(a.inner_html.strip, :all, :target => '_blank')
end
simple_format doc.html.strip
end
end
我使用jQuery和出色的qTip插件来完成其余部分。
这对于显示来说很好用,但由于我的应用程序不知道注释和歌词之间的关系,因此很难添加一个界面以内联方式更新单个注释(或者根本不更新)。
另一方面,我真的不知道用ActiveRecord表示这个的最佳方法。我想一首歌可以“拥有”许多注释,但是我该如何表示哪些歌词被注释了呢?我可以存储起始和结束的单词索引,但这似乎很繁琐,并且对歌词中的微小更改非常敏感。