Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # Faubi implementation of smaracters. Version 1.0
- # Example usage: python smaracters.py "this is an example message" -f lowercase_latin.json
- # lowercase_latin.json: http://pastebin.com/1AsrDEkX
- import sys, argparse, json
- argparser = argparse.ArgumentParser(description='Writes text using phpbb emoji (i.e. smaracters)')
- argparser.add_argument('-l', '--line-length', type=int, default=40, help='Max number of emoji to display per line')
- argparser.add_argument('-s', '--space-length', type=int, default=1, help='Number of spaces to put between characters')
- argparser.add_argument('-f', '--char-file', action='append', help='A json file to load characters from')
- argparser.add_argument('message', help='The text to write with emoji. If not present will load from stdin')
- args = argparser.parse_args()
- letter_height = 0
- characters = {}
- if not args.char_file:
- sys.exit('No character files specified')
- # Load character data from supplied json files
- for json_filename in args.char_file:
- try:
- with open(json_filename) as json_file:
- json_data = json.load(json_file)
- except FileNotFoundError:
- sys.exit('No such file: {0}'.format(json_filename))
- except ValueError:
- sys.exit('Unable to parse file: {0}'.format(json_filename))
- try:
- letter_height = max(letter_height, json_data['letter_height'])
- for character, value in json_data['characters'].items():
- if character not in characters:
- characters[character] = value
- except IndexError:
- sys.exit('Invalid data in file: {0}'.format(json_filename))
- # Make all characters the same height by adding black rows to top of short characters
- for character in characters:
- char_height = len(characters[character])
- if char_height < letter_height:
- char_width = len(characters[character][0])
- characters[character] = [' '*char_width]*(letter_height-char_height) + characters[character]
- emoji = {' ': ':D', '#': ':mrgreen:'}
- if args.message:
- message = args.message
- else:
- message = sys.stdin.readline()[:-1]
- current_line = ['']*letter_height
- current_length = 0
- new_length = 0
- # Loop through once before printing anything to find invalid characters
- for character in message:
- if character not in characters:
- sys.exit('Unsupported character: {0}'.format(character))
- # Loop through characters, adding each to the current line
- for index, character in enumerate(message):
- char_lines = characters[character]
- char_width = len(char_lines[0])
- if char_width > args.line_length:
- sys.exit('Character wider than line length limit: {0}'.format(character))
- new_length += char_width
- # Line break if new character would make line too long
- if new_length > args.line_length:
- for line_number, line in enumerate(current_line):
- print(' '.join(emoji[character] for character in line))
- current_line[line_number] = ''
- print()
- new_length = char_width
- else:
- # Add space before character if not at beginning of line
- if index != 0:
- for line_number in range(len(current_line)):
- current_line[line_number] += ' '*args.space_length
- new_length += args.space_length
- # Add character to current line
- for line_number, char_line in enumerate(char_lines):
- current_line[line_number] += char_line
- current_length = new_length
- # Print last line
- if current_line[0] != '':
- for line_number, line in enumerate(current_line):
- print(' '.join(emoji[character] for character in line))
- current_line[line_number] = ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement