+ list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key
+ case list of
+ [[]] -> return []
+ _ -> return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list
+
+ 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
+ case full of
+ [[]] -> do
+ return False
+ _ -> do
+ rows <- case cur of
+ [[]] -> do
+ execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov key++","++tov id++")"
+ _ -> do
+ execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key
+ if rows == 1 then return True
+ else return False
+
+ update (PGB db) key text author comment = do
+ rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")"
+ if rows == 0 then return ""
+ else do
+ [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')"
+ setCurrent (PGB db) key id
+ return id
+
+ listKeys (PGB db) = do
+ list <- selectReturnTuples db $ "SELECT keyword FROM current keyword ORDER BY lower(keyword)"
+ case list of
+ [[]] -> return []
+ _ -> mapM (\[key] -> return key) list
+
+tov :: String -> String
+tov s = '\'':escapeQuery s++"'"
+
+toHtml :: [Markup] -> String
+toHtml [] = []
+toHtml ((Paragraph):xs) = "<p>\n"++toHtml xs
+toHtml ((Text s):xs) = s++toHtml xs
+toHtml ((Link l d):xs) = "<link: "++l++" desc: "++d++">"++toHtml xs
+toHtml ((Font o d):xs) = "<"++htmlFontOp o++">"++toHtml d++"</"++htmlFontOp o++">"++toHtml xs
+toHtml ((Heading n d):xs) = "\n<h"++show n++">"++toHtml d++"</h"++show n++">\n"++toHtml xs
+toHtml ((Url l):xs) = "<link: "++l++">"++toHtml xs
+toHtml ((Pre s):xs) = "<pre>"++toHtml s++"</pre>"++toHtml xs
+toHtml ((List o l):xs) = "<"++htmlListType o++">\n"++(unlines $ map (\s -> "<li>"++toHtml s++"</li>\n") l) ++ "</"++htmlListType o++">"++toHtml xs
+
+htmlFontOp Bold = "b"
+htmlFontOp Emph = "em"
+htmlFontOp Mono = "tt"
+htmlFontOp Underline = "u"
+htmlFontOp Strike = "strike"
+
+htmlListType True = "ol"
+htmlListType False = "ul"
+
+toLatex :: [Markup] -> String
+toLatex [] = []
+toLatex ((Paragraph):xs) = "\n\n"++toLatex xs
+toLatex ((Text s):xs) = s++toLatex xs
+toLatex ((Link l d):xs) = "{\\em "++d++"}"++toLatex xs
+toLatex ((Font o d):xs) = "{\\"++latexFontOp o++" "++toLatex d++"}"++toHtml xs
+toLatex ((Heading n d):xs) = "\n\\"++(unwords $ take (n-1) $ repeat "sub")++"section{"++toLatex d++"}"++"\n\n"++toLatex xs
+toLatex ((Url l):xs) = "{\\bf "++l++"}"++toLatex xs
+toLatex ((Pre s):xs) = "<pre>"++toLatex s++"</pre>"++toLatex xs
+toLatex ((List o l):xs) = "\n\\begin{"++latexListType o++"}\n"++(unlines $ map (\s -> "\\item "++toLatex s++"\n") l) ++ "\\end{"++latexListType o++"}"++toLatex xs