这是动态创建 XML 的 XML 函数:
def make_toc
builder = Nokogiri::XML::Builder.new do |xml|
xml.ncx( xmlns: "http://www.daisy.org/z3986/2005/ncx/", version: "2005-1", "xml:lang"=>"eng" ) do
xml.head do
xml.meta( content: book.uuid, name: "dtb:uid")
xml.meta( content: "2", name: "dtb:depth")
xml.meta( content: "0", name: "dtb:totalPageCount")
xml.meta( content: "0", name: "dtb:maxPageNumber")
end
xml.docTitle do
xml.text_ book.full_title
end
xml.navMap do
i=0
self.oeb_parts.each do |section|
next if section.name.blank?
i = i + 1
xml.navPoint( id: section.uuid, playOrder: i.to_s) do
xml.navLabel do
xml.text_ section.name
end
xml.content(src: File.basename(section.file.path))
unless self.toc_json.nil?
toc = eval(self.toc_json)
prev_list = 0
value=''
toc.each do |toc_node|
if toc_node['part_id'].to_i == section.id.to_i
j=0
toc_node.each do |key, toc_part|
if key != "part_id" && key != "position" && key != "part_name"
value = ''
j=i+1
i=j
n = toc_part['list'] - prev_list
if n > 0
n.times {
value = value+"xml.navPoint( id: toc_part['id'], playOrder: j.to_s) do"
}
end
m = prev_list - toc_part['list']
if m > 0
m.times {
value = value+"end"
}
end
value = value+"xml.navLabel do
xml.text_ toc_part['value']
end
xml.content(src: File.basename(section.file.path)+'#'+toc_part['id'])"
prev_list = toc_part['list']
"#{value}"
end
end
end
end
m = prev_list - (-1)
if m > 0
m.times {
value=value+"end"
}
end
end
end
end
end
end
end
builder.to_xml
end
如何执行这个?
if n > 0
n.times {
value = value+"xml.navPoint( id: toc_part['id'], playOrder: j.to_s) do"
}
end
我想xml.navPoint( id: toc_part['id'], playOrder: j.to_s) do
在循环中运行。如果我直接调用它而不进行任何插值,则会引发“意外结束”错误。
在视图文件中,我可以使用html_safe
或 raw 将字符串转换为 HTML。但在模型中我不知道如何转换它。
谁能帮我解决这个问题?