Skip to content

Instantly share code, notes, and snippets.

@kasperis7
Last active January 17, 2022 12:24
Show Gist options
  • Save kasperis7/002b09496f1368ec214aa661999ced79 to your computer and use it in GitHub Desktop.
Save kasperis7/002b09496f1368ec214aa661999ced79 to your computer and use it in GitHub Desktop.
A Simple Gitbook Summary Generator

A Simple Gitbook Summary Generator

Let's say you use a local editor like Obsidian or Typora to take notes on a daily basis and save them all on your local file system. One day, you want to build a blog to share these notes, but too lazy to generate an outline for them. Well, this script is for you.

Suppose your notes are organized like this:

notes
├── 2021
│   ├── document_1.md
│   └── document_2.md
├── 2020
│   ├── document_0.md
│   └── cats.md
├── README.md // the homepage content

Just run gitbook_summary_generator.py <path_to_notes> > SUMMARY.md, and you can easily build a simple static blog with gitbook.

Within each directory, the notes are sorted by last modified date. By default, the script reads the first line of the note as the title of the note. If you don't have any title in a note, it uses the document's filename and replaces '_'s to spaces.

You can freely use and modify this script as you want.

-- cyanide

#!/usr/bin/env python3
# gitbook_summary_generator.py
import os,sys
def walk(root):
dirs_0 = []
for parent, dirnames, filenames in os.walk(root):
dirs_0.append([parent, dirnames, filenames])
dirs = []
for d in dirs_0:
if ('.git' in d[0]) or ('_book' in d[0]):
pass
else:
dirs.append(d)
return dirs
def filter_files(dirs):
docs = []
for i in dirs:
for f in i[2]:
if (f[-3:] == '.md') or (f[-3:] == '.MD'):
docs.append(os.path.join(i[0],f))
return docs
def getmtime(path):
return os.path.getmtime(path)
def sort(docs,root):
dirs = []
dir_content = dict()
for d in docs:
dirname = os.path.dirname(d)
if (dirname not in dirs):
dirs.append(dirname)
dir_content[dirname] = []
dir_content[dirname].append(d)
for d in dirs:
dir_content[d].sort(key=getmtime, reverse=True)
dirs.sort(key=getmtime, reverse = True)
dirs.remove(root)
dirs.insert(0,root)
return dirs, dir_content
def generate_title(path):
f = open(path)
first_line = f.readline()
title = os.path.basename(path)[:-3].replace('_',' ')
if (first_line[:2] == '# '):
title = first_line[2:-1]
elif (first_line[:3] == '## '):
title = first_line[3:-1]
return title
def remove_root(path, root):
new_path = path[len(root)+1:]
return new_path
def generate(dirs, dir_content, root):
for d in dirs:
if (d == root):
for doc in dir_content[d]:
if (os.path.basename(doc) != 'SUMMARY.md'):
print('* [' + generate_title(doc) + '](' + remove_root(doc, root) + ')')
else:
print('- ' + os.path.basename(d))
for doc in dir_content[d]:
print(' * [' + generate_title(doc) + '](' + remove_root(doc, root) + ')')
def main(argv):
dirs = walk(argv)
docs = filter_files(dirs)
dirs, dir_content = sort(docs, argv)
generate(dirs, dir_content, argv)
if __name__ == "__main__":
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment