]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - Rakka/Wiki/Interpreter/Base.hs
improved the page editor
[Rakka.git] / Rakka / Wiki / Interpreter / Base.hs
index 0070a837c376df6838bd2796d894cdaef62ccfa6..6b883c376c270d68a2ecfaa00b112706bb358b66 100644 (file)
@@ -3,8 +3,17 @@ module Rakka.Wiki.Interpreter.Base
     )
     where
 
+import           Control.Arrow
+import           Control.Arrow.ListArrow
+import           Data.Map (Map)
+import qualified Data.Map as M
+import           Data.Maybe
+import           Rakka.Page
+import           Rakka.SystemConfig
 import           Rakka.Wiki
 import           Rakka.Wiki.Interpreter
+import           Text.XML.HXT.Arrow.XmlArrow
+import           Text.XML.HXT.Arrow.XmlNodeSet
 
 
 interpreters :: [Interpreter]
@@ -12,6 +21,9 @@ interpreters = [ lineBreakInterp
                , spanInterp
                , divInterp
                , pageNameInterp
+               , otherLangsInterp
+               , newPageInterp
+               , editPageInterp
                ]
 
 
@@ -35,7 +47,8 @@ divInterp :: Interpreter
 divInterp = BlockCommandInterpreter {
               bciName      = "div"
             , bciInterpret
-                = \ _ (BlockCommand _ attrs contents) -> return $ Div attrs contents
+                = \ _ (BlockCommand _ attrs contents)
+                -> return $ Div attrs (map Block contents)
             }
 
 
@@ -45,3 +58,86 @@ pageNameInterp = InlineCommandInterpreter {
                  , iciInterpret
                      = \ ctx _ -> return $ Text (ctxPageName ctx)
                  }
+
+
+otherLangsInterp :: Interpreter
+otherLangsInterp
+    = BlockCommandInterpreter {
+        bciName      = "inOtherLanguages"
+      , bciInterpret
+          = \ ctx _ ->
+            let linkTable = case ctxMainPage ctx of
+                              Just page -> runLA ( getXPathTreesInDoc "/page/otherLang/link"
+                                                   >>>
+                                                   ( getAttrValue0 "lang"
+                                                     &&&
+                                                     getAttrValue0 "page"
+                                                   )
+                                                 ) page
+                              Nothing   -> []
+            in
+              case linkTable of
+                [] -> return EmptyBlock
+                _  -> do Languages langTable <- getSysConf (ctxSysConf ctx)
+                         let merged = mergeTables langTable linkTable
+                         return $ mkLangList merged
+      }
+    where
+      mergeTables :: Map LanguageTag LanguageName
+                  -> [(LanguageTag, PageName)]
+                  -> [(LanguageName, PageName)]
+      mergeTables _ []     = []
+      mergeTables m (x:xs) = let (langTag, name) = x
+                                 langName        = fromMaybe langTag (M.lookup langTag m)
+                             in
+                               (langName, name) : mergeTables m xs
+
+      mkLangList :: [(LanguageName, PageName)] -> BlockElement
+      mkLangList xs = List Bullet (map mkLangLink xs)
+
+      mkLangLink :: (LanguageName, PageName) -> ListItem
+      mkLangLink (langName, name)
+          = [Inline (PageLink (Just name) Nothing (Just langName))]
+
+
+-- <input type="button"
+--        value="Create new page"
+--        onclick="Rakka.newPage()"
+--        class="newButton" />
+newPageInterp :: Interpreter
+newPageInterp 
+    = InlineCommandInterpreter {
+        iciName      = "newPage"
+      , iciInterpret
+          = \ _ (InlineCommand _ args _) ->
+            let label = fromMaybe "Create new page" (lookup "label" args)
+                attrs = [ ("type"   , "button")
+                        , ("value"  , label)
+                        , ("onclick", "Rakka.newPage()")
+                        , ("class"  , "newButton")
+                        ]
+            in
+              return (Input attrs)
+      }
+
+
+-- <input type="button"
+--        value="Edit"
+--        onclick="Rakka.editPage(\"Foo\")"
+--        class="editButton" />
+editPageInterp :: Interpreter
+editPageInterp 
+    = InlineCommandInterpreter {
+        iciName      = "editPage"
+      , iciInterpret
+          = \ ctx (InlineCommand _ args _) ->
+            let name  = fromMaybe (ctxPageName ctx) (lookup "page" args)
+                label = fromMaybe "Edit this page" (lookup "label" args)
+                attrs = [ ("type"   , "button")
+                        , ("value"  , label)
+                        , ("onclick", "Rakka.editPage(\"" ++ name ++ "\")")
+                        , ("class"  , "editButton")
+                        ]
+            in
+              return (Input attrs)
+      }