Skip to content

Instantly share code, notes, and snippets.

@lambdhack
Created June 11, 2019 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdhack/11bbb06664806bc68d815ddafd64eb46 to your computer and use it in GitHub Desktop.
Save lambdhack/11bbb06664806bc68d815ddafd64eb46 to your computer and use it in GitHub Desktop.
A simple script to translate markdown files in leet speak.
#!/usr/bin/env python3
import sys
import os
import re
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
dict_normal = {
'a':'4',
'b':'8',
'c':'c',
'd':'d',
'e':'3',
'f':'f',
'g':'6',
'h':'h',
'i':'1',
'j':'j',
'k':'k',
'l':'l',
'm':'m',
'n':'n',
'o':'0',
'p':'p',
'q':'q',
'r':'r',
's':'5',
't':'7',
'u':'u',
'v':'v',
'w':'w',
'x':'x',
'y':'y',
'z':'z',
'A':'4',
'B':'8',
'C':'C',
'D':'D',
'E':'3',
'F':'F',
'G':'6',
'H':'H',
'I':'1',
'J':'J',
'K':'K',
'L':'L',
'M':'M',
'N':'N',
'O':'0',
'P':'P',
'Q':'Q',
'R':'R',
'S':'5',
'T':'7',
'U':'U',
'V':'V',
'W':'W',
'X':'X',
'Y':'Y',
'Z':'Z'
}
dict_deep = {
'a':'4',
'b':'8',
'c':'(',
'd':'[)',
'e':'3',
'f':'|=',
'g':'6',
'h':'|-|',
'i':'1',
'j':'_|',
'k':'|<',
'l':'|_',
'm':'/\\/\\',
'n':'/\\',
'o':'0',
'p':'|>',
'q':'<|',
'r':'|?',
's':'5',
't':'7',
'u':'|_|',
'v':'\\/',
'w':'\\/\\/',
'x':'><',
'y':'`/',
'z':'-\\_',
'A':'4',
'B':'8',
'C':'(',
'D':'[)',
'E':'3',
'F':'|=',
'G':'6',
'H':'|-|',
'I':'1',
'J':'_|',
'K':'|<',
'L':'|_',
'M':'/\\/\\',
'N':'/\\',
'O':'0',
'P':'|>',
'Q':'<|',
'R':'|?',
'S':'5',
'T':'7',
'U':'|_|',
'V':'\\/',
'W':'\\/\\/',
'X':'><',
'Y':'`/',
'Z':'-\\_'
}
# [text](url) or ![text](url)
LINK_RE = r'\[.*?\]\(.*?\)'
# ```
BLOCKQUOTES_RE = r'\`\`\`'
def contain_link(line):
if re.search(LINK_RE, line):
link = re.compile(LINK_RE)
m = link.search(line)
return m.span()
return False
def contain_blockquotes(line):
return re.search(BLOCKQUOTES_RE, line)
def print_doc():
doc = """
=== A simple script to translate markdown files in leet speak ===
Translate all md files in the current directory:
>_ python leet_speak_markdown_translator.py all
Translate a specified file:
>_ python leet_speak_markdown_translator.py file_in file_out
file_in: the input file
file_out: the output file
file_in and file_out can be the same
"""
print(doc)
def translate_markdown(file_in, file_out, mode='normal'):
file_in = open(file_in, 'r').readlines()
file_out = open(file_out, 'w')
out = ""
in_blockquotes = False
if mode == 'normal':
translate_dict = dict_normal
else:
translate_dict = dict_deep
for line in file_in:
if not contain_blockquotes(line) and not in_blockquotes:
if contain_link(line):
i,j = contain_link(line)
for c, char in enumerate(line):
if c < i or c > j:
if char in alphabet:
out += translate_dict[char]
else:
out += char
else:
out += char
else:
for c, char in enumerate(line):
if char in alphabet:
out += translate_dict[char]
else:
out += char
else:
out += line
if contain_blockquotes(line):
in_blockquotes = not in_blockquotes
file_out.write(out)
file_out.close()
if __name__ == "__main__":
if len(sys.argv) == 3:
translate_markdown(sys.argv[1], sys.argv[2])
elif len(sys.argv) == 2:
if sys.argv[1] == 'all': #safer
for file_name in os.listdir():
if file_name.endswith(".md"):
translate_markdown(file_name, file_name)
else:
print_doc()
else:
print_doc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment