Skip to content

Instantly share code, notes, and snippets.

@inodb
Last active August 29, 2015 14:10
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 inodb/6f571764a6395c9378a0 to your computer and use it in GitHub Desktop.
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 -``
#!/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