module Anansi.Loom.Markdown (loomMarkdown) where
import Control.Monad (forM_)
import Control.Monad.Reader (asks)
import Control.Monad.Writer (tell)
import Data.ByteString (ByteString)
import Data.Monoid (mconcat)
import qualified Data.Text
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Anansi.Types
loomMarkdown :: Loom
loomMarkdown = mapM_ putBlock . documentBlocks where
putBlock b = case b of
BlockText text -> tell (encodeUtf8 text)
BlockFile path content -> do
tell "\n> **\xC2\xBB "
tell =<< escapeText path
tell "**\n\n"
putContent content
tell "\n"
BlockDefine name content -> do
tell "\n> **\xC2\xAB"
tell =<< escapeText name
tell "\xC2\xBB**\n\n"
putContent content
tell "\n"
putContent cs = forM_ cs $ \c -> case c of
ContentText _ text -> do
tell "> "
escapeCode text >>= tell
tell "\n"
ContentMacro _ indent name -> formatMacro indent name >>= tell
formatMacro indent name = do
escIndent <- escapeCode indent
escName <- escapeCode name
return (mconcat ["> ", escIndent, "\xC2\xAB", escName, "\xC2\xBB\n"])
escapeCode :: Text -> LoomM ByteString
escapeCode text = do
tabSize <- asks loomOptionTabSize
return $ encodeUtf8 $ Data.Text.concatMap (\c -> case c of
'\t' -> Data.Text.replicate (fromInteger tabSize) " "
_ -> Data.Text.singleton c) text
escapeText :: Text -> LoomM ByteString
escapeText text = do
tabSize <- asks loomOptionTabSize
return $ encodeUtf8 $ Data.Text.concatMap (\c -> case c of
'\t' -> Data.Text.replicate (fromInteger tabSize) " "
'\\' -> "\\\\"
'_' -> "\\_"
'*' -> "\\*"
'[' -> "\\["
']' -> "\\]"
'`' -> "\\`"
'&' -> "&"
_ -> Data.Text.singleton c) text