簡體   English   中英

截斷Markdown?

[英]Truncate Markdown?

我有一個Rails站點,其中的內容是用markdown編寫的。 我希望顯示每個片段,並帶有“閱讀更多...”鏈接。

我該怎么做? 簡單地截斷原始文本將不起作用,例如..

>> "This is an [example](http://example.com)"[0..25]
=> "This is an [example](http:"

理想情況下,我希望允許作者(可選)插入標記以指定要用作“片段”的內容,如果不是,則需要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

標記可以被認為是EOF標記(在顯示完整文檔時可以忽略)

我正在使用maruku進行Markdown處理(RedCloth非常偏向於Textile,BlueCloth是非常錯誤的,我想要一個本地Ruby解析器,它排除了peg-markdown和RDiscount)

或者(因為無論如何Markdown都被翻譯成HTML)正確地截斷HTML是一種選擇 - 盡管最好不要markdown()整個文檔,只是為了獲得前幾行。

所以,我能想到的選項是(按照優先順序)..

  • 向maruku解析器添加“truncate”選項,該解析器僅解析前x個單詞,或者直到“摘錄”標記。
  • 編寫/找到解析器無關的Markdown truncate'r
  • 編寫/查找智能HTML截斷功能
  • 編寫/查找智能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>

修改后的代碼:

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

這是一個適用於紡織品的解決方案。

  1. 將其轉換為HTML
  2. 截斷它。
  3. 刪除所有切成兩半的HTML標記

     html_string.gsub(/<[^>]*$/, "") 
  4. 然后,使用Hpricot清理它並關閉未關閉的標簽

     html_string = Hpricot( html_string ).to_s 

我在幫助器中執行此操作,並且通過緩存,沒有性能問題。

您可以使用正則表達式查找由“^”字符組成的行:

markdown_string = <<-eos
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
eos

preview = markdown_string[0...(markdown_string =~ /^\^+$/)]
puts preview

而不是試圖截斷文本,為什么不有2個輸入框,一個用於“打開模糊”,一個用於主要“膽量”。 通過這種方式,您的作者無需依賴某種有趣的EOF標記即可准確了解所顯示的內容。

不確定它是否適用於這種情況,但為了完整起見,在下面添加解決方案。 如果要截斷Markdown渲染的內容,可以使用strip_tags方法:

truncate(strip_tags(markdown(article.contents)), length: 50)

來自: http//devblog.boonecommunitynetwork.com/rails-and-markdown/

我將不得不同意“兩個輸入”方法,內容編寫者不必擔心,因為您可以修改背景邏輯,在顯示完整內容時將兩個輸入混合在一起。

full_content = input1 + input2 // perhaps with some complementary html, for a better formatting

一個更簡單的選項:

truncate(markdown(item.description), length: 100, escape: false)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM