browse (empty) → 0.5.0.0
raw patch · 4 files changed
+129/−0 lines, 4 filesdep +basedep +directorydep +filepath
Dependencies added: base, directory, filepath, open-browser, text
Files
- Browse.hs +62/−0
- LICENSE +16/−0
- browse.cabal +44/−0
- test.hs +7/−0
+ Browse.hs view
@@ -0,0 +1,62 @@+{-|++This module provides simple utility functions useful to present web+content to the user via their default browser. Use `browse` in+order to simply show some markup (HTML) to the user, and use+`browseLinked` with an array of `Asset` in order to show a page+linking to multiple files, for example a page linking to a local style+file and a local Javascript file++-}+module Browse (+ browse,+ Asset(..),+ browseLinked,+ ) where++import Control.Exception+import Web.Browser (openBrowser)+import System.IO+import System.Directory (createDirectory)+import System.FilePath+import qualified Data.Text as T+import qualified Data.Text.IO as T++-- | Writes its argument to a file and opens the user's browser +-- to show that file+browse content = do+ let path = "browse.html"+ writeContentToTempFile content path+ openBrowser path+ pure ()++writeContentToTempFile content fileName = do+ handle <- openFile fileName ReadWriteMode+ hPutStr handle content+ hFlush handle++-- | Use this to create resources that are linking to+-- each other, and you want to show to the user all together. For+-- example one resource could contain some HTML linking to another+-- resource containing Javascript or other. +data Asset = Asset {+ location :: FilePath, -- ^ 'location' should contain a relative URL where the resource will be placed. For example this could be @"script.js"@ for a Javascript file+ content :: T.Text+}++writeAsset (Asset loc con) = T.writeFile loc con++-- | Present multiple files via the user's browser, so that they can+-- reference each other. A directory is created, the files+-- are written there, and the browser is opened on the first resource+-- passed as an argument to this function+browseLinked :: [Asset] -> IO ()+browseLinked resources = do+ let containing = "browse"+ try $ createDirectory containing :: IO (Either SomeException ())+ mapM_ writeAsset (combined containing)+ (openBrowser . location . head) (combined containing)+ pure ()+ where+ combined dir = map (applyLocation (combine dir)) resources+ applyLocation f (Asset a b) = Asset (f a) b
+ LICENSE view
@@ -0,0 +1,16 @@+Poetic License+==============++© 2014 Francesco Occhipinti++This work ‘as-is’ I provide.+No warranty express or implied.+ For no purpose fit,+ not even a wee bit.+Liability for damages denied.++Permission is granted hereby,+to copy, share, and modify.+ Use it with glee,+ for profit or free.+On this notice, these rights rely.
+ browse.cabal view
@@ -0,0 +1,44 @@+-- Initial browse.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: browse+version: 0.5.0.0+synopsis: Library to present content to an user via their browser+description: Library to present content to an user via their browser+homepage: https://github.com/danse/browse+license: PublicDomain+license-file: LICENSE+author: danse+maintainer: f.occhipinti@gmail.com+-- copyright: +category: Scripting+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: Browse+ -- other-modules: + -- other-extensions: + build-depends: base >= 4.17.2 && < 4.18,+ directory >= 1.3.7 && < 1.4,+ filepath >= 1.4.2 && < 1.5,+ open-browser >= 0.4.0 && < 0.5,+ text >= 2.0.2 && < 2.1+ -- hs-source-dirs: + default-language: Haskell2010+ +Test-Suite test+ type: exitcode-stdio-1.0+ main-is: test.hs+ build-depends: base >= 4.17.2 && < 4.18,+ directory >= 1.3.7 && < 1.4,+ filepath >= 1.4.2 && < 1.5,+ open-browser >= 0.4.0 && < 0.5,+ text >= 2.0.2 && < 2.1+ default-language: Haskell2010+ other-modules: Browse++source-repository head+ type: git+ location: git@github.com:danse/browse.git
+ test.hs view
@@ -0,0 +1,7 @@+{-# LANGUAGE OverloadedStrings #-}+import Browse++s = Asset "script.js" "window.onload = alert('it works')"+i = Asset "index.html" "<script src=\"script.js\"></script>"++main = browseLinked [i, s]