/r/equesting BBCode decompiler
1
Name:
frozenanus
!divz2ANUSY
2011-07-11 21:09
Project type
Open to everyone
Description
I need a program that will accept a link to a /prog/ post as input, and as produce as output the plain-text/BBCode that would be submitted as a post to reproduce the post in question.
Compensation
$0.03 / hour
Time left
Indefinite
Tags
* bbcode
* /prog/-utility
17
Name:
Anonymous
2011-07-13 13:53
Here's some shit from 2007/2008:
module Language.BBCode.Disassemble (disassembleBBCode) where
import Language.BBCode
import Language.BBCode.Render
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
test :: IO ()
test = do
testcase <- readFile "bbcode.txt"
putStrLn $
concatMap showBBCode $
disassembleBBCode testcase
Newer Posts