Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created October 15, 2015 22:50
Show Gist options
  • Save mastbaum/4f63624ccbe4a679de69 to your computer and use it in GitHub Desktop.
Save mastbaum/4f63624ccbe4a679de69 to your computer and use it in GitHub Desktop.
Markdown lab notebook generator
#!/bin/bash
# Collect the Markdown. Webpage it.
#
# Searches subdirectories for Markdown files and creates a static webpage
# with frames for navigation in a new directory _notebook/. Also copies
# over any images referenced in the md source.
#
# A. Mastbaum <[email protected]>, 10/2015
#
# The MIT License (MIT)
#
# Copyright (c) 2015 A. Mastbaum
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
write_index ()
{
mkdir -p _notebook
cat << EOF > _notebook/index.html
<html>
<head>
<title>Notebook</title>
</head>
<frameset cols="15%,85%">
<frame src="nav.html">
<frame src="" name="_main">
</html>
EOF
}
write_css ()
{
mkdir -p _notebook
cat << EOF > _notebook/style.css
pre {
background-color: #eee;
border: solid 1px #ccc;
padding: 5px;
}
p > code {
color: firebrick;
}
a:visited {
color: blue;
}
img {
display: block;
margin-left: auto;
margin-right: auto;
padding: 20px;
}
img[alt="medium"] {
width: 500px;
}
img[alt="small"] {
width: 250px;
}
EOF
}
mkdir -p _notebook
write_index
write_css
# Navigation frame header
cat << EOF > _notebook/nav.html
<html>
<head>
<title>Notebook</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<h2>Notebook</h2>
EOF
# Find and process Markdown files
for mdfile in `find . -type d -name _notebook -prune -o -name '*.md' -print`
do
name=`head -n 1 $mdfile`
page=$(echo ${mdfile#$"./"} | sed -e 's/\//\_/g')
echo "$mdfile -> _notebook/$page.html"
# Copy referenced images
for image in `sed -nE 's/\!\[(.+)\]\((.+)\)/\2/p' $mdfile`
do
mkdir -p _notebook/$(dirname $image)
cp -vp "$(dirname $mdfile)/$image" "_notebook/$image"
done
# Navigation link
echo "<p><a href=\"$page.html\" target=\"_main\">$name</a></p>" >> _notebook/nav.html
# Sloppy HTML, but prepend the stylesheet link
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"/>" > "_notebook/$page.html"
markdown $mdfile >> "_notebook/$page.html"
# Footer
echo "<hr/><i>Generated from</i> <tt>$mdfile</tt><br/><i>by</i> $USER <i>at</i> `date`" >> "_notebook/$page.html"
done
# Navigation footer
cat << EOF >> _notebook/nav.html
</body></html>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment