#! /usr/bin/gawk -f # Last edited on 2004-02-05 05:29:35 by stolfi BEGIN { abort = -1; chnum = 0; } (abort >= 0) { exit abort; } /^ *$/ { next; } // { gsub(/<[Bb][Rr]>/, " "); gsub(/<[\/]*[Bb]>/, " "); gsub(/<[\/]*sera>/, " "); gsub(/[&]nbsp;/, " "); gsub(/^[\011 ]+/, ""); gsub(/[\011 ]+$/, ""); gsub(/[\011 ][\011 ]+/, " "); } /^<([!]DOCTYPE|META|meta)[^<>]*>$/ { next; } /^<[\/]*(html|head|HTML|HEAD|BLOCKQUOTE|sera)>$/ { next; } /^[^<>]*<[\/]TITLE>$/ { title = $0; gsub(/<TITLE> */, "", title); gsub(/ *<[\/]TITLE>/, "", title); printf "# HTML Title {%s}\n", title; next; } /^<[\/]*BODY[^<>]*>$/ { next; } /^<[Hh][123]><A NAME="mqdm"><[\/]A>[^<>]*<[\/][Hh][123]>$/ { title = $0; gsub(/<[Hh][123]><A NAME="mqdm"><[\/]A> */, "", title); gsub(/ *<[\/][Hh][123]>/, "", title); if (title ~ /[.]/) { data_error("oops - input uses \".\""); } gsub(/ *[:][:] *$/, ".", title); printf "@sec{1}{preface}{0}\n"; printf "@sec{2}{prefacetitle}{0}\n"; printf "@text{gtit}\n\n %s\n\n", title; printf "@sec{2}{prefacebody}\n"; printf "@text{g}\n\n"; next; } /^<[Hh][123]><A NAME="[0-9]+"><[\/]A>[^<>]*<[\/][Hh][123]>$/ { chnum++; en_chnum = $0; gsub(/<[Hh][123]> *<A NAME=" */, "", en_chnum); gsub(/ *"><[\/]A>[^<>]*<[\/][Hh][123]>/, "", en_chnum); if (en_chnum + 0 != chnum) { data_error("chapter number mismatch"); } gz_title = $0; gsub(/<[Hh][123]> *<A NAME="[0-9]+"><[\/]A> */, "", gz_title); gsub(/ *<[\/][Hh][123]>/, "", gz_title); if (gz_title ~ /[.]/) { data_error("oops - input uses \".\""); } gsub(/ *[:][:] *$/, ".", gz_title); printf "\n"; printf "@sec{1}{chapter}{%d}\n", chnum; printf "@sec{2}{chaptertitle}{0}\n"; printf "@text{gtit}\n\n"; printf " %s\n\n", gz_title; printf "@sec{2}{chapterbody}{1}\n"; printf "@text{g}\n\n"; next; } /^<DIV ALIGN=right>/ { next; } /^forms a part of / { next; } /^Texts<[\/]A>/ { next; } // { lin = $0; # Normalize internal spaces: gsub(/ *[:][|][:] */, " :|: ", lin); gsub(/ *[:][:] */, ":: ", lin); gsub(/[ ]+$/, "", lin); gsub(/[ ][ ]+/, " ", lin); gsub(/^[ ]*/, " ", lin); # Insert blank lines between apparent paragraphs: gsub(/ *<[\/]*[Pp]> */, "\n\n ", lin); # Replace SERA full stop "::" by ".": # But first make sure that there is no "." already! if (lin ~ /[.]/) { data_error("oops - input uses \".\""); } gsub(/[:][:]/, ".", lin); # Replace SERA parag mark ":|:" by "=" and a blank line: # But first make sure that there is no "=" already! if (lin ~ /[=]/) { data_error("oops - input uses \"=\""); } gsub(/ *[:][|][:] */, "\n =\n\n ", lin); print lin; next; } function data_error(msg){ printf "%d: error: %s\n", FNR, msg > "/dev/stderr"; abort = 1; exit abort; }