module Text.Sundown.Html.ByteString
( renderHtml
, smartypants
, Extensions (..)
, allExtensions
, noExtensions
, HtmlRenderMode (..)
, noHtmlModes
, allHtmlModes
) where
import Data.Maybe (fromMaybe)
import Foreign.Marshal
import Foreign.Ptr
import Foreign.Storable
import System.IO.Unsafe
import Data.ByteString (ByteString)
import qualified Data.ByteString.Unsafe as BS
import Text.Sundown.Buffer.Foreign
import Text.Sundown.Foreign
import Text.Sundown.Html.Foreign
defaultMaxNesting :: Int
defaultMaxNesting = 16
renderHtml :: ByteString
-> Extensions
-> HtmlRenderMode
-> Bool
-> Maybe Int
-> ByteString
renderHtml input exts mode sp maxNestingM =
unsafePerformIO $
alloca $ \callbacks ->
alloca $ \options ->
BS.unsafeUseAsCStringLen input $ \(ptr, len) -> do
ob <- bufnew 64
sdhtml_renderer callbacks options mode
let maxNesting = fromIntegral $ fromMaybe defaultMaxNesting maxNestingM
markdown <- sd_markdown_new exts maxNesting callbacks (castPtr options)
sd_markdown_render ob ptr (fromIntegral len) markdown
sd_markdown_free markdown
res <- if sp then
do ob' <- bufnew 64
Buffer {buf_data = optr, buf_size = olen} <- peek ob
sdhtml_smartypants ob' optr olen
bufrelease ob
return ob'
else return ob
output <- peek res >>= getBufferData
bufrelease res
return output
noHtmlModes :: HtmlRenderMode
noHtmlModes = HtmlRenderMode False False False False False False False False
False False
allHtmlModes :: HtmlRenderMode
allHtmlModes = HtmlRenderMode True True True True True True True True True True
smartypants :: ByteString -> ByteString
smartypants input =
unsafePerformIO $
BS.unsafeUseAsCStringLen input $ \(ptr, len) -> do
ob <- bufnew 64
sdhtml_smartypants ob ptr (fromIntegral len)
output <- peek ob >>= getBufferData
bufrelease ob
return output