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

Pages: 1-

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-09 22:05

It's fucking ugly and you're a shitty programmer.

Name: Anonymous 2011-03-09 22:20

Mine has no such code repetition, it's all handled by clean macros and functions.

Name: Anonymous 2011-03-10 1:54

>>2-3,5 YHBT

Name: Anonymous 2011-03-10 4:18

I have?

:(

Name: Anonymous 2011-03-10 5:46

I HAVE NOT!

Name: Anonymous 2011-03-10 6:16

>>6
U have autism

Name: Anonymous 2011-03-10 8:46

It doesn't parse properly if the same tag appears more than once (which would require counting tag nesting).

Also it's not SLOW AS FUCK 'cause I wrote it.

REGEXP_BBCODE = /\[([\w#]+)(?:=["'](.+)["'])?\](.*)\[\/\1\]/

def bbcode_strip!(str)
    str.gsub!(REGEXP_BBCODE) { bbcode_strip!($3) }
    return str
end

def bbcode_replace!(str)
    str.gsub!(REGEXP_BBCODE) do |match|
        case $1
        when 'b'
            "<b>#{bbcode_replace!($3)}</b>"
        when 'i'
            "<i>#{bbcode_replace!($3)}</i>"           
        when 'o'
            "<o>#{bbcode_replace!($3)}</o>"
        when 'u'
            "<u>#{bbcode_replace!($3)}</u>"
        when 'sub'
            "<sub>#{bbcode_replace!($3)}</sub>"
        when 'sup'
            "<sup>#{bbcode_replace!($3)}</sup>"
        when 'a'
            "<a href=\"http:\/\/#{$2 || bbcode_strip!($3)}\" title=\"http:\/\/#{$2 || bbcode_strip!($3)}\">#{bbcode_replace!($3)}</a>"
        when 'img'
            "<img src=\"#{$2 || bbcode_strip!($3)}\" alt=\"#{bbcode_strip!($3)}\" title=\"#{bbcode_strip!($3)}\" />"
        when '#'
            $3
        else
            "[#{$1}#{$2 && "=#{$2}"}]#{bbcode_replace!($3)}[/#{$1}]"
        end
    end
    return str
end

Name: Anonymous 2011-03-10 8:52

We need BBCodePARSERCOMBINATORS

Name: Anonymous 2011-03-10 9:04

what is OOOO?

Baby don

Name: Anonymous 2011-03-10 9:48

>>9
I wanted to post my Haskell BBCode engine but it would take like 5 posts.
Still, my engine had working [define]s.

Name: Anonymous 2011-03-10 10:07

>>11
It can't be Haskaaa if it takes up more than 8 lines

Name: Anonymous 2011-03-10 10:36

>>12
~260 lines, 5 separate files. ;~;

Name: Anonymous 2011-03-10 11:47

>>13
What the fuck does it do, implement a full R5RS interpreter and JIT?

Name: Anonymous 2011-03-10 12:46

>>13
Please post it (260 lines fit in a post or two.)

Name: Anonymous 2011-03-10 12:46

>>14
Don't bully me tho :(

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)

Name: Anonymous 2011-03-10 15:32

>>17
"c"
IHBT

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