{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Readers.Docx.Fields ( FieldInfo(..)
, parseFieldInfo
) where
import Data.Functor (($>), void)
import qualified Data.Text as T
import Text.Parsec
import Text.Parsec.Text (Parser)
type URL = T.Text
data FieldInfo = HyperlinkField URL
| UnknownField
deriving (Show)
parseFieldInfo :: T.Text -> Either ParseError FieldInfo
parseFieldInfo = parse fieldInfo ""
fieldInfo :: Parser FieldInfo
fieldInfo =
try (HyperlinkField <$> hyperlink)
<|>
return UnknownField
escapedQuote :: Parser T.Text
escapedQuote = string "\\\"" $> "\\\""
inQuotes :: Parser T.Text
inQuotes =
try escapedQuote <|> (T.singleton <$> anyChar)
quotedString :: Parser T.Text
quotedString = do
char '"'
T.concat <$> manyTill inQuotes (try (char '"'))
unquotedString :: Parser T.Text
unquotedString = T.pack <$> manyTill anyChar (try $ void (lookAhead space) <|> eof)
fieldArgument :: Parser T.Text
fieldArgument = quotedString <|> unquotedString
hyperlinkSwitch :: Parser (T.Text, T.Text)
hyperlinkSwitch = do
sw <- string "\\l"
spaces
farg <- fieldArgument
return (T.pack sw, farg)
hyperlink :: Parser URL
hyperlink = do
many space
string "HYPERLINK"
spaces
farg <- fieldArgument
switches <- spaces *> many hyperlinkSwitch
let url = case switches of
("\\l", s) : _ -> farg <> "#" <> s
_ -> farg
return url