Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created July 23, 2018 14:11
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 mcsf/6f2e54217b61aa876a397258ed0f0c85 to your computer and use it in GitHub Desktop.
Save mcsf/6f2e54217b61aa876a397258ed0f0c85 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Given an input documentation file, output all JavaScript code snippets
# contained therein.
INPUT_DOC="$1"
OPENING_LINES=$( sed -n '/^```js$/=' "$INPUT_DOC" )
for LINE in $OPENING_LINES; do
SNIPPET_LENGTH=$( sed -e "1,${LINE}d" "$INPUT_DOC" | sed -n '/^```$/=' | head -1 )
CLOSING_LINE=$( expr $LINE + $SNIPPET_LENGTH )
# Exclude the opening and closing tags from the range
RANGE_STRING="$( expr $LINE + 1)-$( expr $CLOSING_LINE - 1 )"
echo -e "//////////////"
echo "// BEGIN SNIPPET: $RANGE_STRING"
sed -n "$LINE,/^\`\`\`$/p" "$INPUT_DOC" | sed -e '1d' -e '$d'
echo "// END SNIPPET: $RANGE_STRING"
echo -e "//////////////\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment