X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Wiki.hs;h=41fa15a8cd5d532b84aa03b79a97fc8fef79c017;hb=b9ff8d1cc183f8f07056efb4680b8b56556d4246;hp=bcfd23694a025b65363372dbb3f4f1ac0fa48978;hpb=80f11c089c02ef7a3f13404121715ca0f4d4d281;p=yawbih.git diff --git a/Wiki.hs b/Wiki.hs index bcfd236..41fa15a 100644 --- a/Wiki.hs +++ b/Wiki.hs @@ -1,21 +1,26 @@ module Wiki ( - Backend + Backend (getCurrent,getList,get,setCurrent,update) ,PGB + ,createPGB + ,Markup (Text, Paragraph, Link, Bold, Emph) + ,Document + ,wikiParser ) where import Dbconnect import Data.Char +import Text.ParserCombinators.Parsec class Backend a where --Keyword -> (Full text,date) getCurrent :: a -> String -> IO (Maybe (String,String)) - --Keyword -> [(id,date)] + --Keyword -> [(id,date,author,comment)] 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 @@ -23,6 +28,82 @@ class Backend a where --Keyword -> Full text -> id update :: a -> String -> String -> String -> String -> IO String +data Markup = Text String + | Paragraph + | Link String String + | Bold [Markup] + | Emph [Markup] + +type Document = [Markup] + +wikiParser :: GenParser Char st Document +wikiParser = do + s <- pMain + 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) + +pBold = do + string "**" + s <- pStringParser "**" + return (Bold s) + +pEmph = do + string "//" + s <- pStringParser "//" + return (Emph s) + +pStringParser xs = do + (string xs >> return []) <|> (do + s <- pMain + ss <- pStringParser xs + return (s:ss)) + +pMain = (try (pPara) + <|> pSpace + <|> try(pBold) + <|> try(pEmph) + <|> try (pLinkLong) + <|> try (pLink) + <|> pOtherChar + <|> pText) + +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 @@ -43,24 +124,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