@@ -0,0 +1,179 @@
1+-- This code was copied from the 'haddock' package, modified, and integrated
2+-- into Pandoc by David Lazar.
4+{-# LANGUAGE BangPatterns #-} -- required for versions of Happy before 1.18.6
5+{-# OPTIONS -Wwarn -w #-}
6+-- The above warning supression flag is a temporary kludge.
7+-- While working on this module you are encouraged to remove it and fix
8+-- any warnings in the module. See
9+-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
12+module Text.Pandoc.Readers.Haddock.Parse (parseString, parseParas) where
14+import Text.Pandoc.Readers.Haddock.Lex
15+import Text.Pandoc.Builder
16+import Data.Generics (everywhere, mkT)
17+import Data.Char (isSpace)
18+import Data.Maybe (fromMaybe)
19+import Data.List (stripPrefix)
20+import Data.Monoid (mempty)
28+ '/' { (TokSpecial '/',_) }
29+ '@' { (TokSpecial '@',_) }
30+ '[' { (TokDefStart,_) }
31+ ']' { (TokDefEnd,_) }
32+ DQUO { (TokSpecial '\"',_) }
33+ URL { (TokURL $$,_) }
34+ PIC { (TokPic $$,_) }
35+ ANAME { (TokAName $$,_) }
36+ '/../' { (TokEmphasis $$,_) }
37+ '-' { (TokBullet,_) }
38+ '(n)' { (TokNumber,_) }
39+ '>..' { (TokBirdTrack $$,_) }
40+ PROP { (TokProperty $$,_) }
41+ PROMPT { (TokExamplePrompt $$,_) }
42+ RESULT { (TokExampleResult $$,_) }
43+ EXP { (TokExampleExpression $$,_) }
44+ IDENT { (TokIdent $$,_) }
46+ STRING { (TokString $$,_) }
56+ : apara PARA doc { $1 <> $3 }
59+ | {- empty -} { mempty }
62+ : ulpara { bulletList [$1] }
63+ | olpara { orderedList [$1] }
64+ | defpara { definitionList [$1] }
73+defpara :: { (Inlines, [Blocks]) }
74+ : '[' seq ']' seq { ($2, [plain $4]) }
78+ | codepara { codeBlock $1 }
82+codepara :: { String }
83+ : '>..' codepara { $1 ++ $2 }
86+property :: { Blocks }
87+ : PROP { makeProperty $1 }
89+examples :: { Blocks }
90+ : example examples { $1 <> $2 }
94+ : PROMPT EXP result { makeExample $1 $2 (lines $3) }
95+ | PROMPT EXP { makeExample $1 $2 [] }
98+ : RESULT result { $1 ++ $2 }
102+ : elem seq { $1 <> $2 }
107+ | '@' seq1 '@' { monospace $2 }
110+ : PARA seq1 { linebreak <> $2 }
111+ | elem1 seq1 { $1 <> $2 }
116+ | '/../' { emph (str $1) }
117+ | URL { makeHyperlink $1 }
118+ | PIC { image $1 $1 mempty }
119+ | ANAME { mempty } -- TODO
121+ | DQUO strings DQUO { code $2 }
123+strings :: { String }
125+ | STRING strings { $1 ++ $2 }
128+happyError :: [LToken] -> Maybe a
129+happyError toks = Nothing
131+monospace :: Inlines -> Inlines
132+monospace = everywhere (mkT go)
134+ go (Str s) = Code nullAttr s
135+ go Space = Code nullAttr " "
138+-- | Create a `Hyperlink` from given string.
140+-- A hyperlink consists of a URL and an optional label. The label is separated
141+-- from the url by one or more whitespace characters.
142+makeHyperlink :: String -> Inlines
143+makeHyperlink input = case break isSpace $ strip input of
144+ (url, "") -> link url url (str url)
145+ (url, lb) -> link url url (str label)
146+ where label = dropWhile isSpace lb
148+makeProperty :: String -> Blocks
149+makeProperty s = case strip s of
150+ 'p':'r':'o':'p':'>':xs ->
151+ codeBlockWith ([], ["property"], []) (dropWhile isSpace xs)
153+ error $ "makeProperty: invalid input " ++ show xs
155+-- | Create an 'Example', stripping superfluous characters as appropriate
156+makeExample :: String -> String -> [String] -> Blocks
157+makeExample prompt expression result =
158+ para $ codeWith ([], ["expr"], []) (strip expression ++ "\n")
159+ <> codeWith ([], ["result"], []) (unlines result')
161+ -- 1. drop trailing whitespace from the prompt, remember the prefix
162+ (prefix, _) = span isSpace prompt
164+ -- 2. drop, if possible, the exact same sequence of whitespace
165+ -- characters from each result line
167+ -- 3. interpret lines that only contain the string "<BLANKLINE>" as an
169+ result' = map (substituteBlankLine . tryStripPrefix prefix) result
171+ tryStripPrefix xs ys = fromMaybe ys $ stripPrefix xs ys
173+ substituteBlankLine "<BLANKLINE>" = ""
174+ substituteBlankLine line = line
176+-- | Remove all leading and trailing whitespace
177+strip :: String -> String
178+strip = dropWhile isSpace . reverse . dropWhile isSpace . reverse