📰 Org-mode to Markdown Made Easy

Posted on April 18, 2022 by Myoungjin Jeon
Tags: pandoc, org-mode, markdown, haskell software, converter, stack, fish, fish script, awk

I’m writing blogs in org-mode and..

Not to forget how to write down in org-mode or literate haskell, I keep writing my blog in .lhs or .org and I realized that when I need to post in dev.to. I found myself being a coverter. It is not very hard but annoying process. And I tried pandoc as a commandline tool.

A programmer is a person who is willing to spend 2 hours to make an automatic system, to save 2 minutes for now. 😱

So I was looking for file format converter. which is actually something I always use when I use hakyll – pandoc!

Install pandoc

you can install via pacman on arch linux!

sh> sudo pacman -S pandoc
resolving dependencies...
looking for conflicting packages...

Packages (129) haskell-aeson-1.5.6.0-84  haskell-aeson-pretty-0.8.9-35
               haskell-ansi-terminal-0.11-218  haskell-appar-0.1.8-13
               haskell-asn1-encoding-0.9.6-154  haskell-asn1-parse-0.9.5-154
               haskell-asn1-types-0.3.4-133  haskell-assoc-1.0.2-124
               haskell-async-2.2.4-14  haskell-attoparsec-0.14.4-3

       .. snip ..

               haskell-x509-store-1.6.9-5  haskell-x509-system-1.6.7-8
               haskell-x509-validation-1.6.12-10  haskell-xml-1.3.14-30
               haskell-xml-conduit-1.9.1.1-85  haskell-xml-types-0.3.8-8
               haskell-zip-archive-0.4.1-161  haskell-zlib-0.6.2.3-62
               pandoc-2.14.2-42

Total Download Size:    44.20 MiB
Total Installed Size:  297.95 MiB

:: Proceed with installation? [Y/n] n

Oh… Kay… abort mission. Because I’m using stack and stack has installed many haskell libraries already.

sh> stack install pandoc
.. compiling ....

TBH, if you are compiling for the first time, it would take a lot of time. ghc is not blazing fast software. It is up to you, but if you don’t use other haskell software, I recommends you try to install it via your package system first.

pandoc -t markdown

sh> pandoc -to markdown some-article.org > out.markdown

and that’s it.

Fix metadata

However, you might need to fix your metadata, if you repost in markdown format within some sort of static stie builder like hakyll.

After previous command pandoc -t markdwon, metadata unusable because it looks like below:

--- title: Builtin Benchmark with Go description: writing the benchmark
code in go langauge. keywords: go, golang, benchmark, org-mode, hakyll
author: Myoungjin Jeon ---

You can edit by yourself on your favourite editor or below awk programme can take out the metadata part.

save below as hakyll-pandoc-preproc.awk

#!/usr/bin/awk -f

BEGIN {
    numof_printed = -1;
}

{
    if ( $0 ~ /^--*$/ ) {
        numof_printed++;
        if ( numof_printed > 0 ) {
            print $0;
            exit;
        }
    }

    if ( numof_printed >= 0 ) {
        print $0;
        numof_printed++;
    }
}

and extact like below:

  sh> chmod u+x hakyll-pandoc-preproc.awk
  sh> ./hakyll-pandoc-preproc.awk "your-document.org"
---
title: Builtin Benchmark with Go
description: writing the benchmark code in go langauge.
keywords: go, golang, benchmark, org-mode, hakyll
author: Myoungjin Jeon
---

Or.. maybe it is better for other script language to handle it. I tried with fish shell because yesterday I talked about fish shell is good for scripting as well!

#!/usr/bin/env fish
# filename: pandoc-any-to-markdown.fish
# version: 0.1

set -l input_filepath $argv[1]

if not test -f $input_filepath
    echo \
"Usage: <document file path> > <output file path>
        will print out converted text to stdout.
"
    exit 1;
end

set -l numof_printed -1
# ^ indicating phase -1: meta not found (>=0): meta found
set -l pandoc_from (string split "." $input_filepath | tail -n1)
# ^ pandoc reader relys on extension

begin
    while read -l line
        if string match -q --regex '^---' -- "$line"
            set numof_printed (math "$numof_printed + 1")
            if test $numof_printed -gt 0
                echo $line
                break;
            end
        end

        if test $numof_printed -ge 0
            echo $line
            set numof_printed (math "$numof_printed + 1")
        end
    end

    pandoc -f $pandoc_from -t markdown -
end < $input_filepath

You can find the recent version at here.

And make it executable and run! (now I found myself beging an installer..)

sh> chmod u+x pandoc-any-to-markdown.fish
sh> ./pandoc-any-to-markdown.fish test-pandoc.org

I think I can post more articles to dev.to even if I wrote down in org-mode or literate haskell. Oki… That’s it all for today!