Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

bbcode generator halp

Name: Anonymous 2011-03-09 21:55

I am trying to write a simple bbcode Python script.  How can I improve this?  Also, how can I get rid of the ":arg" when the result is actually printed.  YES I KNOW ITS PROBABLY FUCKING UGLY AND I AM A SHITTY PROGRAMMER FORCED INDENTATION OF ANUS READ SICP ETC

import string

b = '[b]'
b2 = '[/b]'

u = '[u]'
u2 = '[/u]'

o = '[o]'
o2 = '[/o]'

i = '[i]'
i2 = '[/i]'

s = '[s]'
s2 = '[/s]'

m = '[m]'
m2 = '[/m]'

c = '[code]'
c2 = '
'

sp = ''
sp2 = '
'


bbcode = raw_input('ENTER ANUS, RECEIVE HAX: ')

if ':b' in bbcode:
    print b + bbcode + b2
   
if ':u' in bbcode:
    print u + bbcode + u
   
if ':o' in bbcode:
    print o + bbcode + o2

if ':i' in bbcode:
    print i + bbcode + i2
   

if ':s' in bbcode:
    print s + bbcode + s2

if ':m' in bbcode:
    print m + bbcode + m2
   
if ':c' in bbcode:
    print c + bbcode + c2

if ':sp' in bbcode:
    print sp + bbcode + sp2[/code]

Name: Anonymous 2011-03-10 14:52


tokenize :: String -> [String]
tokenize ""         = []
tokenize xss@(x:xs) | x == '('  = "(" : op : tokenize xs'
                    | x == ')'  = ")" : tokenize xs
                    | otherwise = args : tokenize xss'
                    where op    = takeWhile (/= ' ') $ strip xs
                          xs'   = strip $ dropWhile (/= ' ') $ strip xs
                          strip = dropWhile (== ' ')
                          args  = takeWhile (`notElem` "()") xss
                          xss'  = dropWhile (`notElem` "()") xss

data ParseTree = Empty | Node (String,String) ParseTree ParseTree deriving (Show)

parse :: [String] -> ParseTree
-- left as an exercise for the reader

compile :: String -> String
compile xs = compile' $ parse $ tokenize xs
    where compile' :: ParseTree -> String
          compile' Empty                = ""
          compile' (Node (tok,val) l r) = case tok of
                                               "b"   -> "[b]" ++ val' ++ "[/b]"
                                               "o"   -> "[o]" ++ val' ++ "[/o]"
                                               "u"   -> "[u]" ++ val' ++ "[/u]"
                                               "i"   -> "[i]" ++ val' ++ "[/i]"
                                               "s"   -> "[s]" ++ val' ++ "[/s]"
                                               "m"   -> "[m]" ++ val' ++ "[/m]"
                                               "c"   -> "[c]" ++ val' ++ "[/c]"
                                               "hax" -> "[b][o][u][i]" ++ val' ++ "[/i][/u][/o][/b]"
                                               where val' = (compile' l) ++ val ++ (compile' r)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List