import os, sys, re, unicodedata def snippet(text, seq1, seq2): try: start = text.index(seq1)+len(seq1) except ValueError: start=0 end =text.index(seq2, start+1) return text[start:end] def slugify(value, allow_unicode=False, whitespace=True): """ Taken from https://github.com/django/django/blob/master/django/utils/text.py Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated dashes to single dashes. Remove characters that aren't alphanumerics, underscores, or hyphens. Convert to lowercase. Also strip leading and trailing whitespace, dashes, and underscores. """ value = str(value) if allow_unicode: value = unicodedata.normalize('NFKC', value) else: value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = re.sub(r'[^\w\s-]', '', value) if whitespace: return value.strip() else: return re.sub(r'[-\s]+', '-', value).strip('-_') with open('exploits_raw.txt') as filein: raw_txt = filein.read().split('\n\n') if not os.path.exists('./exploits/'): os.mkdir('exploits') rank = 0 for para in raw_txt: if not para: continue exploit = {} try : lines = para.split('\n') while True: if not lines[0]: lines.pop(0) else: break while True: if not lines[-1]: lines.pop() else: break exploit['name'] = lines.pop(0) exploit['info'] = lines.pop(0) exploit['rank'] = int(snippet(exploit['info'], 'Rank ', ' Exploit')) exploit['tags'] = ", ".join([slugify(t, whitespace=False) for t in snippet(exploit['info'],'Exploit', ' (')[3:].split(', ')]) exploit['charge'] = snippet(exploit['info'],'(', ')') breaks = [] for idx, line in enumerate(lines): if line and line[-1] in ['.', ':']: breaks.append(idx) else: if idx not in breaks: lines[-1] = lines[-1]+'.' breaks.append(idx) idx_flavor = breaks[-2]+1 if len(breaks) > 2: for idx in breaks[:-2]: lines[idx]+='\n' exploit['text'] = ' '.join(lines[:idx_flavor]).replace('\n ','\n') exploit['flavor'] = ' '.join(lines[idx_flavor:]) cardtext = """--- rank: {rank} recharge: {charge} tags: [{tags}] --- #### {name} *{info}* {text} *{flavor}* """.format(**exploit) # if exploit['rank'] > 1: # continue with open('exploits/'+slugify(exploit['name'])+'.md','w') as fileout: fileout.write(cardtext) except Exception as e: print(para) raise e