Markdown to Plain Text in Ruby

Converting back to plain text when using ActionText

So apparently ActionText instances have a method for retrieving plain text values with to_plain_text. All together it looks like this:

@post.body => <div>This is my markup</div>
@post.body.to_plain_text => This is my markup

Ruby Output Kramdown to Text/Kramdown

You need the to_kramdown method. It’s not documented directly as it is dynamically called, but see method_missing.

doc = Kramdown::Document.new(source, :input => 'html')
puts doc.to_kramdown

How can I get Sinatra to deliver Markdown as HTML as well as the source code as plain text?

With a little more searching I managed to get it working using send_file:

get '/:topic/source' do
send_file File.dirname(__FILE__) + "/views/#{params[:topic]}.md", :type => :text
end

But I'd like to believe there's a more elegant solution out there, so leaving the question open for now.

Ruby/Redcarpet: strip markdown from text

Yes, it's possible with the Redcarpet::Render::StripDown Class -> http://www.rubydoc.info/github/vmg/redcarpet/Redcarpet/Render/StripDown

It says "Turns Markdown into plaintext"

textarea that was using plain text with option of markdown or textile filter now needs images

There are different directions you could go to:

  • Follow the path of Confluence, which released in their new version 4.0 a rewritten WYSIWYG editor, that stores as source XHTML, not any more wiki markup.
    • Leads to an update of all pages when migrating.
    • Was pretty difficult to implement. I do not know if they use any more the TinyMCE editor of previous versions.
  • Follow the format of markdown how to include images in your source format. So by typing: This is my text. !image.png! The inline image shows ..., you will have a format that is understandable.
    • You have to expand the interpretation, so that the !<filename>! will be mapped that is stored locally anyway.
    • You have to add clear-up dialogs for the images that are yet not known, so doing bulk uploads ...
    • You may provide a drag area on your view, that then shows the filename and gives examples how to include that inside the text area.
  • Go for something in between, by allowing users to drag images inside the editor. There are plugins written in Javascript that allow you to do that, e.g. UI Draggable for jquery
    • I have no idea how to integrate that image inside the text editor. Overlay?

So the second one is the easiest, and the user knows how to do it. If they only decide that this is the solution they want to have :-)

Create text excerpt from HTML paragraphs in Rails

I ended up writing a custom Redcarpet renderer (inspired by Redcarpet::Render::StripDown). which seems the cleanest approach with the least parsing and converting between formats.

module R::Markdown
class ExcerptRenderer < Redcarpet::Render::Base
# Methods where the first argument is the text content
[
# block-level calls
:paragraph,

# span-level calls
:codespan, :double_emphasis,
:emphasis, :underline, :raw_html,
:triple_emphasis, :strikethrough,
:superscript, :highlight, :quote,

# footnotes
:footnotes, :footnote_def, :footnote_ref,

# low level rendering
:entity, :normal_text
].each do |method|
define_method method do |*args|
args.first
end
end

# Methods where content is replaced with an empty space
[
:autolink, :block_html
].each do |method|
define_method method do |*|
" "
end
end

# Methods we are going to [snip]
[
:list, :image, :table, :block_code
].each do |method|
define_method method do |*|
" [#{method}] "
end
end

# Other methods
def link(link, title, content)
content
end

def header(text, header_level)
" #{text} "
end

def block_quote(quote)
" “#{quote}” "
end

# Replace all whitespace with single space
def postprocess(document)
document.gsub(/\s+/, " ").strip
end
end
end

And parse it:

extensions = {
autolink: true,
disable_indented_code_blocks: true,
fenced_code_blocks: true,
lax_spacing: true,
no_intra_emphasis: true,
strikethrough: true,
superscript: true,
tables: true
}

markdown = Redcarpet::Markdown.new(R::Markdown::ExcerptRenderer, extensions)

markdown.render(md).html_safe


Related Topics



Leave a reply



Submit