Skip to content

Instantly share code, notes, and snippets.

@wchargin
Last active May 23, 2018 04:01
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/e371d4f3047cf5d89e7ff9bb47224243 to your computer and use it in GitHub Desktop.
Save wchargin/e371d4f3047cf5d89e7ff9bb47224243 to your computer and use it in GitHub Desktop.
print Markdown code fences, for a weak version of literate Markdown
#!/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;
}
$ 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);
$
@wchargin
Copy link
Author

I'm aware that this has false positives (with ```inline code``` ) and false negatives (leading whitespace). It's good enough for me right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment