Created
September 16, 2020 14:34
-
-
Save lemon32767/219107ffd0546181135b31ace8f5a090 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import sys, string | |
def str_findp(start, s, pred): | |
for i in range(start,len(s)): | |
if pred(s[i]): | |
return i | |
return None | |
#takes a list of lines as template for the ascii art; and a list of tokens (for example, words) to replace it with | |
#for example, you could use C tokenizer to create a list of tokens to pass to this function to format it in an interesting way | |
def perform(template_lines, tokens): | |
idx = 0 | |
tkidx = -1 | |
for line in template_lines: | |
i = 0 | |
while True: | |
tkidx += 1 | |
if tkidx >= len(tokens): | |
return | |
tok = tokens[tkidx] | |
j = str_findp(i, line, lambda c: not (c in string.whitespace)) | |
if j == None: | |
tkidx -= 1 | |
break | |
print(" " * (j - i + 1), end='') | |
print(tok, end='') | |
i = j + len(tok) + 1 | |
print() | |
#usage: ./asciitool.py ascii_art_template.txt replacement_text.txt | |
if __name__ == '__main__': | |
with open(sys.argv[1]) as art: | |
with open(sys.argv[2]) as txt: | |
perform(art.readlines(), ''.join(txt.readlines()).split()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment