aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc.hs111
-rw-r--r--src/pandoc.hs42
2 files changed, 73 insertions, 80 deletions
diff --git a/src/Text/Pandoc.hs b/src/Text/Pandoc.hs
index a813996c0..ca6d00de6 100644
--- a/src/Text/Pandoc.hs
+++ b/src/Text/Pandoc.hs
@@ -63,7 +63,6 @@ module Text.Pandoc
-- * Lists of readers and writers
, readers
, writers
- , iowriters
-- * Readers: converting /to/ Pandoc format
, readMarkdown
, readRST
@@ -81,6 +80,7 @@ module Text.Pandoc
, NoteTable
, HeaderType (..)
-- * Writers: converting /from/ Pandoc format
+ , Writer (..)
, writeNative
, writeMarkdown
, writePlain
@@ -151,6 +151,7 @@ import Text.Pandoc.Writers.AsciiDoc
import Text.Pandoc.Templates
import Text.Pandoc.Parsing
import Text.Pandoc.Shared
+import Data.ByteString.Lazy (ByteString)
import Data.Version (showVersion)
import Text.JSON.Generic
import Paths_pandoc (version)
@@ -178,60 +179,62 @@ readers = [("native" , \_ -> readNative)
readLaTeX st{ stateLiterateHaskell = True})
]
--- | Association list of formats and writers (omitting the
--- binary writers, odt, docx, and epub).
-writers :: [ ( String, WriterOptions -> Pandoc -> String ) ]
-writers = [("native" , writeNative)
- ,("json" , \_ -> encodeJSON)
- ,("html" , writeHtmlString)
- ,("html5" , \o ->
- writeHtmlString o{ writerHtml5 = True })
- ,("html+lhs" , \o ->
- writeHtmlString o{ writerLiterateHaskell = True })
- ,("html5+lhs" , \o ->
- writeHtmlString o{ writerLiterateHaskell = True,
- writerHtml5 = True })
- ,("s5" , \o ->
- writeHtmlString o{ writerSlideVariant = S5Slides
- , writerTableOfContents = False })
- ,("slidy" , \o ->
- writeHtmlString o{ writerSlideVariant = SlidySlides })
- ,("slideous" , \o ->
- writeHtmlString o{ writerSlideVariant = SlideousSlides })
- ,("dzslides" , \o ->
- writeHtmlString o{ writerSlideVariant = DZSlides
- , writerHtml5 = True })
- ,("docbook" , writeDocbook)
- ,("opendocument" , writeOpenDocument)
- ,("latex" , writeLaTeX)
- ,("latex+lhs" , \o ->
- writeLaTeX o{ writerLiterateHaskell = True })
- ,("beamer" , \o ->
- writeLaTeX o{ writerBeamer = True })
- ,("beamer+lhs" , \o ->
- writeLaTeX o{ writerBeamer = True, writerLiterateHaskell = True })
- ,("context" , writeConTeXt)
- ,("texinfo" , writeTexinfo)
- ,("man" , writeMan)
- ,("markdown" , writeMarkdown)
- ,("markdown+lhs" , \o ->
- writeMarkdown o{ writerLiterateHaskell = True })
- ,("plain" , writePlain)
- ,("rst" , writeRST)
- ,("rst+lhs" , \o ->
- writeRST o{ writerLiterateHaskell = True })
- ,("mediawiki" , writeMediaWiki)
- ,("textile" , writeTextile)
- ,("rtf" , writeRTF)
- ,("org" , writeOrg)
- ,("asciidoc" , writeAsciiDoc)
- ]
+data Writer = PureStringWriter (WriterOptions -> Pandoc -> String)
+ | IOStringWriter (WriterOptions -> Pandoc -> IO String)
+ | IOByteStringWriter (WriterOptions -> Pandoc -> IO ByteString)
--- | Association list of formats and writers which require IO to work.
--- These writers produce text output as well as thoses in 'writers'.
-iowriters :: [ (String, WriterOptions -> Pandoc -> IO String) ]
-iowriters = [ ("fb2" , writeFB2)
- ]
+-- | Association list of formats and writers.
+writers :: [ ( String, Writer ) ]
+writers = [
+ ("native" , PureStringWriter writeNative)
+ ,("json" , PureStringWriter $ \_ -> encodeJSON)
+ ,("docx" , IOByteStringWriter writeDocx)
+ ,("odt" , IOByteStringWriter writeODT)
+ ,("epub" , IOByteStringWriter writeEPUB)
+ ,("fb2" , IOStringWriter writeFB2)
+ ,("html" , PureStringWriter writeHtmlString)
+ ,("html5" , PureStringWriter $ \o ->
+ writeHtmlString o{ writerHtml5 = True })
+ ,("html+lhs" , PureStringWriter $ \o ->
+ writeHtmlString o{ writerLiterateHaskell = True })
+ ,("html5+lhs" , PureStringWriter $ \o ->
+ writeHtmlString o{ writerLiterateHaskell = True,
+ writerHtml5 = True })
+ ,("s5" , PureStringWriter $ \o ->
+ writeHtmlString o{ writerSlideVariant = S5Slides
+ , writerTableOfContents = False })
+ ,("slidy" , PureStringWriter $ \o ->
+ writeHtmlString o{ writerSlideVariant = SlidySlides })
+ ,("slideous" , PureStringWriter $ \o ->
+ writeHtmlString o{ writerSlideVariant = SlideousSlides })
+ ,("dzslides" , PureStringWriter $ \o ->
+ writeHtmlString o{ writerSlideVariant = DZSlides
+ , writerHtml5 = True })
+ ,("docbook" , PureStringWriter writeDocbook)
+ ,("opendocument" , PureStringWriter writeOpenDocument)
+ ,("latex" , PureStringWriter writeLaTeX)
+ ,("latex+lhs" , PureStringWriter $ \o ->
+ writeLaTeX o{ writerLiterateHaskell = True })
+ ,("beamer" , PureStringWriter $ \o ->
+ writeLaTeX o{ writerBeamer = True })
+ ,("beamer+lhs" , PureStringWriter $ \o ->
+ writeLaTeX o{ writerBeamer = True, writerLiterateHaskell = True })
+ ,("context" , PureStringWriter writeConTeXt)
+ ,("texinfo" , PureStringWriter writeTexinfo)
+ ,("man" , PureStringWriter writeMan)
+ ,("markdown" , PureStringWriter writeMarkdown)
+ ,("markdown+lhs" , PureStringWriter $ \o ->
+ writeMarkdown o{ writerLiterateHaskell = True })
+ ,("plain" , PureStringWriter writePlain)
+ ,("rst" , PureStringWriter writeRST)
+ ,("rst+lhs" , PureStringWriter $ \o ->
+ writeRST o{ writerLiterateHaskell = True })
+ ,("mediawiki" , PureStringWriter writeMediaWiki)
+ ,("textile" , PureStringWriter writeTextile)
+ ,("rtf" , PureStringWriter writeRTF)
+ ,("org" , PureStringWriter writeOrg)
+ ,("asciidoc" , PureStringWriter writeAsciiDoc)
+ ]
{-# DEPRECATED jsonFilter "Use toJsonFilter instead" #-}
-- | Converts a transformation on the Pandoc AST into a function
diff --git a/src/pandoc.hs b/src/pandoc.hs
index 3baf0251c..e6e9c710c 100644
--- a/src/pandoc.hs
+++ b/src/pandoc.hs
@@ -697,7 +697,7 @@ usageMessage programName = usageInfo
(wrapWords 16 78 $ readers'names) ++ "\nOutput formats: " ++
(wrapWords 16 78 $ writers'names ++ nonTextFormats) ++ "\nOptions:")
where
- writers'names = map fst writers ++ map fst iowriters
+ writers'names = map fst writers
readers'names = map fst readers
-- Determine default reader based on source file extensions
@@ -1038,36 +1038,26 @@ main = do
writerFn "-" = UTF8.putStr
writerFn f = UTF8.writeFile f
- let purewriter = lookup writerName' writers
- let iowriter = lookup writerName' iowriters
- case (purewriter, iowriter) of
- (Nothing, Just iow) -> do
- d <- iow writerOptions doc2
- writerFn outputFile d
- (Nothing, Nothing)
- | writerName' == "epub" ->
- writeEPUB writerOptions doc2
- >>= writeBinary
- | writerName' == "odt" ->
- writeODT writerOptions doc2 >>= writeBinary
- | writerName' == "docx" ->
- writeDocx writerOptions doc2 >>= writeBinary
- | otherwise -> err 9 ("Unknown writer: " ++ writerName')
- (Just w, _)
- | pdfOutput -> do
- res <- tex2pdf latexEngine $ w writerOptions doc2
+ let mbwriter = lookup writerName' writers
+ case mbwriter of
+ Nothing -> err 9 ("Unknown writer: " ++ writerName')
+ Just (IOStringWriter f) -> f writerOptions doc2 >>= writerFn outputFile
+ Just (IOByteStringWriter f) -> f writerOptions doc2 >>= writeBinary
+ Just (PureStringWriter f)
+ | pdfOutput -> do
+ res <- tex2pdf latexEngine $ f writerOptions doc2
case res of
Right pdf -> writeBinary pdf
Left err' -> err 43 $ toString err'
- (Just w, _)
- | htmlFormat && ascii ->
- writerFn outputFile . toEntities =<< selfcontain result
- | otherwise ->
- writerFn outputFile =<< selfcontain result
- where result = w writerOptions doc2 ++ ['\n' | not standalone']
- htmlFormat = writerName' `elem`
+ | otherwise -> selfcontain (f writerOptions doc2 ++
+ ['\n' | not standalone'])
+ >>= writerFn outputFile . handleEntities
+ where htmlFormat = writerName' `elem`
["html","html+lhs","html5","html5+lhs",
"s5","slidy","slideous","dzslides"]
selfcontain = if selfContained && htmlFormat
then makeSelfContained datadir
else return
+ handleEntities = if htmlFormat && ascii
+ then toEntities
+ else id