X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Wiki.hs;h=7d337c9f679b3b14ce01e1bf6716864a3390fb6d;hb=cfd9357931f136f52df2becd0af605111e102234;hp=1708f57f7d094276c3afd7dcc01c81e05f315a39;hpb=38f06e9262e9b2dd14906492539006602225d49e;p=yawbih.git diff --git a/Wiki.hs b/Wiki.hs index 1708f57..7d337c9 100644 --- a/Wiki.hs +++ b/Wiki.hs @@ -1,11 +1,16 @@ module Wiki ( - Backend + Backend (getCurrent,getList,get,setCurrent,update) ,PGB + ,createPGB + ,Markup (Text, Paragraph, Link) + ,Document + ,wikiParser ) where import Dbconnect import Data.Char +import Text.ParserCombinators.Parsec class Backend a where --Keyword -> (Full text,date) @@ -15,7 +20,7 @@ class Backend a where getList :: a -> String -> IO [(String, String, String, String)] --Keyword -> id -> Full text - get :: a -> String -> String -> IO String + get :: a -> String -> String -> IO (Maybe String) --Keyword -> id -> () setCurrent :: a -> String -> String -> IO Bool @@ -24,11 +29,57 @@ class Backend a where update :: a -> String -> String -> String -> String -> IO String data Markup = Text String - | Bold String - | Paragraph [Markup] + | Paragraph + | Link String String type Document = [Markup] +wikiParser :: GenParser Char st Document +wikiParser = do + s <- (try (pPara) + <|> pSpace + <|> try (pLinkLong) + <|> try (pLink) + <|> pOtherChar + <|> pText) + ss <- (wikiParser <|> return []) + return (s:ss) + +pPara = do + pEol + pEol + return (Paragraph) + + +pSpace = do + space + return (Text " ") + +pEol = char '\n' <|> do + char '\r' + char '\n' + +pLinkLong = do + string "[[" + l <- many1 $ noneOf ['|'] + char '|' + d <- many1 $ noneOf "]" + string "]]" + return (Link l d) +pLink = do + string "[[" + l <- many1 $ noneOf "]" + string "]]" + return (Link l l) +pOtherChar = do + c <- oneOf ",;.:!?[]()\'\"=-%$£<>/\\|" + return (Text (c:[])) + +pText = do + t <- many1 alphaNum--(noneOf ['\n','\r','[',']']) + return (Text t) + + newtype PGB = PGB DBService createPGB :: String -> String -> String -> String -> IO PGB @@ -49,24 +100,24 @@ instance Backend PGB where list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list - get (PGB db) key id = return "" + get (PGB db) key id = do + list <- selectReturnTuples db $ "SELECT fulltext from fulltexts WHERE id = "++tov id + case list of + [s]:_ -> return (Just s) + _ -> return Nothing setCurrent (PGB db) key id = do full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'" - cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword='"++tov key++"'" + cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword="++tov key case full of [[]] -> do - print "No text found with this id and key" return False _ -> do rows <- case cur of - [] -> do - print "No link found in current, inserting" - execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov id++","++tov key++")" + [[]] -> do + execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov key++","++tov id++")" _ -> do - print "Link found in current, updating" execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key - print rows if rows == 1 then return True else return False