|
| 1 | +import os |
| 2 | +import re |
| 3 | +from jinja2 import Environment, FileSystemLoader, select_autoescape |
| 4 | + |
| 5 | + |
| 6 | +def atoi(text): |
| 7 | + return int(text) if text.isdigit() else text |
| 8 | + |
| 9 | + |
| 10 | +def natural_keys(text): |
| 11 | + return [atoi(c) for c in re.split('(\d+)', text)] |
| 12 | + |
| 13 | + |
| 14 | +class notebook: |
| 15 | + def __init__(self, path, title): |
| 16 | + # remove ../ from path |
| 17 | + self.path = path.replace('../', '') |
| 18 | + self.title = title |
| 19 | + # set url and update from markdown to ipynb |
| 20 | + self.url = url_prefix + self.path.replace(".md", ".ipynb") |
| 21 | + |
| 22 | + |
| 23 | +def get_title(filename): |
| 24 | + """ Reads the title from a markdown notebook """ |
| 25 | + with open(filename, 'r') as f: |
| 26 | + # get first row that starts with "# " |
| 27 | + for line in f.readlines(): |
| 28 | + # trim leading/trailing whitespaces |
| 29 | + line = line.lstrip().rstrip() |
| 30 | + # check if line is the title |
| 31 | + if line[0:2] == '# ': |
| 32 | + # return title |
| 33 | + return line[2:] |
| 34 | + |
| 35 | + |
| 36 | +def sort_files_titles(files, titles): |
| 37 | + """ Sorts the files and titles either by filenames or titles """ |
| 38 | + # identify numbered files and sort them |
| 39 | + nfiles = [s for s in files if s.split('/')[-1][0].isdigit()] |
| 40 | + nfiles = sorted(nfiles, key=natural_keys) |
| 41 | + ntitles = [titles[files.index(s)] for s in nfiles] |
| 42 | + # sort the files without numbering by the alphabetic order of the titles |
| 43 | + atitles = [titles[files.index(s)] for s in files if s not in nfiles] |
| 44 | + atitles = sorted(atitles, key=natural_keys) |
| 45 | + afiles = [files[titles.index(s)] for s in atitles] |
| 46 | + # merge the numbered and unnumbered sorting |
| 47 | + return nfiles + afiles, ntitles + atitles |
| 48 | + |
| 49 | + |
| 50 | +def get_notebooks(path): |
| 51 | + """ Gets a list of all notebooks in a directory """ |
| 52 | + # get list of files and their titles |
| 53 | + try: |
| 54 | + files = [path + f for f in os.listdir(path) if f.endswith('.md')] |
| 55 | + except FileNotFoundError: |
| 56 | + return {} |
| 57 | + titles = [get_title(f) for f in files] |
| 58 | + # sort the files and titles for display |
| 59 | + files_sorted, titles_sorted = sort_files_titles(files, titles) |
| 60 | + # generate notebook objects from the sorted lists and return |
| 61 | + notebooks = [notebook(f, t) for f, t in zip(files_sorted, titles_sorted)] |
| 62 | + return notebooks |
| 63 | + |
| 64 | + |
| 65 | +def generate_index_html(version_directory, tutorial_directories, title, |
| 66 | + version_note): |
| 67 | + """ Generates the index html file from the given data""" |
| 68 | + # get tutorials from the different directories |
| 69 | + tutorials = {} |
| 70 | + for dir in tutorial_directories: |
| 71 | + tutorials[dir] = get_notebooks(version_directory + dir + '/') |
| 72 | + |
| 73 | + # Load environment for Jinja and template |
| 74 | + env = Environment( |
| 75 | + loader=FileSystemLoader("../"), |
| 76 | + autoescape=select_autoescape() |
| 77 | + ) |
| 78 | + template = env.get_template("website/index.html.jinja") |
| 79 | + |
| 80 | + # render template and return |
| 81 | + html = template.render(tutorials=tutorials, title=title, |
| 82 | + version_note=version_note) |
| 83 | + return html |
| 84 | + |
| 85 | + |
| 86 | +# url prefix for the links |
| 87 | +url_prefix = "https://nbviewer.org/urls/qutip.org/qutip-tutorials/" |
| 88 | +# tutorial directories |
| 89 | +tutorial_directories = [ |
| 90 | + 'heom', |
| 91 | + 'lectures', |
| 92 | + 'pulse-level-circuit-simulation', |
| 93 | + 'python-introduction', |
| 94 | + 'quantum-circuits', |
| 95 | + 'time-evolution', |
| 96 | + 'visualization', |
| 97 | + 'miscellaneous' |
| 98 | +] |
| 99 | + |
| 100 | +# +++ READ PREFIX AND SUFFIX +++ |
| 101 | +prefix = "" |
| 102 | +suffix = "" |
| 103 | + |
| 104 | +with open('prefix.html', 'r') as f: |
| 105 | + prefix = f.read() |
| 106 | +with open('suffix.html', 'r') as f: |
| 107 | + suffix = f.read() |
| 108 | + |
| 109 | +# +++ VERSION 4 INDEX FILE +++ |
| 110 | +title = 'Tutorials for QuTiP Version 4' |
| 111 | +version_note = 'This are the tutorials for QuTiP Version 4. You can \ |
| 112 | + find the tutorials for QuTiP Version 5 \ |
| 113 | + <a href="./index-v5.html">here</a>.' |
| 114 | + |
| 115 | +html = generate_index_html('../tutorials-v4/', tutorial_directories, title, |
| 116 | + version_note) |
| 117 | +with open('index.html', 'w+') as f: |
| 118 | + f.write(prefix) |
| 119 | + f.write(html) |
| 120 | + f.write(suffix) |
| 121 | + |
| 122 | +# +++ VERSION 5 INDEX FILE +++ |
| 123 | +title = 'Tutorials for QuTiP Version 5' |
| 124 | +version_note = 'This are the tutorials for QuTiP Version 5. You can \ |
| 125 | + find the tutorials for QuTiP Version 4 \ |
| 126 | + <a href="./index.html">here</a>.' |
| 127 | + |
| 128 | +html = generate_index_html('../tutorials-v5/', tutorial_directories, title, |
| 129 | + version_note) |
| 130 | +with open('index-v5.html', 'w+') as f: |
| 131 | + f.write(prefix) |
| 132 | + f.write(html) |
| 133 | + f.write(suffix) |
0 commit comments