{-# LANGUAGE NoImplicitPrelude #-}
module Text.Pandoc.Readers.Docx.Fields ( FieldInfo(..)
, parseFieldInfo
) where
import Prelude
import Text.Parsec
import Text.Parsec.String (Parser)
type URL = String
data FieldInfo = HyperlinkField URL
| UnknownField
deriving (Show)
parseFieldInfo :: String -> Either ParseError FieldInfo
parseFieldInfo = parse fieldInfo ""
fieldInfo :: Parser FieldInfo
fieldInfo =
try (HyperlinkField <$> hyperlink)
<|>
return UnknownField
escapedQuote :: Parser String
escapedQuote = string "\\\""
inQuotes :: Parser String
inQuotes =
(try escapedQuote) <|> (anyChar >>= (\c -> return [c]))
quotedString :: Parser String
quotedString = do
char '"'
concat <$> manyTill inQuotes (try (char '"'))
unquotedString :: Parser String
unquotedString = manyTill anyChar (try $ lookAhead space *> return () <|> eof)
fieldArgument :: Parser String
fieldArgument = quotedString <|> unquotedString
hyperlinkSwitch :: Parser (String, String)
hyperlinkSwitch = do
sw <- string "\\l"
spaces
farg <- fieldArgument
return (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