1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import os, json, flask from sys import platform from datetime import datetime
if not os.path.exists('projects/'): os.mkdir('projects/')
PATH = os.getcwd() HEAD = 'PUT YOUR TEXT BELOW\n___________________' HTML = 'online.html'
def read_file(file): ext = file.split('.')[-1] with open(file, 'r') as f: if ext == 'txt': return f.read().replace(HEAD, '').strip('\n') elif ext == 'json': return json.load(f) else: return f.read() def write_file(file, cont): ext = file.split('.')[-1] with open(file, 'w') as f: if ext == 'txt': f.write(HEAD+'\n\n'+cont) elif ext == 'json': json.dump(cont, f, indent=4, ensure_ascii=False) else: f.write(cont) def time_now(exact_time=True): now = datetime.now() if exact_time: return now.strftime('%B %d %Y %H:%M:%S') else: return now.strftime('%B %-d, %Y')
def generate_chapters(config): chapters = [] for item in config['index']: subchapters = [] for subitem in item['subindex']: subsubtitle = subitem['subsubtitle'] filepath = subitem['filepath'] content = read_file(PATH+filepath) subchapters.append({'subsubtitle': subsubtitle, 'content': content.replace('\n\n', '<br/>')}) subtitle = item['subtitle'] filepath = item['filepath'] content = read_file(PATH+filepath) chapters.append({'subtitle': subtitle, 'content': content.replace('\n\n', '<br/>'), 'subchapters': subchapters}) return chapters
def view(title): app = flask.Flask(__name__) @app.route("/") def index(): project = title.lower().replace(' ', '_') code = read_file(PATH+'/projects/'+project+'/code.py') config = read_file(PATH+'/projects/'+project+'/config.json') abstract = read_file(PATH+'/projects/'+project+'/abstract.txt') chapters = generate_chapters(config) return flask.render_template(HTML, title=config['title'], date=config['date'], code=code, abstract=abstract, chapters=chapters) app.run(use_reloader=False, debug=True)
def create(title): project = title.lower().replace(' ', '_') if not os.path.exists(PATH+'/projects/'+project+'/'): os.mkdir(PATH+'/projects/'+project+'/') config = {'title': title, 'date': time_now(False), 'index': []} write_file(PATH+'/projects/'+project+'/config.json', config) write_file(PATH+'/projects/'+project+'/abstract.txt', 'THIS IS THE ABSTRACT') write_file(PATH+'/projects/'+project+'/code.py', '# Source code of the project: '+project+'.\n\nfrom sympy import *\n\n') print('Done. Please check '+project+' in '+PATH.replace(' ', '\ ')+'/projects/')
def chapter(title, subtitle): project = title.lower().replace(' ', '_') if not os.path.exists(PATH+'/projects/'+project+'/chapters/'): os.mkdir(PATH+'/projects/'+project+'/chapters/') filename = subtitle.lower().replace(' ', '_') filepath = '/projects/'+project+'/chapters/'+filename+'.txt' write_file(PATH+filepath, 'THIS IS THE CHAPTER') config = read_file(PATH+'/projects/'+project+'/config.json') config['index'].append({'subtitle': subtitle, 'filepath': filepath, 'subindex': []}) write_file(PATH+'/projects/'+project+'/config.json', config) print('Updated! You can edit new chapter '+filename+'.txt in '+PATH.replace(' ', '\ ')+'/projects/'+project+'/chapters/')
def subchapter(title, subtitle, subsubtitle): project = title.lower().replace(' ', '_') foldername = subtitle.lower().replace(' ', '_') folderpath = '/projects/'+project+'/chapters/'+foldername+'/' if not os.path.exists(PATH+folderpath): os.mkdir(PATH+folderpath) filename = subsubtitle.lower().replace(' ', '_') filepath = folderpath+filename+'.txt' write_file(PATH+filepath, 'THIS IS THE SUBCHAPTER') config = read_file(PATH+'/projects/'+project+'/config.json') idx = next((index for (index, d) in enumerate(config['index']) if d["filepath"].replace('.txt', '/') == folderpath), None) config['index'][idx]['subindex'].append({'subsubtitle': subsubtitle, 'filepath': filepath}) write_file(PATH+'/projects/'+project+'/config.json', config) print('Updated! You can edit new subchapter '+filename+'.txt in '+PATH.replace(' ', '\ ')+folderpath)
def markdown(title): project = title.lower().replace(' ', '_') config = read_file(PATH+'/projects/'+project+'/config.json') abstract = read_file(PATH+'/projects/'+project+'/abstract.txt') code = read_file(PATH+'/projects/'+project+'/code.py') chapters = generate_chapters(config) text = '# ' + config['title'] + '\n\n' text += '<center>' + config['date'] + '</center>\n\n' text += '## Abstract\n\n' text += abstract + '\n\n' for chapter in chapters: text += '## ' + chapter['subtitle'] + '\n\n' text += chapter['content'].replace('<br/>', '\n\n') + '\n\n' for subchapter in chapter['subchapters']: text += '### ' + subchapter['subsubtitle'] + '\n\n' text += subchapter['content'].replace('<br/>', '\n\n') + '\n\n' text += '## Code' + '\n\n' text += '```python\n' + code + '\n```\n' write_file(PATH+'/projects/'+project+'/'+project+'.md', text) print('Done. New '+project+'.md is saved in '+PATH.replace(' ', '\ ')+'/projects/'+project+'/')
|