~aleteoryx/tclircc

ref: bcf1accce7bc7023aeac374e70380e3dbea98733 tclircc/bin/docextract.tcl -rwxr-xr-x 1.6 KiB
bcf1acccAleteoryx docgen cleaning 22 days ago
                                                                                
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

# taken from
# https://github.com/jcowgar/misctcl/blob/master/docextract.tcl

package require cmdline

proc main {} {
	if {[llength $::argv] == 0} {
		puts "Invalid usage, please use -help for help."
		exit 0
	}

	set options {
		{o.arg       "./"  "set the output directory"}
		{ext.arg     "man" "set the extension of the extracted document files"}
		{doctools.arg no    "generate HTML output via doctools package"}
		{all                "output output even when no valid comments where found"}
	}

	set usage "\[options] filename1 \[filename2] \[...]\noptions:"
	if {[catch {array set params [::cmdline::getoptions ::argv $options $usage]} msg]} {
		puts $msg
		exit 0
	}

	if {$params(doctools)} {
		package require doctools
	}

	file mkdir $params(o)

	foreach fname $::argv {
		set fh [open $fname r]
		set comments ""

		set inDocComment 0

		while {[gets $fh line] >=0} {
			set line [string trim $line]
			if {$inDocComment && [string index $line 0] != "#"} {
				set inDocComment 0
			} elseif {[string range $line 0 1]=="#*"} {
				set inDocComment 1
			} elseif {$inDocComment} {
				append comments "[string range $line 2 end]\n"
			}
		}

		close $fh

		if {$params(all) || [string length $comments] > 0} {
			set ofh [open [file join $params(o) [file rootname $fname].$params(ext)] w]
			puts $ofh $comments
			close $ofh

			if {$params(doctools)} {
				::doctools::new .dt -format html
				set html [.dt format $comments]

				set ofh [open [file join $params(o) [file rootname $fname].html] w]
				puts $ofh $html
				close $ofh
			}
		}
	}
}

main