urlpath (empty) → 0.0.1
raw patch · 4 files changed
+196/−0 lines, 4 filesdep +basedep +mtldep +textsetup-changed
Dependencies added: base, mtl, text, transformers
Files
- LICENSE +21/−0
- Setup.hs +2/−0
- src/UrlPath.hs +100/−0
- urlpath.cabal +73/−0
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2014 Athan Clark++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/UrlPath.hs view
@@ -0,0 +1,100 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE KindSignatures #-}+{-# LANGUAGE InstanceSigs #-}+{-# LANGUAGE TypeFamilies #-}+++module UrlPath+ ( UrlReader (..)+ , Url (..)+ , module UrlPath.Types ) where++import UrlPath.Types++import qualified Data.Text as T++import Control.Monad.Trans+import Control.Monad.Reader.Class++import Data.Monoid+import Control.Applicative+import Control.Monad+import Data.Functor.Identity+++-- | Convenience typeclass for a uniform interface into pure @Reader@-like +-- monads.+class MonadReader T.Text m => UrlReader (m :: * -> *) where+ runUrlReader :: m a -- ^ Monadic reader-like computation+ -> T.Text -- ^ @T.Text@ index+ -> a -- ^ Result++instance MonadReader T.Text Identity where+ ask = return ""++instance UrlReader Identity where+ runUrlReader x _ = runIdentity x++instance UrlReader AbsoluteUrl where+ runUrlReader = runAbsoluteUrl++instance UrlReader RelativeUrl where+ runUrlReader = runRelativeUrl++instance UrlReader GroundedUrl where+ runUrlReader = runGroundedUrl++-- | @Url@ takes an input type @a@, and returns a modality @f@ around @T.Text@.+class Monad m => Url a m where+ renderUrl :: a -- ^ Url-like type (@UrlString@ or @T.Text@).+ -> m T.Text -- ^ Rendered Url in some context @f@++instance Url T.Text Identity where+ renderUrl = Identity++instance Url UrlString RelativeUrl where+ renderUrl x = RelativeUrl $ \_ -> expandRelative x++instance Url UrlString GroundedUrl where+ renderUrl x = GroundedUrl $ \_ -> expandGrounded x++instance Url UrlString AbsoluteUrl where+ renderUrl = expandAbsolute++instance Monad m => Url UrlString (RelativeUrlT m) where+ renderUrl x = RelativeUrlT $ \_ -> return $ expandRelative x++instance Monad m => Url UrlString (GroundedUrlT m) where+ renderUrl x = GroundedUrlT $ \_ -> return $ expandGrounded x++instance Monad m => Url UrlString (AbsoluteUrlT m) where+ renderUrl = expandAbsolute+++-- * Example+--+-- | Here is an example:+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- > {-# LANGUAGE ExtendedDefaultRules #-}+-- >+-- > import Lucid+-- > import Lucid.Path+-- > import qualified Data.Text as T+-- > import qualified Data.Text.Lazy as LT+-- >+-- > foo :: LT.Text+-- > foo = (runUrlReader $ renderTextT bar) "example.com"+-- >+-- > bar :: HtmlT AbsoluteUrl ()+-- > bar = do+-- > url <- lift $ renderUrl $ "foo.php" <?> ("bar","baz")+-- > script_ [src_ url] ""+--+-- Where @foo@ is now+--+-- > Lucid> foo+-- > "<script src=\"example.com/foo.php?bar=baz\"></script>"
+ urlpath.cabal view
@@ -0,0 +1,73 @@+Name: urlpath+Version: 0.0.1+Author: Athan Clark <athan.clark@gmail.com>+Maintainer: Athan Clark <athan.clark@gmail.com>+License: BSD3+License-File: LICENSE+Category: Web+Synopsis: Painfully simple URL writing combinators+Description:+ Simple URL DSL for Haskell.+ .+ Use raw combinators (kinda useless) ...+ .+ > render $ "foo.php" <?> ("key1","bar") <&> ("key2","baz")+ >+ > ↪ "foo.php?key1=bar&key2=baz"+ .+ ... or use the MonadReader instance for a configurable root ...+ .+ > let url = runReader $ expandAbsolute $ "foo.php" <?> ("key1","bar") <&> ("key2","baz")+ > url "example.com"+ >+ > ↪ "example.com/foo.php?key1=bar&key2=baz"+ .+ ... in Lucid ...+ .+ > (runReader $ renderTextT $+ > (\a -> do+ > foo <- lift $ expandAbsolute a+ > script_ [src_ foo] "" )+ > ("foo" <?> ("bar","baz"))+ > ) "example.com"+ >+ > ↪ "<script src=\"example.com/foo?bar=baz\"></script>"+ .+ ... and in Scotty ...+ .+ > main :: IO ()+ > main = scottyT 3000+ > rootConf+ > rootConf+ > run+ > + > where+ > rootConf = flip runReaderT "http://example.com"+ > + > run :: ( MonadIO m+ > , MonadReader T.Text m ) =>+ > ScottyT LT.Text m ()+ > run = get "/" $ do+ > path <- lift $ expandAbsolute $ "foo" <?> ("bar","baz")+ > text $ LT.fromStrict path+ >+ > λ> curl localhost:3000/+ > ↪ "http://example.com/foo?bar=baz"+ ++Cabal-Version: >= 1.10+Build-Type: Simple++Library+ Default-Language: Haskell2010+ HS-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules: UrlPath+ Build-Depends: base >= 4 && < 5+ , mtl+ , text+ , transformers++Source-Repository head+ Type: git+ Location: https://github.com/athanclark/urlpath.git