Markdown to plain text in Ruby?
Asked Answered
S

3

19

I'm currently using BlueCloth to process Markdown in Ruby and show it as HTML, but in one location I need it as plain text (without some of the Markdown). Is there a way to achieve that?

Is there a markdown-to-plain-text method? Is there an html-to-plain-text method that I could feel the result of BlueCloth?

Stunt answered 30/3, 2010 at 17:46 Comment(0)
L
12

RedCarpet gem has a Redcarpet::Render::StripDown renderer which "turns Markdown into plaintext".

Copy and modify it to suit your needs.

Or use it like this:

require 'redcarpet'
require 'redcarpet/render_strip'    
Redcarpet::Markdown.new(Redcarpet::Render::StripDown).render(markdown)
Lyonnesse answered 14/11, 2014 at 4:12 Comment(0)
G
2

Converting HTML to plain text with Ruby is not a problem, but of course you'll lose all markup. If you only want to get rid of some of the Markdown syntax, it probably won't yield the result you're looking for.

The bottom line is that unrendered Markdown is intended to be used as plain text, therefore converting it to plain text doesn't really make sense. All Ruby implementations that I have seen follow the same interface, which does not offer a way to strip syntax (only including to_html, and text, which returns the original Markdown text).

Geller answered 30/3, 2010 at 22:18 Comment(2)
I'm fine with some of the markup, like lists; but for example, links are hard to read for non-technical people.Stunt
Line-wrapping, links (should be made into [1] references and added to the bottom), headers, white-spacing between elements, etc. can all be made better.Pussyfoot
L
-2

It's not ruby, but one of the formats Pandoc now writes is 'plain'. Here's some arbitrary markdown:

# 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*. 

(Not sure how to turn off the syntax highlighting!) Here's the associated 'plain':

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.

You can get an idea what it does with the different elements it parses out of documents from the definition of plainify in pandoc/blob/master/src/Text/Pandoc/Writers/Markdown.hs in the Github repository; there is also a tutorial that shows how easy it is to modify the behavior.

Lockett answered 13/4, 2010 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.