Ruby Output Kramdown to Text/Kramdown

415 Views Asked by At

I'm looking for a way to translate an html file to a Markdown type syntax. That's the request from my client. They think that this type of archival method is good for manipulation into one of their GUI apps, while maintaining the ability to edit with an acceptable amount of retained markup.

I'm looking at Kramdown, but it's confusing. I have created a Kramdown object imported from the html file:

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

I'd like to output the Kramdown format, but I don't understand how to save it as a Kramdown format.

Is there a way to save the file with the Kramdown format? The original html file is parsed into Kramdown, now I want to save that file much like how I would a Markdown file, with that simple markup syntax. This works just fine:

puts doc.to_html

...so doc is just fine. I just need a way to output the Kramdown version. Any insight appreciated. Cheers

2

There are 2 best solutions below

1
On BEST ANSWER

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
1
On

You could use https://github.com/xijo/reverse_markdown:

input  = '<strong>feelings</strong>'
result = ReverseMarkdown.convert input
result.inspect # " **feelings** "