packages feed

bamboo-plugin-photo (empty) → 2009.7.5

raw patch · 12 files changed

+421/−0 lines, 12 filesdep +basedep +bytestringdep +data-defaultsetup-changed

Dependencies added: base, bytestring, data-default, directory, filepath, hack, hack-contrib, haskell98, hxt, mps, utf8-string, xhtml

Files

+ LICENSE view
@@ -0,0 +1,9 @@+Copyright (c) 2009, Jinjing Wang+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 the <ORGANIZATION> nor the names of its 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 HOLDER 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.
+ Nemesis view
@@ -0,0 +1,24 @@+nemesis = do+  +  clean+    [ "**/*.hi"+    , "**/*.o"+    , "manifest"+    ]+    +  desc "prepare cabal dist"+  task "dist" $ do+    sh "cabal clean"+    sh "cabal configure"+    sh "cabal sdist"++  desc "start console"+  task "i" (sh "ghci -isrc src/Bamboo/Plugin/Photo.hs")++  desc "put all .hs files in manifest"+  task "manifest" $ do+    sh "find . | grep 'hs$' > manifest"++  desc "show sloc"+  task "stat" $ do+    sh "cloc -match-f=hs$ --quiet src"
+ Setup.lhs view
@@ -0,0 +1,4 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> main = defaultMain
+ bamboo-plugin-photo.cabal view
@@ -0,0 +1,28 @@+Name:                 bamboo-plugin-photo+Version:              2009.7.5+Build-type:           Simple+Synopsis:             A photo album middleware+Description:+        +    It checks for a plugin tag in html and generate a photo album with thumbnails in place, by some internal IO options. The current version makes some assumptions on where the photo should be stored, so it's mostly useful for bamboo. A download of appropriate javascript is required, e.g. innerfade.js.++License:              BSD3+License-file:         LICENSE+Author:               Wang, Jinjing+Maintainer:           Wang, Jinjing <nfjinjing@gmail.com>+Build-Depends:        base+Cabal-version:        >= 1.2+category:             Web+homepage:             http://github.com/nfjinjing/hack/tree/master+data-files:           readme.md, changelog.md, known-issues.md, Nemesis++library+  ghc-options: -Wall+  build-depends: base >= 4 && < 5, mps > 2009.5, hack >= 2009.5.19, hack-contrib >= 2009.7.3, data-default, utf8-string, bytestring, haskell98, filepath, directory, xhtml, hxt+  hs-source-dirs: src/+  exposed-modules:  +                      Bamboo.Plugin.Photo+                      Bamboo.Plugin.Photo.Config+                      Bamboo.Plugin.Photo.Model+                      Bamboo.Plugin.Photo.Util+                      Bamboo.Plugin.Photo.View
+ changelog.md view
@@ -0,0 +1,4 @@+2009.7.5+--------++Init
+ known-issues.md view
@@ -0,0 +1,1 @@+* It assumes album in `db/public/images/your_album_name`
+ readme.md view
@@ -0,0 +1,23 @@+Example+=======++    < plugin +      type="photo"+      name="09-07-05 Project K on"+      show_description="no"+    />++Args+====++    data AlbumData = +        Prefix+      | Name+      | Pictures+      | ShowDescription+      | Type+      | Width+      deriving (Show)+++use snake case
+ src/Bamboo/Plugin/Photo.hs view
@@ -0,0 +1,74 @@+{-# LANGUAGE NamedFieldPuns #-}+{-# LANGUAGE Arrows #-}++module Bamboo.Plugin.Photo (photo) where++import Bamboo.Plugin.Photo.Config+import Data.ByteString.Lazy.Char8 (pack, unpack)+import Hack+import Hack.Contrib.Middleware.Censor+import MPSUTF8 hiding (at)+import Prelude hiding ((.), (>), (^), (/))+import Text.XML.HXT.Arrow hiding (first, mkText)+import Text.XML.HXT.Parser.HtmlParsec+import qualified Bamboo.Plugin.Photo.Model as Model+import qualified Bamboo.Plugin.Photo.View as View+++photo :: Middleware+photo = censor code+  where+    code r = do+      bodies <- r.body.unpack.play+      let b = if bodies.null then r.body else bodies.first.pack+      return r {body = b}++play :: String -> IO [String]+play s = runX (processor s)+++processor :: String -> IOSArrow XmlTree String+processor s =+  readString +    [ (a_validate,"0")+    , (a_parse_html, "1")+    , (a_encoding, utf8)+    ] s+  > process_source+  > writeDocumentToString +    [ (a_output_encoding, utf8)+    , (a_output_html, "1")+    , (a_no_empty_elements, "1")+    ]+  where+    process_source = processBottomUp (highlight_code `when` is_code_block)+    highlight_code = replaceChildren highlighted +      > changeElemName (const $ mkName "div")+  +    highlighted = proc x -> do+      name        <- getAttrValue "name"                -< x+      prefix      <- getAttrValue "prefix"              -< x+      show_desc   <- getAttrValue "show_description"    -< x+      album_type  <- getAttrValue "album_type"          -< x +      let {+        args = +          [ ("name", name)+          , ("prefix", prefix)+          , ("show_description", show_desc)+          , ("album_type", album_type)+          ]+        }+      h <- arrIO plug -< args+      returnA -< parseHtmlContent h .first+  +    is_code_block = +        isElem+      > hasName plugin_id+      > hasAttrValue plugin_type_key (is plugin_type_value)+++-- album-plugin+-- <plugin type="photo" name="some-album" prefix="\d+-"++plug :: [(String, String)] -> IO String+plug args = args.Model.from_list ^ View.render ^ show
+ src/Bamboo/Plugin/Photo/Config.hs view
@@ -0,0 +1,30 @@+module Bamboo.Plugin.Photo.Config where++import MPS.Light ((/))+import Prelude hiding ((/))++album_id          :: String+db_id             :: String+db_uri            :: String+image_id          :: String+image_uri         :: String+picture_prefix    :: String+plugin_id         :: String+plugin_type_key   :: String+plugin_type_value :: String+public_id         :: String+public_uri        :: String+thumb_id          :: String++album_id          = "album"+db_id             = "db"+db_uri            = db_id+image_id          = "images"+image_uri         = public_uri / image_id+picture_prefix    = ""+plugin_id         = "plugin"+plugin_type_key   = "type"+plugin_type_value = "photo"+public_id         = "public"+public_uri        = db_uri / public_id+thumb_id          = "thumb"
+ src/Bamboo/Plugin/Photo/Model.hs view
@@ -0,0 +1,108 @@+module Bamboo.Plugin.Photo.Model where++import Bamboo.Plugin.Photo.Config+import Bamboo.Plugin.Photo.Util+import Control.Monad (when)+import Data.Default+import Data.Maybe+import List (sort)+import MPSUTF8 hiding (at)+import Prelude hiding ((.), (>), (^), (/), id)+import System+import System.FilePath++data AlbumTypeData = Galleria | Fade | SlideViewer | Popeye deriving (Eq, Show, Read)++instance Default AlbumTypeData where+  def = Fade++data Album = Album+  { uid    :: String -- album/08-06-10+  , prefix :: String+  , show_description :: Bool+  , album_type :: AlbumTypeData+  , width :: Int+  , pictures :: [String]+  }+  deriving (Show, Eq)++get :: String -> Bool -> AlbumTypeData -> Int -> String -> IO Album+get pre desc t w id = do+  id.image_path.convert_if_missing_thumb w+  get_pictures id ^ Album id pre desc t w++get_pictures :: String -> IO [String]+get_pictures = image_path > ls > (^ filter is_image) > (^ sort)++get_picture_title :: String -> String -> String+get_picture_title pre x +  | pre.empty = x+  | otherwise = x.split pre.last.dropExtension++for_post :: String -> String -> IO Album+for_post pre id = get pre False def def (album_id / id.id_to_resource)++picture_links :: Album -> [String]+picture_links x = x.pictures .map ("/" / image_id / x.uid /)++picture_thumbs :: Album -> [String]+picture_thumbs x = +  x.pictures .map ("/" / image_id / x.uid / thumb_id /)++picture_titles :: Album -> [String]+picture_titles x = x.pictures .map (get_picture_title (x.prefix))++data_list :: Album -> [(String, String, String)]+data_list x = zip3 (x.picture_links) (x.picture_titles) (x.picture_thumbs)++data AlbumData = +    Prefix+  | Name+  | Pictures+  | ShowDescription+  | AlbumType+  | Width+  deriving (Show)++from_list :: [(String, String)] -> IO Album+from_list xs = get prefix' show_description' t w (album_id / at Name)+  where+    at x              = xs.lookup (x.show_data).fromJust+    prefix'           = at' Prefix $ picture_prefix+    show_description' = at' ShowDescription "n" .parse_boolean+    t                 = at' AlbumType "fade" .camel_case .read+    w                 = at' Width "400" .read+    at' x d = case xs.lookup (x.show_data) of+      Just y -> if y.null then d else y+      Nothing -> d++++convert_if_missing_thumb :: Int -> String -> IO ()+convert_if_missing_thumb w dir = do+  let thumb_path = dir / thumb_id+  thumb_exists <- dir_exist thumb_path+  when (not thumb_exists) $ do+    mkdir thumb_path >> convert w dir++convert :: Int -> String -> IO ()+convert w dir = do+  images <- ls dir ^ filter is_image+  images.map (convert_cmd w dir).sequence >>= print+  return ()+  where++image_extensions :: [String]+image_extensions = ["jpg", "jpeg", "png", "gif"]++is_image :: String -> Bool+is_image x = image_extensions.any (flip ends_with (x.lower))++convert_cmd :: Int -> String -> String -> IO ExitCode+convert_cmd w dir file = do+  sh $ ["convert -resize", w.show, i, o].join " ".trace'+  where+    i = quote $ dir / file+    o = quote $ dir / thumb_id / file+    sh = u2b > system+    quote x = "\"" ++ x ++ "\""
+ src/Bamboo/Plugin/Photo/Util.hs view
@@ -0,0 +1,27 @@+module Bamboo.Plugin.Photo.Util where++import Bamboo.Plugin.Photo.Config+import MPSUTF8+import Prelude hiding ((.), (>), (^), (/), id)+import System.Directory++mkdir :: String -> IO ()+mkdir = u2b > createDirectory++show_data :: (Show a) => a -> String+show_data = show > snake_case++parse_boolean :: String -> Bool+parse_boolean = belongs_to ["true", "1", "y", "yes", "yeah"]++ls_l :: String -> IO [String]+ls_l x = ls x ^ map (x /)++image_path :: String -> String+image_path id = image_uri / id++spaced_url :: String -> String+spaced_url = gsub "/" " / "++id_to_resource :: String -> String+id_to_resource x = x.split "/" .tail.join "/"
+ src/Bamboo/Plugin/Photo/View.hs view
@@ -0,0 +1,89 @@+{-# LANGUAGE NoImplicitPrelude #-}+module Bamboo.Plugin.Photo.View where++import Bamboo.Plugin.Photo.Model+import MPSUTF8+import Prelude hiding ((.), (>), (^), (/), id, div)+import Text.XHtml.Strict hiding (select, sub, meta)++id :: String -> HtmlAttr+id = identifier++div_id  :: String -> Html -> Html +div_id s = thediv ! [id s]++div_class :: String -> Html -> Html+div_class s = thediv ! [theclass s]++div_class_id :: String -> String -> Html -> Html+div_class_id x y = thediv ! [theclass x, id y]++span        :: Html -> Html+div         :: Html -> Html+d           :: Html -> Html+ul          :: Html -> Html++span       = thespan+div        = thediv+d          = div+ul         = ulist++klass :: String -> HtmlAttr+klass = theclass++c :: String -> Html -> Html+c x = d ! [klass x]+-- i x = d ! [id x]++ic, ci:: String -> String -> Html -> Html+ic x y = d ! [id x, klass y]+ci x y = ic y x++link :: String -> Html -> HotLink+link = hotlink++img         :: Html+space_html  :: Html++img        = image+space_html = primHtml "&#160;"++render :: Album -> Html+render = show_album++empty_html :: Html+empty_html = toHtml ""++show_album :: Album -> Html+show_album x = case x.album_type of+  Fade -> show_fade x+  Galleria -> show_galleria x+  SlideViewer -> show_slide_viewer x+  Popeye -> show_popeye x++show_popeye :: Album -> Html+show_popeye x = c "popeye" << ul << x.data_list.map picture_li+  where +    picture_li (l, t, i) = li << +      link l << img ! [src i, alt t]++show_slide_viewer :: Album -> Html+show_slide_viewer x = ul ! [id "slide-viewer", klass "svw"] +  << x.data_list.map picture_li +  where +    picture_li (_, t, i) = li << +      img ! [src i, alt t]++show_galleria :: Album -> Html+show_galleria x = ul ! [klass "gallery"] << x.data_list.map picture_li+  where +    picture_li (_, t, i) = li << +      img ! [src i, alt t]++show_fade :: Album -> Html+show_fade x = ul ! [klass "fade-album"] << x.data_list.map picture_li+  where +    picture_li (l, t, i) = li << +      [ toHtml $ link l << img ! [src i, alt t]+      , if x.show_description then p << t else empty_html+      ]