#! /usr/bin/gawk -f # Last edited on 2025-11-01 20:40:54 by stolfi BEGIN { skip = 1; chap = 0; chap_num = "???"; nread = 0; beg_line = -1 } // { nread++ gsub(/[ ]/, "", $0) gsub(/^[ ]*/, "", $0) gsub(/[ ]*$/, "", $0) } /^[0-9]+ *$/ { next } /CAPÍTULO *[CLXVI]*$/ { next; } /MACHADO DE ASSIS$/ { next; } /DON *CASMURRO$/ { next; } /^Capítulo[ ]+[0-9]+$/ { if (skip) { beg_line = nread printf "beg at line %d\n", beg_line > "/dev/stderr"; skip = 0; } # Keep processing..." } (skip) { next } /^Livros Grátis$/ { end_line = nread - 1 printf "end at line %d\n", end_line > "/dev/stderr" printf "total %d lines read\n", end_line - beg_line + 1 > "/dev/stderr" exit(0) } /^Cap.tulo[ ]+[0-9]+$/ { chap = 1; chap_num = $2; chap_title = "???" next; } // { gsub(/^[-] /, "--- ", $0) gsub(/título2 *$/, "título", $0) gsub(/[…]/, "...", $0) gsub(/[“]/, "«", $0) gsub(/[”]/, "»", $0) gsub(/D.ª /, "Da~", $0) } /^$/ { print ""; nwrite++; next } /^[#]/ { print; nwrite++; next } // { if (chap == 0) { print; nwrite++; } else if (chap == 1) { chap_title = $0; printf "# \\chapt{%s}{%s}\n", chap_num, chap_title; nwrite++; printf "Capítulo %s - %s\n", chap_num, chap_title > "/dev/stderr" chap = 2 } else if (chap == 2) { if ($0 != chap_num) { data_error(("missing title echo")) } chap = 0; chap_num = "???"; chap_title = "???" } next } END { if (skip) { data_error(("skipped all")); } } function data_error(msg) { printf "%s:%s: ** %s\n", FILENAME, FNR, msg > "/dev/stderr" printf " [[%s]]\n", $0 > "/dev/stderr" exit(1) }