]> ruin.nu Git - yawbih.git/blobdiff - Wiki.hs
Latex output
[yawbih.git] / Wiki.hs
diff --git a/Wiki.hs b/Wiki.hs
index 2d394b8243d273dcd3fac82abe0c1b51e9e3d1f2..210708df3d3a100fb50d267f33d1e647604acb37 100644 (file)
--- a/Wiki.hs
+++ b/Wiki.hs
@@ -2,10 +2,12 @@ module Wiki (
        Backend (getCurrent,getList,get,setCurrent,update,listKeys)
        ,PGB
        ,createPGB
-       ,Markup (Text, Paragraph, Link, Bold, Emph, Heading, Url, Underline, Strike, Pre,List)
+       ,Markup (Text, Paragraph, Font, Link, Heading, Url, Pre,List)
+       ,FontOp (Bold, Emph, Mono, Underline, Strike)
        ,Document
        ,wikiParser
-       ,htmlOutput
+       ,toHtml
+       ,toLatex
 
 ) where
 
@@ -35,15 +37,14 @@ class Backend a where
 data Markup = Text String
        | Paragraph 
        | Link String String
-       | Bold [Markup]
-       | Emph [Markup]
+       | Font FontOp [Markup]
        | Heading Int [Markup]
        | Url String
-       | Underline [Markup]
-       | Strike [Markup]
        | Pre [Markup]
        | List Bool [[Markup]]
 
+data FontOp = Bold | Emph | Mono | Underline | Strike
+
 type Document = [Markup]
 
 wikiParser :: Parser Document  
@@ -102,13 +103,15 @@ pPre = string "<pre>" >> do
        return (Pre s)
 
 pBold,pEmph,pUnderline,pStrike :: Parser Markup        
-pBold = pS "**" (\s -> Bold s)
-pEmph = pS "//" (\s -> Emph s)
-pUnderline = pS "__" (\s -> Underline s)
-pStrike = pS "--" (\s -> Strike s)
-pS s f = string s >> do
+pBold = pFont "**" Bold
+pEmph = pFont "//" Emph
+pUnderline = pFont "__" Underline
+pStrike = pFont "--" Strike
+pMono = pFont "||" Mono
+
+pFont s o = string s >> do
        s <- pStopAt s
-       return (f s)
+       return (Font o s)
 
 pOtherChar :: Parser Markup    
 pOtherChar = do
@@ -156,6 +159,7 @@ pMain = choice [
        ,try(pEmph) 
        ,try(pUnderline) 
        ,try(pStrike) 
+       ,try(pMono) 
        ,try (pLink)
        ,try (pURL)
        ,try (pMail)
@@ -169,6 +173,7 @@ pOneLine = choice [
        ,try pEmph
        ,try pUnderline
        ,try pStrike
+       ,try pMono
        ,try pLink
        ,try pURL
        ,try pMail
@@ -238,18 +243,44 @@ 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 ((Bold d):xs) = "<b>"++toHtml d++"</b>"++toHtml xs
-toHtml ((Emph d):xs) = "<em>"++toHtml d++"</em>"++toHtml xs
-toHtml ((Underline d):xs) = "<u>"++toHtml d++"</u>"++toHtml xs
-toHtml ((Strike d):xs) = "<strike>"++toHtml d++"</strike>"++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) = "<"++listType o++">\n"++(unlines $ map (\s -> "<li>"++toHtml s++"</li>\n") l) ++ "</"++listType o++">"++toHtml xs
-
-listType True = "ol"
-listType False = "ul"
-
-htmlOutput s = case parse wikiParser "" s of
-                                       Right n -> putStr (toHtml n)
+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
+
+latexFontOp Bold = "bf"
+latexFontOp Emph = "em"
+latexFontOp Mono = "tt"
+latexFontOp Underline = "u"
+latexFontOp Strike = "strike"
+
+latexListType True = "enumeration"
+latexListType False = "itemize"
+
+output s f = case parse wikiParser "" s of
+                                       Right n -> putStr (f n)
                                        Left e -> print e
+
+htmlOutput s = output s toHtml
+latexOutput s = output s toLatex