]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - Rakka/Wiki/Interpreter/Base.hs
we can now create new pages
[Rakka.git] / Rakka / Wiki / Interpreter / Base.hs
index fa225e6b27144a5a253852bb66faacaf36401dd8..6feb92a77c82dcec77157cf3f0bff2929328a06f 100644 (file)
@@ -1,23 +1,31 @@
 module Rakka.Wiki.Interpreter.Base
-    ( baseInterpreters
+    ( interpreters
     )
     where
 
+import           Control.Arrow
+import           Control.Arrow.ListArrow
+import           Data.Map (Map)
+import qualified Data.Map as M
+import           Data.Maybe
+import           Network.URI
 import           Rakka.Page
+import           Rakka.SystemConfig
 import           Rakka.Wiki
 import           Rakka.Wiki.Interpreter
-import           Rakka.Wiki.Interpreter.Base.Image
+import           Text.XML.HXT.Arrow.XmlArrow
+import           Text.XML.HXT.Arrow.XmlNodeSet
 
 
-baseInterpreters :: [Interpreter]
-baseInterpreters = [ lineBreakInterp
-                   , spanInterp
-                   , divInterp
-                   , imageInterp
-                   , imgFrameInterp
-
-                   , pageNameInterp
-                   ]
+interpreters :: [Interpreter]
+interpreters = [ lineBreakInterp
+               , spanInterp
+               , divInterp
+               , pageNameInterp
+               , otherLangsInterp
+               , newPageInterp
+               , editPageInterp
+               ]
 
 
 lineBreakInterp :: Interpreter
@@ -40,7 +48,8 @@ divInterp :: Interpreter
 divInterp = BlockCommandInterpreter {
               bciName      = "div"
             , bciInterpret
-                = \ _ (BlockCommand _ attrs contents) -> return $ Div attrs contents
+                = \ _ (BlockCommand _ attrs contents)
+                -> return $ Div attrs (map Block contents)
             }
 
 
@@ -48,7 +57,94 @@ pageNameInterp :: Interpreter
 pageNameInterp = InlineCommandInterpreter {
                    iciName      = "pageName"
                  , iciInterpret
-                     = \ ctx (InlineCommand _ _ _) -> case ctxPage ctx of
-                                                        Nothing   -> return $ Text "(None)"
-                                                        Just page -> return $ Text $ pageName page
-                 }
\ No newline at end of file
+                     = \ 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(\"http://example.org/\")"
+--        class="newButton" />
+newPageInterp :: Interpreter
+newPageInterp 
+    = InlineCommandInterpreter {
+        iciName      = "newPage"
+      , iciInterpret
+          = \ ctx (InlineCommand _ args _) ->
+            do BaseURI baseURI <- getSysConf (ctxSysConf ctx)
+               
+               let label = fromMaybe "Create new page" (lookup "label" args)
+                   uri   = uriToString id baseURI ""
+                   attrs = [ ("type"   , "button")
+                           , ("value"  , label)
+                           , ("onclick", "Rakka.newPage(\"" ++ uri ++ "\")")
+                           , ("class"  , "newButton")
+                           ]
+
+               return (Input attrs)
+      }
+
+
+-- <input type="button"
+--        value="Edit"
+--        onclick="Rakka.editPage(\"http://example.org/\", \"Foo\")"
+--        class="editButton" />
+editPageInterp :: Interpreter
+editPageInterp 
+    = InlineCommandInterpreter {
+        iciName      = "editPage"
+      , iciInterpret
+          = \ ctx (InlineCommand _ args _) ->
+            do BaseURI baseURI <- getSysConf (ctxSysConf ctx)
+
+               let name  = fromMaybe (ctxPageName ctx) (lookup "page" args)
+                   label = fromMaybe "Edit this page" (lookup "label" args)
+                   uri   = uriToString id baseURI ""
+                   attrs = [ ("type"   , "button")
+                           , ("value"  , label)
+                           , ("onclick", "Rakka.editPage(\"" ++ uri ++ "\", \"" ++ name ++ "\")")
+                           , ("class"  , "editButton")
+                           ]
+
+               return (Input attrs)
+      }