Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created September 25, 2018 11:32
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/e7a166b31472fafe067dfebf8fe8563d to your computer and use it in GitHub Desktop.
Save mcsf/e7a166b31472fafe067dfebf8fe8563d 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