Last active
August 29, 2015 14:10
-
-
Save inodb/6f571764a6395c9378a0 to your computer and use it in GitHub Desktop.
Bash function to get the code parts from rst file (uses awk). Use either with ``rst_to_code filename`` or ``cat *.rst | rst_to_code -``
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
#!/bin/sh | |
# Get only code blocks from rst file, use like: | |
# rst2code index.rst | |
# or | |
# cat index.rst | rst2code - | |
# pipe to bash if it is bash code e.g. | |
# rst2code index.rst | bash -x | |
rst2code() { | |
awk 'BEGIN {code=0} | |
{ | |
if ($0 ~ "::" && $0 !~ "toctree") { code=1; lines=0; } | |
else { | |
if (code) { | |
no_edit = $0 | |
gsub(" +","", $0) | |
no_whitespace = $0 | |
if (length(no_whitespace) == 0 && lines > 0) { code=0 } | |
else { print no_edit; lines +=1; } | |
} | |
} | |
}' $1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment