Added pAuthorLine and tests

master
Levi Pearson 2014-02-27 23:59:58 -07:00
parent 81fa9981d7
commit 89a146a36c
2 changed files with 91 additions and 6 deletions

View File

@ -20,18 +20,18 @@ data Document = Doc
, _header :: Maybe DocHeader , _header :: Maybe DocHeader
, _preamble :: Maybe SectionBody , _preamble :: Maybe SectionBody
, _sections :: Seq Section , _sections :: Seq Section
} deriving (Show) } deriving (Show, Eq)
data DocHeader = DocHead data DocHeader = DocHead
{ _docTitle :: Text { _docTitle :: Text
} deriving (Show) } deriving (Show, Eq)
data Section = Sec data Section = Sec
{ _secAttrs :: Attributes { _secAttrs :: Attributes
, _secTitle :: Text , _secTitle :: Text
, _secBody :: Maybe SectionBody , _secBody :: Maybe SectionBody
, _secChildren :: Seq Section , _secChildren :: Seq Section
} deriving (Show) } deriving (Show, Eq)
type SectionBody = Seq Block type SectionBody = Seq Block
@ -39,15 +39,33 @@ data Block = Block
{ _blockKind :: BlockKind { _blockKind :: BlockKind
, _blockAttrs :: Attributes , _blockAttrs :: Attributes
, _blockTitle :: Maybe Text , _blockTitle :: Maybe Text
} deriving (Show) } deriving (Show, Eq)
data BlockKind = Paragraph data BlockKind = Paragraph
| DelimBlock | DelimBlock
| List | List
deriving (Show) deriving (Show, Eq)
data Inlines = TextChunk Text data Inlines = TextChunk Text
deriving (Show) deriving (Show, Eq)
data AuthorInfo = Author
{ _firstName :: [Text]
, _middleName :: Maybe [Text]
, _lastName :: Maybe [Text]
, _email :: Maybe Text
} deriving (Show, Eq)
data RevisionInfo = Rev
{ _revNum :: Maybe Text
, _revDate :: Maybe Text
, _revRemark :: Maybe Text
} deriving (Show, Eq)
data AttrEntry = Attr
{ _attName :: Text
, _attVal :: Text
} deriving (Show, Eq)
pCountedChar :: Char -> Parser Int pCountedChar :: Char -> Parser Int
pCountedChar c = ((+) <$> (char c >> pure 1) <*> pCountedChar c) pCountedChar c = ((+) <$> (char c >> pure 1) <*> pCountedChar c)
@ -93,3 +111,30 @@ titleCharLevel c = case c of
'+' -> return 4 '+' -> return 4
_ -> fail "Bad title underline character" _ -> fail "Bad title underline character"
pAuthorLine :: Parser AuthorInfo
pAuthorLine = do
author <- collapseSp <$> takeTill (\c -> c == '<' || isEndOfLine c)
let (f, m, l) = case T.words author of
(frst : mid : lst : []) ->
(T.splitOn "_" frst, Just $ T.splitOn "_" mid, Just $ T.splitOn "_" lst)
(frst : lst : []) ->
(T.splitOn "_" frst, Nothing, Just $ T.splitOn "_" lst)
(frst : []) ->
(T.splitOn "_" frst, Nothing, Nothing)
_ -> ([author], Nothing, Nothing)
email <- do char '<'
em <- takeTill (== '>')
char '>'
return (Just em)
<|> return Nothing
satisfy isEndOfLine
return (Author f m l email)
pRevInfo :: Parser RevisionInfo
pRevInfo = pStdRevInfo <|> pRCSInfo <|> pure (Rev Nothing Nothing Nothing)
where
pStdRevInfo = fail "NotImpl"
pRCSInfo = fail "NotImpl"
pAttributeEntry :: Parser AttrEntry
pAttributeEntry = undefined

View File

@ -92,3 +92,43 @@ headerParseSpec = do
parseOnly pTitle "A two-line level 4 title\n++\n" parseOnly pTitle "A two-line level 4 title\n++\n"
`shouldBe` Right (4, "A two-line level 4 title") `shouldBe` Right (4, "A two-line level 4 title")
describe "Document Author" $ do
describe "pAuthorLine" $ do
it "parses a line with just a first name" $
parseOnly pAuthorLine "Joe\n"
`shouldBe` Right (Author ["Joe"] Nothing Nothing Nothing)
it "parses a line with a two-word first name" $
parseOnly pAuthorLine "Joe_Dee\n"
`shouldBe` Right (Author ["Joe", "Dee"] Nothing Nothing Nothing)
it "parses a line with a first and last name" $
parseOnly pAuthorLine "Joe Bloggs\n"
`shouldBe` Right (Author ["Joe"] Nothing (Just ["Bloggs"]) Nothing)
it "parses a line with two first and two last names" $
parseOnly pAuthorLine "Joe_Dee de_Winter\n"
`shouldBe`
Right (Author ["Joe", "Dee"] Nothing (Just ["de", "Winter"]) Nothing)
it "parses a first, middle, and last name" $
parseOnly pAuthorLine "Joe Jay Bloggs\n"
`shouldBe`
Right (Author ["Joe"] (Just ["Jay"]) (Just ["Bloggs"]) Nothing)
it "parses multiple first, middle, and last names" $
parseOnly pAuthorLine "Joe_Dee Dee_Ann de_Winter\n"
`shouldBe`
Right (Author ["Joe", "Dee"]
(Just ["Dee", "Ann"])
(Just ["de", "Winter"]) Nothing)
it "parses first name and email address" $
parseOnly pAuthorLine "Joe <JoeBloggs@mail.com>\n"
`shouldBe`
Right (Author ["Joe"] Nothing Nothing (Just "JoeBloggs@mail.com"))
it "parses first and last names and email address" $
parseOnly pAuthorLine "Joe Bloggs <JoeBloggs@mail.com>\n"
`shouldBe`
Right (Author ["Joe"] Nothing (Just ["Bloggs"]) (Just "JoeBloggs@mail.com"))
it "Parses first, middle and last names and email address" $
parseOnly pAuthorLine "Joe Jay Bloggs <JoeBloggs@mail.com>\n"
`shouldBe`
Right (Author ["Joe"]
(Just ["Jay"])
(Just ["Bloggs"]) (Just "JoeBloggs@mail.com"))