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-10 4:29

>>3
You forgot to take into consideration that most people do not BBCode their whole post, but often word by word with different tags. For example, BBCode. This would literally take me forever with your ``tool''.

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