Ajout table des matieres dans le code

This commit is contained in:
Francois Pelletier 2023-07-10 14:59:05 -04:00
parent a27e005ee3
commit 1f063f7ba7

View file

@ -362,11 +362,89 @@
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Création d'une table des matières"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"source": [
"list_index_html = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(\"html\")) for f in fn if f.endswith('index.html')]"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# extract title from index.html\n",
"list_title = []\n",
"for index_html in list_index_html:\n",
" soup = BeautifulSoup(open(index_html), 'html.parser')\n",
" title = soup.title.string\n",
" page = url+index_html.replace(\"html/\",\"\").replace(\"modules/\",\"\").replace(\"index.html\",\"\")\n",
" title = title.replace(\" » Propulsé par ThriveCart\",\"\")\n",
" level = len([i for i in page.split(\"/\") if i != \"\"])-3\n",
" list_title.append({\"page\": page, \"title\": title, \"level\": level})"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"def generate_link(page, title, level):\n",
" if level == 3:\n",
" splitss = page.split(\"/\")\n",
" splitss.remove(splitss[-3])\n",
" page = \"/\".join(splitss)\n",
" link = (\"Module\" if level==2 else \"-\")+\" [\"+title+\"](\"+page+\")\\n\"\n",
" return link"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"if os.path.exists(\"table_matiere.md\"):\n",
" os.remove(\"table_matiere.md\")\n",
"with open(\"table_matiere.md\", \"a\") as f:\n",
" f.write(\"# Table des matières\\n\\n\")\n",
" for tt in list_title:\n",
" if tt[\"level\"] == 2:\n",
" f.write(\"\\n\\n----\\n\\n\")\n",
" f.write(generate_link(tt[\"page\"], tt[\"title\"], tt[\"level\"]))\n",
" if tt[\"level\"] == 2:\n",
" f.write(\"\\n\\n\")"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"!pandoc -i table_matiere.md -o table_matiere.html"
],
"metadata": {
"collapsed": false
}