readme-script
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import re
|
||||
codeRe = "```\s*python([\s\S]*?)```"
|
||||
import util
|
||||
from collections import defaultdict
|
||||
def title_case(str):
|
||||
return str[:1].upper() + str[1:].lower()
|
||||
EMOJIS = {
|
||||
@ -18,34 +18,24 @@ EMOJIS = {
|
||||
'utility': ':wrench:'
|
||||
}
|
||||
def tagger():
|
||||
tag_data = open('tag_database').read()
|
||||
tag_dict = {}
|
||||
tag_list = tag_data.split('\n')
|
||||
for tag in tag_list:
|
||||
category = tag.split(':')[1]
|
||||
snippet = tag.split(':')[0]
|
||||
if category in tag_dict:
|
||||
tag_dict[category].append(snippet)
|
||||
else:
|
||||
tag_dict[category] = [snippet]
|
||||
return tag_dict
|
||||
tag_db = defaultdict(list)
|
||||
for snippet in util.read_snippets():
|
||||
tag_db[snippet.category[0]].append(snippet)
|
||||
return tag_db
|
||||
|
||||
start = open("static-parts/readme-start.md").read() + '\n\n'
|
||||
end = open("static-parts/readme-end.md").read()
|
||||
start = util.read_readme_start()
|
||||
end = util.read_readme_end()
|
||||
toAppend = ''
|
||||
tag_dict = tagger()
|
||||
toAppend += '## Table of Contents \n'
|
||||
for category in tag_dict:
|
||||
for category in sorted(tag_dict):
|
||||
toAppend = toAppend + '### ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n<details><summary>View contents</summary> <ul>'
|
||||
for snippet in sorted(tag_dict[category]):
|
||||
toAppend += f'<li><a href = "#{snippet}"><code>{snippet}</code></a></li>\n'
|
||||
for snippet in sorted(tag_dict[category],key=lambda snippet : snippet.name):
|
||||
toAppend += f'<li><a href = "#{snippet.name}"><code>{snippet.name}</code></a></li>\n'
|
||||
toAppend += '</ul></details>\n\n'
|
||||
toAppend += '<hr></hr> \n\n'
|
||||
for category in tag_dict:
|
||||
toAppend = toAppend + '## ' + EMOJIS[category] + ' ' + title_case(category) +'\n\n'
|
||||
for snippet in sorted(tag_dict[category]):
|
||||
someFile = open("snippets/" + snippet + '.md')
|
||||
for category in sorted(tag_dict):
|
||||
toAppend = toAppend + '## ' + EMOJIS[category] + ' ' + title_case(scategory) +'\n\n'
|
||||
for snippet in sorted(tag_dict[category],key=lambda snippet : snippet.name):
|
||||
fileData = someFile.read()
|
||||
codeParts = re.split(codeRe,fileData)
|
||||
toAppend += codeParts[0] + '```py{codeParts[1]} \n ```'.format(codeParts= codeParts) +codeParts[2] + '<details><summary>View Examples</summary>\n\n```py\n{codeParts[3]}\n```\n</details>\n\n<br><a href = "#table-of-contents">:arrow_up: Back to top</a>\n '.format(codeParts=codeParts) + '\n'
|
||||
open("README.md",'w').write(start+toAppend+'\n'+end)
|
||||
toAppend += f'###{snippet.title}\n\n```py{snippet.read_code()} \n ```'.format(codeParts= codeParts) +codeParts[2] + '<details><summary>View Examples</summary>\n\n```py\n{codeParts[3]}\n```\n</details>\n\n<br><a href = "#table-of-contents">:arrow_up: Back to top</a>\n '.format(codeParts=codeParts) + '\n'
|
||||
open("README.md",'w').write(start+toAppend+'\n'+end)'''
|
||||
50
scripts/util.py
Normal file
50
scripts/util.py
Normal file
@ -0,0 +1,50 @@
|
||||
import os,re
|
||||
def tagger():
|
||||
snippet_files = [snippet.replace('.md','')for snippet in os.listdir('snippets')]
|
||||
|
||||
tag_database_file = open('tag_database')
|
||||
|
||||
tag_database = tag_database_file.read()
|
||||
|
||||
tag_database_file.close()
|
||||
|
||||
tag_database = [tag for tag in tag_database.split('\n') if tag.strip() != '']
|
||||
|
||||
tag_db = {}
|
||||
|
||||
for tag in tag_database:
|
||||
tag_db[tag.split(':')[0].strip()] = tag.split(':')[1].strip().split(',')
|
||||
return tag_db
|
||||
def read_snippets():
|
||||
snippet_files = os.listdir('snippets')
|
||||
|
||||
snippets = []
|
||||
|
||||
class snippet():
|
||||
def __init__(self,file_location):
|
||||
with open(file_location) as f:
|
||||
self.content = f.read()
|
||||
self.codeRe = "```\s*python([\s\S]*?)```"
|
||||
self.titleRe = '###\\s*([\\w]+)'
|
||||
self.descRe = '###\s*\w+\s*([\s\S]+)```\s*python[\s\S]+```\s*```\s*python[\s\S]+```'
|
||||
self.name = self.read_title()
|
||||
self.category = tagger()[self.name]
|
||||
def read_code(self):
|
||||
return re.findall(self.codeRe,self.content)[0].strip()
|
||||
def read_title(self):
|
||||
return re.findall(self.titleRe,self.content)[0].strip()
|
||||
def read_description(self):
|
||||
return re.findall(self.descRe,self.content)[0].strip()
|
||||
def read_example(self):
|
||||
return re.findall(self.codeRe,self.content)[1].strip()
|
||||
for snippet_file in snippet_files:
|
||||
snippets.append(snippet(f'snippets/{snippet_file}'))
|
||||
return snippets
|
||||
def read_readme_start():
|
||||
with open('static-part/readme-start.md') as f:
|
||||
readme_start = f.read()
|
||||
return readme_start
|
||||
def read_readme_end():
|
||||
with open('static-part/readme-end.md') as f:
|
||||
readme_end = f.read()
|
||||
return readme_end
|
||||
Reference in New Issue
Block a user