Expose fewer internal functions, making more information available only through toHtml :: String -> Html and toParts :: String -> (Html, String, String). Make the required changes to dependent files. Yesod.hs still needs one fix though.

This commit is contained in:
evancz 2012-05-29 13:25:43 -05:00
parent 1600aae698
commit a6cf0d2815
11 changed files with 234 additions and 236 deletions

View file

@ -7,7 +7,7 @@ import Data.List (isPrefixOf, isSuffixOf)
import Happstack.Server import Happstack.Server
import Happstack.Server.Compression import Happstack.Server.Compression
import System.Environment import System.Environment
import Language.Elm import qualified Language.Elm as Elm
serve :: String -> IO () serve :: String -> IO ()
serve libLoc = do serve libLoc = do
@ -25,7 +25,7 @@ serveElm libLoc fp = do
let ('/':path) = fp let ('/':path) = fp
guard (".elm" `isSuffixOf` path) guard (".elm" `isSuffixOf` path)
content <- liftIO (readFile path) content <- liftIO (readFile path)
ok . toResponse $ generateHtml libLoc (pageTitle path) content ok . toResponse $ Elm.toHtml libLoc (pageTitle path) content
main = getArgs >>= parse main = getArgs >>= parse

View file

@ -1,5 +1,5 @@
Name: Elm-server Name: Elm-server
Version: 0.1.1.8 Version: 0.1.2
Synopsis: The Elm language server. Synopsis: The Elm language server.
Description: This package provides a standalone, Happstack-based Elm server. Description: This package provides a standalone, Happstack-based Elm server.
@ -35,4 +35,4 @@ Executable elm-server
HTTP >= 4000, HTTP >= 4000,
happstack-server == 7.0.2, happstack-server == 7.0.2,
deepseq, deepseq,
Elm >= 0.1.1.8 Elm >= 0.1.2

View file

