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