6 module Rakka.Wiki.Interpreter.Base
10 import Control.Applicative
12 import Control.Arrow.ListArrow
13 import Control.Arrow.Unicode
14 import qualified Data.CaseInsensitive as CI
16 import qualified Data.Map as M
18 import Data.Monoid.Unicode
19 import qualified Data.Text as T
20 import Prelude.Unicode
22 import Rakka.SystemConfig
24 import Rakka.Wiki.Interpreter
25 import Text.XML.HXT.Arrow.XmlArrow
26 import Text.XML.HXT.XPath
28 interpreters ∷ [Interpreter]
29 interpreters = [ lineBreakInterp
41 lineBreakInterp :: Interpreter
42 lineBreakInterp = InlineCommandInterpreter {
45 = \ _ (InlineCommand _ attrs _) -> return $ LineBreak attrs
49 spanInterp :: Interpreter
50 spanInterp = InlineCommandInterpreter {
53 = \ _ (InlineCommand _ attrs contents) -> return $ Span attrs contents
57 divInterp :: Interpreter
58 divInterp = BlockCommandInterpreter {
61 = \ _ (BlockCommand _ attrs contents)
62 -> return $ Div attrs (map Block contents)
66 pageNameInterp :: Interpreter
67 pageNameInterp = InlineCommandInterpreter {
70 = \ ctx _ -> return $ Text (fromMaybe "" $ ctxPageName ctx)
73 otherLangsInterp ∷ Interpreter
75 = BlockCommandInterpreter {
76 bciName = "inOtherLanguages"
78 = \(InterpreterContext {..}) _ →
79 let linkTable = case ctxMainPage of
80 Just page → runLA ( getXPathTreesInDoc "/page/otherLang/link"
82 ( getAttrValue0 "lang"
90 [] -> return EmptyBlock
91 _ -> do Languages langTable ← getSysConf ctxSysConf
92 let merged = mergeTables langTable $
93 (CI.mk ∘ T.pack ⁂ T.pack) <$> linkTable
94 pure $ mkLangList merged
97 mergeTables ∷ Map LanguageTag LanguageName
98 → [(LanguageTag, PageName)]
99 → [(LanguageName, PageName)]
100 mergeTables _ [] = []
101 mergeTables m (x:xs) = let (langTag, name) = x
102 langName = fromMaybe (CI.foldedCase langTag)
105 (langName, name) : mergeTables m xs
107 mkLangList ∷ [(LanguageName, PageName)] → BlockElement
108 mkLangList = List Bullet ∘ (mkLangLink <$>)
110 mkLangLink ∷ (LanguageName, PageName) → ListItem
111 mkLangLink (langName, name)
112 = [Inline (PageLink (Just name) Nothing (Just langName))]
115 -- <input type="button"
116 -- value="Create new page"
117 -- onclick="Rakka.newPage()"
118 -- class="newButton controls" />
119 newPageInterp :: Interpreter
121 = InlineCommandInterpreter {
124 = \ _ (InlineCommand _ args _) ->
125 let label = fromMaybe "Create new page" (lookup "label" args)
126 attrs = [ ("type" , "button")
128 , ("onclick", "Rakka.newPage()")
129 , ("class" , "newButton controls")
136 -- <input type="button"
138 -- onclick="Rakka.editPage(\"Foo\")"
139 -- class="editButton controls" />
140 editPageInterp ∷ Interpreter
142 = InlineCommandInterpreter {
145 = \ctx (InlineCommand _ args _) →
146 let name = fromMaybe (fromMaybe "" $ ctxPageName ctx) (lookup "page" args)
147 label = fromMaybe "Edit this page" (lookup "label" args)
148 attrs = [ ("type" , "button")
150 , ("onclick", "Rakka.editPage(\"" ⊕ name ⊕ "\")")
151 , ("class" , "editButton controls")
158 -- <input type="button"
160 -- class="loginButton controls" />
161 loginInterp :: Interpreter
163 = InlineCommandInterpreter {
167 let attrs = [ ("type" , "button")
169 , ("class", "loginButton controls")
176 -- <input type="text"
177 -- class="searchField" />
178 searchFieldInterp :: Interpreter
180 = InlineCommandInterpreter {
181 iciName = "searchField"
184 let attrs = [ ("type" , "text")
185 , ("class", "searchField")
191 -- <input type="button"
192 -- value="Configuration"
193 -- class="configButton controls" />
194 configurationInterp ∷ Interpreter
196 = InlineCommandInterpreter {
197 iciName = "configuration"
200 let attrs = [ ("type" , "button")
201 , ("value", "Configuration")
202 , ("class", "configButton controls")