X-Git-Url: https://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FWiki%2FInterpreter%2FBase.hs;h=73950537c9bd7179b26655f0f38531df69201de4;hb=42f51754dea02201aececaacbf194d714cd58aaf;hp=70951e6884ab1d43936d0d830d35e41e3ac16a50;hpb=7a4f13a3d483c950743e1ced001ade4406d239d3;p=Rakka.git diff --git a/Rakka/Wiki/Interpreter/Base.hs b/Rakka/Wiki/Interpreter/Base.hs index 70951e6..7395053 100644 --- a/Rakka/Wiki/Interpreter/Base.hs +++ b/Rakka/Wiki/Interpreter/Base.hs @@ -1,35 +1,46 @@ +{-# LANGUAGE + OverloadedStrings + , RecordWildCards + , UnicodeSyntax + #-} module Rakka.Wiki.Interpreter.Base ( interpreters ) where - -import Control.Arrow -import Control.Arrow.ListArrow -import Data.Map (Map) +import Control.Applicative +import Control.Arrow +import Control.Arrow.ListArrow +import Control.Arrow.Unicode +import qualified Data.CaseInsensitive as CI +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 Text.XML.HXT.Arrow.XmlArrow -import Text.XML.HXT.Arrow.XmlNodeSet - - -interpreters :: [Interpreter] +import Data.Maybe +import Data.Monoid.Unicode +import qualified Data.Text as T +import Prelude.Unicode +import Rakka.Page +import Rakka.SystemConfig +import Rakka.Wiki +import Rakka.Wiki.Interpreter +import Text.XML.HXT.Arrow.XmlArrow +import Text.XML.HXT.XPath + +interpreters ∷ [Interpreter] interpreters = [ lineBreakInterp , spanInterp , divInterp , pageNameInterp , otherLangsInterp + , newPageInterp , editPageInterp + , loginInterp + , searchFieldInterp + , configurationInterp ] - lineBreakInterp :: Interpreter lineBreakInterp = InlineCommandInterpreter { - iciName = "br" + iciName = "br" , iciInterpret = \ _ (InlineCommand _ attrs _) -> return $ LineBreak attrs } @@ -37,7 +48,7 @@ lineBreakInterp = InlineCommandInterpreter { spanInterp :: Interpreter spanInterp = InlineCommandInterpreter { - iciName = "span" + iciName = "span" , iciInterpret = \ _ (InlineCommand _ attrs contents) -> return $ Span attrs contents } @@ -45,7 +56,7 @@ spanInterp = InlineCommandInterpreter { divInterp :: Interpreter divInterp = BlockCommandInterpreter { - bciName = "div" + bciName = "div" , bciInterpret = \ _ (BlockCommand _ attrs contents) -> return $ Div attrs (map Block contents) @@ -54,72 +65,142 @@ divInterp = BlockCommandInterpreter { pageNameInterp :: Interpreter pageNameInterp = InlineCommandInterpreter { - iciName = "pageName" + iciName = "pageName" , iciInterpret - = \ ctx _ -> return $ Text (ctxPageName ctx) + = \ ctx _ -> return $ Text (fromMaybe "" $ ctxPageName ctx) } - -otherLangsInterp :: Interpreter +otherLangsInterp ∷ Interpreter otherLangsInterp = BlockCommandInterpreter { - bciName = "inOtherLanguages" + bciName = "inOtherLanguages" , bciInterpret - = \ ctx _ -> - let linkTable = case ctxMainPage ctx of - Just page -> runLA ( getXPathTreesInDoc "/page/otherLang/link" - >>> - ( getAttrValue0 "lang" - &&& - getAttrValue0 "page" - ) - ) page - Nothing -> [] + = \(InterpreterContext {..}) _ → + let linkTable = case ctxMainPage 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 + _ -> do Languages langTable ← getSysConf ctxSysConf + let merged = mergeTables langTable $ + (CI.mk ∘ T.pack ⁂ T.pack) <$> linkTable + pure $ mkLangList merged } where - mergeTables :: Map LanguageTag LanguageName - -> [(LanguageTag, PageName)] - -> [(LanguageName, PageName)] + mergeTables ∷ Map LanguageTag LanguageName + → [(LanguageTag, PageName)] + → [(LanguageName, PageName)] mergeTables _ [] = [] mergeTables m (x:xs) = let (langTag, name) = x - langName = fromMaybe langTag (M.lookup langTag m) + langName = fromMaybe (CI.foldedCase langTag) + (M.lookup langTag m) in (langName, name) : mergeTables m xs - mkLangList :: [(LanguageName, PageName)] -> BlockElement - mkLangList xs = List Bullet (map mkLangLink xs) + mkLangList ∷ [(LanguageName, PageName)] → BlockElement + mkLangList = List Bullet ∘ (mkLangLink <$>) - mkLangLink :: (LanguageName, PageName) -> ListItem + mkLangLink ∷ (LanguageName, PageName) → ListItem mkLangLink (langName, name) = [Inline (PageLink (Just name) Nothing (Just langName))] +-- +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 controls") + ] + in + return (Input attrs) + } + + -- -editPageInterp :: Interpreter +-- onclick="Rakka.editPage(\"Foo\")" +-- class="editButton controls" /> +editPageInterp ∷ Interpreter editPageInterp = InlineCommandInterpreter { - iciName = "editPage" + iciName = "editPage" + , iciInterpret + = \ctx (InlineCommand _ args _) → + let name = fromMaybe (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 controls") + ] + in + return (Input attrs) + } + + +-- +loginInterp :: Interpreter +loginInterp + = InlineCommandInterpreter { + iciName = "login" + , iciInterpret + = \ _ _ -> + let attrs = [ ("type" , "button") + , ("value", "Login") + , ("class", "loginButton controls") + ] + in + return (Input attrs) + } + + +-- +searchFieldInterp :: Interpreter +searchFieldInterp + = InlineCommandInterpreter { + iciName = "searchField" + , iciInterpret + = \ _ _ -> + let attrs = [ ("type" , "text") + , ("class", "searchField") + ] + in + return (Input attrs) + } + +-- +configurationInterp ∷ Interpreter +configurationInterp + = InlineCommandInterpreter { + iciName = "configuration" , 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) + = \_ _ → + let attrs = [ ("type" , "button") + , ("value", "Configuration") + , ("class", "configButton controls") + ] + in + return (Input attrs) }