#!/usr/bin/env mawk -f BEGIN { RS = "\r?\n([ \t]*\r?\n)+" FS = "\r?\n" ORS = "\n\n" OFS = "\n" enum = "^[ \t]*[0-9]+[.):][ \t]" list = "^[ \t]*[-*o.][ \t]" ind = "^(\t| )" bigind = "^(\t+ | )" quote = "^[:|>] " line = "^[ \t]*(---+|===+|___+)[ \t]*$" hdr = "^[ \t]*#+.+#+[ \t]*$" norm = "^([A-Za-np-z]|o[a-z])" state = "norm" titleprinted = 0 print "" } function guess_para_mode() { if ( /^[ \t\r\n]*$/ ) return "empty" if ( $1 ~ bigind && $2 ~ bigind ) return "pre" if ( $1 ~ ind && $2 ~ ind ) return "quote" if ( $1 ~ norm && $2 ~ norm && $3 ~ ind ) return "defs" if ( $1 ~ norm && $2 ~ ind ) return "defs" if ( $2 ~ line && $3 == "" ) return "hdr" if ( $1 ~ line ) return "sep" if ( $1 ~ enum || $2 ~ enum ) return "enum" if ( $1 ~ list || $2 ~ list ) return "list" if ( $1 ~ quote || $2 ~ quote ) return "quote" if ( $1 ~ hdr ) return "hdr" if ( $1 ~ ind ) return "pre" } function peel_explanation() { if ( ( state == "norm" && $1 ~ norm && $2 ~ enum ) || ( state == "norm" && $1 ~ norm && $2 ~ list ) || ( state == "norm" && $1 ~ norm && $2 ~ quote ) ) { print handle_inlines( $1 ); $1 = "" } } function state_changes() { ostate = state state = guess_para_mode() if ( ostate == "defs" && state != "defs" ) print "" if ( ostate == "enum" && state != "enum" ) print "" if ( ostate == "list" && state != "list" ) print "" if ( ostate != "defs" && state == "defs" ) print "
" if ( ostate != "enum" && state == "enum" ) print "
    " if ( ostate != "list" && state == "list" ) print "
" if ( state == "enum" ) print "" if ( state == "list" ) print "" print "" }