{"id":366,"date":"2023-08-11T08:54:08","date_gmt":"2023-08-11T14:54:08","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=366"},"modified":"2023-08-11T08:55:11","modified_gmt":"2023-08-11T14:55:11","slug":"automatically-generate-a-table-of-contents-using-a-python-script","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/automatically-generate-a-table-of-contents-using-a-python-script\/","title":{"rendered":"Automatically generate a Table of Contents using a python script"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#abb8c3\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" no-line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-php\">import re\n\ndef generate_table_of_content(content):\n    toc = \"## Table of Contents\\n\\n\"\n    headings = re.findall(r'^(#+)\\s+(.+)$', content, flags=re.MULTILINE)\n    \n    for heading in headings:\n        level, title = len(heading[0]), heading[1]\n        indent = \"  \" * (level - 1)\n        link = re.sub(r'[^\\w\\- ]', '', title.lower().replace(' ', '-'))\n        toc += f\"{indent}- [{title}](#{link})\\n\"\n    \n    return toc\n\ndef main():\n    input_file = \"README.md\"\n    output_file = \"TableOfContent.md\"\n\n    with open(input_file, \"r\") as file:\n        content = file.read()\n\n    toc = generate_table_of_content(content)\n\n    with open(output_file, \"w\") as file:\n        file.write(content + \"\\n\\n\" + toc)\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre><\/div><\/div>\n\n\n\n<p><strong>Usage<\/strong>:<\/p>\n\n\n\n<p>save a file named &#8216;conv.py&#8217; <\/p>\n\n\n\n<p>Open it and edit the name of your README file. <\/p>\n\n\n\n<p>Execute with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python conv.py<\/code><\/pre>\n\n\n\n<p>The End. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usage: save a file named &#8216;conv.py&#8217; Open it and edit the name of your README file. Execute with: The End.<\/p>\n","protected":false},"author":2,"featured_media":250,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,14,50,31,45,40],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/366"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=366"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/366\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/366\/revisions\/367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/250"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}