
import Text.ParserCombinators.Parsec

import Parse

parseTag name = do
    string ('<' : name)
    many (noneOf ">")
    char '>'
    manyTill anyChar (try $ string "</" >> string name >> char '>')

parseTA = try (parseTag "textarea") <|> (anyChar >> parseTA)

extractPhilosphy :: [WikiText] -> (String, [String], [String])
extractPhilosphy _ = ("", [], [])

unescape [] = []
unescape ('&':'l':'t':';':xs)         = '<' : unescape xs
unescape ('&':'g':'t':';':xs)         = '>' : unescape xs
unescape ('&':'a':'m':'p':';':xs)     = '&' : unescape xs
unescape ('&':'q':'u':'o':'t':';':xs) = '"' : unescape xs
unescape ('&':'a':'p':'o':'s':';':xs) = '\'' : unescape xs
unescape (x:xs) = x : unescape xs

stripComments [] = []
stripComments ('<':'!':'-':'-':xs) = dropTillEnd xs
    where dropTillEnd ('-':'-':'>':xs) = stripComments xs
          dropTillEnd (x:xs) = dropTillEnd xs
stripComments (x:xs) = x : stripComments xs

main = do
    contents <- getContents
    case parse parseTA "wiki" contents of
        Left error -> putStrLn $ show error
        Right s -> putStrLn . stripComments . unescape $ s

