packages feed

sitemap (empty) → 0.1

raw patch · 5 files changed

+148/−0 lines, 5 filesdep +basedep +lensdep +taggysetup-changed

Dependencies added: base, lens, taggy, taggy-lens, text

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Alp Mestanogullari++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 Alp Mestanogullari 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ sitemap.cabal view
@@ -0,0 +1,25 @@+name:                sitemap+version:             0.1+synopsis:            Sitemap parser+description:         Sitemap parser. See the "Web.Sitemap" module for an example.+license:             BSD3+license-file:        LICENSE+author:              Alp Mestanogullari+maintainer:          alpmestan@gmail.com+copyright:           2015 Alp Mestanogullari+category:            Web+build-type:          Simple+cabal-version:       >=1.10++library+  exposed-modules:     Web.Sitemap+  other-modules:       Web.Sitemap.Types+  build-depends:+      base >=4 && <5+    , lens+    , taggy >= 0.1.3+    , taggy-lens+    , text+  hs-source-dirs:      src+  default-language:    Haskell2010+  ghc-options: 		     -Wall -O2
+ src/Web/Sitemap.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE OverloadedStrings #-}+------------------------------------------------------------------------------+-- |+-- Module:      Web.Sitemap+-- Copyright:   (c) 2015 Alp Mestanogullari+-- License:     BSD3+-- Maintainer:  Alp Mestanogullari <alpmestan@gmail.com>+-- Stability:   experimental+--+-- Parse sitemaps and extract information with lenses.+--+------------------------------------------------------------------------------++module Web.Sitemap+  ( -- * Usage+    -- $usage++    -- * Parsing+    parseSitemap++    -- * A sitemap 'Entry', with lenses+  , Entry(..)+  , loc+  , lastmod+  , changefreq+  , priority+  , URL+  ) where++import Control.Lens hiding (elements)+import Data.Text.Read (double)+import Text.Taggy.Lens+import Web.Sitemap.Types++import qualified Data.Text      as T+import qualified Data.Text.Lazy as LT++readDouble :: T.Text -> Maybe Double+readDouble = either (const Nothing) (Just . fst) . double++-- | Turn a sitemap into a list of 'Entry's.+parseSitemap :: LT.Text -> [Entry]+parseSitemap src =+  src ^.. html+        . elements+        . named (only "urlset")+        . elements+        . named (only "url")+        . to entry++entry :: HasElements s => s -> Entry+entry u = +  Entry (u ^?! elements . named (only "loc") . contents)+        (u ^? elements . named (only "lastmod") . contents)+        (u ^? elements . named (only "changefreq") . contents)+        (u ^? elements . named (only "priority") . contents . to readDouble . _Just)++-- $usage+--+-- Simply feed 'parseSitemap' with a sitemap as+-- some lazy 'LT.Text' (that you can fetch from+-- a distant web server or locally) and then+-- extract all the information you want using the+-- lenses.+--+-- > parseSitemap sitemapfilecontent ^.. traverse.loc+--+-- would hand you back all the 'URL's the sitemap points to.
+ src/Web/Sitemap/Types.hs view
@@ -0,0 +1,23 @@+{-# LANGUAGE TemplateHaskell #-}+module Web.Sitemap.Types+  ( URL+  , Entry(..)+  , loc+  , lastmod+  , changefreq+  , priority+  ) where++import Control.Lens+import Data.Text (Text)++type URL = Text++data Entry = Entry+  { _loc :: !URL+  , _lastmod :: Maybe Text+  , _changefreq :: Maybe Text+  , _priority :: Maybe Double+  } deriving (Eq, Show)++makeLenses ''Entry