-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
105 lines (78 loc) · 2.77 KB
/
Main.hs
File metadata and controls
105 lines (78 loc) · 2.77 KB
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
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Maybe
import Data.List (intercalate)
import System.IO
import qualified Data.Text.IO as T
import Data.Semigroup((<>))
import Options.Applicative
import Lib
-- Options
data Options = Options
{ input :: Maybe String
, output :: Maybe String
, format :: Maybe String
, htmlPage :: Bool
, htmlTitle :: Maybe String
, htmlCss :: Maybe String } deriving (Show)
optionsParser :: Parser Options
optionsParser = Options
<$> (optional $
strOption (short 'i'
<> long "input"
<> metavar "INPUT_FILE"
<> help "Markdown file to read"))
<*> (optional $
strOption (short 'o'
<> long "output"
<> metavar "OUTPUT_FILE"
<> help "File to write the output to"))
<*> (optional $
strOption (short 'f'
<> long "format"
<> metavar "FORMAT"
<> help ("The format of the output, supported formats are: " ++ intercalate "," formats)))
<*> (switch (long "html-page" <> help "Produce HTML as a full page (doctype, head, and body)"))
<*> (optional $
strOption (long "html-title"
<> metavar "TITLE"
<> help "The title of the generated HTML page"))
<*> (optional $
strOption (long "html-css"
<> metavar "CSS_FILE"
<> help "The CSS file that will be linked to the generated HTML page"))
-- Supported formats
data Format = FormatHtml
instance Show Format where
show FormatHtml = "html"
fromString :: String -> Format
fromString "html" = FormatHtml
fromString _ = FormatHtml
formats :: [String]
formats = map show [FormatHtml]
-- Main
main = execParser opts >>= main'
where opts = info (helper <*> optionsParser)
(fullDesc
<> header "Marquee: Markdown transpiler"
<> progDesc "Reads Markdown and output something else")
main' :: Options -> IO ()
main' options = do
input <- maybe (return stdin) (flip openFile ReadMode) (input options)
output <- maybe (return stdout) (flip openFile WriteMode) (output options)
let outputFormat = maybe FormatHtml fromString (format options)
(dispatch outputFormat) options input output
hClose output
hClose input
-- Outputs
dispatch :: Format -> (Options -> Handle -> Handle -> IO ())
dispatch _ = outputHtml
outputHtml :: Options -> Handle -> Handle -> IO ()
outputHtml options input output = do
markdown <- T.hGetContents input
let writer = if htmlPage options
then writeHtmlDocument (fromMaybe "" (htmlTitle options)) (htmlCss options)
else writeHtml
ast = renderAST markdown
html = renderHtml . writer $ ast
hPutStrLn output html