Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature markdown inline html rendering in shell #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions _shell/markdown-inline-tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env zsh

# Copyright (C) 2012-2018 Dyne.org Foundation
#
# Designed, written and maintained by Denis Roio <[email protected]>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

parser=""

# check for existance of pandoc in path
if command -v pandoc > /dev/null; then
parser="pandoc -f gfm -t html5"
else
echo "Required program not found: pandoc"
echo "Install pandoc on this system to render markdown"
return 1
fi

## temporary output
TMPDIR=${TMPDIR:-/tmp}
dtmp=`mktemp -d ${TMPDIR}/markdown-inline-tag.XXXXXXXXXX`
[[ "$dtmp" == "" ]] && return 1
otmp=`mktemp -p $dtmp`

if [[ "$2" == "" ]]; then
echo "Render HTML files with <markdown> tagged content inline"
echo "usage: markdown-inline-tag [-w] input.html output.html"
return 1
fi
# TODO: better argument handling
watch=0
if [[ "$1" =~ "-w" ]]; then
watch=1
shift
fi
input="$1"
output="$2"

tokenize() {
#######################################
## we support the <markdown> tag inline

# parses the html and put all stuff contained in <markdown /> tags
# inside separate files, leaving a trace of them into the main html
# (a line starting with tmp.md$RAND)
awk 'BEGIN { srand(); markdown=0; outfile="" }
/<markdown>/ { markdown=1; out="tmp.md" rand(); outfile="'"${dtmp}"'/" out;
print out; next }
/<\/markdown>/ { markdown=0; next }
{ if(markdown==1) { print $0 >outfile; next } else { print $0 } }
' > $otmp
}

collate() {
# first pass marks the markdown parts and saves them separate
mds=(`find ${dtmp} -name 'tmp.md*'`)
# second pass substituted saved parts with rendered markdown
# parses all html and renders each markdown in the html
for i in $mds; do
md=`basename $i`
newtemp=`mktemp -p $dtmp`
cat $otmp | awk '
/^'"$md"'/ { system("cat '"$i"' | '"$parser"'"); next }
{ print $0; }' > $newtemp
rm $otmp; otmp=$newtemp
done
}

render() {
cat $input | tokenize
collate
cat $otmp > $output
}

if [[ $watch == 1 ]]; then
ts=""; tts=""
while true; do
ts=`stat $input | grep '^Change:'`
if [[ "$ts" != "$tts" ]]; then
render
tts="$ts"
fi
sleep .5
done
else
render
fi
# clean up from temporary files
rm -rf "$dtmp"
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
},
"scripts": {
"css-deploy": "npm run css-build && npm run css-postcss",
"css-build": "node-sass _sass/main.scss css/main.css",
"css-postcss": "postcss --use autoprefixer --output css/main.css css/main.css",
"css-build": "node-sass _sass/main.scss pub/css/main.css",
"css-postcss": "postcss --use autoprefixer --output pub/css/main.css pub/css/main.css",
"css-watch": "npm run css-build -- --watch",
"deploy": "npm run css-deploy && npm run js-build",
"js-build": "babel _javascript --out-dir lib",
"js-build": "babel _javascript --out-dir pub/lib",
"js-watch": "npm run js-build -- --watch",
"start": "npm-run-all --parallel css-watch js-watch"
"index": "_shell/markdown-inline-tag -w views/index.html pub/index.html",
"start": "npm-run-all --parallel css-watch js-watch index"
}
}
Empty file added pub/.keep
Empty file.
File renamed without changes.