]> gitweb @ CieloNegro.org - Rakka.git/blob - Rakka/Wiki/Interpreter/Base.hs
6b883c376c270d68a2ecfaa00b112706bb358b66
[Rakka.git] / Rakka / Wiki / Interpreter / Base.hs
1 module Rakka.Wiki.Interpreter.Base
2     ( interpreters
3     )
4     where
5
6 import           Control.Arrow
7 import           Control.Arrow.ListArrow
8 import           Data.Map (Map)
9 import qualified Data.Map as M
10 import           Data.Maybe
11 import           Rakka.Page
12 import           Rakka.SystemConfig
13 import           Rakka.Wiki
14 import           Rakka.Wiki.Interpreter
15 import           Text.XML.HXT.Arrow.XmlArrow
16 import           Text.XML.HXT.Arrow.XmlNodeSet
17
18
19 interpreters :: [Interpreter]
20 interpreters = [ lineBreakInterp
21                , spanInterp
22                , divInterp
23                , pageNameInterp
24                , otherLangsInterp
25                , newPageInterp
26                , editPageInterp
27                ]
28
29
30 lineBreakInterp :: Interpreter
31 lineBreakInterp = InlineCommandInterpreter {
32                     iciName      = "br"
33                   , iciInterpret
34                       = \ _ (InlineCommand _ attrs _) -> return $ LineBreak attrs
35                   }
36
37
38 spanInterp :: Interpreter
39 spanInterp = InlineCommandInterpreter {
40                iciName      = "span"
41              , iciInterpret
42                  = \ _ (InlineCommand _ attrs contents) -> return $ Span attrs contents
43              }
44
45
46 divInterp :: Interpreter
47 divInterp = BlockCommandInterpreter {
48               bciName      = "div"
49             , bciInterpret
50                 = \ _ (BlockCommand _ attrs contents)
51                 -> return $ Div attrs (map Block contents)
52             }
53
54
55 pageNameInterp :: Interpreter
56 pageNameInterp = InlineCommandInterpreter {
57                    iciName      = "pageName"
58                  , iciInterpret
59                      = \ ctx _ -> return $ Text (ctxPageName ctx)
60                  }
61
62
63 otherLangsInterp :: Interpreter
64 otherLangsInterp
65     = BlockCommandInterpreter {
66         bciName      = "inOtherLanguages"
67       , bciInterpret
68           = \ ctx _ ->
69             let linkTable = case ctxMainPage ctx of
70                               Just page -> runLA ( getXPathTreesInDoc "/page/otherLang/link"
71                                                    >>>
72                                                    ( getAttrValue0 "lang"
73                                                      &&&
74                                                      getAttrValue0 "page"
75                                                    )
76                                                  ) page
77                               Nothing   -> []
78             in
79               case linkTable of
80                 [] -> return EmptyBlock
81                 _  -> do Languages langTable <- getSysConf (ctxSysConf ctx)
82                          let merged = mergeTables langTable linkTable
83                          return $ mkLangList merged
84       }
85     where
86       mergeTables :: Map LanguageTag LanguageName
87                   -> [(LanguageTag, PageName)]
88                   -> [(LanguageName, PageName)]
89       mergeTables _ []     = []
90       mergeTables m (x:xs) = let (langTag, name) = x
91                                  langName        = fromMaybe langTag (M.lookup langTag m)
92                              in
93                                (langName, name) : mergeTables m xs
94
95       mkLangList :: [(LanguageName, PageName)] -> BlockElement
96       mkLangList xs = List Bullet (map mkLangLink xs)
97
98       mkLangLink :: (LanguageName, PageName) -> ListItem
99       mkLangLink (langName, name)
100           = [Inline (PageLink (Just name) Nothing (Just langName))]
101
102
103 -- <input type="button"
104 --        value="Create new page"
105 --        onclick="Rakka.newPage()"
106 --        class="newButton" />
107 newPageInterp :: Interpreter
108 newPageInterp 
109     = InlineCommandInterpreter {
110         iciName      = "newPage"
111       , iciInterpret
112           = \ _ (InlineCommand _ args _) ->
113             let label = fromMaybe "Create new page" (lookup "label" args)
114                 attrs = [ ("type"   , "button")
115                         , ("value"  , label)
116                         , ("onclick", "Rakka.newPage()")
117                         , ("class"  , "newButton")
118                         ]
119             in
120               return (Input attrs)
121       }
122
123
124 -- <input type="button"
125 --        value="Edit"
126 --        onclick="Rakka.editPage(\"Foo\")"
127 --        class="editButton" />
128 editPageInterp :: Interpreter
129 editPageInterp 
130     = InlineCommandInterpreter {
131         iciName      = "editPage"
132       , iciInterpret
133           = \ ctx (InlineCommand _ args _) ->
134             let name  = fromMaybe (ctxPageName ctx) (lookup "page" args)
135                 label = fromMaybe "Edit this page" (lookup "label" args)
136                 attrs = [ ("type"   , "button")
137                         , ("value"  , label)
138                         , ("onclick", "Rakka.editPage(\"" ++ name ++ "\")")
139                         , ("class"  , "editButton")
140                         ]
141             in
142               return (Input attrs)
143       }