#!/usr/bin/env newlisp ;; @module newlispdoc ;; @description Generates documentation and hightligted source from newLISP source files. ;; @version 1.3 - handle remote files specified in URLs in a url-file ;; @version 1.4 - title on page changed to index or module name and new index module option ;; @version 1.5 - removed font restrictions on h1,h2,h3,h4 and added hr as a legal tag ;; @version 1.6 - don't allow 3 semicolons at the beginning of a comment line ;; @version 1.7 - 1.6 did not sense lines with semicolon only as parapgraph separators ;; @version 1.8 - fixed problem of "true" appearing before multiple syntax statements ;; @version 1.9 - write-line is now version sensitive for 10.0 ;; @version 2.0 - adds handling of < ?...> XML tag ;; @version 2.1 - generate more compliant HTML 4.01 for doc and syntax highlighting ;; but special arithmetik op characters in functionnames still cause ;; non-compliant HTML (this occurs only in modules/gmp.lsp) ;; @version 2.2 - fix issue with paragraph spacing and module text font from 2.1 ;; @version 2.3 - handle scientific notation with e ;; @version 2.4 - improved keyword regex for syntax highlighting ;; @version 2.5 - scientific notation with E ;; @version 2.6 - allow custom tags e.g: @MyTag the descriptive text (may contain @link) ;; @version 2.7 - link rag did not work at beginnning of line ;; @version 2.8 - support for newLISPdoc tag color ;; @version 2.9 - correctly highlight & and ^ keywords ;; @version 3.0 - when -d is specified, add download link ;; @version 3.1 - changed write-buffer to shorter write, name to term ;; @version 3.2 - formatting when view source, new stylesheet newlispdoc.css ;; @version 3.3 - allow descriving variables using the @syntax tag. They will be colored red as well. ;; @version 3.4 - make sure all @xxxxxx custom tags are title cased ;; @version 3.5 - added META tag for UTF-8 encoding for html output ;; @version 3.6 - allow trailing spaces on lines in index files ;; @version 3.7 - forgotten * in @example pattern ;; ;; @author Lutz Mueller, 2006-2012 ;; @location http://newlisp.org ;; ;;
;; newlispdoc afile.lsp bfile.lsp ;; newlispdoc -s afile.lsp bfile.lsp ;; newlispdoc -s *.lsp ;; newlispdoc -s -d *.lsp ;;;; newlispdoc can take a file of URLs and generate documentation and syntax ;; highlighted source from remote files: ;;
;; ; or on Win32 ;; newlisp newlispdoc afile.lsp bfile.lsp ;; newlisp newlispdoc -s afile.lsp bfile.lsp ;; newlisp newlispdoc -s *.lsp ;; newlisp newlispdoc -s -d *.lsp ;;
;; newlispdoc -url file-with-urls.txt 10000 ;; newlispdoc -s -url file-with-urls.txt 10000 ;;;; file-with-urls.txt contains one URL per line in the file. A URLstarts either ;; with http:// or file:// - This allows mixing remote and local files. An optional ;; timeout of 10 seconds is specified after the url file name. If no timeout is specified ;; newlispdoc assumes 5 seconds. ;; ;; Execute from within the same directory of the source files when ;; no url file is given. ;; ;; For each file a file with the same name and extension '.html' is generated ;; and written to the current directory. A file 'index.htm' is written as ;; an index for all other files generated. If the '-s' switch is specified, ;; a file with the extension '.src.html' is generated for each file. ;; ;; Please read @link http://newlisp.org/newLISPdoc.html newLISPdoc.html to learn ;; about tagging of newLISP source code for newlispdoc. ;; ; make compatible with older versions of newLISP (when (< (sys-info -2) 10111) (constant (global 'write) write-buffer) (constant (global 'term) name)) ; get list of files from command line (set 'files (2 (main-args))) (when (empty? files) (println "USAGE: newlispdoc [-s] [-d]
;; ; or on Win32 ;; newlisp newlispdoc -url file-with-urls.txt 10000 ;; newlisp newlispdoc -s -url file-with-urls.txt 10000 ;;
[/text]) ; ---------- routines for generating syntax-highlighted file[/text]) (set 'prolog3 {}) (set 'epilog [text] %s
- ∂ -
generated with newLISP and newLISPdoc
\n" src-file "\n" {
" text "[p]") (replace "
" text "[/p]") (replace "" text "[pre]") (replace "" text "[/pre]") (replace "
" text "[blockquote]") (replace "
" text "[hr]") (replace "
" text "[hr/]") ) (define (unprotect-html text) (replace "[h1]" text "") (replace "[h2]" text "
") (replace "[/h2]" text "") (replace "[/h3]" text "") (replace "[/h4]" text "") (replace "[i]" text "") (replace "[/i]" text "") (replace "[em]" text "") (replace "[/em]" text "") (replace "[b]" text "") (replace "[/b]" text "") (replace "[tt]" text "") (replace "[/tt]" text "") (replace "[p]" text "") (replace "[h3]" text "
") (replace "[h4]" text "
") (replace "[/h1]" text "
") (replace "[/p]" text "
") (replace "[br]" text "
") (replace "[br/]" text "
") (replace "[pre]" text "") (replace "[/pre]" text "") (replace "[center]" text "") (replace "[/center]" text " ") (replace "[li]" text "- ") (replace "[/li]" text "
") (replace "[ul]" text "") (replace "[/ul]" text "
") (replace "[blockquote]" text "") (replace "[/blockquote]" text "") (replace "[hr]" text "
") (replace "[hr/]" text "
") ) ; format the example tags (define (format-example text) (replace "<" text "<") (replace ">" text ">") (string "Example:\n") ) ; write the module tag link on the index page ; put source link on doc page if -s flag (define (format-module text desc filename , module) (set 'module (string "" (replace ";;" text "") "
\nModule: " text "
")) (write indexpage (string {} module "\n" )) (write indexpage (string "" desc "
\n")) (let (link "") (if source-link (setq link (string {source }))) (if download-link (write link (string {download}))) (string link module) ) ) ; write the function name links on the index page under the module (define (format-func-link func-name file-name) (let (names (if (find ":" func-name) (parse func-name ":") (list "" func-name))) (write indexpage (string {} (names 1) { })) (string (names 0) "_" (names 1)) ) ) ; format the syntax line (define (format-syntax text file-name, tag) (replace "<([^<]*?)>" text (string "" $1 "") 0) (replace {^ *\((.*?) (.*?\))} text (string "(" $1 " " $2) 0) (replace {^ *\(([^ ]*?)\)} text (string "(" $1 ")") 0) (replace {^ *([^ ()]*?)$} text (string "" $1 "") 0) (string (if (!= old-syntax $1) (begin (set 'old-syntax $1) (set 'tag (format-func-link $1 file-name)) (set 'tag (string {})) (string "§
\n" tag "" old-syntax "
\n")) "") "syntax: " (trim text) "
\n") ) (define (format-parameter param text) (string "parameter: " (format-text (trim param)) " - " (format-text text) "
\n") ) (define (format-return text) (string "return: " (format-text text) "
\n") ) (define (format-text text) (replace "<([^<]*?)>" text (string "" $1 "") 0) (replace "'([^\\s].*?[^\\s])'" text (string "" $1 "") 0) ) ;---------------------------------- End newlisdoc formatting subroutines ---------------- ; MAIN function (dolist (fle files) (println fle) (set 'html "") (set 'original (read-file fle time-out)) (if (not original) (begin (println "Could not read " fle) (exit 1))) (if (starts-with original "ERR:") (println "Could not read " fle " " original)) (set 'outfile (last (parse fle "\\\\|/" 0))) (set 'page (parse (replace "\r\n" original "\n") "\n")) (set 'page (filter (fn (ln) (or (starts-with ln ";;(?!;)" 0) (= (length (trim ln)) 0))) page)) (set 'page (join page "\n")) ; link to another index page (when (find ";; *@index ([^ ]*?) ([^ ]*?)\\s*\n" page 0) (write indexpage (string {Index: } $1 "
\n")) (when (find ";; *@description (.*?)\n" page 0) (write indexpage (string $1 "
\n"))) ) (if (find ";; *@module " page 0) (begin (replace {;;(.*) @link ([^ ]*?) ([^ ]*?)\s} page (string $1 { } $3 { }) 0) (replace ";; *@example *\n(.*?)\n\\s*\n" page (format-example $1) 4) (set 'page (protect-html page)) (set 'desc "") (replace ";; *@description (.*?)\n" page (begin (set 'desc $1) (string "" desc "
\n") ) 0) (replace ";; *@module (.*?)\n" page (format-module $1 desc outfile) 0) (set 'module-title $1) (replace ";; *@author (.*?)\n" page (string "Author: " $1 "
\n") 0) (replace ";; *@version (.*?)\n" page (string "Version: " $1 "
\n") 0) (replace ";; *@location (.*?)\n" page (string {Location: } $1 "
\n") 0) (replace ";; *@syntax (.*?)\n" page (format-syntax $1 outfile) 0) (replace ";; *@param (.*?) (.*?)\n" page (format-parameter $1 $2) 0) (replace ";; *@return (.*?)\n" page (format-return $1) 0) ;; custom tags, upper-case first letter (replace ";; *@([a-zA-Z_-]*?) (.*?)\n" page (string "" (title-case $1) ": " $2 "
\n") 0) (replace ";;\\s*\n" page "
\n" 0) (replace ";;(.*\n)" page (format-text $1) 0) (set 'page (unprotect-html page)) (write html (format prolog1 outfile)) (write html stylesheet) (write html (format prolog2 outfile)) (write html prolog3) (write html page) (write html epilog) (write-file (string outfile ".html") html) ; write syntax highlighted source (if source-link (write-syntax-highlight module-title original outfile)) ) ) ) ; write the modules index page (write indexpage epilog) (write-file "index.html" indexpage) (exit)