1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-|
Module : $header$
Copyright : (c) Laurent P René de Cotret, 2020
License : GNU GPL, version 2 or above
Maintainer : laurent.decotret@outlook.com
Stability : internal
Portability : portable
Utilities to clean pandoc-plot output directories.
-}
module Text.Pandoc.Filter.Plot.Clean (
cleanOutputDirs
, readDoc
) where
import Control.Monad.Reader (runReaderT, forM, liftIO)
import qualified Data.ByteString.Lazy as B
import Data.Char (toLower)
import Data.Default.Class (def)
import Data.List (nub)
import Data.Maybe (fromMaybe, catMaybes)
import Data.Text (Text)
import qualified Data.Text.IO as Text
import System.Directory (removePathForcibly)
import System.FilePath (takeExtension)
import Text.Pandoc.Class (runIO)
import Text.Pandoc.Definition
import Text.Pandoc.Error (handleError)
import qualified Text.Pandoc.Readers as P
import qualified Text.Pandoc.Options as P
import Text.Pandoc.Walk (query, Walkable)
import Text.Pandoc.Filter.Plot.Parse
import Text.Pandoc.Filter.Plot.Types
-- | Clean all output related to pandoc-plot. This includes output directories specified
-- in the configuration and in the document/block. Note that *all* files in pandoc-plot output
-- directories will be removed.
--
-- The cleaned directories are returned.
cleanOutputDirs :: Walkable Block b => Configuration -> b -> IO [FilePath]
cleanOutputDirs conf doc = do
directories <- sequence $ query (\b -> [outputDir b]) doc
forM (nub . catMaybes $ directories) removeDir
where
outputDir b =
maybe
(return Nothing)
(\tk -> runReaderT (parseFigureSpec b >>= return . fmap directory) (PlotEnv tk conf))
(plotToolkit b)
removeDir :: FilePath -> IO FilePath
removeDir d = removePathForcibly d >> return d
-- | Read document, guessing what extensions and reader options are appropriate. If
-- the file cannot be read for any reason, an error is thrown.
readDoc :: FilePath -> IO Pandoc
readDoc fp = handleError =<< (runIO $ do
let fmt = fromMaybe mempty (formatFromFilePath fp)
(reader, exts) <- P.getReader fmt
let readerOpts = def {P.readerExtensions = exts}
case reader of
P.TextReader fct -> do
t <- liftIO $ Text.readFile fp
fct readerOpts t
P.ByteStringReader bst -> do
b <- liftIO $ B.readFile fp
bst readerOpts b)
-- Determine format based on file extension
-- Note : this is exactly the heuristic used by pandoc here:
-- https://github.com/jgm/pandoc/blob/master/src/Text/Pandoc/App/FormatHeuristics.hs
--
-- However, this is not exported, so I must re-define it here.
formatFromFilePath :: FilePath -> Maybe Text
formatFromFilePath x =
case takeExtension (map toLower x) of
".adoc" -> Just "asciidoc"
".asciidoc" -> Just "asciidoc"
".context" -> Just "context"
".ctx" -> Just "context"
".db" -> Just "docbook"
".doc" -> Just "doc" -- so we get an "unknown reader" error
".docx" -> Just "docx"
".dokuwiki" -> Just "dokuwiki"
".epub" -> Just "epub"
".fb2" -> Just "fb2"
".htm" -> Just "html"
".html" -> Just "html"
".icml" -> Just "icml"
".json" -> Just "json"
".latex" -> Just "latex"
".lhs" -> Just "markdown+lhs"
".ltx" -> Just "latex"
".markdown" -> Just "markdown"
".md" -> Just "markdown"
".ms" -> Just "ms"
".muse" -> Just "muse"
".native" -> Just "native"
".odt" -> Just "odt"
".opml" -> Just "opml"
".org" -> Just "org"
".pdf" -> Just "pdf" -- so we get an "unknown reader" error
".pptx" -> Just "pptx"
".roff" -> Just "ms"
".rst" -> Just "rst"
".rtf" -> Just "rtf"
".s5" -> Just "s5"
".t2t" -> Just "t2t"
".tei" -> Just "tei"
".tei.xml" -> Just "tei"
".tex" -> Just "latex"
".texi" -> Just "texinfo"
".texinfo" -> Just "texinfo"
".text" -> Just "markdown"
".textile" -> Just "textile"
".txt" -> Just "markdown"
".wiki" -> Just "mediawiki"
".xhtml" -> Just "html"
".ipynb" -> Just "ipynb"
".csv" -> Just "csv"
['.',y] | y `elem` ['1'..'9'] -> Just "man"
_ -> Nothing
|