>>6
Truly sharp and scientific. Now we can logically reason about posts using BBCode formalism.
Name:
Anonymous2007-08-17 6:44 ID:y7jA2KvV
>>6
More like module Language.BBCode, to follow the already existing namespaces.
Also, nice misuse of show! GRR!
Also, you shouldn't call the Tag datatype just that as it compasses both Tag and Text. Check some HTML specs for what they might be using -- try finding some big word, if you misuse it it's still ok.
I always wanted to learn Parsec, so I wrote a BBCode parser! module Language.BBCode.Parser
(BBCode(..),
-- A fast monadic BBCode parser utilizing the industrial
-- strength combinator library Parsec.
parseBBCode)
where
import Text.ParserCombinators.Parsec
-- Belongs in another module.
data BBCode = Element String [BBCode]
| Text String
-- An optimizing BBCode rendering machine.
-- Belongs in another module.
render (Element e []) = ""
render (Element e bs) = "[" ++ e ++ "]" ++
(concatMap render bs) ++
"[/" ++ e ++ "]"
render (Text s) = s
bbcode = many (bbcodeElement <|> bbcodeText)
bbcodeElement = do element <- try bbcodeOpenElement
content <- bbcode
bbcodeCloseElement element
return (Element element content)
bbcodeOpenElement = do char '['
s <- many1 letter
char ']'
return s
bbcodeCloseElement s = string ("[/" ++ s ++ "]")
bbcodeText = many1 (noneOf "[") >>= return . Text
parseBBCode bb = case parse bbcode "" bb of
Left e -> error $ show e
Right bs -> bs
-- A simple test case.
testbb = "[b][u][o]EXPERT PROGRAMMER[/o][/u][/b] " ++
"[spoiler][i]Read SICP![/i][/spoiler]"
test = do putStrLn testbb
putStrLn $ concatMap render $ parseBBCode testbb
>>6
Oh wow. My BBCode module. Were the recent sinewavefags using this or has someone come up with something better?
Name:
Anonymous2008-02-09 10:09
>>26
Nah, I use a custom one, it's pretty much the same though... and for the second version of my plotter I just write out the opening and closing tags :/
>>38
Show is really supposed to give representations that can be read back in. It pissed me off when I'm at the GHCi console and I want to see what's inside a url:
someURL http://dis.4chan.org/prog/ goddamn it show me the structure fields
<interactive>1:0: Not in scope: `goddamn'
...
Uh, yeah. I'd rather have "toString" function than piss all over how "Show" is supposed to work. I can't remember which URL library did this (there are too many), I think it's the one that HTTP uses.