[Break Layout out of HSX and into its own package Niklas Broberg **20080327220408] { adddir ./HSX addfile ./HSX/Layout.hs hunk ./HSX/Layout.hs 1 +{-# OPTIONS_GHC -fglasgow-exts #-} +{-# OPTIONS_GHC -fallow-undecidable-instances #-} +{-# OPTIONS_GHC -fallow-overlapping-instances #-} +module HSX.Layout ( + Layout(..), IsLayout(..), + above, beside, (^^), (<>) + )where + +import Prelude hiding ((^^)) + +import HSX.XMLGenerator + +data Layout m + = Above (Layout m) (Layout m) + | Beside (Layout m) (Layout m) + | Item (XMLGenT m [Child m]) + +class IsLayout m a where + toLayout :: a -> Layout m + + +instance IsLayout m (Layout m) where + toLayout = id + +instance (EmbedAsChild m c) => IsLayout m c where + toLayout = Item . asChild + +(^^),(<>),above,beside :: (IsLayout m a, IsLayout m b) => a -> b -> Layout m +a ^^ b = Above (toLayout a) (toLayout b) +a <> b = Beside (toLayout a) (toLayout b) +above = (^^) +beside = (<>) + + +instance (XMLGenerator m, + EmbedAsChild m (XMLGenT m (XML m)), + EmbedAsChild m (XMLGenT m [XML m]), + EmbedAsAttr m (Attr String String)) + => EmbedAsChild m (Layout m) where + asChild a@(Above _ _) = asChild ( + <% + mapM mkRow $ foldAbove a :: XMLGenT m [XML m] + %>
:: XMLGenT m (XML m)) + asChild b@(Beside _ _) = asChild ( + <% + <% + mapM mkCell $ foldBeside b :: XMLGenT m [XML m] + %> :: XMLGenT m (XML m) + %>
:: XMLGenT m (XML m)) + asChild (Item xml) = xml + +foldAbove :: Layout m -> [Layout m] +foldAbove (Above a b) = foldAbove a ++ foldAbove b +foldAbove l = [l] + +foldBeside :: Layout m -> [Layout m] +foldBeside (Beside a b) = foldBeside a ++ foldBeside b +foldBeside l = [l] + +mkRow :: forall m c . (XMLGenerator m, + EmbedAsChild m c, + EmbedAsChild m (XMLGenT m (XML m))) + => c -> XMLGenT m (XML m) +mkRow xml = <% (mkCell xml :: XMLGenT m (XML m)) %> + +mkCell :: (XMLGenerator m, EmbedAsChild m c) => c -> XMLGenT m (XML m) +mkCell xml = <% xml %> addfile ./LICENSE hunk ./LICENSE 1 +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. 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. + +3. Neither the name of the author nor the names of his contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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. addfile ./Setup.hs hunk ./Setup.hs 1 +import Distribution.Simple +main = defaultMain }