def _get_title(md_file): """Get the wiki's title. Established: The first line of wiki is the title, written in html comment syntax. such as: <!-- title : The wiki title --> """ notations = {"left" : "<!--", "right" : "-->"} _, md_name = _get_dir_and_md_name(md_file) with open(md_file, "rb") as fd: first_line = fd.readline().strip() if "title" not in first_line.lower(): sys.exit(comm.color_error( "[%s] first line must have `title` keyword" % md_name)) if not (first_line.startswith(notations["left"]) and first_line.endswith(notations["right"])): sys.exit(comm.color_error("[%s] title syntax is wrong!" % md_name)) for notation in notations.values(): first_line = first_line.replace(notation, "") first_line = first_line.strip() # strip the space between `title` and `:` first_line = first_line.split(":", 1) first_line[0] = first_line[0].strip().lower() first_line = ":".join(first_line) title = first_line.lstrip("title:").strip() return title
def _check_suffix(md_file): """Check if the md_file's suffix is right. Supported markdown suffixes are configs.SUFFIXES. Do not use other suffixes. """ if comm.filter_suffix(md_file): _, md_name = _get_dir_and_md_name(md_file) sys.exit(comm.color_error("[%s] suffix is wrong!" % md_name))
def generator(md_file, debug_mode=False): if not _check_path_exists(md_file): _, md_name = _get_dir_and_md_name(md_file) sys.exit(comm.color_error("[%s] file does not exists" % md_name)) _check_suffix(md_file) dir_name, md_name = _get_dir_and_md_name(md_file) title = _get_title(md_file) html = _md2html(md_file, title) if debug_mode: print(html) else: _update_wiki_page(dir_name, md_name, html) _update_dir_page(dir_name, md_name, title) comm.print_ok("[%s]" % md_name, " updated ok.")