2017-03-04 13:03:41 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2007-11-04 00:27:58 +01:00
|
|
|
{-
|
2016-03-23 01:20:39 +01:00
|
|
|
Copyright (C) 2006-2016 John MacFarlane <jgm@berkeley.edu>
|
2007-11-04 00:27:58 +01: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
|
|
|
|
-}
|
|
|
|
|
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc
|
2016-03-23 01:20:39 +01:00
|
|
|
Copyright : Copyright (C) 2006-2016 John MacFarlane
|
2011-02-15 19:40:50 +01:00
|
|
|
License : GNU GPL, version 2 or above
|
2007-11-04 00:27:58 +01:00
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
2011-02-15 19:40:50 +01:00
|
|
|
Stability : alpha
|
2007-11-04 00:27:58 +01:00
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
This helper module exports the main writers, readers, and data
|
|
|
|
structure definitions from the Pandoc libraries.
|
|
|
|
|
|
|
|
A typical application will chain together a reader and a writer
|
|
|
|
to convert strings from one format to another. For example, the
|
|
|
|
following simple program will act as a filter converting markdown
|
|
|
|
fragments to reStructuredText, using reference-style links instead of
|
|
|
|
inline links:
|
|
|
|
|
|
|
|
> module Main where
|
|
|
|
> import Text.Pandoc
|
2008-08-01 01:16:31 +02:00
|
|
|
>
|
2016-12-03 22:35:58 +01:00
|
|
|
> markdownToRST :: String -> Either PandocError String
|
2016-10-23 21:48:22 +02:00
|
|
|
> markdownToRST =
|
2016-12-03 22:35:58 +01:00
|
|
|
> writeRST def {writerReferenceLinks = True} . readMarkdown def
|
2011-02-15 19:40:50 +01:00
|
|
|
>
|
2016-12-03 22:35:58 +01:00
|
|
|
> main = getContents >>= either error return markdownToRST >>= putStrLn
|
2007-11-04 00:27:58 +01:00
|
|
|
|
2009-10-05 00:09:23 +02:00
|
|
|
Note: all of the readers assume that the input text has @'\n'@
|
|
|
|
line endings. So if you get your input text from a web form,
|
|
|
|
you should remove @'\r'@ characters using @filter (/='\r')@.
|
|
|
|
|
2007-11-04 00:27:58 +01:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Text.Pandoc
|
2011-02-15 19:40:50 +01:00
|
|
|
(
|
2007-11-04 00:27:58 +01:00
|
|
|
-- * Definitions
|
|
|
|
module Text.Pandoc.Definition
|
2010-12-24 22:39:27 +01:00
|
|
|
-- * Generics
|
|
|
|
, module Text.Pandoc.Generic
|
2012-07-25 19:45:45 +02:00
|
|
|
-- * Options
|
|
|
|
, module Text.Pandoc.Options
|
2017-02-10 20:59:54 +01:00
|
|
|
-- * Logging
|
|
|
|
, module Text.Pandoc.Logging
|
2016-11-27 11:51:40 +01:00
|
|
|
-- * Typeclass
|
2016-12-10 23:14:43 +01:00
|
|
|
, PandocMonad
|
|
|
|
, runIO
|
|
|
|
, runPure
|
|
|
|
, runIOorExplode
|
2017-02-04 21:06:36 +01:00
|
|
|
, setVerbosity
|
2016-10-24 22:31:36 +02:00
|
|
|
-- * Error handling
|
|
|
|
, module Text.Pandoc.Error
|
2017-04-17 10:51:37 +02:00
|
|
|
-- * Lists of writers
|
2016-11-27 06:05:53 +01:00
|
|
|
, writers
|
2007-11-04 00:27:58 +01:00
|
|
|
-- * Readers: converting /to/ Pandoc format
|
2017-04-17 10:51:37 +02:00
|
|
|
, module Text.Pandoc.Readers
|
2007-11-04 00:27:58 +01:00
|
|
|
-- * Writers: converting /from/ Pandoc format
|
2016-11-27 06:05:53 +01:00
|
|
|
, Writer(..)
|
2010-07-04 22:55:48 +02:00
|
|
|
, writeNative
|
2013-07-24 07:31:50 +02:00
|
|
|
, writeJSON
|
2007-11-04 00:27:58 +01:00
|
|
|
, writeMarkdown
|
2010-03-18 07:45:43 +01:00
|
|
|
, writePlain
|
2007-11-04 00:27:58 +01:00
|
|
|
, writeRST
|
|
|
|
, writeLaTeX
|
2017-01-28 09:52:45 +01:00
|
|
|
, writeBeamer
|
2007-11-04 00:27:58 +01:00
|
|
|
, writeConTeXt
|
2008-02-24 06:48:41 +01:00
|
|
|
, writeTexinfo
|
2017-01-25 21:51:26 +01:00
|
|
|
, writeHtml4
|
|
|
|
, writeHtml4String
|
|
|
|
, writeHtml5
|
|
|
|
, writeHtml5String
|
2017-01-27 22:39:36 +01:00
|
|
|
, writeRevealJs
|
|
|
|
, writeS5
|
|
|
|
, writeSlidy
|
|
|
|
, writeSlideous
|
|
|
|
, writeDZSlides
|
2013-12-01 21:11:39 +01:00
|
|
|
, writeICML
|
2017-01-26 22:40:57 +01:00
|
|
|
, writeDocbook4
|
|
|
|
, writeDocbook5
|
2017-03-28 09:51:30 +02:00
|
|
|
, writeJATS
|
2013-03-20 04:35:14 +01:00
|
|
|
, writeOPML
|
2008-03-19 19:45:51 +01:00
|
|
|
, writeOpenDocument
|
2007-11-04 00:27:58 +01:00
|
|
|
, writeMan
|
2017-03-15 21:04:14 +01:00
|
|
|
, writeMs
|
2008-07-27 05:25:51 +02:00
|
|
|
, writeMediaWiki
|
2013-07-14 14:40:27 +02:00
|
|
|
, writeDokuWiki
|
2016-06-29 08:11:42 +02:00
|
|
|
, writeZimWiki
|
2010-04-10 21:38:07 +02:00
|
|
|
, writeTextile
|
2007-11-04 00:27:58 +01:00
|
|
|
, writeRTF
|
2010-07-03 05:12:14 +02:00
|
|
|
, writeODT
|
2012-01-03 21:10:10 +01:00
|
|
|
, writeDocx
|
2017-01-26 20:39:32 +01:00
|
|
|
, writeEPUB2
|
|
|
|
, writeEPUB3
|
2011-02-15 19:40:50 +01:00
|
|
|
, writeFB2
|
2010-12-04 11:27:39 +01:00
|
|
|
, writeOrg
|
2011-11-19 06:09:23 +01:00
|
|
|
, writeAsciiDoc
|
2014-06-19 00:32:13 +02:00
|
|
|
, writeHaddock
|
2015-03-23 19:35:44 +01:00
|
|
|
, writeCommonMark
|
2013-04-03 06:08:38 +02:00
|
|
|
, writeCustom
|
2015-12-24 17:36:58 +01:00
|
|
|
, writeTEI
|
2017-03-10 10:16:27 +01:00
|
|
|
, writeMuse
|
2009-12-31 02:10:57 +01:00
|
|
|
-- * Rendering templates and default templates
|
|
|
|
, module Text.Pandoc.Templates
|
2011-01-28 17:42:04 +01:00
|
|
|
-- * Miscellaneous
|
2016-11-27 06:05:53 +01:00
|
|
|
, getWriter
|
2015-10-10 22:42:02 +02:00
|
|
|
, pandocVersion
|
2007-11-04 00:27:58 +01:00
|
|
|
) where
|
|
|
|
|
2017-03-04 13:03:41 +01:00
|
|
|
import Data.Aeson
|
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import Data.List (intercalate)
|
|
|
|
import Text.Pandoc.Class
|
2007-11-04 00:27:58 +01:00
|
|
|
import Text.Pandoc.Definition
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Error
|
2010-12-24 22:39:27 +01:00
|
|
|
import Text.Pandoc.Generic
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Logging
|
|
|
|
import Text.Pandoc.Options
|
2017-04-17 10:51:37 +02:00
|
|
|
import Text.Pandoc.Readers
|
|
|
|
import Text.Pandoc.Shared (pandocVersion)
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Templates
|
|
|
|
import qualified Text.Pandoc.UTF8 as UTF8
|
|
|
|
import Text.Pandoc.Writers.AsciiDoc
|
|
|
|
import Text.Pandoc.Writers.CommonMark
|
2007-11-04 00:27:58 +01:00
|
|
|
import Text.Pandoc.Writers.ConTeXt
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.Custom
|
|
|
|
import Text.Pandoc.Writers.Docbook
|
2017-03-28 09:51:30 +02:00
|
|
|
import Text.Pandoc.Writers.JATS
|
2012-01-03 21:10:10 +01:00
|
|
|
import Text.Pandoc.Writers.Docx
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.DokuWiki
|
2010-07-03 07:07:00 +02:00
|
|
|
import Text.Pandoc.Writers.EPUB
|
2011-02-15 19:40:50 +01:00
|
|
|
import Text.Pandoc.Writers.FB2
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.Haddock
|
|
|
|
import Text.Pandoc.Writers.HTML
|
2013-12-01 21:11:39 +01:00
|
|
|
import Text.Pandoc.Writers.ICML
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.LaTeX
|
2007-11-04 00:27:58 +01:00
|
|
|
import Text.Pandoc.Writers.Man
|
2017-03-15 21:04:14 +01:00
|
|
|
import Text.Pandoc.Writers.Ms
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.Markdown
|
2008-07-27 05:25:51 +02:00
|
|
|
import Text.Pandoc.Writers.MediaWiki
|
2017-03-10 10:16:27 +01:00
|
|
|
import Text.Pandoc.Writers.Muse
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.Native
|
|
|
|
import Text.Pandoc.Writers.ODT
|
|
|
|
import Text.Pandoc.Writers.OpenDocument
|
|
|
|
import Text.Pandoc.Writers.OPML
|
2010-12-04 11:27:39 +01:00
|
|
|
import Text.Pandoc.Writers.Org
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.RST
|
|
|
|
import Text.Pandoc.Writers.RTF
|
2015-12-24 17:36:58 +01:00
|
|
|
import Text.Pandoc.Writers.TEI
|
2017-03-04 13:03:41 +01:00
|
|
|
import Text.Pandoc.Writers.Texinfo
|
|
|
|
import Text.Pandoc.Writers.Textile
|
|
|
|
import Text.Pandoc.Writers.ZimWiki
|
2012-08-10 05:19:06 +02:00
|
|
|
import Text.Parsec.Error
|
2010-12-11 02:30:32 +01:00
|
|
|
|
2016-11-27 06:05:53 +01:00
|
|
|
data Writer m = StringWriter (WriterOptions -> Pandoc -> m String)
|
|
|
|
| ByteStringWriter (WriterOptions -> Pandoc -> m BL.ByteString)
|
2011-01-23 02:52:25 +01:00
|
|
|
|
2012-07-25 04:05:38 +02:00
|
|
|
-- | Association list of formats and writers.
|
2016-11-27 06:05:53 +01:00
|
|
|
writers :: PandocMonad m => [ ( String, Writer m) ]
|
|
|
|
writers = [
|
|
|
|
("native" , StringWriter writeNative)
|
|
|
|
,("json" , StringWriter $ \o d -> return $ writeJSON o d)
|
|
|
|
,("docx" , ByteStringWriter writeDocx)
|
|
|
|
,("odt" , ByteStringWriter writeODT)
|
2017-01-30 10:19:30 +01:00
|
|
|
,("epub" , ByteStringWriter writeEPUB3)
|
2017-01-26 20:39:32 +01:00
|
|
|
,("epub2" , ByteStringWriter writeEPUB2)
|
|
|
|
,("epub3" , ByteStringWriter writeEPUB3)
|
2016-11-27 06:05:53 +01:00
|
|
|
,("fb2" , StringWriter writeFB2)
|
2017-01-25 21:51:26 +01:00
|
|
|
,("html" , StringWriter writeHtml5String)
|
|
|
|
,("html4" , StringWriter writeHtml4String)
|
|
|
|
,("html5" , StringWriter writeHtml5String)
|
2016-11-27 06:05:53 +01:00
|
|
|
,("icml" , StringWriter writeICML)
|
2017-01-27 22:39:36 +01:00
|
|
|
,("s5" , StringWriter writeS5)
|
|
|
|
,("slidy" , StringWriter writeSlidy)
|
|
|
|
,("slideous" , StringWriter writeSlideous)
|
|
|
|
,("dzslides" , StringWriter writeDZSlides)
|
|
|
|
,("revealjs" , StringWriter writeRevealJs)
|
2017-01-26 22:40:57 +01:00
|
|
|
,("docbook" , StringWriter writeDocbook5)
|
|
|
|
,("docbook4" , StringWriter writeDocbook4)
|
|
|
|
,("docbook5" , StringWriter writeDocbook5)
|
2017-03-28 09:51:30 +02:00
|
|
|
,("jats" , StringWriter writeJATS)
|
2016-11-27 06:05:53 +01:00
|
|
|
,("opml" , StringWriter writeOPML)
|
|
|
|
,("opendocument" , StringWriter writeOpenDocument)
|
|
|
|
,("latex" , StringWriter writeLaTeX)
|
2017-01-28 09:52:45 +01:00
|
|
|
,("beamer" , StringWriter writeBeamer)
|
2016-11-27 06:05:53 +01:00
|
|
|
,("context" , StringWriter writeConTeXt)
|
|
|
|
,("texinfo" , StringWriter writeTexinfo)
|
|
|
|
,("man" , StringWriter writeMan)
|
2017-03-15 21:04:14 +01:00
|
|
|
,("ms" , StringWriter writeMs)
|
2016-11-27 06:05:53 +01:00
|
|
|
,("markdown" , StringWriter writeMarkdown)
|
|
|
|
,("markdown_strict" , StringWriter writeMarkdown)
|
|
|
|
,("markdown_phpextra" , StringWriter writeMarkdown)
|
|
|
|
,("markdown_github" , StringWriter writeMarkdown)
|
|
|
|
,("markdown_mmd" , StringWriter writeMarkdown)
|
|
|
|
,("plain" , StringWriter writePlain)
|
|
|
|
,("rst" , StringWriter writeRST)
|
|
|
|
,("mediawiki" , StringWriter writeMediaWiki)
|
|
|
|
,("dokuwiki" , StringWriter writeDokuWiki)
|
|
|
|
,("zimwiki" , StringWriter writeZimWiki)
|
|
|
|
,("textile" , StringWriter writeTextile)
|
2016-12-03 17:10:50 +01:00
|
|
|
,("rtf" , StringWriter writeRTF)
|
2016-11-27 06:05:53 +01:00
|
|
|
,("org" , StringWriter writeOrg)
|
|
|
|
,("asciidoc" , StringWriter writeAsciiDoc)
|
|
|
|
,("haddock" , StringWriter writeHaddock)
|
|
|
|
,("commonmark" , StringWriter writeCommonMark)
|
|
|
|
,("tei" , StringWriter writeTEI)
|
2017-03-10 10:16:27 +01:00
|
|
|
,("muse" , StringWriter writeMuse)
|
2012-07-25 04:05:38 +02:00
|
|
|
]
|
2011-02-15 19:40:50 +01:00
|
|
|
|
2016-11-27 06:05:53 +01:00
|
|
|
getWriter :: PandocMonad m => String -> Either String (Writer m)
|
|
|
|
getWriter s
|
2014-04-28 05:56:50 +02:00
|
|
|
= case parseFormatSpec s of
|
2015-11-22 16:37:51 +01:00
|
|
|
Left e -> Left $ intercalate "\n" [m | Message m <- errorMessages e]
|
2014-04-28 05:56:50 +02:00
|
|
|
Right (writerName, setExts) ->
|
2016-11-27 06:05:53 +01:00
|
|
|
case lookup writerName writers of
|
2014-04-28 05:56:50 +02:00
|
|
|
Nothing -> Left $ "Unknown writer: " ++ writerName
|
2016-11-27 06:05:53 +01:00
|
|
|
Just (StringWriter r) -> Right $ StringWriter $
|
2014-04-28 05:56:50 +02:00
|
|
|
\o -> r o{ writerExtensions = setExts $
|
|
|
|
getDefaultExtensions writerName }
|
2016-11-27 06:05:53 +01:00
|
|
|
Just (ByteStringWriter r) -> Right $ ByteStringWriter $
|
2014-04-28 05:56:50 +02:00
|
|
|
\o -> r o{ writerExtensions = setExts $
|
|
|
|
getDefaultExtensions writerName }
|
2012-01-24 20:50:35 +01:00
|
|
|
|
2013-07-24 07:31:50 +02:00
|
|
|
writeJSON :: WriterOptions -> Pandoc -> String
|
|
|
|
writeJSON _ = UTF8.toStringLazy . encode
|