Last active
May 23, 2018 04:01
-
-
Save wchargin/e371d4f3047cf5d89e7ff9bb47224243 to your computer and use it in GitHub Desktop.
print Markdown code fences, for a weak version of literate Markdown
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 | |
BEGIN { in_code_block = 0 } | |
/^```/ { | |
if (!in_code_block) { | |
in_code_block = 1; | |
first_line = 1; | |
} else { | |
in_code_block = 0; | |
print ""; | |
} | |
} | |
{ | |
if (in_code_block && !first_line) { | |
print; | |
} | |
first_line = 0; | |
} |
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
$ cat sample.md | |
Here is some text, | |
and some more text. | |
```javascript | |
const message = "This is JavaScript!"; | |
``` | |
More text follows, and then | |
```javascript | |
console.log(message); | |
``` | |
$ awk -f print_code_fences.awk sample.md | |
const message = "This is JavaScript!"; | |
console.log(message); | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm aware that this has false positives (
with ```inline code```
) and false negatives (leading whitespace). It's good enough for me right now.