在Ruby中标记为纯文本?

我目前正在使用BlueCloth在Ruby中处理Markdown并将其显​​示为HTML,但在一个位置我需要它作为纯文本(没有一些Markdown)。 有没有办法实现这一目标?

是否有降价到纯文本的方法? 是否有一个html到纯文本的方法,我能感觉到BlueCloth的结果?

RedCarpet gem有一个Redcarpet :: Render :: StripDown渲染器,它将“Markdown变为明文”。

复制并修改它以满足您的需求。

或者像这样使用它:

Redcarpet::Markdown.new(Redcarpet::Render::StripDown).render(markdown) 

使用Ruby将HTML转换为纯文本不是问题 ,但当然你会丢失所有标记。 如果您只想摆脱一些 Markdown语法,它可能不会产生您正在寻找的结果。

底线是未渲染的Markdown旨在用作纯文本,因此转换为纯文本并不真正有意义。 我见过的所有Ruby实现都遵循相同的接口,它没有提供剥离语法的方法(仅包括to_htmltext ,它返回原始的Markdown文本)。

它不是ruby,但Pandoc现在写的格式之一是’普通’。 这是一些任意降价:

 # My Great Work ## First Section Here we discuss my difficulties with [Markdown](http://wikipedia.org/Markdown) ## Second Section We begin with a quote: > We hold these truths to be self-evident ... then some code: #! /usr/bin/bash That's *all*. 

(不知道如何关闭语法高亮!)这是相关的’普通’:

 My Great Work ============= First Section ------------- Here we discuss my difficulties with Markdown Second Section -------------- We begin with a quote: We hold these truths to be self-evident ... then some code: #! /usr/bin/bash That's all. 

您可以从Github存储库中pandoc / blob / master / src / Text / Pandoc / Writers / Markdown.hs中的plainify定义解析文档中的不同元素,了解它的作用。 还有一个教程 ,显示修改行为是多么容易。