1
1
-----------------------------------------------------------------------------
2
2
--
3
3
-- Module : Main
4
- -- Copyright : (c) Phil Freeman 2013
4
+ -- Copyright : (c) Phil Freeman 2013-2015
5
5
-- License : MIT
6
6
--
7
7
@@ -18,40 +18,40 @@ module Main (
18
18
main
19
19
) where
20
20
21
- import Web.Scotty
22
- import qualified Web.Scotty as Scotty
23
21
import qualified Language.PureScript as P
24
22
25
23
import Data.Version (showVersion )
26
-
27
- import Network.HTTP.Types (status500 )
28
-
29
24
import Data.Monoid
30
25
import Data.String
31
26
import Data.Maybe (mapMaybe )
32
27
import Data.List (intercalate )
33
- import System.Console.CmdTheLine
28
+ import Data.FileEmbed
29
+
30
+ import qualified Data.ByteString as B
31
+ import qualified Data.ByteString.Char8 as BC8
32
+ import qualified Data.ByteString.Lazy.Char8 as BLC8
33
+
34
+ import qualified Data.Map as M
35
+
34
36
import Control.Applicative
35
37
import Control.Monad (when , forM_ )
36
38
import Control.Monad.Trans
37
- import qualified Data.Map as M
39
+ import Control.Monad.Reader
40
+
41
+ import Network.HTTP.Types (status500 )
42
+
43
+ import Web.Scotty
44
+ import qualified Web.Scotty as Scotty
45
+
38
46
import Text.Blaze.Html
39
47
import Text.Blaze.Internal
40
48
import Text.Blaze.Html.Renderer.Text
41
49
import qualified Text.Blaze.Html5 as H
42
50
import qualified Text.Blaze.Html5.Attributes as A
43
- import qualified System.IO.UTF8 as U
44
51
45
52
import qualified Paths_trypurescript as Paths
46
53
47
- import qualified Data.ByteString as B
48
- import qualified Data.ByteString.UTF8 as BU
49
- import qualified Data.ByteString.Lazy.UTF8 as BUL
50
-
51
- import Data.FileEmbed
52
-
53
- prelude :: String
54
- prelude = BU. toString $ (embedFile " prelude/prelude.purs" )
54
+ import System.Environment (getArgs )
55
55
56
56
data Compiled = Compiled { js :: String
57
57
, externs :: String
@@ -60,16 +60,19 @@ data Compiled = Compiled { js :: String
60
60
newtype Response = Response { runResponse :: Either String Compiled }
61
61
62
62
options :: P. Options P. Compile
63
- options = P. defaultCompileOptions { P. optionsAdditional = P. CompileOptions " PS" [" Main" ] [] }
63
+ options = P. defaultCompileOptions
64
+ { P. optionsAdditional = P. CompileOptions " PS" [] []
65
+ , P. optionsMain = Just " Main"
66
+ }
64
67
65
68
compile :: [P. Module ] -> String -> IO Response
66
- compile _ input | length input > 5000 = return $ Response $ Left " Please limit your input to 5000 characters"
69
+ compile _ input | length input > 20000 = return $ Response $ Left " Please limit your input to 20000 characters"
67
70
compile prelude input = do
68
- case P. runIndentParser " " P. parseModules input of
71
+ case either Left ( Right . map snd ) $ P. parseModulesFromFiles ( const " <file> " ) [( undefined , input)] of
69
72
Left parseError -> do
70
73
return $ Response $ Left $ show parseError
71
74
Right modules -> do
72
- case P. compile options (prelude ++ modules) [] of
75
+ case flip runReaderT options $ P. compile (prelude ++ modules) [" Generated by trypurescript " ] of
73
76
Left error ->
74
77
return $ Response $ Left error
75
78
Right (js, externs, _) ->
@@ -82,42 +85,39 @@ mono :: H.Html -> H.Html
82
85
mono h = h ! A. class_ " mono"
83
86
84
87
css :: String
85
- css = BU. toString $ (embedFile " assets/style.css" )
88
+ css = BC8. unpack $ (embedFile " assets/style.css" )
86
89
87
90
gaq :: String
88
- gaq = BU. toString $ (embedFile " assets/gaq.js" )
91
+ gaq = BC8. unpack $ (embedFile " assets/gaq.js" )
89
92
90
- ace :: String
91
- ace = BU. toString $ (embedFile " assets/ace .js" )
93
+ scripts :: String
94
+ scripts = BC8. unpack $ (embedFile " assets/scripts .js" )
92
95
93
96
defaultCode :: String
94
- defaultCode = BU. toString $ (embedFile " examples/default.purs" )
97
+ defaultCode = BC8. unpack $ (embedFile " examples/default.purs" )
95
98
96
99
examples :: [(String , (String , String ))]
97
100
examples =
98
- [ (" adt" , (" Algebraic Data Types" , BU. toString $ (embedFile " examples/adt.purs" )))
99
- , (" ops" , (" Operators" , BU. toString $ (embedFile " examples/operators.purs" )))
100
- , (" arrays" , (" Arrays" , BU. toString $ (embedFile " examples/arrays.purs" )))
101
- , (" rows" , (" Row Polymorphism" , BU. toString $ (embedFile " examples/rows.purs" )))
102
- , (" ffi" , (" FFI" , BU. toString $ (embedFile " examples/ffi.purs" )))
103
- , (" mutable" , (" Mutable Variables" , BU. toString $ (embedFile " examples/mutable.purs" )))
104
- , (" modules" , (" Modules" , BU. toString $ (embedFile " examples/modules.purs" )))
105
- , (" rank2" , (" Rank N Types" , BU. toString $ (embedFile " examples/rankn.purs" )))
106
- , (" recursion" , (" Recursion" , BU. toString $ (embedFile " examples/recursion.purs" )))
107
- , (" do" , (" Do Notation" , BU. toString $ (embedFile " examples/do.purs" )))
108
- , (" tco" , (" Tail-Call Elimination" , BU. toString $ (embedFile " examples/tco.purs" )))
109
- , (" typeclasses" , (" Type Classes" , BU. toString $ (embedFile " examples/typeclasses.purs" )))
101
+ [ (" adt" , (" Algebraic Data Types" , BC8. unpack $ (embedFile " examples/adt.purs" )))
102
+ , (" ops" , (" Operators" , BC8. unpack $ (embedFile " examples/operators.purs" )))
103
+ , (" arrays" , (" Arrays" , BC8. unpack $ (embedFile " examples/arrays.purs" )))
104
+ , (" rows" , (" Row Polymorphism" , BC8. unpack $ (embedFile " examples/rows.purs" )))
105
+ , (" ffi" , (" FFI" , BC8. unpack $ (embedFile " examples/ffi.purs" )))
106
+ , (" mutable" , (" Mutable Variables" , BC8. unpack $ (embedFile " examples/mutable.purs" )))
107
+ , (" recursion" , (" Recursion" , BC8. unpack $ (embedFile " examples/recursion.purs" )))
108
+ , (" do" , (" Do Notation" , BC8. unpack $ (embedFile " examples/do.purs" )))
109
+ , (" tco" , (" Tail-Call Elimination" , BC8. unpack $ (embedFile " examples/tco.purs" )))
110
+ , (" typeclasses" , (" Type Classes" , BC8. unpack $ (embedFile " examples/typeclasses.purs" )))
110
111
]
111
112
112
- page :: Maybe String -> Maybe String -> Maybe Response -> ActionM ()
113
- page ex input res = html $ renderHtml $ do
113
+ page :: String -> ActionM ()
114
+ page input = html $ renderHtml $ do
114
115
H. docType
115
116
H. html $ do
116
117
H. head $ do
117
118
H. title $ H. toHtml $ str " Try PureScript!"
118
119
H. meta ! A. name " viewport" ! A. content " width=device-width, initial-scale=1.0"
119
- H. link ! A. rel " stylesheet" ! A. type_ " text/css" ! A. href " http://fonts.googleapis.com/css?family=PT+Serif:400,700"
120
- H. link ! A. rel " stylesheet" ! A. type_ " text/css" ! A. href " http://fonts.googleapis.com/css?family=Ubuntu+Mono"
120
+ H. link ! A. rel " stylesheet" ! A. type_ " text/css" ! A. href " http://fonts.googleapis.com/css?family=Roboto:300,600"
121
121
H. style $ H. toHtml $ str css
122
122
H. script ! A. type_ " text/javascript" $ preEscapedToHtml gaq
123
123
H. script ! A. type_ " text/javascript" ! A. src " //cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js" $ mempty
@@ -132,52 +132,36 @@ page ex input res = html $ renderHtml $ do
132
132
! customAttribute " data-canonical-src" " https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"
133
133
H. div ! A. class_ " wrapper" $ do
134
134
H. div ! A. class_ " header" $ do
135
- H. h1 $ H. toHtml $ str " Try PureScript!"
135
+ H. h1 $ H. toHtml $ str " Try PureScript!"
136
136
H. div ! A. class_ " body" $ do
137
- H. p $ H. toHtml $ str " Type PureScript code below and press 'Compile', or select one of the examples below:"
138
-
139
- H. h2 $ H. toHtml $ str " Examples"
140
- H. ul $ do
141
- forM_ examples $ \ (name, (title, _)) ->
142
- H. li $ H. a ! A. href (fromString $ " /example/" ++ name) $ H. toHtml title
143
-
144
- let (success, text) = responseToJs res
145
-
146
- H. h2 $ H. toHtml $ str " PureScript Code"
147
- H. form ! A. action " /compile/html" ! A. method " POST" $ do
148
- H. div ! A. id " code" $ mempty
149
- H. textarea ! A. name " code" ! A. id " textarea" ! A. style " display: none;" $ maybe mempty (H. toHtml . str) input
150
- H. div $ H. button ! A. type_ " submit" $ H. toHtml $ str " Compile"
151
- H. script ! A. type_ " text/javascript" $ preEscapedToHtml ace
152
-
153
- H. h2 $ H. toHtml $ str " Generated Javascript"
154
- H. pre $ H. code $ H. toHtml . str $ text
155
-
156
- responseToJs :: Maybe Response -> (Bool , String )
157
- responseToJs Nothing = (False , " " )
158
- responseToJs (Just (Response (Left err))) = (False , err)
159
- responseToJs (Just (Response (Right (Compiled " " " " )))) = (False , " Please enter some input" )
160
- responseToJs (Just (Response (Right (Compiled js _)))) = (True , js)
137
+ H. p $ H. toHtml $ str " Type PureScript code below and press 'Compile', or select one of the examples below:"
138
+
139
+ H. h2 $ H. toHtml $ str " Examples"
140
+ H. ul $ do
141
+ forM_ examples $ \ (name, (title, _)) ->
142
+ H. li $ H. a ! A. href (fromString $ " /example/" ++ name) $ H. toHtml title
143
+
144
+ H. h2 $ H. toHtml $ str " PureScript Code"
145
+ H. div ! A. id " code" $ mempty
146
+ H. textarea ! A. name " code" ! A. id " textarea" ! A. style " display: none;" $ H. toHtml $ str input
147
+ H. p $ H. button ! A. id " compile" $ H. toHtml $ str " Compile and Run"
148
+ H. script ! A. type_ " text/javascript" $ preEscapedToHtml scripts
149
+ H. div ! A. id " results" $ mempty
161
150
162
151
server :: Int -> IO ()
163
152
server port = do
164
- let preludeModules = either (error . show ) id $ P. runIndentParser " " P. parseModules prelude
153
+ let preludeModules = either (error . show ) ( map snd ) $ P. parseModulesFromFiles ( const " <prelude> " ) [( undefined , P. prelude)]
165
154
scotty port $ do
166
155
get " /" $ do
167
- page Nothing ( Just defaultCode) Nothing
156
+ page defaultCode
168
157
get " /example/:name" $ do
169
158
name <- param " name"
170
159
case lookup name examples of
171
160
Nothing -> raise " No such example"
172
161
Just (_, code) -> do
173
- response <- lift $ compile preludeModules code
174
- page (Just name) (Just code) (Just response)
175
- post " /compile/html" $ do
176
- code <- param " code"
177
- response <- lift $ compile preludeModules code
178
- page Nothing (Just code) (Just response)
162
+ page code
179
163
post " /compile/text" $ do
180
- code <- BUL. toString <$> body
164
+ code <- BLC8. unpack <$> body
181
165
response <- lift $ compile preludeModules code
182
166
case runResponse response of
183
167
Left err -> do
@@ -186,21 +170,7 @@ server port = do
186
170
Right comp ->
187
171
Scotty. text . fromString $ js comp
188
172
189
- term :: Term (IO () )
190
- term = server <$> port
191
-
192
- port :: Term Int
193
- port = value $ opt 80 $ (optInfo [ " p" , " port" ])
194
- { optDoc = " The port to listen on" }
195
-
196
- termInfo :: TermInfo
197
- termInfo = defTI
198
- { termName = " trypurescript"
199
- , version = showVersion Paths. version
200
- , termDoc = " Try PureScript in the browser"
201
- }
202
-
203
173
main :: IO ()
204
- main = run (term, termInfo)
205
-
206
-
174
+ main = do
175
+ [port] <- getArgs
176
+ server ( read port)
0 commit comments