1# Take apart bits of HTML and puts them back together again in new and 2# fascinating ways. Copyright (C) 2002 Free Software Foundation, Inc. 3# Contributed by Phil Edwards <pme@gcc.gnu.org>. Simple two-state automaton 4# inspired by Richard Henderson's gcc/mkmap-symver.awk. 5 6# 'file' is the name of the file on stdin 7# 'title' is the text to print at the start of the list 8 9BEGIN { 10 state = "looking"; 11 entries = 0; 12 printf (" <li>%s\n", title); 13 printf (" <ul>\n"); 14} 15 16# Searching for the little table of contents at the top. 17state == "looking" && /^<h1>Contents/ { 18 state = "entries"; 19 next; 20} 21 22# Ignore everything else up to that point. 23state == "looking" { 24 next; 25} 26 27# An entry in the table of contents. Pull that line apart. 28state == "entries" && /<li>/ { 29 extract_info($0); 30 next; 31} 32 33# End of the list. Don't bother reading the rest of the file. (It could 34# also contain more <li>'s, so that would be incorrect as well as wasteful.) 35state == "entries" && /^<\/ul>/ { 36 exit; 37} 38 39END { 40 for (i = 0; i < entries; i++) 41 printf (" %s\n", entry[i]); 42 printf (" </ul>\n </li>\n\n"); 43} 44 45function extract_info(line) { 46 # thistarget will be things like "#5" or "elsewhere.html" 47 match(line,"href=\".*\""); 48 thistarget = substr(line,RSTART+6,RLENGTH-7); 49 50 # take apart the filename 51 split(file,X,"/"); 52 if (thistarget ~ /^#/) { 53 # local name, use directory and filename 54 target = file thistarget 55 } else { 56 # different file, only use directory 57 target = X[1] "/" thistarget 58 } 59 60 # visible text 61 gsub("</a></li>","",line); 62 start = index(line,"\">") + 2; 63 thistext = substr(line,start); 64 65 # Assemble and store the HTML for later output. 66 entry[entries++] = "<li><a href=\"" target "\">" thistext "</a></li>" 67} 68 69# vim:sw=2 70