import markdown import os DEFAULT_TITLE = "RNode Bootstrap Console" SOURCES_PATH="./source" BUILD_PATH="./build" INPUT_ENCODING="utf-8" OUTPUT_ENCODING="utf-8" LXMF_ADDRESS = "8dd57a738226809646089335a6b03695" document_start = """ {PAGE_TITLE}
{MENU}
""" document_end = """""" menu_md = """
[Start]({CONTENT_PATH}index.html) | [Replicate]({CONTENT_PATH}replicate.html) | [Guides]({CONTENT_PATH}guides.html) | [Software]({CONTENT_PATH}software/index.html)| [Help](help.html) | [Contribute]({CONTENT_PATH}contribute.html)
""" url_maps = [ # { "path": "", "target": "/.md"}, ] def scan_pages(base_path): files = [file for file in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, file)) and file[:1] != "."] directories = [file for file in os.listdir(base_path) if os.path.isdir(os.path.join(base_path, file)) and file[:1] != "."] page_sources = [] for file in files: if file.endswith(".md"): page_sources.append(base_path+"/"+file) for directory in directories: page_sources.extend(scan_pages(base_path+"/"+directory)) return page_sources def get_prop(md, prop): try: pt = "["+prop+"]: <> (" pp = md.find(pt) if pp != -1: ps = pp+len(pt) pe = md.find(")", ps) return md[ps:pe] else: return None except Exception as e: print("Error while extracting topic property: "+str(e)) return None def list_topic(topic): base_path = SOURCES_PATH+"/"+topic files = [file for file in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, file)) and file[:1] != "." and file != "index.md"] topic_entries = [] for file in files: if file.endswith(".md"): fp = base_path+"/"+file f = open(fp, "rb") link_path = fp.replace(SOURCES_PATH, ".").replace(".md", ".html") md = f.read().decode(INPUT_ENCODING) topic_entries.append({ "title": get_prop(md, "title"), "image": get_prop(md, "image"), "date": get_prop(md, "date"), "excerpt": get_prop(md, "excerpt"), "md": md, "file": link_path }) topic_entries.sort(key=lambda e: e["date"], reverse=True) return topic_entries def render_topic(topic_entries): md = "" for topic in topic_entries: md += "" md += "" md += "" md += ""+str(topic["title"])+"" md += ""+str(topic["date"])+"" md += ""+str(topic["excerpt"])+"" md += "" md += "" return md def generate_html(f, root_path): md = f.read().decode(INPUT_ENCODING) page_title = get_prop(md, "title") if page_title == None: page_title = DEFAULT_TITLE else: page_title += " | "+DEFAULT_TITLE tt = "{TOPIC:" tp = md.find(tt) if tp != -1: ts = tp+len(tt) te = md.find("}", ts) topic = md[ts:te] rt = tt+topic+"}" tl = render_topic(list_topic(topic)) print("Found topic: "+str(topic)+", rt "+str(rt)) md = md.replace(rt, tl) menu_html = markdown.markdown(menu_md.replace("{CONTENT_PATH}", root_path), extensions=["markdown.extensions.fenced_code"]).replace("

", "") page_html = markdown.markdown(md, extensions=["markdown.extensions.fenced_code"]).replace("{ASSET_PATH}", root_path) page_html = page_html.replace("{LXMF_ADDRESS}", LXMF_ADDRESS) page_date = get_prop(md, "date") if page_date != None: page_html = page_html.replace("{DATE}", page_date) return document_start.replace("{ASSET_PATH}", root_path).replace("{MENU}", menu_html).replace("{PAGE_TITLE}", page_title) + page_html + document_end source_files = scan_pages(SOURCES_PATH) for um in url_maps: with open(SOURCES_PATH+"/"+um["target"], "rb") as f: of = BUILD_PATH+um["target"].replace(SOURCES_PATH, "").replace(".md", ".html") root_path = "../" html = generate_html(f, root_path) print("Map path : "+str(um["path"])) print("Map target : "+str(um["target"])) print("Mapped root path: "+str(root_path)) if not os.path.isdir(BUILD_PATH+"/"+um["path"]): os.makedirs(BUILD_PATH+"/"+um["path"], exist_ok=True) with open(BUILD_PATH+"/"+um["path"]+"/index.html", "wb") as wf: wf.write(html.encode(OUTPUT_ENCODING)) for mdf in source_files: with open(mdf, "rb") as f: of = BUILD_PATH+mdf.replace(SOURCES_PATH, "").replace(".md", ".html") root_path = "../"*(len(of.replace(BUILD_PATH+"/", "").split("/"))-1) html = generate_html(f, root_path) if not os.path.isdir(os.path.dirname(of)): os.makedirs(os.path.dirname(of), exist_ok=True) with open(of, "wb") as wf: wf.write(html.encode(OUTPUT_ENCODING))