-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
59 lines (54 loc) · 2.45 KB
/
Main.hs
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
module Main where
import Text.Html.Consolidate
import System.Console.CmdArgs.Explicit
import System.IO
import Network.URI
import Network.Browser
import Network.Browser.Simple
-- | The entry point to the `jespresso` program. Can be run in two
-- modes:
-- * extraction; `jespresso [-e] [<uri>]`. Takes text (HTML) via stdin
-- and outputs all the JavaScript in it (including externally
-- referenced) via stdout. If an (optional) url is supplied, the HTML
-- is downloaded from that URL instead of reading it from the stdin.
-- * normalization: `jespresso -n [<uri>]`. Transforms the HTML and
-- JavaScript into an equivalent one so that all the JavaScript is
-- inlined and in a single <script> tag. If an (optional) url is
-- supplied, the HTML is downloaded from that URL instead of reading
-- it from the stdin.
main :: IO Int
main = do args <- processArgs arguments
(input, cookies) <- case muri args of
Just uri -> download uri []
Nothing -> getContents >>= \c -> return (c, [])
output <- case pmode args of
Extraction -> extractPretty input (muri args)
Consolidation -> consolidate input (muri args)
putStr output
return 1
arguments :: Mode Parameters
arguments = (modeEmpty defaultArgs)
{modeNames = ["jespresso"]
,modeHelp = ""
,modeArgs = ([flagArg addURI "URI"], Nothing)
,modeGroupFlags =
toGroup
[flagNone ["e", "extract"] addExtract extractHelp
,flagNone ["n", "normalize"] addNormalize normalizeHelp]}
where defaultArgs = Parameters {pmode = Extraction
,muri = Nothing}
addURI u params = case parseURI u of
Nothing -> Left "Invalid URI"
Just uri -> Right $ params {muri = Just uri}
addExtract params = params {pmode = Extraction}
addNormalize params = params {pmode = Consolidation}
extractHelp = "Extract JavaScript from the supplied HTML and \
\print to stdout"
normalizeHelp = "Transform HTML (with JavaScript in it) into an \
\equivalent one so that all the JavaScript is inlined \
\and in a single <script> tag."
data Parameters = Parameters {pmode :: ProgramMode
,muri :: Maybe URI}
deriving (Show)
data ProgramMode = Extraction | Consolidation
deriving (Show)