dbr*_*dbr 10 ruby markdown ruby-on-rails
我有一个Rails站点,其中的内容是用markdown编写的.我希望显示每个片段,并带有"阅读更多..."链接.
我该怎么做?简单地截断原始文本将不起作用,例如..
>> "This is an [example](http://example.com)"[0..25]
=> "This is an [example](http:"
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望允许作者(可选)插入标记以指定要用作"片段"的内容,如果不是,则需要250个单词,并附加"..." - 例如..
This article is an example of something or other.
This segment will be used as the snippet on the index page.
^^^^^^^^^^^^^^^
This text will be visible once clicking the "Read more.." link
Run Code Online (Sandbox Code Playgroud)
标记可以被认为是EOF标记(在显示完整文档时可以忽略)
我正在使用maruku进行Markdown处理(RedCloth非常偏向于Textile,BlueCloth是非常错误的,我想要一个本地Ruby解析器,它排除了peg-markdown和RDiscount)
或者(因为无论如何将Markdown翻译成HTML)正确地截断HTML是一种选择 - 尽管最好不要markdown()
整个文档,只是为了获得前几行.
所以,我能想到的选项是(按照优先顺序)..
- 编写/查找智能HTML截断功能
以下来自http://mikeburnscoder.wordpress.com/2006/11/11/truncating-html-in-ruby/,经过一些修改将正确截断HTML,并且可以轻松地在结束标记之前附加字符串.
>> puts "<p><b><a href=\"hi\">Something</a></p>".truncate_html(5, at_end = "...")
=> <p><b><a href="hi">Someth...</a></b></p>
Run Code Online (Sandbox Code Playgroud)
修改后的代码:
require 'rexml/parsers/pullparser'
class String
def truncate_html(len = 30, at_end = nil)
p = REXML::Parsers::PullParser.new(self)
tags = []
new_len = len
results = ''
while p.has_next? && new_len > 0
p_e = p.pull
case p_e.event_type
when :start_element
tags.push p_e[0]
results << "<#{tags.last}#{attrs_to_s(p_e[1])}>"
when :end_element
results << "</#{tags.pop}>"
when :text
results << p_e[0][0..new_len]
new_len -= p_e[0].length
else
results << "<!-- #{p_e.inspect} -->"
end
end
if at_end
results << "..."
end
tags.reverse.each do |tag|
results << "</#{tag}>"
end
results
end
private
def attrs_to_s(attrs)
if attrs.empty?
''
else
' ' + attrs.to_a.map { |attr| %{#{attr[0]}="#{attr[1]}"} }.join(' ')
end
end
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3570 次 |
最近记录: |