Created
July 23, 2018 14:11
-
-
Save mcsf/6f2e54217b61aa876a397258ed0f0c85 to your computer and use it in GitHub Desktop.
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
#!/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