Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created August 10, 2018 21:57
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 wchargin/e7e72370efe58d0959e2fb68065ae539 to your computer and use it in GitHub Desktop.
Save wchargin/e7e72370efe58d0959e2fb68065ae539 to your computer and use it in GitHub Desktop.
print code fences, optionally only those tagged with a language
#!/usr/bin/awk -f
#
# To print all fenced code blocks in `my_file.md`:
# ./print_code_fences my_file.md
#
# To print only fenced code blocks tagged with `javascript`:
# ./print_code_fences.awk -v lang=javascript my_file.md
BEGIN { in_code_block = 0 }
/^```/ {
if (!in_code_block) {
sub(/^`*/, "");
this_lang = $0;
if (!lang || lang == this_lang) {
in_code_block = 1;
next;
}
} else {
in_code_block = 0;
print "";
}
}
in_code_block
Here is some text
```javascript
// with some JavaScript code
```
and some more text
```c
/* and some C code */
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment