blob: 6a540980aadb32507ac04a7426115675251eac12 [file] [log] [blame]
Rob Pikeae4123f2008-09-10 11:46:05 -07001#!/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 Pikecd7062e2009-10-13 17:17:30 -070013# missing second arg means process whole file
Rob Pikeae4123f2008-09-10 11:46:05 -070014#
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 Pike40d54352009-01-09 15:16:31 -080023# 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 Pikeae4123f2008-09-10 11:46:05 -070028echo "<pre> <!-- $* -->"
29
30case $# in
313)
32 if test "$3" = "END" # $2 to end of file
33 then
34 awk '
Rob Pike40d54352009-01-09 15:16:31 -080035 function LINE() { printf("%.2d\t%s\n", NR, $0) }
Rob Pikeae4123f2008-09-10 11:46:05 -070036 BEGIN { printing = 0 }
Rob Pike40d54352009-01-09 15:16:31 -080037 '$2' { printing = 1; LINE(); getline }
38 printing { if($0 ~ /./) { LINE() } else { print "" } }
Rob Pikeae4123f2008-09-10 11:46:05 -070039 '
40 else # $2 through $3
41 awk '
Rob Pike40d54352009-01-09 15:16:31 -080042 function LINE() { printf("%.2d\t%s\n", NR, $0) }
Rob Pikeae4123f2008-09-10 11:46:05 -070043 BEGIN { printing = 0 }
Rob Pike40d54352009-01-09 15:16:31 -080044 '$2' { printing = 1; LINE(); getline }
45 '$3' && printing { if(printing) {printing = 0; LINE(); exit} }
46 printing { if($0 ~ /./) { LINE() } else { print "" } }
Rob Pikeae4123f2008-09-10 11:46:05 -070047 '
48 fi
49 ;;
502) # one line
51 awk '
Rob Pike40d54352009-01-09 15:16:31 -080052 function LINE() { printf("%.2d\t%s\n", NR, $0) }
53 '$2' { LINE(); getline; exit }
Rob Pikeae4123f2008-09-10 11:46:05 -070054 '
55 ;;
561) # whole file
57 awk '
Rob Pike40d54352009-01-09 15:16:31 -080058 function LINE() { printf("%.2d\t%s\n", NR, $0) }
59 { if($0 ~ /./) { LINE() } else { print "" } }
Rob Pikeae4123f2008-09-10 11:46:05 -070060 '
61 ;;
62*)
63 echo >&2 usage: prog.sh file.go /func.main/ /^}/
64esac <$1 |
65sed '
66 s/&/\&amp;/g
67 s/"/\&quot;/g
68 s/</\&lt;/g
69 s/>/\&gt;/g
70'
71
72echo '</pre>'