def generate_table_of_contents(): toc = md_toc.build_toc(PATH_TO_GITHUB_WORKSPACE + PATH_TO_GENERATED_DB_SCHEMA_FILE, keep_header_levels=6, parser="github") with open(PATH_TO_GITHUB_WORKSPACE + PATH_TO_GENERATED_DB_SCHEMA_FILE,"r") as fin: filedata = fin.read() filedata = filedata.replace("<!-- TOC -->", toc) with open(PATH_TO_GITHUB_WORKSPACE + PATH_TO_GENERATED_DB_SCHEMA_FILE,"w") as fout: fout.write(filedata)
def add_md_toc(md_input, skip_lines=0, toc_levels=2, toc_marker='<!-- toc -->'): """ add_md_toc will parse the input MarkDown and add a TOC between the toc_markers Example ------- copy: content: "{{ lookup('template','eos-device-documentation.j2') | arista.avd.add_md_toc(skip_lines=3) }}" dest: "{{ devices_dir }}/{{ inventory_hostname }}.md" mode: 0664 Parameters ---------- md_input: str MarkDown which will be processed skip_lines: int, optional Skip first x lines when parsing MD file default: 0 toc_levels: int, optional How many levels of headings will be included in the TOC (Default:2) default: 2 toc_marker: str, optional TOC will be inserted or updated between two of these markers in the MD file default: '<!-- toc -->' Returns ------- str MD with added TOC """ if isinstance(md_input, Undefined) or md_input is None or HAS_MD_TOC is False: # Return None return # Generate TOC from variable with StringIO(md_input) as md: stdin = sys.stdin sys.stdin = md toc = md_toc.build_toc('-', keep_header_levels=toc_levels, skip_lines=skip_lines) sys.stdin = stdin # Insert TOC between markers toc_marker = re.escape(toc_marker) toc_pattern = re.compile(fr"{toc_marker}[\S\s]*?{toc_marker}") return toc_pattern.sub(toc, md_input, count=1)
def get_toc(self, file_path: str) -> str: table_of_contents = md_toc.build_toc(file_path, parser="redcarpet") return "\n".join( [ "Table of Contents", "=================", table_of_contents, ] )
def test_add_md_toc(self, MD_INPUT, TOC_LEVEL, SKIP_LINES): if MD_INPUT is None or isinstance(MD_INPUT, Undefined): resp = add_md_toc(MD_INPUT) assert resp is None else: # Extract the TOC from the response with open(MD_INPUT, "r") as input_file: resp = add_md_toc(input_file.read( ), skip_lines=SKIP_LINES, toc_levels=TOC_LEVEL, toc_marker=TOC_MARKER) m = re.compile( r'(<avd_unit_test_tag_start>)([\S\s]*?)(<avd_unit_test_tag_end>)') toc_output = m.search(resp).group(2) # Generate TOC for input file toc_input = md_toc.build_toc( MD_INPUT, list_marker='-', keep_header_levels=TOC_LEVEL, skip_lines=SKIP_LINES) assert toc_output.strip() == toc_input.strip()
def main(): """ Main entry point for module execution.""" argument_spec = dict(md_file=dict(type='str', required=True), skip_lines=dict(type='int', required=False, default=0), toc_levels=dict(type='int', required=False, default=2), toc_marker=dict(type='str', required=False, default='<!-- toc -->')) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) result = dict(changed=False) # Test if md_toc is installed if HAS_MD_TOC is False: module.fail_json( msg='Error md_toc is not installed. Please install using pip') # If set, parse MD file and store generated TOC if (module.params['md_file'] is not None): md_file = module.params['md_file'] skip_lines = module.params['skip_lines'] or 0 toc_levels = module.params['toc_levels'] or 2 toc_marker = module.params['toc_marker'] or '<!-- toc -->' hash_origin = hashlib.sha224(open(md_file, 'rb').read()).hexdigest() toc = md_toc.build_toc(filename=md_file, keep_header_levels=toc_levels, skip_lines=skip_lines) md_toc.write_string_on_file_between_markers(filename=md_file, string=toc, marker=toc_marker) hash_final = hashlib.sha224(open(md_file, 'rb').read()).hexdigest() result['checksum'] = hash_final if hash_final != hash_origin: result['changed'] = True module.exit_json(**result)
def main(): parent = Path() docs = parent.joinpath('docs') if not docs.exists(): os.mkdir(docs) try: oc_yaml_abs = parent.joinpath("opencontrol.yaml").resolve(strict=True) except FileNotFoundError: print("No opencontrol.yaml file!") project = opencontrol.load_project_from_path(parent) ssp_doc = parent.joinpath(docs, 'ssp.md') with open(ssp_doc, 'w', newline="") as output: # Table of Contents placeholder. print('<!--TOC-->\n\n', file=output) # Create SSP document. print(ssp.build_ssp(project, { "include-control-descriptions": True, }), file=output) # Make the Table of Contents placeholder. toc = md_toc.build_toc(ssp_doc, keep_header_levels=2, skip_lines=2) md_toc.write_string_on_file_between_markers(ssp_doc, toc, '<!--TOC-->')
with open(IN_PATH) as f: data = f.read() COMMANDS = [ 'cptools-run', 'cptools-companion-server', 'cptools-stress-test', 'cptools-make-file' ] print('Substituting commands...') for command in COMMANDS: print(f'Substituting command {command}...') data = data.replace(f'$$${command} info$$$', get_help_message(command)) print('Substituting default files...') data = data.replace('$$$default configuration$$$', get_file(path.join('cptools', 'local_data', 'default_config.yml'))) \ .replace('$$$default stress test info$$$', get_file(path.join('cptools', 'local_data', 'default_stress_test.yml'))) with open(OUT_PATH, 'w') as f: f.write(data) print('Generating toc...') toc = md_toc.build_toc(OUT_PATH, parser='github') with open(OUT_PATH) as f: data = f.read() # print(data) with open(OUT_PATH, 'w') as f: f.write(data.replace('$$$toc$$$', toc))
def main(): md_files = find_all_markdown_files() print("Found markdown files:", md_files) updated_files = [] for md_file in md_files: toc = md_toc.build_toc(md_file, keep_header_levels=6, parser="github") with open(md_file) as fin: original_contents = fin.read() md_toc.write_string_on_file_between_markers(md_file, toc, "<!-- TOC -->") run_proc( f"/opt/clean_markdown/node_modules/prettier/bin-prettier.js --write {md_file}" ) with open(md_file) as fin: new_contents = fin.read() if original_contents != new_contents: updated_files.append((md_file, new_contents)) indent_per_level = " " index = "" seen = set([""]) for md_file in md_files: loc = os.path.split(md_file)[0] if loc not in seen: indent = indent_per_level * (len(loc.split(os.sep)) - 1) index += indent + "- " index += f"[{os.path.split(loc)[-1]}]({loc})" index += "\n" seen.add(loc) fn = os.path.split(md_file)[-1] index += indent_per_level * (len(md_file.split(os.sep)) - 1) index += "- " index += f"[{fn}]({md_file})" index += "\n" with open("README.md") as fin: original_readme = fin.read() md_toc.write_string_on_file_between_markers("README.md", index, "<!--- Index -->") run_proc( f"/opt/clean_markdown/node_modules/prettier/bin-prettier.js --write README.md" ) with open("README.md") as fin: new_readme = fin.read() if original_readme != new_readme: updated_files.append(("README.md", new_readme)) github = github3.login(token=os.environ["INPUT_GH_TOKEN"]) env_repo = os.environ["GITHUB_REPOSITORY"] owner = env_repo.split("/")[0] repo = env_repo.split("/")[1] repository = github.repository(owner, repo) branch_name = os.environ["GITHUB_HEAD_REF"] commit_message = os.environ["INPUT_COMMIT_MESSAGE"] print("Updated files:") pprint(updated_files) for t in updated_files: print("Pushing:", t[0]) repository.file_contents("/" + t[0], ref=branch_name).update(commit_message, t[1].encode("utf-8"), branch=branch_name)
to_print += '### ' + dsl_command + '\n' else: # for classes that don't use the `action=` paradigm like `expand()` and `resultify()` to_print += '### ' + dsl_name + '\n' if 'brief_description' in dic: to_print += dic['brief_description'] + '\n\n' del dic['brief_description'] if dic: # if the dic is empty, then don't create a table temp_table = Tomark.table([dic]) temp_table_split = temp_table.split("\n") #better_table = "|"+re.sub('\(\)',f'(action={action})', dsl_name) + ('|' * temp_table_split[0].count('|')) + '\n' better_table = ('|' * (temp_table_split[0].count('|') + 1)) + '\n' better_table += temp_table_split[1] + '-----|\n' better_table += '|_DSL parameters_' + temp_table_split[0] + "\n" better_table += '|_DSL arguments_' + temp_table_split[2] + "\n" to_print += better_table + '\n' else: to_print += '\n' file_name = 'DSL_Documentation.md' fid = open(file_name, 'w') fid.write(to_print) fid.close() with_toc = "# Table of contents\n\n" with_toc += md_toc.build_toc( os.path.dirname(os.path.abspath(__file__)) + '/' + file_name) with_toc += to_print fid = open(file_name, 'w') fid.write(with_toc) fid.close()
"""Adds a clickable TOC to the top of your Markdown documents.""" import pathlib import md_toc # get all .md files in the current directory recursively md_files = pathlib.Path.cwd().rglob('*.md') for file_ in md_files: with open(file_, 'r+') as f: content = f.read() # only add the TOC if specified for the file if content.find( '<!--TOC-->' ) >= 0: # .find() returns -1 if substring doesn't exist # the below settings keep two levels of headings for the TOC: h3 and h4 # h3 should be the main headings for a page on our platform toc = md_toc.build_toc(file_, keep_header_levels=4) # write to the beginning of the file f.seek(0, 0) f.write(f"{toc}{content}") if toc: print(f"Added TOC to: {file_}") else: print('...')
def write_toc(file): toc = md_toc.build_toc(file, keep_header_levels=3, skip_lines=5) md_toc.write_string_on_file_between_markers(file, toc, '<!--TOC-->')
docfiles = {} for file in files: if file in ignores: continue module = imp(file.replace(".py", "")) doc = transform(module.doc) docs += [doc] docfiles[file] = doc # Full Docs: docs = "\n---\n\n".join(docs) docs = "# Math Docs\n___\n\n" + "<TOC>\n\n___\n\n" + docs with open("./docs/index.md", "w+") as file: file.write(docs) docs = docs.replace("<TOC>", md_toc.build_toc('./docs/index.md')) with open("./docs/index.md", "w+") as file: file.write(docs) # File Docs: for file, filedoc in docfiles.items(): filename = file.replace(".py", "") + ".md" with open(f"./docs/{filename}", "w+") as f: f.write(filedoc) filedocs = filedoc.replace("<TOC>", md_toc.build_toc(f"./docs/{filename}")) with open(f"./docs/{filename}", "w+") as f: f.write(filedocs)