Created
August 10, 2018 21:57
-
-
Save wchargin/e7e72370efe58d0959e2fb68065ae539 to your computer and use it in GitHub Desktop.
print code fences, optionally only those tagged with a language
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
#!/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 |
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
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