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

Command line BB generator

Name: Anonymous 2011-08-10 0:49

$ bb -h | bb -code
usage: bb [-h] [-b] [-i] [-o] [-u] [-s] [-m] [-spoiler] [-sub] [-sup] [-aa]
          [-code] [--faggot] [--mode {all,random,ransom}]
          [text [text ...]]

BBCode encode user input.

positional arguments:
  text                  text to be encoded

optional arguments:
  -h, --help            show this help message and exit
  -b                    insert bold tag
  -i                    insert italics tag
  -o                    insert overline tag
  -u                    insert underline tag
  -s                    insert strikethrough tag
  -m                    insert monospace tag
  -spoiler              insert spoiler tag
  -sub                  insert subscript tag
  -sup                  insert superscript tag
  -aa                   insert Shift JIS tag
  -code                 insert code tag
  --faggot              insert ``faggot'' quotes
  --mode {all,random,ransom}
                        special modes for applying all tags to the text, a
                        random selection of tags to the text, or a random
                        selection of tags to each character of the text.

$ cat ~/bin/bb | bb -code
#!/usr/bin/env python

import argparse
import random
import sys


_TAG_DESC_PAIRS = [
        ('b', 'bold'), ('i', 'italics'), ('o', 'overline'), ('u', 'underline'),
        ('s', 'strikethrough'), ('m', 'monospace'), ('spoiler', 'spoiler'),
        ('sub', 'subscript'), ('sup', 'superscript'), ('aa', 'Shift JIS'),
        ('code', 'code')]


_TAGS = [tag for tag, desc in _TAG_DESC_PAIRS]


def _random_tags():
    return random.sample(_TAGS, random.randint(1, len(_TAGS)-1))


def text_to_bbcode(text, tags):
    while tags:
        text = '[{0}]{1}[/{0}]'.format(tags.pop(), text)
    return text


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='BBCode encode user input.')
    parser.add_argument('text', nargs='*', help='text to be encoded')
    for tag, desc in _TAG_DESC_PAIRS:
        parser.add_argument('-%s' % tag, action='append_const', dest='tags',
                            const=tag, help='insert %s tag' % desc)
    parser.add_argument('--faggot', action='store_true',
                        help="insert ``faggot'' quotes")
    parser.add_argument('--mode', choices=['all', 'random', 'ransom'],
                        help=('special modes for applying all tags to the '
                              'text, a random selection of tags to the text, '
                              'or a random selection of tags to each '
                              'character of the text.'))
    args = parser.parse_args()
    # Make sure 'code' comes last because tags inside it will not render.
    if args.tags and 'code' in args.tags:
        args.tags.append(args.tags.pop(args.tags.index('code')))
    text = ' '.join(args.text) or sys.stdin.read().strip()
    if args.faggot is True:
        text = "``%s''" % text
    if args.mode is None:
        print text_to_bbcode(text, args.tags)
    if args.mode == 'all':
        print text_to_bbcode(text, _TAGS)
    elif args.mode == 'random':
        print text_to_bbcode(text, _random_tags())
    elif args.mode == 'ransom':
        print ''.join(c if c.isspace() else text_to_bbcode(c, _random_tags()) for c in text)


If you decide to hack it, post the code here. Have fun.

Name: Anonymous 2011-08-11 15:57

$ ./sex.py
Greetings {spoiler /prog/} from an {b.i.o.u EXPERT {sup B{sup B{sup C{sub O{sub D{sub E}}}}}} PROGRAMMER !!}
Since you can't into Unix, I wrote you a S{sub e}x{sup p}C{sub o}d{sup e} interpreter.
It's doesn't support things like \{sup*3 this\} because I didn't feel like it.
That is left as an exercise for the hacker on ``her'' way to becoming a {b.i.o.u ``SEXPERT'' PROGRAMMER}.
{i Have fun !!}

Greetings /prog/ from an EXPERT BBCODE PROGRAMMER !!
Since you can't into Unix, I wrote you a SexpCode interpreter.
It's doesn't support things like {sup*3 this} because I didn't feel like it.
That is left as an exercise for the hacker on ``her'' way to becoming a ``SEXPERT'' PROGRAMMER.
Have fun !!

$ cat sex.py | bb -code
#!/usr/bin/env python

import sys


def eval_(exp):
    words = exp[1:-1].split(' ')
    tags = words.pop(0).split('.')
    output = parse(' '.join(words))
    while tags:
        output = '[{0}]{1}[/{0}]'.format(tags.pop(0), output)
    return output


def parse(exp):
    start = None
    depth = 0
    output = str(exp)
    for i, c in enumerate(exp):
        if c == '{' and (i == 0 or exp[i-1] != '\\'):
            if start is None:
                start = i
            else:
                depth += 1
        elif c == '}':
            if depth == 0:
                old = exp[start:i+1]
                new = eval_(old)
                output = output.replace(old, new)
                start = None
            else:
                depth -= 1
    return output.replace('\{', '{').replace('\}', '}')


if __name__ == '__main__':
    text = ' '.join(sys.argv[1:]) or sys.stdin.read()
    print parse(text)

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