git.iko.soy/typeable/pandoc
on master help sign in
Changes to literate haskell options.

- Removed writerLiterateHaskell from WriterOptions. - Removed readerLiterateHaskell from ReaderOptions. - Added Ext_literate_haskell to Extensions. Test for this instead of the above. - Removed failUnlessLHS from Shared. Note: At this point, +lhs and .lhs extension no longer has any effect. Need to fix.

commitdfa4b76630837560189844eb79c83abcb619b0f6 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 8 Aug 2012, 23:18 (14 years ago) parent34f1f97
14 files changed+22−56
benchmark/benchmark-pandoc.hs+3−10 pandoc.cabal+1 src/Text/Pandoc.hs−19 src/Text/Pandoc/Options.hs+5−4 src/Text/Pandoc/Parsing.hs−5 src/Text/Pandoc/Readers/LaTeX.hs+2−2 src/Text/Pandoc/Readers/Markdown.hs+1−1 src/Text/Pandoc/Readers/RST.hs+1−1 src/Text/Pandoc/Writers/HTML.hs+1−1 src/Text/Pandoc/Writers/LaTeX.hs+1−1 src/Text/Pandoc/Writers/Markdown.hs+3−6 src/Text/Pandoc/Writers/RST.hs+1−1 src/pandoc.hs−4 tests/Tests/Readers/Markdown.hs+3−1
benchmark/benchmark-pandoc.hs+3−10
@@ -2,7 +2,6 @@ import Text.Pandoc
22import Text.Pandoc.Shared (readDataFile, normalize)
33import Criterion.Main
44import Criterion.Config
5−import Data.List (isSuffixOf)
65import Text.JSON.Generic
76import System.Environment (getArgs)
87import Data.Monoid
@@ -14,24 +13,18 @@ readerBench doc (name, reader) =
1413 let writer = case lookup name writers of
1514 Just (PureStringWriter w) -> w
1615 _ -> error $ "Could not find writer for " ++ name
17− inp = writer def{ writerWrapText = True
18− , writerLiterateHaskell =
19− "+lhs" `isSuffixOf` name } doc
16+ inp = writer def{ writerWrapText = True } doc
2017 -- we compute the length to force full evaluation
2118 getLength (Pandoc (Meta a b c) d) =
2219 length a + length b + length c + length d
2320 in bench (name ++ " reader") $ whnf (getLength .
24− reader def{ readerSmart = True
25− , readerLiterateHaskell = "+lhs" `isSuffixOf` name
26− }) inp
21+ reader def{ readerSmart = True }) inp
2722
2823writerBench :: Pandoc
2924 -> (String, WriterOptions -> Pandoc -> String)
3025 -> Benchmark
3126writerBench doc (name, writer) = bench (name ++ " writer") $ nf
32− (writer def{
33− writerWrapText = True
34− , writerLiterateHaskell = "+lhs" `isSuffixOf` name }) doc
27+ (writer def{ writerWrapText = True }) doc
3528
3629normalizeBench :: Pandoc -> [Benchmark]
3730normalizeBench doc = [ bench "normalize - with" $ nf (encodeJSON . normalize) doc
pandoc.cabal+1
@@ -392,6 +392,7 @@ Test-Suite test-pandoc
392392 QuickCheck >= 2.4 && < 2.6,
393393 HUnit >= 1.2 && < 1.3,
394394 template-haskell >= 2.4 && < 2.8,
395+ containers >= 0.1 && < 0.5,
395396 ansi-terminal == 0.5.*
396397 Other-Modules: Tests.Old
397398 Tests.Helpers
src/Text/Pandoc.hs−19
@@ -149,17 +149,11 @@ readers :: [(String, ReaderOptions -> String -> Pandoc)]
149149readers = [("native" , \_ -> readNative)
150150 ,("json" , \_ -> decodeJSON)
151151 ,("markdown" , readMarkdown)
152− ,("markdown+lhs" , \opt ->
153− readMarkdown opt{ readerLiterateHaskell = True })
154152 ,("rst" , readRST)
155− ,("rst+lhs" , \opt ->
156− readRST opt{ readerLiterateHaskell = True })
157153 ,("docbook" , readDocBook)
158154 ,("textile" , readTextile) -- TODO : textile+lhs
159155 ,("html" , readHtml)
160156 ,("latex" , readLaTeX)
161− ,("latex+lhs" , \opt ->
162− readLaTeX opt{ readerLiterateHaskell = True })
163157 ]
164158
165159data Writer = PureStringWriter (WriterOptions -> Pandoc -> String)
@@ -178,11 +172,6 @@ writers = [
178172 ,("html" , PureStringWriter writeHtmlString)
179173 ,("html5" , PureStringWriter $ \o ->
180174 writeHtmlString o{ writerHtml5 = True })
181− ,("html+lhs" , PureStringWriter $ \o ->
182− writeHtmlString o{ writerLiterateHaskell = True })
183− ,("html5+lhs" , PureStringWriter $ \o ->
184− writeHtmlString o{ writerLiterateHaskell = True,
185− writerHtml5 = True })
186175 ,("s5" , PureStringWriter $ \o ->
187176 writeHtmlString o{ writerSlideVariant = S5Slides
188177 , writerTableOfContents = False })
@@ -196,22 +185,14 @@ writers = [
196185 ,("docbook" , PureStringWriter writeDocbook)
197186 ,("opendocument" , PureStringWriter writeOpenDocument)
198187 ,("latex" , PureStringWriter writeLaTeX)
199− ,("latex+lhs" , PureStringWriter $ \o ->
200− writeLaTeX o{ writerLiterateHaskell = True })
201188 ,("beamer" , PureStringWriter $ \o ->
202189 writeLaTeX o{ writerBeamer = True })
203− ,("beamer+lhs" , PureStringWriter $ \o ->
204− writeLaTeX o{ writerBeamer = True, writerLiterateHaskell = True })
205190 ,("context" , PureStringWriter writeConTeXt)
206191 ,("texinfo" , PureStringWriter writeTexinfo)
207192 ,("man" , PureStringWriter writeMan)
208193 ,("markdown" , PureStringWriter writeMarkdown)
209− ,("markdown+lhs" , PureStringWriter $ \o ->
210− writeMarkdown o{ writerLiterateHaskell = True })
211194 ,("plain" , PureStringWriter writePlain)
212195 ,("rst" , PureStringWriter writeRST)
213− ,("rst+lhs" , PureStringWriter $ \o ->
214− writeRST o{ writerLiterateHaskell = True })
215196 ,("mediawiki" , PureStringWriter writeMediaWiki)
216197 ,("textile" , PureStringWriter writeTextile)
217198 ,("rtf" , PureStringWriter writeRTF)
src/Text/Pandoc/Options.hs+5−4
@@ -38,6 +38,7 @@ module Text.Pandoc.Options ( Extension(..)
3838 , HTMLSlideVariant (..)
3939 , WriterOptions (..)
4040 , def
41+ , isEnabled
4142 ) where
4243import Data.Set (Set)
4344import qualified Data.Set as Set
@@ -77,6 +78,7 @@ data Extension = Ext_footnotes
7778 | Ext_superscript
7879 | Ext_subscript
7980 | Ext_hard_line_breaks
81+ | Ext_literate_haskell
8082 deriving (Show, Read, Enum, Eq, Ord, Bounded)
8183
8284pandocExtensions :: Set Extension
@@ -128,7 +130,6 @@ data ReaderOptions = ReaderOptions{
128130 , readerOldDashes :: Bool -- ^ Use pandoc <= 1.8.2.1 behavior
129131 -- in parsing dashes; -- is em-dash;
130132 -- - before numerial is en-dash
131− , readerLiterateHaskell :: Bool -- ^ Interpret as literate Haskell
132133 , readerCitations :: [String] -- ^ List of available citations
133134 , readerApplyMacros :: Bool -- ^ Apply macros to TeX math
134135 , readerIndentedCodeClasses :: [String] -- ^ Default classes for
@@ -145,7 +146,6 @@ instance Default ReaderOptions
145146 , readerColumns = 80
146147 , readerTabStop = 4
147148 , readerOldDashes = False
148− , readerLiterateHaskell = False
149149 , readerCitations = []
150150 , readerApplyMacros = True
151151 , readerIndentedCodeClasses = []
@@ -201,7 +201,6 @@ data WriterOptions = WriterOptions
201201 , writerReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
202202 , writerWrapText :: Bool -- ^ Wrap text to line length
203203 , writerColumns :: Int -- ^ Characters in a line (for text wrapping)
204− , writerLiterateHaskell :: Bool -- ^ Write as literate haskell
205204 , writerEmailObfuscation :: ObfuscationMethod -- ^ How to obfuscate emails
206205 , writerIdentifierPrefix :: String -- ^ Prefix for section & note ids in HTML
207206 , writerSourceDirectory :: FilePath -- ^ Directory path of 1st source file
@@ -240,7 +239,6 @@ instance Default WriterOptions where
240239 , writerReferenceLinks = False
241240 , writerWrapText = True
242241 , writerColumns = 72
243− , writerLiterateHaskell = False
244242 , writerEmailObfuscation = JavascriptObfuscation
245243 , writerIdentifierPrefix = ""
246244 , writerSourceDirectory = "."
@@ -262,3 +260,6 @@ instance Default WriterOptions where
262260 , writerReferenceDocx = Nothing
263261 }
264262
263+-- | Returns True if the given extension is enabled.
264+isEnabled :: Extension -> WriterOptions -> Bool
265+isEnabled ext opts = ext `Set.member` (writerExtensions opts)
src/Text/Pandoc/Parsing.hs−5
@@ -49,7 +49,6 @@ module Text.Pandoc.Parsing ( (>>~),
4949 withHorizDisplacement,
5050 withRaw,
5151 nullBlock,
52− failUnlessLHS,
5352 escaped,
5453 characterReference,
5554 updateLastStrPos,
@@ -421,10 +420,6 @@ withRaw parser = do
421420nullBlock :: Parser [Char] st Block
422421nullBlock = anyChar >> return Null
423422
424−-- | Fail unless we're in literate haskell mode.
425−failUnlessLHS :: Parser [tok] ParserState ()
426−failUnlessLHS = getOption readerLiterateHaskell >>= guard
427−
428423-- | Parses backslash, then applies character parser.
429424escaped :: Parser [Char] st Char -- ^ Parser for character to escape
430425 -> Parser [Char] st Char
src/Text/Pandoc/Readers/LaTeX.hs+2−2
@@ -187,7 +187,7 @@ inline = (mempty <$ comment)
187187 <|> (mathInline $ char '$' *> mathChars <* char '$')
188188 <|> (superscript <$> (char '^' *> tok))
189189 <|> (subscript <$> (char '_' *> tok))
190− <|> (failUnlessLHS *> char '|' *> doLHSverb)
190+ <|> (guardEnabled Ext_literate_haskell *> char '|' *> doLHSverb)
191191 <|> (str <$> count 1 tildeEscape)
192192 <|> (str <$> string "]")
193193 <|> (str <$> string "#") -- TODO print warning?
@@ -737,7 +737,7 @@ environments = M.fromList
737737 , ("itemize", bulletList <$> listenv "itemize" (many item))
738738 , ("description", definitionList <$> listenv "description" (many descItem))
739739 , ("enumerate", ordered_list)
740− , ("code", failUnlessLHS *>
740+ , ("code", guardEnabled Ext_literate_haskell *>
741741 (codeBlockWith ("",["sourceCode","literate","haskell"],[]) <$>
742742 verbEnv "code"))
743743 , ("verbatim", codeBlock <$> (verbEnv "verbatim"))
src/Text/Pandoc/Readers/Markdown.hs+1−1
@@ -422,7 +422,7 @@ codeBlockIndented = do
422422
423423lhsCodeBlock :: Parser [Char] ParserState (F Blocks)
424424lhsCodeBlock = do
425− failUnlessLHS
425+ guardEnabled Ext_literate_haskell
426426 (return . B.codeBlockWith ("",["sourceCode","literate","haskell"],[]) <$>
427427 (lhsCodeBlockBird <|> lhsCodeBlockLaTeX))
428428 <|> (return . B.codeBlockWith ("",["sourceCode","haskell"],[]) <$>
src/Text/Pandoc/Readers/RST.hs+1−1
@@ -407,7 +407,7 @@ mathBlockMultiline = try $ do
407407
408408lhsCodeBlock :: Parser [Char] ParserState Block
409409lhsCodeBlock = try $ do
410− getOption readerLiterateHaskell >>= guard
410+ guardEnabled Ext_literate_haskell
411411 optional codeBlockStart
412412 pos <- getPosition
413413 when (sourceColumn pos /= 1) $ fail "Not in first column"
src/Text/Pandoc/Writers/HTML.hs+1−1
@@ -397,7 +397,7 @@ blockToHtml _ (RawBlock "html" str) = return $ preEscapedString str
397397blockToHtml _ (RawBlock _ _) = return mempty
398398blockToHtml opts (HorizontalRule) = return $ if writerHtml5 opts then H5.hr else H.hr
399399blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
400− let tolhs = writerLiterateHaskell opts &&
400+ let tolhs = isEnabled Ext_literate_haskell opts &&
401401 any (\c -> map toLower c == "haskell") classes &&
402402 any (\c -> map toLower c == "literate") classes
403403 classes' = if tolhs
src/Text/Pandoc/Writers/LaTeX.hs+1−1
@@ -290,7 +290,7 @@ blockToLaTeX (BlockQuote lst) = do
290290blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
291291 opts <- gets stOptions
292292 case () of
293− _ | writerLiterateHaskell opts && "haskell" `elem` classes &&
293+ _ | isEnabled Ext_literate_haskell opts && "haskell" `elem` classes &&
294294 "literate" `elem` classes -> lhsCodeBlock
295295 | writerListings opts -> listingsCodeBlock
296296 | writerHighlight opts && not (null classes) -> highlightedCodeBlock
src/Text/Pandoc/Writers/Markdown.hs+3−6
@@ -49,9 +49,6 @@ data WriterState = WriterState { stNotes :: Notes
4949 , stRefs :: Refs
5050 , stPlain :: Bool }
5151
52−isEnabled :: Extension -> WriterOptions -> Bool
53−isEnabled ext opts = ext `Set.member` (writerExtensions opts)
54−
5552-- | Convert Pandoc to Markdown.
5653writeMarkdown :: WriterOptions -> Pandoc -> String
5754writeMarkdown opts document =
@@ -255,12 +252,12 @@ blockToMarkdown opts (Header level inlines) = do
255252 contents <> cr <> text (replicate (offset contents) '-') <>
256253 blankline
257254 -- ghc interprets '#' characters in column 1 as linenum specifiers.
258− _ | stPlain st || writerLiterateHaskell opts ->
255+ _ | stPlain st || isEnabled Ext_literate_haskell opts ->
259256 contents <> blankline
260257 _ -> text (replicate level '#') <> space <> contents <> blankline
261258blockToMarkdown opts (CodeBlock (_,classes,_) str)
262259 | "haskell" `elem` classes && "literate" `elem` classes &&
263− writerLiterateHaskell opts =
260+ isEnabled Ext_literate_haskell opts =
264261 return $ prefixed "> " (text str) <> blankline
265262blockToMarkdown opts (CodeBlock attribs str) = return $
266263 if isEnabled Ext_delimited_code_blocks opts && attribs /= nullAttr
@@ -274,7 +271,7 @@ blockToMarkdown opts (BlockQuote blocks) = do
274271 st <- get
275272 -- if we're writing literate haskell, put a space before the bird tracks
276273 -- so they won't be interpreted as lhs...
277− let leader = if writerLiterateHaskell opts
274+ let leader = if isEnabled Ext_literate_haskell opts
278275 then " > "
279276 else if stPlain st
280277 then " "
src/Text/Pandoc/Writers/RST.hs+1−1
@@ -164,7 +164,7 @@ blockToRST (CodeBlock (_,classes,_) str) = do
164164 opts <- stOptions <$> get
165165 let tabstop = writerTabStop opts
166166 if "haskell" `elem` classes && "literate" `elem` classes &&
167− writerLiterateHaskell opts
167+ isEnabled Ext_literate_haskell opts
168168 then return $ prefixed "> " (text str) $$ blankline
169169 else return $ "::" $+$ nest tabstop (text str) $$ blankline
170170blockToRST (BlockQuote blocks) = do
src/pandoc.hs−4
@@ -980,9 +980,6 @@ main = do
980980 , readerColumns = columns
981981 , readerTabStop = tabStop
982982 , readerOldDashes = oldDashes
983− , readerLiterateHaskell =
984− "+lhs" `isSuffixOf` readerName' ||
985− lhsExtension sources
986983 , readerCitations = map CSL.refId refs
987984 , readerIndentedCodeClasses = codeBlockClasses
988985 , readerApplyMacros = not laTeXOutput
@@ -1005,7 +1002,6 @@ main = do
10051002 writerReferenceLinks = referenceLinks,
10061003 writerWrapText = wrap,
10071004 writerColumns = columns,
1008− writerLiterateHaskell = False,
10091005 writerEmailObfuscation = if strict
10101006 then ReferenceObfuscation
10111007 else obfuscationMethod,
tests/Tests/Readers/Markdown.hs+3−1
@@ -6,6 +6,7 @@ import Test.Framework
66import Tests.Helpers
77import Tests.Arbitrary()
88import Text.Pandoc.Builder
9+import qualified Data.Set as Set
910-- import Text.Pandoc.Shared ( normalize )
1011import Text.Pandoc
1112
@@ -91,7 +92,8 @@ tests = [ testGroup "inline code"
9192 =?> para (note (para "See [^1]"))
9293 ]
9394 , testGroup "lhs"
94− [ test (readMarkdown def{ readerLiterateHaskell = True })
95+ [ test (readMarkdown def{ readerExtensions = Set.insert
96+ Ext_literate_haskell $ readerExtensions def })
9597 "inverse bird tracks and html" $
9698 "> a\n\n< b\n\n<div>\n"
9799 =?> codeBlockWith ("",["sourceCode","literate","haskell"],[]) "a"