Skip to content

Instantly share code, notes, and snippets.

@alganet
Created May 30, 2015 04:11
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 alganet/300123a28f95349c0a60 to your computer and use it in GitHub Desktop.
Save alganet/300123a28f95349c0a60 to your computer and use it in GitHub Desktop.
runup_builder
runup_builder () {
(
tab=$(printf '\t')
line="[0-9][0-9]*${tab}"
stream_doc="/^0${tab}\(.*\)$/"
doc_line="/^${line}\(.*\)/"
doc_indent="/^${line}\(${tab}\| \)\(.*\)$/"
doc_fence="/^${line}\(~~~\|\`\`\`\)$/"
doc_direc="/^${line}\(\[@\]\:\)\([a-zA-Z0-9]*\)\s*(*\([^)]*\)\s*)*\s*$/"
cat <<-SED > foo & cat foo
:stream
$ { b endstream }
${stream_doc} { b _file }
h
s/^/<STREAM 0/;p
g
n
b stream
:_file
$ { b endfile }
s/^/<FILE /;p
a <DOC\ 0
n
b _body_in
:_docfile
$ { b endfile }
# chekc if emptu line
a DOC>\ 0
s/^/FILE> 0/;p
n
b stream
:_body
$ { b endbody }
s/^/DOC /;p
N
s/^.*\\
//
:_body_in
$ { b endbody }
${doc_indent} { b _code_indented_open }
${doc_fence} { h ; b _code_fenced }
${doc_direc} { b _directive }
${doc_line} { b _body }
b _docfile
:_directive
$ { b endbody }
s/^/DIRECT /;p
n
b _body_in
:_code_indented_open
$ { b endbody }
i <CODE\ 0
b _code_indented
:_code_indented
$ { b endbody }
s/^/CODE /;p
n
${doc_indent} { b _code_indented }
/^${line}$/ { b _code_indented }
${doc_line} { b _code_indented_close }
b _docfile
:_code_indented_close
i CODE>\ 0
$ { b endbody }
b _body
:_code_fenced
s/^/<FENCE /;p
$ { b endbody }
n
:_code_fenced_in
s/^/FENCE /;p
$ { b endbody }
n
${doc_fence} {
G
/^${line}~~~\\
${line}~~~$/ { b _code_fenced_close }
/^${line}\`\`\`\\
${line}\`\`\`$/ { b _code_fenced_close }
s/^\(${line}\)\(~~~\|\`\`\`\).*/\1\2/
b _code_fenced_in
}
b _code_fenced_in
:_code_fenced_close
$ { b endbody }
s/^\(${line}\)\(~~~\|\`\`\`\).*/\1\2/
s/^/FENCE> /;p
n
b _body_in
:endbody
a DOC>\ 0
a FILE>\ 0
a STREAM>\ 0
q
:endfile
a FILE>\ 0
q
:endstream
a FILE>\ 0
a STREAM>\ 0
q
SED
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment