Skip to content

Instantly share code, notes, and snippets.

@masaedw
Created September 24, 2011 13:19
Show Gist options
  • Select an option

  • Save masaedw/1239318 to your computer and use it in GitHub Desktop.

Select an option

Save masaedw/1239318 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Attoparsec
import qualified Data.Attoparsec as A
import qualified Data.ByteString as B
data MDElement = Elem B.ByteString
| Link B.ByteString
deriving (Show, Eq)
data MDLine = Line [MDElement]
| QuotedLine B.ByteString
deriving (Show, Eq)
mdEx = choice [preLine, normalLine] `sepBy` eol
preLine = do
q <- try $ choice [string "\t", string " "]
l <- A.takeWhile isLineChar
return $ QuotedLine $ B.concat [q, l]
where isLineChar = (`B.notElem` "\r\n")
normalLine = do
l <- A.many $ choice [wikiLink, lineChar]
return $ Line l
lineChar = do
c <- satisfy (`B.notElem` "\r\n")
return $ Elem $ B.pack [c]
wikiLink = do
name <- string "[[" *> A.takeWhile wikiNameChar <* string "]]"
return $ Link name
where wikiNameChar = inClass "a-zA-Z0-9"
eol = choice [try (string "\n\r"),
try (string "\r\n"),
string "\n",
string "\r"]
main = do
f <- B.readFile "sample.md"
let p = parse mdEx f
print $ feed p B.empty

This is a header

This is a small header

body body body body body body

blockquote blockquote

  • list
  • list
  • list

aaaooolink

  1. list
  2. list
  3. list

日本語[[wikiLink]] [[not wiki link]]

日本語だよ〜

quoted
quotedだよ〜

tab-auoted
tabだよ〜
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment