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

View file

@ -1,5 +1,5 @@
Name: Elm-server
Version: 0.1.1.8
Version: 0.1.2
Synopsis: The Elm language server.
Description: This package provides a standalone, Happstack-based Elm server.
@ -35,4 +35,4 @@ Executable elm-server
HTTP >= 4000,
happstack-server == 7.0.2,
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.
-}
module Language.Elm.Yesod (generateWidget) where
module Language.Elm.Yesod (toWidget) where
import Text.Blaze (preEscapedToMarkup)
import Text.Hamlet
import Text.Julius
import Text.Lucius
import Text.Cassius
import Yesod.Widget
import Language.Elm
import Language.Elm.Initialize
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:
-- |toWidget takes some Elm code in String format and produces a widget. Usage example:
--
-- > generateWidget [elmFile|elm-source/somePage.elm|]
generateWidget :: String -- ^ The Elm source code
-- > toWidget [elmFile|elm-source/somePage.elm|]
toWidget :: String -- ^ The Elm source code
-> GWidget sub master ()
generateWidget source =
let expr = initialize source
js = compileToJS expr
noscript = either id extract expr
in do toWidgetHead css
toWidget source =
let (html, css, js) = toParts source
in do toWidgetHead [cassius| css |]
toWidgetHead [julius| #{js} |]
[whamlet|
<div #widthChecker style="width:100%; height:1px; position:absolute; top:-1px;">
<span #content>
<script type="text/javascript">
Dispatcher.initialize()
<noscript>^{preEscapedToMarkup noscript}
|]
[whamlet| ^{html} |]

View file

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

View file

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

View file

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

View file

@ -1,8 +1,8 @@
module Main where
import Data.List (isPrefixOf)
import Language.Elm.CompileToJS
import Language.Elm.GenerateHtml
import CompileToJS
import GenerateHtml
import System.Environment
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

View file

@ -1,5 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
module Language.Elm.GenerateHtml (generateHtml) where
module GenerateHtml (generateHtml, body, css) where
import Text.Blaze (preEscapedToMarkup)
import Text.Blaze.Html (Html)
@ -7,19 +7,18 @@ import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html5 ((!))
import qualified Text.Blaze.Html5.Attributes as A
import Language.Elm.Initialize
import Language.Elm.CompileToJS
import Language.Elm.ExtractNoscript
import Initialize
import CompileToJS
import ExtractNoscript
css = preEscapedToMarkup $
("* { padding:0; margin:0; \
css = "* { padding:0; margin:0; \
\hyphens: auto; -moz-hyphens: auto;\
\ -webkit-hyphens: auto; -ms-hyphens: auto; }\
\body { font-family: Arial; }\
\a:link {text-decoration: none}\
\a:visited {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 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
makeScript libLoc
H.script ! A.type_ "text/javascript" $ preEscapedToMarkup js
H.style ! A.type_ "text/css" $ css
H.body $ do
H.style ! A.type_ "text/css" $ preEscapedToMarkup (css :: String)
H.body $ body noscript
body noscript = do
H.div ! A.id "widthChecker" ! A.style "width:100%; height:1px; position:absolute; top:-1px;" $ ""
H.span ! A.id "content" $ ""
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 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.
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.
The documentation for the Elm language is available at <http://elm-lang.org/Documentation.elm>
-}
module Language.Elm (
module Language.Elm.Initialize,
module Language.Elm.CompileToJS,
module Language.Elm.ExtractNoscript,
generateHtml,
elm,
elmFile) where
toHtml,
toParts) where
import CompileToJS
import ExtractNoscript
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