Search code examples
rubymarkdownrtfrichtextpandoc

Convert Markdown text to RTF, using Ruby and Pandoc?


Playing with Ruby and Ruby-Pandoc. Seems like a nice tool, if I can get it to work.

I'd like to convert some Markdown text (with embedded lists and other fanciness) to Rich Text. Here's the text I'm converting:

Title
===

This is a paragraph. Hallelujah.

Here comes a nested list.
---

* List item 1
  * List item 1.1
  * List item 1.2
* List item 2
  * List item 2.1

Here's my Ruby code...

require 'pandoc-ruby'

input = File.read(test.md)
converter = PandocRuby.new(input, from: :markdown, to: :rtf)
puts converter.convert

... which (after saving the output to a file) produces a document without anything but a title:

screenshot of RTF

Here's the code of the RTF file:

{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs36 Title\par}
{\pard \ql \f0 \sa180 \li0 \fi0 This is a paragraph. Hallelujah.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs32 Here comes a nested list.\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 1\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 1.1\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 1.2\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 2\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 2.1\sa180\par}

In addition, even if it did show up in my RTF viewer (Mac TextEdit), the RTF code seems to have lost all list nesting. I don't know how to diagnose this, whether I have not stated necessary header information or something in Ruby-Pandoc.

Thanks in advance!


Solution

  • Wrap your output in a prolog and closing brace. Like this:

    {\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}}
    {\pard \ql \f0 \sa180 \li0 \fi0 \b \fs36 Title\par}
    {\pard \ql \f0 \sa180 \li0 \fi0 This is a paragraph. Hallelujah.\par}
    {\pard \ql \f0 \sa180 \li0 \fi0 \b \fs32 Here comes a nested list.\par}
    {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 1\par}
    {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 1.1\par}
    {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 1.2\par}
    {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 2\par}
    {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab List item 2.1\sa180\par}
    }
    

    Update

    am I missing a "wrapper" configuration setting?

    Yes, as per the gem readme.

    If you are trying to generate a standalone file with full file headers rather than just a marked up fragment, remember to pass the :standalone option so the correct header and footer are added.

    what about the lack of nesting lists?

    As per the PanDoc documentation, your source text is missing 2 more spaces of indentation.

    So putting that together, here is the complete solution.

    Your source file:

    Title
    ===
    
    This is a paragraph. Hallelujah.
    
    Here comes a nested list.
    ---
    
    * List item 1
        * List item 1.1
        * List item 1.2
    * List item 2
        * List item 2.1
    

    The conversion code:

    require 'pandoc-ruby'
    input = File.read('./test.md')
    puts PandocRuby.markdown(input).to_rtf(:standalone)