Skip to content

Instantly share code, notes, and snippets.

@alganet
Last active July 23, 2016 07:52
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/542f46865420529c9bd2 to your computer and use it in GitHub Desktop.
Save alganet/542f46865420529c9bd2 to your computer and use it in GitHub Desktop.
WIP sed markdown parser. Prepare input with `sed '=' "/filepath.md" | sed "N;s/\n/$(printf '\t')/"`
:stream
$ { b endstream }
/^0 \(.*\)$/ { 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 }
/^[0-9][0-9]* \( \| \)\(.*\)$/ { b _code_indented_open }
/^[0-9][0-9]* \(~~~\|```\)$/ { h ; b _code_fenced }
/^[0-9][0-9]* \(\[@\]\:\)\([a-zA-Z0-9]*\)\s*(*\([^)]*\)\s*)*\s*$/ { b _directive }
/^[0-9][0-9]* \(.*\)/ { 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
/^[0-9][0-9]* \( \| \)\(.*\)$/ { b _code_indented }
/^[0-9][0-9]* $/ { b _code_indented }
/^[0-9][0-9]* \(.*\)/ { 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
/^[0-9][0-9]* \(~~~\|```\)$/ {
G
/^[0-9][0-9]* ~~~\
[0-9][0-9]* ~~~$/ { b _code_fenced_close }
/^[0-9][0-9]* ```\
[0-9][0-9]* ```$/ { b _code_fenced_close }
s/^\([0-9][0-9]* \)\(~~~\|```\).*/\1\2/
b _code_fenced_in
}
b _code_fenced_in
:_code_fenced_close
$ { b endbody }
s/^\([0-9][0-9]* \)\(~~~\|```\).*/\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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment