## Make sure you have installed markdown library (pip install Markdown) ## and the python version is 3.6+. import os, sys, re, json, html from os.path import abspath, dirname, exists, isdir, join from shutil import copyfile, copytree, rmtree from markdown import markdown POCKETLANG_VERSION = "v0.1.0" ## Page contnet will be ordered as the below list. Each entry in the DOC_PAGES ## is called a section (which will be it's own folder) and each entry in the ## section is a page (which will be in it's own file). Additionally each page ## has it's own topic that'll be generated as h2 tags. ## ## A generated context of the function collect_doc_pages() will be in the form ## of :- { section : [ page : (html, [topic]) ] } ## where html is the html version of the markdown file. ## DOC_PAGES = { "Reference" : [ "Getting-Started", "Cheat-Sheet", "Build-From-Source", "Installation", ], "Api-Docs" : [ "Fiber", "Module", ], } ## The absolute path of this file's directory, when run as a script. ## This file is not intended to be included in other files at the moment. THIS_PATH = abspath(dirname(__file__)) BUILD_DIR = join(THIS_PATH, "build") STATIC_DIR = join(THIS_PATH, "static") MARKDOWN_DIR = join(THIS_PATH, "markdown") DOCS_URL_PATH = f"docs/{POCKETLANG_VERSION}/" ## Html generated at docs/* path. def main(): check_wasm() clean() make_build_dir() gen_home_page() context = collect_doc_pages() generate_pages(context) print("Docs generated successfully") ## INTERNAL ################################################################### ## Opens the file at the path read it and return it's content. def read(path): with open(path, 'r') as fp: return fp.read() def check_wasm(): if not exists(join(STATIC_DIR, 'wasm/')): print("[Warning] pocketlang web assembly files weren't generated.") print(" to compile, see docs/wasm/README.md") ## Remove all generated files at the build path. def clean(): REMOVE_IGNORE =('.git',) if not exists(BUILD_DIR): return for item in os.listdir(BUILD_DIR): if item in REMOVE_IGNORE: continue item = join(BUILD_DIR, item) if isdir(item): rmtree(item) else: os.remove(item) ## Generate necessary folder and files to the build dir, copy static folder and ## prepare for the generation. def make_build_dir(): ## Create '.nojekyll' for github pages. if not exists(BUILD_DIR): os.makedirs(BUILD_DIR) open(join(BUILD_DIR, '.nojekyll'), 'w').close() copytree(STATIC_DIR, join(BUILD_DIR, "static")) def gen_home_page(): template = read(join(THIS_PATH, "index.html")) template = template.replace("{{ STATIC_DIR }}", "./static/") template = template.replace("{{ DOCS_URL }}", DOCS_URL_PATH) with open(join(BUILD_DIR, 'index.html'), 'w') as fp: fp.write(template) ## FIXME: wirte a better md -> html compiler (or use some libraries). ## Compile the markdown files to html. and return the a tuple of (html, topics) ## where topics are h2 tag headers inside the markdown files.(h1 tag is ## reserved for the page heading). def compile_markdown(md): topics = [] md_new = "" in_code = False for line in md.splitlines(): stripped_line = line.strip() ## Get topics. if stripped_line.startswith("## "): topic = stripped_line[3:] topics.append(topic) md_new += f'
\n'
in_code = True
## End of codeblock.
elif in_code and stripped_line.startswith('```'):
md_new += '
\n'
in_code = False
else:
if in_code: line = html.escape(line)
md_new += line + "\n"
return markdown(md_new), topics
## Collect all markdown and generate the context (context is mentioned at the
## top of the script).
def collect_doc_pages():
context = dict()
for section in DOC_PAGES:
context[section] = dict()
for file_name in DOC_PAGES[section]:
md = read(join(MARKDOWN_DIR, section, file_name + '.md'))
context[section][file_name] = compile_markdown(md)
return context
## Generate navigation tree html tags with the tree information defined at the
## markdown/tree.json file (since python 3.6 dicionaries are ordered we're not
## using the OrderedDict here).
def generate_navtree(context):
gen = ""; indentation = ' ' * 4
def write(html_line):
nonlocal gen; nonlocal indentation
gen += indentation + html_line + '\n'
for section in context:
write('