| 
									
										
										
										
											2021-11-09 06:46:22 +03:00
										 |  |  | #!/usr/bin/env bash
 | 
					
						
							| 
									
										
										
										
											2021-11-21 17:05:41 +03:00
										 |  |  | #---------------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2021-11-09 06:46:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | CMD="$0 $@" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | SCRIPT_NAME=$(basename $0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 15:25:41 +03:00
										 |  |  | printusage(){ | 
					
						
							|  |  |  | 	echo "Usage:" | 
					
						
							|  |  |  | 	echo "  $SCRIPT_NAME [OPTIONS] [[INPUT] OUTPUT]" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | printhelp(){ | 
					
						
							|  |  |  | 	echo "Generate docs from latex package/class" | 
					
						
							|  |  |  | 	echo | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 	printusage | 
					
						
							|  |  |  | 	echo | 
					
						
							|  |  |  | 	echo "Options:" | 
					
						
							|  |  |  | 	echo "  -h | --help         Show this message and exit" | 
					
						
							|  |  |  | 	echo "  -p | --prefix PREFIX" | 
					
						
							|  |  |  | 	echo "                      Set the doc comment PREFIX (default: \"%\")" | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | 	echo "  -s | --strip        Strip docs out" | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | 	echo "  -n | --no-msg       Don't add the \"generated with\" message to output" | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | 	echo | 
					
						
							|  |  |  | 	echo "This will:" | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 	echo "  - read the INPUT" | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | 	echo "  - keep lines starting with \\def\\<module-name>@[A-Z]\\+" | 
					
						
							|  |  |  | 	echo "  - keep lines starting with '%%'" | 
					
						
							|  |  |  | 	echo "  - %%%%% Text -> \\subsection(Text)" | 
					
						
							|  |  |  | 	echo "  - %%%% Text -> \\section(Text)" | 
					
						
							|  |  |  | 	echo "  - %% >> code -> \\begin{verbatim}code\\end{verbatim}" | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 	echo "  - write the result to OUTPUT" | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | 	echo | 
					
						
							| 
									
										
										
										
											2021-11-23 01:16:37 +03:00
										 |  |  | 	echo "If no OUTPUT is given $SCRIPT_NAME will read stdout. If no INPUT" | 
					
						
							|  |  |  | 	echo "is given $SCRIPT_NAME will read stdin." | 
					
						
							| 
									
										
										
										
											2021-11-22 22:14:52 +03:00
										 |  |  | 	echo | 
					
						
							| 
									
										
										
										
											2021-11-18 16:49:24 +03:00
										 |  |  | 	echo "PREFIX can replace the second \"%\" in the above patterns to make it" | 
					
						
							|  |  |  | 	echo "possible to integrate multiple layers of documentation in one file" | 
					
						
							|  |  |  | 	echo "and to integrate them in various ways, for example, in the photobook" | 
					
						
							|  |  |  | 	echo "document class documentation \"M\" prefix is used to indicate" | 
					
						
							|  |  |  | 	echo "meta-command docs, this enables us to document them in the relevant" | 
					
						
							|  |  |  | 	echo "location (i.e. at the implementation) in source but move the docs to" | 
					
						
							|  |  |  | 	echo "a unified location in docs, effectively decoupling the source and doc" | 
					
						
							|  |  |  | 	echo "structure when needed." | 
					
						
							|  |  |  | 	echo | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | 	echo "Strip mode is the reverse of of the default, it will strip out docs" | 
					
						
							|  |  |  | 	echo "and empty lines." | 
					
						
							|  |  |  | 	echo | 
					
						
							|  |  |  | 	echo "NOTE: stripping will not remove non-doc comments." | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | 	echo "NOTE: the idea of keeping latex docs in a latex file is far simpler" | 
					
						
							|  |  |  | 	echo "      than all the stuff crammed into .dtx, at least for my needs:" | 
					
						
							|  |  |  | 	echo "          - keep the code readable" | 
					
						
							|  |  |  | 	echo "          - keep the docs readable" | 
					
						
							|  |  |  | 	echo "      in both the repo and in installed form, so .dtx is not used." | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 17:05:41 +03:00
										 |  |  | printerror(){ | 
					
						
							|  |  |  | 	echo Error: $@ | 
					
						
							|  |  |  | 	echo | 
					
						
							|  |  |  | 	printusage | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | printmsg(){ | 
					
						
							|  |  |  | 	# print message... | 
					
						
							|  |  |  | 	if [ -z $NO_MSG ] ; then  | 
					
						
							|  |  |  | 		echo "%----------------------------------------------------------------------" | 
					
						
							|  |  |  | 		echo "% $1 file generated by:"  | 
					
						
							|  |  |  | 		echo "%    $CMD" | 
					
						
							|  |  |  | 		echo "% NOTE: multiple messages indicate multiple runs." | 
					
						
							|  |  |  | 		echo "%----------------------------------------------------------------------" | 
					
						
							|  |  |  | 	fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 17:05:41 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #---------------------------------------------------------------------- | 
					
						
							|  |  |  | # Args and defaults... | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | PREFIX=% | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | while true ; do | 
					
						
							|  |  |  | 	case $1 in | 
					
						
							|  |  |  | 		-h|--help) | 
					
						
							|  |  |  | 			printhelp | 
					
						
							|  |  |  | 			exit | 
					
						
							|  |  |  | 			;; | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 		-p|--prefix) | 
					
						
							| 
									
										
										
										
											2021-11-20 00:26:26 +03:00
										 |  |  | 			PREFIX=$2 | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 			shift | 
					
						
							|  |  |  | 			shift | 
					
						
							|  |  |  | 			;; | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | 		-s|--strip) | 
					
						
							|  |  |  | 			STRIP_DOC=1 | 
					
						
							|  |  |  | 			shift | 
					
						
							|  |  |  | 			;; | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | 		-n|--no-msg) | 
					
						
							|  |  |  | 			NO_MSG=1 | 
					
						
							|  |  |  | 			shift | 
					
						
							|  |  |  | 			;; | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		# handle unknown options... | 
					
						
							|  |  |  | 		-*|--*) | 
					
						
							|  |  |  | 			printerror "unknown option \"$1\"" | 
					
						
							|  |  |  | 			exit | 
					
						
							|  |  |  | 			;; | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 16:56:10 +03:00
										 |  |  | 		# non-flag, option parsing done... | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | 		*) | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 			;; | 
					
						
							|  |  |  | 	esac | 
					
						
							|  |  |  | done | 
					
						
							| 
									
										
										
										
											2021-11-09 06:46:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 22:14:52 +03:00
										 |  |  | INPUT=${1:-/dev/stdin} | 
					
						
							| 
									
										
										
										
											2021-11-14 13:42:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 01:16:37 +03:00
										 |  |  | OUTPUT=${2:-/dev/stdout} | 
					
						
							| 
									
										
										
										
											2021-11-14 13:42:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 00:56:47 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:49:24 +03:00
										 |  |  | # generate the module name... | 
					
						
							|  |  |  | MODULE=$(basename "$INPUT") | 
					
						
							|  |  |  | MODULE=${MODULE/.*/} | 
					
						
							| 
									
										
										
										
											2021-11-14 13:42:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 17:05:41 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #---------------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2021-11-16 00:58:45 +03:00
										 |  |  | # do the work... | 
					
						
							| 
									
										
										
										
											2021-11-21 17:05:41 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | # make docs... | 
					
						
							|  |  |  | if [ -z $STRIP_DOC ] ; then | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | 	printmsg "Documentation" \
 | 
					
						
							|  |  |  | 		> "$OUTPUT" | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | 	cat "$INPUT" \
 | 
					
						
							|  |  |  | 		| egrep '(^%'$PREFIX'|^\\edef\\'$MODULE'@[A-Z][A-Z]+)' \
 | 
					
						
							|  |  |  | 		| sed 's/^\(\\edef\\\)'$MODULE'@/%'$PREFIX'\1/' \
 | 
					
						
							|  |  |  | 		| sed 's/%'$PREFIX'%%%% \(.*\)/%'$PREFIX'\\subsubsection{\1}\\label{subsubsec:\1}/' \
 | 
					
						
							|  |  |  | 		| sed 's/%'$PREFIX'%%% \(.*\)/%'$PREFIX'\\subsection{\1}\\label{subsec:\1}/' \
 | 
					
						
							|  |  |  | 		| sed 's/%'$PREFIX'%% \(.*\)/%'$PREFIX'\\section{\1}\\label{sec:\1}/' \
 | 
					
						
							|  |  |  | 		| sed 's/%'$PREFIX'\s\+>>\s\+\(.*\)/%'$PREFIX'\\begin{verbatim} \1 \\end{verbatim}/' \
 | 
					
						
							|  |  |  | 		| cut -c 3- - \
 | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | 		>> "$OUTPUT" | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | # make stripped code... | 
					
						
							|  |  |  | else | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | 	printmsg "Stripped code" \
 | 
					
						
							|  |  |  | 		> "$OUTPUT" | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | 	cat "$INPUT" \
 | 
					
						
							|  |  |  | 		| egrep -v '%'$PREFIX'' \
 | 
					
						
							|  |  |  | 		| egrep -v '^(\s*%)?\s*$' \
 | 
					
						
							| 
									
										
										
										
											2021-12-03 01:27:55 +03:00
										 |  |  | 		>> "$OUTPUT" | 
					
						
							| 
									
										
										
										
											2021-11-28 00:28:39 +03:00
										 |  |  | fi | 
					
						
							| 
									
										
										
										
											2021-11-14 13:42:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 16:56:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 17:05:41 +03:00
										 |  |  | #---------------------------------------------------------------------- | 
					
						
							|  |  |  | #                                           vim:set ts=4 sw=4 nowrap : |