on master help sign in
Add markdown support. Add minification support.
commit0391c1ce9a9205bd11892069c24dbdeb8e865bf1 authorevancz <info@elm-lang.org> dateTue 11 Sep 2012, 02:53 (14 years ago) parent38d0e8f
6 files changed+78−32
elm/Elm.cabal+6−2 elm/changelog.txt+21 elm/src/Compiler.hs+16−9 elm/src/Gen/CompileToJS.hs+12−7 elm/src/Gen/GenerateHtml.hs+22−13 elm/src/Types/Hints.hs+1−1
elm/Elm.cabal+6−2
@@ -68,7 +68,9 @@ Library
6868 text,
6969 template-haskell,
7070 shakespeare,
71− pandoc
71+ pandoc,
72+ bytestring,
73+ hjsmin
7274
7375Executable elm
7476 Main-is: Compiler.hs
@@ -106,4 +108,6 @@ Executable elm
106108 blaze-markup == 0.5.1.*,
107109 deepseq,
108110 cmdargs,
109− pandoc
111+ pandoc,
112+ bytestring,
113+ hjsmin
elm/changelog.txt+21
@@ -1,4 +1,25 @@
11
2+Release 0.4.0
3+=============
4+
5+This version is all about graphics: nicer API with more features and major
6+efficiency improvements. I am really excited about this release!
7+
8+* Add native Markdown support. You can now embed markdown directly in .elm files
9+ and it is used as an `Element`. Syntax is `[markdown| ... |]` where `...` is
10+ formatted as described [here](http://daringfireball.net/projects/markdown/).
11+ Content can span multiple lines too.
12+
13+* Drastically improve the `collage` interface. You can now move, rotate, and scale
14+ the following forms:
15+ - Lines
16+ - Shapes (shapes can be textured now too)
17+ - Images
18+ - Elements (any Element you want can be turned into a Form with `toForm`)
19+ This will make it way easier to make games in Elm.
20+
21+* Add `--minify` flag, to minify JS code.
22+
223Release 0.3.6
324=============
425
elm/src/Compiler.hs+16−9
@@ -7,6 +7,9 @@ import Data.Maybe (fromMaybe)
77import System.Console.CmdArgs
88import Text.Blaze.Html.Renderer.String (renderHtml)
99
10+import qualified Text.Jasmine as JS
11+import qualified Data.ByteString.Lazy.Char8 as BS
12+
1013import Ast
1114import Initialize
1215import CompileToJS
@@ -22,6 +25,7 @@ data ELM =
2225 , only_js :: Bool
2326 , import_js :: [FilePath]
2427 , generate_noscript :: Bool
28+ , minify :: Bool
2529 }
2630 deriving (Data,Typeable,Show,Eq)
2731
@@ -33,26 +37,29 @@ elm = ELM { make = False &= help "automatically compile dependencies."
3337 , only_js = False &= help "Compile only to JavaScript."
3438 , import_js = [] &= typFile &= help "Include a JavaScript file before the body of the Elm program. Can be used many times. Files will be included in the given order."
3539 , generate_noscript = True &= help "Add generated <noscript> tag to HTML output."
40+ , minify = False &= help "Minify generated JavaScript"
3641 } &=
3742 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
38− summary "The Elm Compiler v0.3.6.2, (c) Evan Czaplicki"
43+ summary "The Elm Compiler v0.4.0, (c) Evan Czaplicki"
3944
4045main = do
4146 args <- cmdArgs elm
42− mini <- getDataFileName "elm-runtime-0.3.6.2.js"
47+ mini <- getDataFileName "elm-runtime-0.4.0.js"
4348 compileArgs mini args
4449
45−compileArgs mini (ELM _ [] _ _ _ _ _) =
50+compileArgs mini (ELM _ [] _ _ _ _ _ _) =
4651 putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
47−compileArgs mini (ELM make files rtLoc split only js nscrpt) =
48− mapM_ (fileTo get what js nscrpt $ fromMaybe mini rtLoc) files
52+compileArgs mini (ELM make files rtLoc split only js nscrpt isMini) =
53+ mapM_ (fileTo isMini get what js nscrpt $ fromMaybe mini rtLoc) files
4954 where get = if make then getModules [] else getModule
5055 what = if only then JS else
5156 if split then Split else HTML
5257
5358data What = JS | HTML | Split
5459
55−fileTo get what jsFiles noscript rtLoc file = do
60+fileTo isMini get what jsFiles noscript rtLoc file = do
61+ let jsStyle = if isMini then Minified else Readable
62+ let formatJS = if isMini then BS.unpack . JS.minify . BS.pack else id
5663 ems <- get file
5764 jss <- concat `fmap` mapM readFile jsFiles
5865 case ems of
@@ -62,11 +69,11 @@ fileTo get what jsFiles noscript rtLoc file = do
6269 js = name ++ ".js"
6370 html = name ++ ".html"
6471 in case what of
65− JS -> writeFile js $ jss ++ concatMap jsModule ms
66− HTML -> writeFile html . renderHtml $ modulesToHtml "" rtLoc jss noscript ms
72+ JS -> writeFile js . formatJS $ jss ++ concatMap jsModule ms
73+ HTML -> writeFile html . renderHtml $ modulesToHtml jsStyle "" rtLoc jss noscript ms
6774 Split -> do
6875 writeFile html . renderHtml $ linkedHtml rtLoc js ms
69− writeFile js $ jss ++ concatMap jsModule ms
76+ writeFile js . formatJS $ jss ++ concatMap jsModule ms
7077
7178getModules :: [String] -> FilePath -> IO (Either String [([String],Module)])
7279getModules uses file = do
elm/src/Gen/CompileToJS.hs+12−7
@@ -5,7 +5,7 @@ import Ast
55import Control.Arrow (first)
66import Control.Monad (liftM,(<=<),join,ap)
77import Data.Char (isAlpha,isDigit)
8−import Data.List (intercalate,sortBy,inits)
8+import Data.List (intercalate,sortBy,inits,foldl')
99import Data.Map (toList)
1010import Data.Maybe (mapMaybe)
1111import qualified Text.Pandoc as Pan
@@ -84,8 +84,8 @@ jsImport' (modul, Importing []) = jsImport' (modul, Hiding [])
8484jsImport' (modul, Importing vs) =
8585 concatMap (\v -> assign v $ modul ++ "." ++ v) vs
8686jsImport' (modul, Hiding vs) =
87− concat [ "\nfor(var i in " ++ modul ++ "){"
88− , assign "hiddenVars" . jsList $ map (\v -> "'" ++ v ++ "'") vs
87+ concat [ assign "hiddenVars" . jsList $ map (\v -> "'" ++ v ++ "'") vs
88+ , "\nfor(var i in " ++ modul ++ "){"
8989 , "\nif (hiddenVars.indexOf(i) >= 0) continue;"
9090 , globalAssign "this[i]" $ modul ++ "[i]"
9191 , "}" ]
@@ -142,10 +142,15 @@ toJS' expr =
142142 Let defs e -> jsLet defs e
143143 Case e cases -> caseToJS e cases
144144 Data name es -> (\ss -> jsList $ show name : ss) `liftM` mapM toJS' es
145− Markdown doc ->
146− return $ concat [ "text('<div style=\"height:0;width:0;\">&nbsp;</div>"
147− , filter (/='\n') $ Pan.writeHtmlString Pan.defaultWriterOptions doc
148− , "<div style=\"height:0;width:0;\">&nbsp;</div>')" ]
145+ Markdown doc -> return $ "text('" ++ pad ++ md ++ pad ++ "')"
146+ where md = formatMarkdown $ Pan.writeHtmlString Pan.defaultWriterOptions doc
147+ pad = "<div style=\"height:0;width:0;\">&nbsp;</div>"
148+
149+formatMarkdown = concatMap f
150+ where f '\'' = "\\'"
151+ f '\n' = ""
152+ f '"' = "\""
153+ f c = [c]
149154
150155jsLet defs e' = do
151156 body <- (++) `liftM` jsDefs defs `ap` (ret `liftM` toJS' e')
elm/src/Gen/GenerateHtml.hs+22−13
@@ -1,7 +1,8 @@
11{-# LANGUAGE OverloadedStrings #-}
22module GenerateHtml (generateHtml,
33 body, css, widgetBody,
4− modulesToHtml, linkedHtml
4+ modulesToHtml, linkedHtml,
5+ JSStyle (..)
56 ) where
67
78import Data.List (intercalate)
@@ -11,6 +12,9 @@ import qualified Text.Blaze.Html5 as H
1112import Text.Blaze.Html5 ((!))
1213import qualified Text.Blaze.Html5.Attributes as A
1314
15+import Text.Jasmine (minify)
16+import qualified Data.ByteString.Lazy.Char8 as BS
17+
1418import Ast
1519import Initialize
1620import CompileToJS
@@ -28,11 +32,15 @@ css = H.style ! A.type_ "text/css" $ preEscapedToMarkup
2832 \ -webkit-hyphens: auto; -ms-hyphens: auto; }\
2933--}
3034
31−makeScript :: Either String String -> H.Html
32−makeScript (Left s) =
35+data JSStyle = Minified | Readable
36+
37+makeScript :: JSStyle -> Either String String -> H.Html
38+makeScript _ (Left s) =
3339 H.script ! A.type_ "text/javascript" ! A.src (H.toValue s) $ ""
34−makeScript (Right s) =
35− H.script ! A.type_ "text/javascript" $ preEscapedToMarkup s
40+makeScript jsStyle (Right s) = H.script ! A.type_ "text/javascript" $ preEscapedToMarkup content
41+ where content = case jsStyle of
42+ Minified -> BS.unpack . minify . BS.pack $ s
43+ Readable -> s
3644
3745-- |This function compiles Elm code into simple HTML.
3846--
@@ -45,11 +53,11 @@ generateHtml :: String -- ^ Location of elm-min.js as expected by the browser
4553 -> Html
4654generateHtml libLoc title source =
4755 case initialize source of
48− Left err -> createHtml libLoc title (Right $ showErr err) (H.noscript "")
49− Right (escs, modul) -> modulesToHtml title libLoc [] True [(escs,modul)]
56+ Left err -> createHtml Readable libLoc title (Right $ showErr err) (H.noscript "")
57+ Right (escs, modul) -> modulesToHtml Readable title libLoc [] True [(escs,modul)]
5058
5159
52−modulesToHtml title libLoc jss nscrpt pairs = createHtml libLoc title' js noscript
60+modulesToHtml jsStyle title libLoc jss nscrpt pairs = createHtml jsStyle libLoc title' js noscript
5361 where modules = map snd pairs
5462 js = Right $ jss ++ concatMap jsModule pairs
5563 noscript = if nscrpt then extract $ last modules else ""
@@ -59,20 +67,21 @@ modulesToHtml title libLoc jss nscrpt pairs = createHtml libLoc title' js noscri
5967
6068
6169linkedHtml rtLoc jsLoc modules =
62− createHtml rtLoc title (Left jsLoc) (H.noscript "")
70+ createHtml Readable rtLoc title (Left jsLoc) (H.noscript "")
6371 where title = (\(Module names _ _ _) -> intercalate "." names) $
6472 snd (last modules)
6573
6674
67−createHtml libLoc title js noscript =
75+createHtml jsStyle libLoc title js noscript =
6876 H.docTypeHtml $ do
6977 H.head $ do
7078 H.meta ! A.charset "UTF-8"
7179 H.title . H.toHtml $ title
72− makeScript (Left libLoc)
73− makeScript js
7480 css
75− H.body $ body noscript
81+ H.body $ do
82+ makeScript Readable (Left libLoc)
83+ makeScript jsStyle js
84+ body noscript
7685
7786body noscript = do
7887 H.div ! A.id "widthChecker" ! A.style "width:100%; height:1px; position:absolute; top:-1px;" $ ""
elm/src/Types/Hints.hs+1−1
@@ -15,7 +15,7 @@ textToText = [ "header", "italic", "bold", "underline"
1515 , "overline", "strikeThrough", "monospace" ]
1616
1717textAttrs = [ "toText" -: string ==> text
18− , "link" -: string ==> text ==> text
18+ --, "link" -: string ==> text ==> text
1919 , numScheme (\t -> t ==> text ==> text) "Text.height"
2020 ] ++ hasType (text ==> text) textToText
2121