git.iko.soy/typeable/compaREST
on master help sign in
typeable/compaREST/app/FormatHeuristic.hs
95 lines 3.6 kB history raw
1-- |
2-- Originally based on:
3-- https://github.com/jgm/pandoc/blob/master/src/Text/Pandoc/App/FormatHeuristics.hs
4module FormatHeuristic
5 ( formatFromFilePath,
6 )
7where
9import Data.ByteString.Lazy (ByteString)
10import Data.Char (toLower)
11import Data.Functor
12import qualified Data.Map as M
13import qualified Data.OpenApi.Compare.Report.Html.Template as Html
14import Data.Text (Text)
15import qualified Data.Text.Lazy as TL
16import qualified Data.Text.Lazy.Encoding as TL
17import System.FilePath (takeExtension)
18import Text.DocTemplates.Internal
19import Text.Pandoc
21formatFromFilePath :: forall m. PandocMonad m => FilePath -> Maybe (Pandoc -> m ByteString, FilePath)
22formatFromFilePath x =
23 case takeExtension (map toLower x) of
24 ".adoc" -> fWithDefaultFile "asciidoc" def
25 ".asciidoc" -> fWithDefaultFile "asciidoc" def
26 ".context" -> fWithDefaultFile "context" def
27 ".ctx" -> fWithDefaultFile "context" def
28 ".db" -> fWithDefaultFile "docbook" def
29 ".doc" -> fWithDefaultFile "doc" def -- so we get an "unknown reader" error
30 ".docx" -> fWithDefaultFile "docx" def
31 ".dokuwiki" -> fWithDefaultFile "dokuwiki" def
32 ".epub" -> fWithDefaultFile "epub" def
33 ".fb2" -> fWithDefaultFile "fb2" def
34 ".htm" -> fWithDefaultFile "html" def
35 ".html" -> fWithDefaultFile "html" def
36 ".icml" -> fWithDefaultFile "icml" def
37 ".json" -> fWithDefaultFile "json" def
38 ".latex" -> fWithDefaultFile "latex" def
39 ".lhs" -> fWithDefaultFile "markdown+lhs" def
40 ".ltx" -> fWithDefaultFile "latex" def
41 ".markdown" -> markdown
42 ".mkdn" -> markdown
43 ".mkd" -> markdown
44 ".mdwn" -> markdown
45 ".mdown" -> markdown
46 ".Rmd" -> markdown
47 ".md" -> markdown
48 ".ms" -> fWithDefaultFile "ms" def
49 ".muse" -> fWithDefaultFile "muse" def
50 ".native" -> fWithDefaultFile "native" def
51 ".odt" -> fWithDefaultFile "odt" def
52 ".opml" -> fWithDefaultFile "opml" def
53 ".org" -> fWithDefaultFile "org" def
54 -- so we get an "unknown reader" error
55 ".pdf" -> fWithDefaultFile "pdf" def
56 ".pptx" -> fWithDefaultFile "pptx" def
57 ".roff" -> fWithDefaultFile "ms" def
58 ".rst" -> fWithDefaultFile "rst" def
59 ".rtf" -> fWithDefaultFile "rtf" def
60 ".s5" -> fWithDefaultFile "s5" def
61 ".t2t" -> fWithDefaultFile "t2t" def
62 ".tei" -> fWithDefaultFile "tei" def
63 ".tei.xml" -> fWithDefaultFile "tei" def
64 ".tex" -> fWithDefaultFile "latex" def
65 ".texi" -> fWithDefaultFile "texinfo" def
66 ".texinfo" -> fWithDefaultFile "texinfo" def
67 ".text" -> markdown
68 ".textile" -> fWithDefaultFile "textile" def
69 ".txt" -> markdown
70 ".wiki" -> fWithDefaultFile "mediawiki" def
71 ".xhtml" -> fWithDefaultFile "html" def
72 ".ipynb" -> fWithDefaultFile "ipynb" def
73 ".csv" -> fWithDefaultFile "csv" def
74 ".bib" -> fWithDefaultFile "biblatex" def
75 ['.', y] | y `elem` ['1' .. '9'] -> fWithDefaultFile "man" def
76 "" ->
77 (,x <> ".html")
78 <$> f
79 "html"
80 def
81 { writerTemplate = Just Html.template
82 , -- Not actually used. Needed to silence warning.
83 writerVariables = Context $ M.fromList [("pagetitle", toVal ("CompaREST" :: Text))]
84 }
85 _ -> Nothing
86 where
87 markdown :: Maybe (Pandoc -> m ByteString, FilePath)
88 markdown = fWithDefaultFile "markdown" markdownOpt
90 markdownOpt = def {writerExtensions = githubMarkdownExtensions}
91 fWithDefaultFile k opt = (,x) <$> f k opt
92 f k opt =
93 lookup k writers <&> \case
94 TextWriter g -> fmap (TL.encodeUtf8 . TL.fromStrict) . g opt
95 ByteStringWriter g -> g opt