Created
November 9, 2013 17:58
-
-
Save lcolladotor/7388033 to your computer and use it in GitHub Desktop.
Textmate command for creating Jekyll-Bootstrap posts via Knitr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh" | |
# Source some support functions we need. | |
. "${TM_SUPPORT_PATH}/lib/html.sh" | |
. "${TM_SUPPORT_PATH}/lib/webpreview.sh" | |
# Get the viewer program. Any program (that works with open -a <name>) can be used, | |
# except 'html' which is reserved to mean the internal HTML window. This is also the | |
# default option. | |
## I am using the following extension to preview markdown files | |
## https://chrome.google.com/webstore/detail/jmchmkecamhbiokiopfpnfgbidieafmd | |
V="Google Chrome" | |
M=${TM_LATEX_MASTER:-$TM_FILEPATH} | |
DIR=`dirname "$M"` | |
FILE=`basename "${M%.*}".Rmd` | |
FINAL="${FILE%.*}.md" | |
echo $FILE | |
echo $DIR | |
echo $FINAL | |
# Switch to the right directory. | |
cd "$TM_PROJECT_DIRECTORY" | |
cd "$DIR" | |
# Prepare output window. | |
html_header 'knit html result' "$FILE" | |
echo '<h2>Kniting…</h2>' | |
SW=${TM_SWEAVE_WD:=${TM_DIRECTORY}} | |
echo -e "setwd('$SW'); library(knitr); KnitPost <- function(input, base.url = '/') { opts_knit\$set(base.url = base.url); fig.path <- paste0('figs/', sub('.Rmd$', '', basename(input)), '/'); opts_chunk\$set(fig.path = fig.path); opts_chunk\$set(fig.cap = 'center'); render_jekyll(); knit(input) }; KnitPost('$FILE')" | R --vanilla --quiet | pre | |
# If there is output and it's newer than the current document, then we go directly to preview or link to it. | |
if [ -s "${FINAL}" -a ! "$FINAL" -ot "$FILE" ]; then | |
if [[ "$V" == html ]]; then | |
WEB_LOC="file://${DIR}/${FINAL}" | |
WEB_LOC="${WEB_LOC//\\#/%23}" | |
redirect "${WEB_LOC}" | |
else | |
V=${V%.app} # strip potential .app extension | |
if find_app >/dev/null "$V.app"; then | |
open -a "$V" "$FINAL" | |
else | |
strong "Error locating external viewer: $V" | |
fi | |
fi | |
else | |
strong "Error: Markdown file not written to disk" | |
fi | |
html_footer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment