def convert(fname): """Convert an m file into an IPython notebook Takes most of the grunt work out of translating a numerical-tours matlab file into an ipython notebook. It will auto-populate the notebook with headings, markdown, code, and exercises as appropriate. Ideally, all one then has to do is translate the code itself. Some code transformations are in place (see CODE_REPLS and _parse_code in this file). """ with open(fname) as fid: lines = fid.readlines() nb = Notebook() nb.add_heading(lines[0][3:].rstrip()) state = 'markdown' out_lines = [] for line in lines[1:]: new_state, new_line = parse_line(line, state) if not new_state == state: get_section(nb, state, out_lines) out_lines = [new_line] state = new_state else: out_lines.append(new_line) # handle the last section get_section(nb, state, out_lines) fname = fname.replace('.m', '.ipynb') fname = os.path.basename(fname) dname = os.path.dirname('__file__') path = os.path.join(os.path.abspath(dname), fname) nb.save(path)