Name: Anonymous 2010-04-01 9:42
Hey I need a HTML to BB code decompiler. This is because I'm too lazy to write out my bb code by hand.
notepad.exe -> edit -> replace -> find what: < replace with [ -> replace all -> find what: > replace with ] -> replace all
<spoiler> and <o> tags
tr/<>/[]/g
use Acme::shiichan::shitchan::HMA::BBCoeds;
module Language.BBCode (BBCode(..)) where
data BBCode = Element String [BBCode]
| Text String
deriving Showmodule Language.BBCode.Disassemble (disassembleBBCode) where
import Language.BBCode
import Text.ParserCombinators.Parsec
import Data.List
html = many (try htmlHardBreak <|> try htmlBreak <|> htmlElement <|> htmlText)
htmlText = many1 (noneOf "<") >>= return . Text
htmlHardBreak = (string "<br clear=all>") >> (return (Element "br" []))
htmlBreak = (string "<br/>\n") >> (return (Text "\n"))
htmlElement = do
(s, as) <- try htmlOpenElement
content <- html
htmlCloseElement s
case lookup "class" as of
Just c -> return $ Element c content
Nothing -> return $ Element s content
htmlOpenElement = do try $ char '<'
s <- many1 $ noneOf "/ >"
as <- manyTill (space >> spaces >> htmlAttribute) (char '>')
return (s, as)
htmlCloseElement s = do try $ string $ "</" ++ s ++ ">"
htmlAttribute = do key <- many1 $ noneOf "="
char '='
c <- oneOf "\"'"
value <- many $ noneOf (if c == '"' then "\"" else "'")
char c
return (key, value)
eval :: BBCode -> [BBCode]
eval (Element "a" x) = concatMap eval x
eval (Element "quote" x) = Text "> " : concatMap eval x
eval (Element s x) = (Element s (concatMap eval x)) : []
eval (Text x) = (Text (subst ">" ">" x)) : []
subst _ _ [] = []
subst a b ax@(x:xs) = if isPrefixOf a ax
then b ++ (subst a b (drop (length a) ax))
else x : subst a b xs
disassembleBBCode bb = case parse html "" bb of
Left e -> error $ show e
Right bs -> concatMap eval bs