Skip to content

Instantly share code, notes, and snippets.

@ehamberg
Created August 25, 2016 10:03

Revisions

  1. ehamberg created this gist Aug 25, 2016.
    39 changes: 39 additions & 0 deletions pandoc.js.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    {-# Language ForeignFunctionInterface #-}
    {-# Language OverloadedStrings #-}

    module Main where

    import Text.Pandoc

    import GHCJS.Marshal
    import GHCJS.Foreign.Callback
    import JavaScript.Object
    import JavaScript.Object.Internal (Object(..))

    convert :: String -> String
    convert s = do
    let readerOpts = def { readerSmart = True }

    let Right doc = readMarkdown readerOpts s

    let writerOptions = def { writerHtml5 = True }
    writeHtmlString writerOptions doc

    foreign import javascript unsafe "convert_ = $1"
    js_set_convert :: Callback a -> IO ()

    main :: IO ()
    main = do
    putStrLn "Haskell convert core starting..."

    callback <- syncCallback1 ThrowWouldBlock $ \jsval -> do
    let o = Object jsval
    getProp "in" o
    >>= fromJSValUnchecked
    >>= pure . convert
    >>= toJSVal
    >>= \v -> setProp "out" v o

    js_set_convert callback

    putStr "Haskell convert callback initialized."