hakyll-contrib-csv (empty) → 0.1.0.0
raw patch · 7 files changed
+261/−0 lines, 7 filesdep +basedep +blaze-htmldep +bytestringsetup-changed
Dependencies added: base, blaze-html, bytestring, cassava, hakyll, hakyll-contrib-csv, hspec, vector
Files
- LICENSE +30/−0
- README.md +79/−0
- Setup.hs +3/−0
- hakyll-contrib-csv.cabal +44/−0
- src/Hakyll/Contrib/Csv.hs +57/−0
- test/CsvSpec.hs +47/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Erik Stevenson (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Erik Stevenson nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,79 @@+# CSV to HTML table generator + +## Turn this + +```csv +Year,Make,Model,Description,Price +1997,Ford,E350,"ac, abs, moon",3000.00 +1999,Chevy,"Venture ""Extended Edition""","",4900.00 +1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00 +1996,Jeep,Grand Cherokee,"MUST SELL! +air, moon roof, loaded",4799.00 +``` + +## Into this + +```html +<table width="100%" class="display"> + <thead> + <tr> + <td>Year</td> + <td>Make</td> + <td>Model</td> + <td>Description</td> + <td>Price</td> + </tr> + </thead> + <tbody> + <tr> + <td>1997</td> + <td>Ford</td> + <td>E350</td> + <td>ac, abs, moon</td> + <td>3000.00</td> + </tr> + <tr> + <td>1999</td> + <td>Chevy</td> + <td>Venture "Extended Edition"</td> + <td></td> + <td>4900.00</td> + </tr> + <tr> + <td>1999</td> + <td>Chevy</td> + <td>Venture "Extended Edition, Very Large"</td> + <td></td> + <td>5000.00</td> + </tr> + <tr> + <td>1996</td> + <td>Jeep</td> + <td>Grand Cherokee</td> + <td>MUST SELL! air, moon roof, loaded</td> + <td>4799.00</td> + </tr> + </tbody> +</table> +``` + +# Usage + +```haskell +{-# LANGUAGE OverloadedStrings #-} + +import Hakyll +import Hakyll.Contrib.Csv + +main :: IO () +main = hakyll $ do + + match "csv/*.csv" $ do + route $ setExtension "html" `composeRoutes` gsubRoute "csv/" (const "") + compile $ + csvTable + >>= loadAndApplyTemplate "templates/layout.html" defaultContext + >>= relativizeUrls + + match "templates/*" $ compile templateCompiler +```
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple + +main = defaultMain
+ hakyll-contrib-csv.cabal view
@@ -0,0 +1,44 @@+name: hakyll-contrib-csv+version: 0.1.0.0+synopsis: Generate Html tables from Csv files+description:+ A Hakyll extension for incorporating Csv data into your static site.+homepage: https://github.com/narrative/hakyll-contrib-csv#readme+bug-reports: https://github.com/narrative/hakyll-contrib-csv/issues+license: BSD3+license-file: LICENSE+author: Erik Stevenson+maintainer: eriknstevenson@gmail.com+copyright: 2016 Erik Stevenson+category: Web+build-type: Simple+cabal-version: >=1.10++extra-source-files:+ README.md++library+ hs-source-dirs: src+ exposed-modules: Hakyll.Contrib.Csv+ default-language: Haskell2010+ ghc-options: -Wall+ build-depends: base < 4+ , blaze-html+ , bytestring+ , cassava+ , hakyll+ , vector++test-suite hakyll-contrib-csv-spec+ main-is: Spec.hs+ hs-source-dirs: test+ type: exitcode-stdio-1.0+ default-language: Haskell2010+ ghc-options: -Wall+ build-depends: base < 4+ , blaze-html+ , bytestring+ , cassava+ , hakyll-contrib-csv+ , hspec+ other-modules: CsvSpec
+ src/Hakyll/Contrib/Csv.hs view
@@ -0,0 +1,57 @@+-- | This module implements translation of CSV files to Blaze+-- HTML for use with the Hakyll static site compiler.++{-# LANGUAGE OverloadedStrings #-}++module Hakyll.Contrib.Csv ( csvCompileTable+ , csvTable+ , csvCompileTableContents+ , csvTableContents) where++import Control.Monad+import qualified Data.ByteString.Char8 as BS+import qualified Data.ByteString.Lazy.Char8 as LBS+import Data.Csv+import Data.Vector (Vector)+import qualified Data.Vector as V+import Hakyll+import qualified Text.Blaze.Html.Renderer.String as H+import Text.Blaze.Html5 (Html)+import qualified Text.Blaze.Html5 as H++-- |A Hakyll 'compiler' used to build a plain Html table from Csv data.+csvCompileTable :: Compiler (Item String)+csvCompileTable = getResourceLBS >>= withItemBody (return . H.renderHtml . csvTable)++-- |Generates Blaze Html data from a lazy bytestring of Csv data.+csvTable :: LBS.ByteString -> Html+csvTable bs =+ case decode NoHeader bs of+ Left err -> do+ H.h1 "Error parsing CSV file!"+ H.p $ H.toHtml err+ Right rows ->+ H.table $ do+ H.thead $ makeRow (V.head rows)+ H.tbody $ forM_ (V.tail rows) makeRow++-- |A Hakyll 'compiler' that builds only the inner part of an Html table from Csv data.+-- Typically inserted into '<table>' tags with a hakyll template.+csvCompileTableContents :: Compiler (Item String)+csvCompileTableContents = getResourceLBS >>= withItemBody (return . H.renderHtml . csvTableContents)++-- |Generates Blaze Html data representing the table without outer '<table>' tags.+-- This is useful when used in conjuction with a template to add additional properties to the table.+csvTableContents :: LBS.ByteString -> Html+csvTableContents bs =+ case decode NoHeader bs of+ Left err -> do+ H.h1 "Error parsing CSV file!"+ H.p $ H.toHtml err+ Right rows -> do+ H.thead $ makeRow (V.head rows)+ H.tbody $ forM_ (V.tail rows) makeRow++makeRow :: Vector BS.ByteString -> Html+makeRow row = H.tr $ forM_ row $ \column -> H.td $ H.toHtml . BS.unpack $ column+
+ test/CsvSpec.hs view
@@ -0,0 +1,47 @@+{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE OverloadedStrings #-} + +module CsvSpec where + +import Control.Monad +import Data.ByteString.Lazy.Char8 +import Data.Csv +import GHC.Generics +import Hakyll.Contrib.Csv +import Test.Hspec +import qualified Text.Blaze.Html.Renderer.String as H +import Text.Blaze.Html5 (Html) +import qualified Text.Blaze.Html5 as H + +spec :: Spec +spec = + describe "csvTable" $ do + it "generates correct Html." $ + (H.renderHtml . csvTableContents $ testCsv) `shouldBe` H.renderHtml testHtml + +data Person = Person {name :: !String, age :: !Int} + deriving (Generic, Show) + +instance ToNamedRecord Person +instance FromNamedRecord Person +instance DefaultOrdered Person + +testCsv :: ByteString +testCsv = encodeDefaultOrderedByName testPeople + +testHtml :: Html +testHtml = do + H.thead $ H.tr $ do + H.td $ H.toHtml ("name" :: String) + H.td $ H.toHtml ("age" :: String) + H.tbody $ forM_ testPeople $ \person -> + H.tr $ do + H.td $ H.toHtml $ name person + H.td $ H.toHtml $ age person + +testPeople :: [Person] +testPeople = + [ Person "Bobby" 29 + , Person "Michelle" 34 + , Person "Rachael" 19 + ]
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}