git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #808 from davidlazar/master

Improvements to Haddock reader

commit5aec167e71b3da98a0f209e3c75af019682ad354 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 30 Mar 2013, 09:16 (14 years ago) parent7cb8b608663667
3 files changed+16−12
src/Text/Pandoc/Readers/Haddock.hs+4−2 src/Text/Pandoc/Readers/Haddock/Lex.x+5−3 src/Text/Pandoc/Readers/Haddock/Parse.y+7−7
src/Text/Pandoc/Readers/Haddock.hs+4−2
@@ -24,8 +24,10 @@ readHaddock :: ReaderOptions -- ^ Reader options
2424readHaddock _ s = Pandoc (Meta [] [] []) blocks
2525 where
2626 blocks = case parseParas (tokenise s (0,0)) of
27− Nothing -> []
28− Just x -> mergeLists (toList x)
27+ Left [] -> error "parse failure"
28+ Left (tok:_) -> error $ "parse failure " ++ pos (tokenPos tok)
29+ where pos (l, c) = "(line " ++ show l ++ ", column " ++ show c ++ ")"
30+ Right x -> mergeLists (toList x)
2931
3032-- similar to 'docAppend' in Haddock.Doc
3133mergeLists :: [Block] -> [Block]
src/Text/Pandoc/Readers/Haddock/Lex.x+5−3
@@ -19,7 +19,8 @@
1919module Text.Pandoc.Readers.Haddock.Lex (
2020 Token(..),
2121 LToken,
22− tokenise
22+ tokenise,
23+ tokenPos
2324 ) where
2425
2526import Data.Char
@@ -54,14 +55,15 @@ $ident = [$alphanum \'\_\.\!\#\$\%\&\*\+\/\<\=\>\?\@\\\\\^\|\-\~\:]
5455<line> {
5556 $ws* \> { begin birdtrack }
5657 $ws* \>\>\> { strtoken TokExamplePrompt `andBegin` exampleexpr }
57− $ws* \n { token TokPara `andBegin` para }
5858
59− -- Here, we really want to be able to say
59+ $ws* \n { token TokPara `andBegin` para }
60+ -- ^ Here, we really want to be able to say
6061 -- $ws* (\n | <eof>) { token TokPara `andBegin` para}
6162 -- because otherwise a trailing line of whitespace will result in
6263 -- a spurious TokString at the end of a docstring. We don't have <eof>,
6364 -- though (NOW I realise what it was for :-). To get around this, we always
6465 -- append \n to the end of a docstring.
66+
6567 () { begin string }
6668}
6769
src/Text/Pandoc/Readers/Haddock/Parse.y+7−7
@@ -46,7 +46,7 @@ import Data.Sequence (viewr, ViewR(..))
4646 PARA { (TokPara,_) }
4747 STRING { (TokString $$,_) }
4848
49−%monad { Maybe }
49+%monad { Either [LToken] }
5050
5151%name parseParas doc
5252%name parseString seq
@@ -76,7 +76,7 @@ defpara :: { (Inlines, [Blocks]) }
7676
7777para :: { Blocks }
7878 : seq { para' $1 }
79− | codepara { codeBlock $1 }
79+ | codepara { codeBlockWith ([], ["haskell"], []) $1 }
8080 | property { $1 }
8181 | examples { $1 }
8282
@@ -118,16 +118,16 @@ elem1 :: { Inlines }
118118 | URL { makeHyperlink $1 }
119119 | PIC { image $1 $1 mempty }
120120 | ANAME { mempty } -- TODO
121− | IDENT { code $1 }
122− | DQUO strings DQUO { code $2 }
121+ | IDENT { codeWith ([], ["haskell"], []) $1 }
122+ | DQUO strings DQUO { codeWith ([], ["haskell"], []) $2 }
123123
124124strings :: { String }
125125 : STRING { $1 }
126126 | STRING strings { $1 ++ $2 }
127127
128128{
129−happyError :: [LToken] -> Maybe a
130−happyError toks = Nothing
129+happyError :: [LToken] -> Either [LToken] a
130+happyError toks = Left toks
131131
132132para' :: Inlines -> Blocks
133133para' (Many ils) =
@@ -162,7 +162,7 @@ makeProperty s = case strip s of
162162-- | Create an 'Example', stripping superfluous characters as appropriate
163163makeExample :: String -> String -> [String] -> Blocks
164164makeExample prompt expression result =
165− para $ codeWith ([], ["expr"], []) (strip expression ++ "\n")
165+ para $ codeWith ([], ["haskell", "expr"], []) (strip expression ++ "\n")
166166 <> codeWith ([], ["result"], []) (unlines result')
167167 where
168168 -- 1. drop trailing whitespace from the prompt, remember the prefix