1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
{-# LANGUAGE OverloadedStrings #-}
module Text.Markdown.Inline
( Inline (..)
, inlineParser
, toInline
) where
import Prelude hiding (takeWhile)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Attoparsec.Text
import Control.Applicative
import Data.Monoid ((<>))
import qualified Data.Map as Map
import Text.Markdown.Types (Inline(..))
import Data.XML.Types (Content (..))
import Text.XML.Stream.Parse (decodeHtmlEntities)
type RefMap = Map.Map Text Text
toInline :: RefMap -> Text -> [Inline]
toInline refmap t =
case parseOnly (inlineParser refmap) t of
Left s -> [InlineText $ T.pack s]
Right is -> is
inlineParser :: RefMap -> Parser [Inline]
inlineParser = fmap combine . many . inlineAny
combine :: [Inline] -> [Inline]
combine [] = []
combine (InlineText x:InlineText y:rest) = combine (InlineText (x <> y):rest)
combine (InlineText x:rest) = InlineText x : combine rest
combine (InlineItalic x:InlineItalic y:rest) = combine (InlineItalic (x <> y):rest)
combine (InlineItalic x:rest) = InlineItalic (combine x) : combine rest
combine (InlineBold x:InlineBold y:rest) = combine (InlineBold (x <> y):rest)
combine (InlineBold x:rest) = InlineBold (combine x) : combine rest
combine (InlineCode x:InlineCode y:rest) = combine (InlineCode (x <> y):rest)
combine (InlineCode x:rest) = InlineCode x : combine rest
combine (InlineLink u t c:rest) = InlineLink u t (combine c) : combine rest
combine (InlineImage u t c:rest) = InlineImage u t c : combine rest
combine (InlineHtml t:rest) = InlineHtml t : combine rest
combine (InlineFootnote x:rest) = InlineFootnote x : combine rest
combine (InlineFootnoteRef x:rest) = InlineFootnoteRef x : combine rest
specials :: [Char]
specials = "*_`\\[]!<&{}"
inlineAny :: RefMap -> Parser Inline
inlineAny refs =
inline refs <|> special
where
special = InlineText . T.singleton <$> satisfy (`elem` specials)
inline :: RefMap -> Parser Inline
inline refs =
text
<|> escape
<|> footnote
<|> footnoteRef
<|> paired "**" InlineBold <|> paired "__" InlineBold
<|> paired "*" InlineItalic <|> paired "_" InlineItalic
<|> doubleCodeSpace <|> doubleCode <|> code
<|> link
<|> image
<|> autoLink
<|> html
<|> entity
where
inlinesTill :: Text -> Parser [Inline]
inlinesTill end =
go id
where
go front =
(string end *> pure (front []))
<|> (do
x <- inlineAny refs
go $ front . (x:))
text = InlineText <$> takeWhile1 (`notElem` specials)
paired t wrap = wrap <$> do
_ <- string t
is <- inlinesTill t
if null is then fail "wrapped around something missing" else return is
doubleCodeSpace = InlineCode . T.pack <$> (string "`` " *> manyTill anyChar (string " ``"))
doubleCode = InlineCode . T.pack <$> (string "``" *> manyTill anyChar (string "``"))
code = InlineCode <$> (char '`' *> takeWhile1 (/= '`') <* char '`')
footnoteRef = InlineFootnoteRef <$> (char '{' *> decimal <* char '}')
footnote = InlineFootnote <$> (string "{^" *> decimal <* char '}')
escape = InlineText . T.singleton <$>
(char '\\' *> satisfy (`elem` ("\\`*_{}[]()#+-.!>" :: String)))
takeBalancedBrackets =
T.pack <$> go (0 :: Int)
where
go i = do
c <- anyChar
case c of
'[' -> (c:) <$> go (i + 1)
']'
| i == 0 -> return []
| otherwise -> (c:) <$> go (i - 1)
_ -> (c:) <$> go i
parseUrl = fixUrl . T.pack <$> parseUrl' (0 :: Int)
parseUrl' level
| level > 0 = do
c <- anyChar
let level'
| c == ')' = level - 1
| otherwise = level
c' <-
if c == '\\'
then anyChar
else return c
cs <- parseUrl' level'
return $ c' : cs
| otherwise = (do
c <- hrefChar
if c == '('
then (c:) <$> parseUrl' 1
else (c:) <$> parseUrl' 0) <|> return []
parseUrlTitle defRef = parseUrlTitleInline <|> parseUrlTitleRef defRef
parseUrlTitleInside endTitle = do
url <- parseUrl
mtitle <- (Just <$> title) <|> (skipSpace >> endTitle >> pure Nothing)
return (url, mtitle)
where
title = do
_ <- space
skipSpace
_ <- char '"'
t <- T.stripEnd . T.pack <$> go
return $
if not (T.null t) && T.last t == '"'
then T.init t
else t
where
go = (char '\\' *> anyChar >>= \c -> (c:) <$> go)
<|> (endTitle *> return [])
<|> (anyChar >>= \c -> (c:) <$> go)
parseUrlTitleInline = char '(' *> parseUrlTitleInside (char ')')
parseUrlTitleRef defRef = do
ref' <- (skipSpace *> char '[' *> takeWhile (/= ']') <* char ']') <|> return ""
let ref = if T.null ref' then defRef else ref'
case Map.lookup (T.unwords $ T.words ref) refs of
Nothing -> fail "ref not found"
Just t -> either fail return $ parseOnly (parseUrlTitleInside endOfInput) t
link = do
_ <- char '['
rawContent <- takeBalancedBrackets
content <- either fail return $ parseOnly (inlineParser refs) rawContent
(url, mtitle) <- parseUrlTitle rawContent
return $ InlineLink url mtitle content
image = do
_ <- string "!["
content <- takeBalancedBrackets
(url, mtitle) <- parseUrlTitle content
return $ InlineImage url mtitle content
fixUrl t
| T.length t > 2 && T.head t == '<' && T.last t == '>' = T.init $ T.tail t
| otherwise = t
autoLink = do
_ <- char '<'
a <- string "http:" <|> string "https:"
b <- takeWhile1 (/= '>')
_ <- char '>'
let url = a `T.append` b
return $ InlineLink url Nothing [InlineText url]
html = do
c <- char '<'
t <- takeWhile1 (\x -> ('A' <= x && x <= 'Z') || ('a' <= x && x <= 'z') || x == '/')
if T.null t
then fail "invalid tag"
else do
t2 <- takeWhile (/= '>')
c2 <- char '>'
return $ InlineHtml $ T.concat
[ T.singleton c
, t
, t2
, T.singleton c2
]
entity =
rawent "<"
<|> rawent ">"
<|> rawent "&"
<|> rawent """
<|> rawent "'"
<|> decEnt
<|> hexEnt
<|> namedEnt
rawent t = InlineHtml <$> string t
decEnt = do
s <- string "&#"
t <- takeWhile1 $ \x -> ('0' <= x && x <= '9')
c <- char ';'
return $ InlineHtml $ T.concat
[ s
, t
, T.singleton c
]
hexEnt = do
s <- string "&#x" <|> string "&#X"
t <- takeWhile1 $ \x -> ('0' <= x && x <= '9') || ('A' <= x && x <= 'F') || ('a' <= x && x <= 'f')
c <- char ';'
return $ InlineHtml $ T.concat
[ s
, t
, T.singleton c
]
namedEnt = do
_s <- char '&'
t <- takeWhile1 (/= ';')
_c <- char ';'
case decodeHtmlEntities t of
ContentText t' -> return $ InlineHtml t'
ContentEntity _ -> fail "Unknown named entity"
hrefChar :: Parser Char
hrefChar = (char '\\' *> anyChar) <|> satisfy (notInClass " )")
|