Skip to content

Instantly share code, notes, and snippets.

@gregcaporaso
Created June 17, 2015 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregcaporaso/7a141d6c30e621ae6351 to your computer and use it in GitHub Desktop.
Save gregcaporaso/7a141d6c30e621ae6351 to your computer and use it in GitHub Desktop.
Used for gregcaporaso/An-Introduction-to-Applied-Bioinformatics#140 to convert the ipython notebooks to markdown files.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hello world!"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"***WROTE: book/Index.md\n",
"***WROTE: book/applications/biological-diversity.md\n",
"***WROTE: book/fundamentals/database-searching.md\n",
"***WROTE: book/fundamentals/msa-assignment.md\n",
"***WROTE: book/fundamentals/multiple-sequence-alignment.md\n",
"***WROTE: book/fundamentals/pairwise-alignment-exercises.md\n",
"***WROTE: book/fundamentals/pairwise-alignment.md\n",
"***WROTE: book/fundamentals/phylogeny-reconstruction.md\n",
"***WROTE: book/fundamentals/sequence-mapping-and-clustering.md\n",
"***WROTE: book/getting-started/reading-iab.md\n"
]
}
],
"source": [
"from random import random\n",
"import hashlib\n",
"\n",
"def randhash():\n",
" m = hashlib.sha1()\n",
" m.update(str(random()))\n",
" return m.hexdigest()[:6]\n",
"\n",
"import os\n",
"ind = '/Users/caporaso/temp/An-Introduction-To-Applied-Bioinformatics-master/'\n",
"d = 'book'\n",
"try:\n",
" os.makedirs('./%s' % d)\n",
"except OSError:\n",
" pass\n",
"\n",
"cmd = \"ipython nbconvert %s --to markdown --output %s\"\n",
"\n",
"for root, dirs, files in os.walk(ind):\n",
" out_root = root.replace(ind, \"\")\n",
" for f in files:\n",
" if '.ipynb_checkpoints' in root: continue\n",
" fn, ext = os.path.splitext(f)\n",
" if ext =='.ipynb':\n",
" try:\n",
" os.makedirs(os.path.join(d, out_root))\n",
" except OSError:\n",
" pass\n",
" inf = os.path.join(root, f)\n",
" outf = os.path.join(d, out_root, '%s.md' % fn)\n",
" x = cmd % (inf, outf)\n",
" y = !$x\n",
" lines = open(outf)\n",
" out_lines = []\n",
" in_code = False\n",
" for line in lines:\n",
" line = line.strip('\\n')\n",
" if line.startswith(' '):\n",
" if not in_code:\n",
" in_code = True\n",
" out_lines.append('```python')\n",
" line = line[4:]\n",
" if len(line) > 0:\n",
" out_lines.append('>>> %s' % line)\n",
" else:\n",
" out_lines.append('...')\n",
" else:\n",
" if in_code:\n",
" in_code = False\n",
" out_lines.append('```')\n",
" \n",
" if line.startswith('#'):\n",
" out_lines.append(\"%s <link src='%s'/> \"% (line, randhash()))\n",
" else:\n",
" out_lines.append(line)\n",
" if in_code:\n",
" out_lines.append('```')\n",
" outs = '\\n'.join(out_lines)\n",
" outs = outs.replace('\\n\\n\\n', '\\n\\n')\n",
" open(outf, 'w').write(outs)\n",
" print \"***WROTE: %s\" % outf"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'021fa4'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment