2018-03-18 19:45:25 +01:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2017-12-30 23:25:38 +01:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
2014-05-08 21:50:20 +02:00
|
|
|
{-
|
2019-02-04 22:52:31 +01:00
|
|
|
Copyright (C) 2012-2019 John MacFarlane <jgm@berkeley.edu>
|
2014-05-08 21:50:20 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
-}
|
2018-03-18 19:45:25 +01:00
|
|
|
import Prelude
|
2010-12-11 08:35:31 +01:00
|
|
|
import Text.Pandoc
|
2018-10-14 08:10:27 +02:00
|
|
|
import Text.Pandoc.Error (PandocError(..))
|
|
|
|
import Control.Monad.Except (throwError)
|
2017-06-10 20:57:38 +02:00
|
|
|
import qualified Text.Pandoc.UTF8 as UTF8
|
2017-01-27 11:15:45 +01:00
|
|
|
import qualified Data.ByteString as B
|
2010-12-11 08:35:31 +01:00
|
|
|
import Criterion.Main
|
2015-10-10 01:01:08 +02:00
|
|
|
import Criterion.Types (Config(..))
|
2017-12-30 23:25:38 +01:00
|
|
|
import Data.List (intersect)
|
2014-08-01 13:32:13 +02:00
|
|
|
import Data.Maybe (mapMaybe)
|
2017-01-27 11:29:26 +01:00
|
|
|
import System.Environment (getArgs)
|
2010-12-11 08:35:31 +01:00
|
|
|
|
|
|
|
readerBench :: Pandoc
|
2017-12-30 23:25:38 +01:00
|
|
|
-> String
|
2014-08-01 13:32:13 +02:00
|
|
|
-> Maybe Benchmark
|
2017-12-30 23:25:38 +01:00
|
|
|
readerBench doc name =
|
|
|
|
case res of
|
|
|
|
Right (readerFun, inp) ->
|
|
|
|
Just $ bench (name ++ " reader")
|
|
|
|
$ nf (\i -> either (error . show) id $ runPure (readerFun i))
|
|
|
|
inp
|
|
|
|
Left _ -> Nothing
|
2018-10-14 08:10:27 +02:00
|
|
|
where res = runPure $
|
|
|
|
case (getReader name, getWriter name) of
|
|
|
|
(Right (TextReader r, rexts),
|
|
|
|
Right (TextWriter w, wexts)) -> do
|
|
|
|
inp <- w def{ writerWrapText = WrapAuto
|
|
|
|
, writerExtensions = wexts } doc
|
|
|
|
return $ (r def{ readerExtensions = rexts }, inp)
|
|
|
|
_ -> throwError $ PandocSomeError
|
|
|
|
$ "could not get text reader and writer for " ++ name
|
2010-12-11 08:35:31 +01:00
|
|
|
|
|
|
|
writerBench :: Pandoc
|
2017-12-30 23:25:38 +01:00
|
|
|
-> String
|
|
|
|
-> Maybe Benchmark
|
|
|
|
writerBench doc name =
|
|
|
|
case res of
|
|
|
|
Right writerFun ->
|
|
|
|
Just $ bench (name ++ " writer")
|
|
|
|
$ nf (\d -> either (error . show) id $
|
|
|
|
runPure (writerFun d)) doc
|
2018-10-14 08:10:27 +02:00
|
|
|
Left _ -> Nothing
|
2017-12-30 23:25:38 +01:00
|
|
|
where res = runPure $ do
|
2018-10-14 08:10:27 +02:00
|
|
|
case (getWriter name) of
|
|
|
|
Right (TextWriter w, wexts) ->
|
|
|
|
return $ w def{ writerExtensions = wexts }
|
|
|
|
_ -> throwError $ PandocSomeError
|
|
|
|
$ "could not get text reader and writer for " ++ name
|
2010-12-11 08:35:31 +01:00
|
|
|
|
2012-07-26 19:02:00 +02:00
|
|
|
main :: IO ()
|
2010-12-11 08:35:31 +01:00
|
|
|
main = do
|
2017-12-30 23:25:38 +01:00
|
|
|
args <- filter (\x -> take 1 x /= "-") <$> getArgs
|
2017-12-30 20:22:55 +01:00
|
|
|
print args
|
2017-06-10 20:57:38 +02:00
|
|
|
let matchReader (n, TextReader _) =
|
2017-12-30 23:25:38 +01:00
|
|
|
null args || ("reader" `elem` args && n `elem` args)
|
2017-12-30 20:22:55 +01:00
|
|
|
matchReader _ = False
|
2017-06-10 23:39:49 +02:00
|
|
|
let matchWriter (n, TextWriter _) =
|
2017-12-30 23:25:38 +01:00
|
|
|
null args || ("writer" `elem` args && n `elem` args)
|
2017-12-30 20:22:55 +01:00
|
|
|
matchWriter _ = False
|
2017-12-30 23:25:38 +01:00
|
|
|
let matchedReaders = map fst $ (filter matchReader readers
|
|
|
|
:: [(String, Reader PandocPure)])
|
|
|
|
let matchedWriters = map fst $ (filter matchWriter writers
|
|
|
|
:: [(String, Writer PandocPure)])
|
2017-06-10 20:57:38 +02:00
|
|
|
inp <- UTF8.toText <$> B.readFile "test/testsuite.txt"
|
2017-01-27 11:15:45 +01:00
|
|
|
let opts = def
|
2016-12-10 23:42:28 +01:00
|
|
|
let doc = either (error . show) id $ runPure $ readMarkdown opts inp
|
2014-08-01 13:32:13 +02:00
|
|
|
let readerBs = mapMaybe (readerBench doc)
|
2017-12-30 23:25:38 +01:00
|
|
|
$ filter (/="haddock")
|
|
|
|
(matchedReaders `intersect` matchedWriters)
|
|
|
|
-- we need the corresponding writer to generate
|
|
|
|
-- input for the reader
|
|
|
|
let writerBs = mapMaybe (writerBench doc) matchedWriters
|
2015-10-10 01:01:08 +02:00
|
|
|
defaultMainWith defaultConfig{ timeLimit = 6.0 }
|
|
|
|
(writerBs ++ readerBs)
|