Original code and demo at this gist.
Given a Markdown document like
Here is some text,
and some more text.
```javascript
const message = "This is JavaScript!";
```
More text follows, and then
```javascript
console.log(message);
```
I want to print out the sections in code fences, not including the code fences, separated by a single blank line:
const message = "This is JavaScript!";
console.log(message);
I came up with the following AWK script, which seems to do the job nicely:
#!/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;
}
A goal is for the script to be dependency-minimal. I don't want to have to install an implementation of CommonMark or an Erlang environment. AWK fits this bill well.
Correspondingly, a non-goal is for this script to be correct in all
cases: I’m happy to accept false positives on lines starting with
```inline code``` like this
, and similar edge cases.
I’m mostly looking for critique of my AWK, with respect to which I am a total newbie. But any comments are welcome!