@ -15,42 +15,22 @@
A full example implementation is provided in the examples folder of the Elm github repository. A full example implementation is provided in the examples folder of the Elm github repository.
-} -}
module Language.Elm.Yesod (generateWidget) where module Language.Elm.Yesod (toWidget) where
import Text.Blaze (preEscapedToMarkup) import Text.Blaze (preEscapedToMarkup)
import Text.Hamlet import Text.Hamlet
import Text.Julius import Text.Julius
import Text.Lucius import Text.Cassius
import Yesod.Widget import Yesod.Widget
import Language.Elm
import Language.Elm.Initialize -- |toWidget takes some Elm code in String format and produces a widget. Usage example:
import Language.Elm.CompileToJS
import Language.Elm.ExtractNoscript
css = [lucius|
* { padding:0; margin:0;
hyphens: auto; -moz-hyphens: auto;
-webkit-hyphens: auto; -ms-hyphens: auto;}
body { font-family: Arial; }
a:link, a:visited, a:active { text-decoration: none}
a:hover {text-decoration: underline; color: #ff8f12;}
|]
-- |generateWidget takes some Elm code in String format and produces a widget. Usage example:
-- --
-- > generateWidget [elmFile|elm-source/somePage.elm|] -- > toWidget [elmFile|elm-source/somePage.elm|]
generateWidget :: String -- ^ The Elm source code toWidget :: String -- ^ The Elm source code
-> GWidget sub master () -> GWidget sub master ()
generateWidget source = toWidget source =
let expr = initialize source let (html, css, js) = toParts source
js = compileToJS expr in do toWidgetHead [cassius| css |]
noscript = either id extract expr
in do toWidgetHead css
toWidgetHead [julius| #{js} |] toWidgetHead [julius| #{js} |]
[whamlet| [whamlet| ^{html} |]
<div #widthChecker style="width:100%; height:1px; position:absolute; top:-1px;">
<span #content>
<script type="text/javascript">
Dispatcher.initialize()
<noscript>^{preEscapedToMarkup noscript}
|]

View file

@ -1,5 +1,5 @@
Name: Elm-yesod Name: Elm-yesod
Version: 0.1.1.8 Version: 0.1.2
Synopsis: The Elm language Yesod compatibility module. Synopsis: The Elm language Yesod compatibility module.
Description: This module provides a simple function to embed Elm code Description: This module provides a simple function to embed Elm code
as a Yesod widget. as a Yesod widget.
@ -28,7 +28,7 @@ Library
exposed-modules: Language.Elm.Yesod exposed-modules: Language.Elm.Yesod
Build-depends: base >=4.2 && <5, Build-depends: base >=4.2 && <5,
blaze-markup == 0.5.*, blaze-markup == 0.5.*,
Elm >= 0.1.1.8, Elm >= 0.1.2,
yesod-core >= 1, yesod-core >= 1,
hamlet, hamlet,
shakespeare-css, shakespeare-css,

View file

@ -22,34 +22,37 @@ Category: Compiler, Language
Build-type: Simple Build-type: Simple
Extra-source-files: README.md Extra-source-files: README.md
Cabal-version: >=1.6 Cabal-version: >=1.8
source-repository head source-repository head
type: git type: git
location: git://github.com/evancz/Elm.git location: git://github.com/evancz/Elm.git
Executable elm Library
-- .hs or .lhs file containing the Main module. exposed-modules: Language.Elm
Main-is: Compiler.hs ghc-options: -O2
Hs-Source-Dirs: src, src/Parse, src/Types, src/Language Hs-Source-Dirs: src, src/Parse, src/Types
other-modules: Ast, other-modules: Ast,
CompileToJS,
ExtractNoscript,
GenerateHtml,
Guid,
Initialize,
Rename,
Binop, Binop,
Combinators, Combinators,
Constrain,
Guid,
Hints,
Lexer, Lexer,
ParsePatterns, ParsePatterns,
Parser, Parser,
ParserLib, ParserLib,
ParseTypes, ParseTypes,
Rename,
Tokens, Tokens,
Types, Types,
Constrain,
Hints,
Types, Types,
Unify Unify
ghc-options: -O2
Build-depends: base >=4.2 && <5, Build-depends: base >=4.2 && <5,
containers >= 0.3, containers >= 0.3,
transformers >= 0.2, transformers >= 0.2,
@ -59,16 +62,16 @@ Executable elm
blaze-markup == 0.5.1.*, blaze-markup == 0.5.1.*,
deepseq deepseq
Library Executable elm
exposed-modules: Language.Elm, Main-is: Compiler.hs
Language.Elm.CompileToJS, ghc-options: -O2
Language.Elm.ExtractNoscript,
Language.Elm.Initialize,
Language.Elm.GenerateHtml,
Language.Elm.Quasi
Hs-Source-Dirs: src, src/Parse, src/Types Hs-Source-Dirs: src, src/Parse, src/Types
other-modules: Ast, other-modules: Ast,
CompileToJS,
ExtractNoscript,
GenerateHtml,
Guid, Guid,
Initialize,
Rename, Rename,
Binop, Binop,
Combinators, Combinators,
@ -91,8 +94,4 @@ Library
parsec >= 3.1.1, parsec >= 3.1.1,
blaze-html == 0.5.0.*, blaze-html == 0.5.0.*,
blaze-markup == 0.5.1.*, blaze-markup == 0.5.1.*,
deepseq, deepseq
template-haskell,
haskell-src-meta == 0.5.*,
text,
attoparsec == 0.10.*

View file

@ -1,5 +1,5 @@
module Language.Elm.CompileToJS (compile, compileToJS) where module CompileToJS (compile, compileToJS) where
import Ast import Ast
import Control.Monad (liftM,(<=<),join) import Control.Monad (liftM,(<=<),join)
@ -7,7 +7,7 @@ import Data.Char (isAlpha)
import Data.List (intercalate,sortBy) import Data.List (intercalate,sortBy)
import Data.Map (toList) import Data.Map (toList)
import Language.Elm.Initialize import Initialize
compile = (return . addMain . toJS) <=< initialize compile = (return . addMain . toJS) <=< initialize

View file

@ -1,8 +1,8 @@
module Main where module Main where
import Data.List (isPrefixOf) import Data.List (isPrefixOf)
import Language.Elm.CompileToJS import CompileToJS
import Language.Elm.GenerateHtml import GenerateHtml
import System.Environment import System.Environment
import Text.Blaze.Html.Renderer.String (renderHtml) import Text.Blaze.Html.Renderer.String (renderHtml)

View file

@ -1,5 +1,5 @@
module Language.Elm.ExtractNoscript (extract) where module ExtractNoscript (extract) where
import Ast import Ast

View file

@ -1,5 +1,5 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Language.Elm.GenerateHtml (generateHtml) where module GenerateHtml (generateHtml, body, css) where
import Text.Blaze (preEscapedToMarkup) import Text.Blaze (preEscapedToMarkup)
import Text.Blaze.Html (Html) import Text.Blaze.Html (Html)
@ -7,19 +7,18 @@ import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html5 ((!)) import Text.Blaze.Html5 ((!))
import qualified Text.Blaze.Html5.Attributes as A import qualified Text.Blaze.Html5.Attributes as A
import Language.Elm.Initialize import Initialize
import Language.Elm.CompileToJS import CompileToJS
import Language.Elm.ExtractNoscript import ExtractNoscript
css = preEscapedToMarkup $ css = "* { padding:0; margin:0; \
("* { padding:0; margin:0; \
\hyphens: auto; -moz-hyphens: auto;\ \hyphens: auto; -moz-hyphens: auto;\
\ -webkit-hyphens: auto; -ms-hyphens: auto; }\ \ -webkit-hyphens: auto; -ms-hyphens: auto; }\
\body { font-family: Arial; }\ \body { font-family: Arial; }\
\a:link {text-decoration: none}\ \a:link {text-decoration: none}\
\a:visited {text-decoration: none}\ \a:visited {text-decoration: none}\
\a:active {text-decoration: none}\ \a:active {text-decoration: none}\
\a:hover {text-decoration: underline; color: #ff8f12;}" :: String) \a:hover {text-decoration: underline; color: #ff8f12;}"
makeScript :: String -> H.Html makeScript :: String -> H.Html
makeScript s = H.script ! A.type_ "text/javascript" ! A.src (H.toValue s) $ "" makeScript s = H.script ! A.type_ "text/javascript" ! A.src (H.toValue s) $ ""
@ -44,8 +43,10 @@ generateHtml libLoc title source =
H.title . H.toHtml $ title H.title . H.toHtml $ title
makeScript libLoc makeScript libLoc
H.script ! A.type_ "text/javascript" $ preEscapedToMarkup js H.script ! A.type_ "text/javascript" $ preEscapedToMarkup js
H.style ! A.type_ "text/css" $ css H.style ! A.type_ "text/css" $ preEscapedToMarkup (css :: String)
H.body $ do H.body $ body noscript
body noscript = do
H.div ! A.id "widthChecker" ! A.style "width:100%; height:1px; position:absolute; top:-1px;" $ "" H.div ! A.id "widthChecker" ! A.style "width:100%; height:1px; position:absolute; top:-1px;" $ ""
H.span ! A.id "content" $ "" H.span ! A.id "content" $ ""
H.script ! A.type_ "text/javascript" $ "Dispatcher.initialize()" H.script ! A.type_ "text/javascript" $ "Dispatcher.initialize()"

View file

@ -1,4 +1,4 @@
module Language.Elm.Initialize (initialize) where module Initialize (initialize) where
import Ast import Ast
import Control.Arrow (first, second) import Control.Arrow (first, second)

View file

@ -1,21 +1,39 @@
{- | This module re-exports the modules necessary for compiling Elm code into the {- | This module exports the functions necessary for compiling Elm code into the
respective HTML, JS and CSS code. respective HTML, JS and CSS code.
It also provides a predefined generateHtml function for use with the Blaze markup library It also provides a predefined compileToHtml function for use with the Blaze markup library
as well as a simple QuasiQuoter for embedding literal elm code in a Haskell file. as well as a simple QuasiQuoter for embedding literal elm code in a Haskell file.
The documentation for the Elm language is available at <http://elm-lang.org/Documentation.elm> The documentation for the Elm language is available at <http://elm-lang.org/Documentation.elm>
-} -}
module Language.Elm ( module Language.Elm (
module Language.Elm.Initialize, toHtml,
module Language.Elm.CompileToJS, toParts) where
module Language.Elm.ExtractNoscript,
generateHtml, import CompileToJS
elm, import ExtractNoscript
elmFile) where import GenerateHtml
import Initialize
import Text.Blaze.Html (Html)
-- |This function compiles Elm code into a full HTML page.
--
-- Usage example:
--
-- > toHtml "/elm-min.js" "Some title" [elmFile|elm-source/somePage.elm|]
toHtml :: String -- ^ Location of elm-min.js as expected by the browser
-> String -- ^ The page title
-> String -- ^ The elm source code.
-> Html
toHtml = generateHtml
-- |This function compiles Elm code to three separate parts: HTML, CSS,
-- and JavaScript. The HTML is only the contents of the body, so the three
-- parts must be combined in a basic HTML skeleton.
toParts :: String -- ^ The Elm source code.
-> (Html, String, String) -- ^ HTML, CSS, and JavaScript in that order.
toParts source = (html, css, js)
where expr = initialize source
js = compileToJS expr
html = body $ either id extract expr
import Language.Elm.CompileToJS
import Language.Elm.ExtractNoscript
import Language.Elm.Initialize
import Language.Elm.GenerateHtml
import Language.Elm.Quasi