Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright 2009 The Go Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style |
| 4 | # license that can be found in the LICENSE file. |
| 5 | |
| 6 | # generate HTML for a program excerpt. |
| 7 | # first arg is file name |
| 8 | # second arg is awk pattern to match start line |
| 9 | # third arg is awk pattern to stop processing |
| 10 | # |
| 11 | # missing third arg means print one line |
| 12 | # third arg "END" means proces rest of file |
Rob Pike | cd7062e | 2009-10-13 17:17:30 -0700 | [diff] [blame] | 13 | # missing second arg means process whole file |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 14 | # |
| 15 | # examples: |
| 16 | # |
| 17 | # prog.sh foo.go # whole file |
| 18 | # prog.sh foo.go "/^func.main/" # signature of main |
| 19 | # prog.sh foo.go "/^func.main/" "/^}/ # body of main |
| 20 | # |
| 21 | # non-blank lines are annotated with line number in file |
| 22 | |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 23 | # line numbers are printed %.2d to make them equal-width for nice formatting. |
| 24 | # the format gives a leading 0. the format %2d gives a leading space but |
| 25 | # that appears to confuse sanjay's makehtml formatter into bungling quotes |
| 26 | # because it makes some lines look indented. |
| 27 | |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 28 | echo "<pre> <!-- $* -->" |
| 29 | |
| 30 | case $# in |
| 31 | 3) |
| 32 | if test "$3" = "END" # $2 to end of file |
| 33 | then |
| 34 | awk ' |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 35 | function LINE() { printf("%.2d\t%s\n", NR, $0) } |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 36 | BEGIN { printing = 0 } |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 37 | '$2' { printing = 1; LINE(); getline } |
| 38 | printing { if($0 ~ /./) { LINE() } else { print "" } } |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 39 | ' |
| 40 | else # $2 through $3 |
| 41 | awk ' |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 42 | function LINE() { printf("%.2d\t%s\n", NR, $0) } |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 43 | BEGIN { printing = 0 } |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 44 | '$2' { printing = 1; LINE(); getline } |
| 45 | '$3' && printing { if(printing) {printing = 0; LINE(); exit} } |
| 46 | printing { if($0 ~ /./) { LINE() } else { print "" } } |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 47 | ' |
| 48 | fi |
| 49 | ;; |
| 50 | 2) # one line |
| 51 | awk ' |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 52 | function LINE() { printf("%.2d\t%s\n", NR, $0) } |
| 53 | '$2' { LINE(); getline; exit } |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 54 | ' |
| 55 | ;; |
| 56 | 1) # whole file |
| 57 | awk ' |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 58 | function LINE() { printf("%.2d\t%s\n", NR, $0) } |
| 59 | { if($0 ~ /./) { LINE() } else { print "" } } |
Rob Pike | ae4123f | 2008-09-10 11:46:05 -0700 | [diff] [blame] | 60 | ' |
| 61 | ;; |
| 62 | *) |
| 63 | echo >&2 usage: prog.sh file.go /func.main/ /^}/ |
| 64 | esac <$1 | |
| 65 | sed ' |
| 66 | s/&/\&/g |
| 67 | s/"/\"/g |
| 68 | s/</\</g |
| 69 | s/>/\>/g |
| 70 | ' |
| 71 | |
| 72 | echo '</pre>' |