File: tt.15_format_lines

package info (click to toggle)
goawk 1.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,560 kB
  • sloc: awk: 3,060; yacc: 198; fortran: 189; python: 131; sh: 58; makefile: 12
file content (33 lines) | stat: -rw-r--r-- 797 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# fmt - format
#    input:  text
#    output: text formatted into lines of <= 72 characters

BEGIN {
        maxlen = 72
}

/^[ \t]/ { printline(); print; next }      # verbatim
###/^ +/ { printline();  }			# whitespace == break

/./  { for (i = 1; i <= NF; i++) addword($i); next }

/^$/ { printline(); print "" }
END  { printline() }

function addword(w) {
    ## print "adding [", w, "] ", length(w), length(line), maxlen
    if (length(line) + length(w) > maxlen)
        printline()
    if (length(w) > 2 && ( w ~ /[\.!]["?)]?$/ || w ~ /[?!]"?$/) &&
		w !~ /^(Mr|Dr|Ms|Mrs|vs|Ph.D)\.$/)
        w = w " "
    line = line " " w
}

function printline() {
    if (length(line) > 0) {
        sub(/ +$/, "", line)
        print substr(line, 2)   # removes leading blank
        line = ""
    }
}