From b5529b261604d476e70120a0634f0826188ecdd8 Mon Sep 17 00:00:00 2001 From: Puck Meerburg Date: Mon, 23 Jun 2025 12:22:20 +0000 Subject: [PATCH] docs: autogenerate code part of table of contents Change-Id: I6a6a6964ec580d2403029e21ce785da000830c3d --- docs/.gitignore | 2 + docs/docread/default.nix | 1 + docs/docread/docread.egg | 2 +- docs/docread/docread.scm | 50 ++++++++++++++++++++++-- docs/modules/ROOT/nav.adoc | 35 +---------------- docs/modules/generated/pages/.gitkeep | 0 docs/modules/generated/partials/.gitkeep | 0 7 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 docs/modules/generated/pages/.gitkeep create mode 100644 docs/modules/generated/partials/.gitkeep diff --git a/docs/.gitignore b/docs/.gitignore index 4e71956..a221c57 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -2,3 +2,5 @@ /node_modules /modules/generated/pages/* !/modules/generated/pages/.gitkeep +/modules/generated/partials/* +!/modules/generated/partials/.gitkeep diff --git a/docs/docread/default.nix b/docs/docread/default.nix index f57f8b7..129eadc 100644 --- a/docs/docread/default.nix +++ b/docs/docread/default.nix @@ -5,6 +5,7 @@ eggDerivation { buildInputs = with chickenPackages.chickenEggs; [ r7rs + srfi-132 (pkgs.callPackage ../../core {}) ]; } diff --git a/docs/docread/docread.egg b/docs/docread/docread.egg index 8eb4b38..1cd5fdd 100644 --- a/docs/docread/docread.egg +++ b/docs/docread/docread.egg @@ -1,7 +1,7 @@ ((version "0.0.1") (synopsis "read doc comments") (author "puck") - (dependencies r7rs zilch) + (dependencies r7rs srfi-132 zilch) (component-options (csc-options "-X" "r7rs" "-R" "r7rs" "-optimize-level" "3")) (components diff --git a/docs/docread/docread.scm b/docs/docread/docread.scm index ed31120..e610749 100644 --- a/docs/docread/docread.scm +++ b/docs/docread/docread.scm @@ -1,4 +1,4 @@ -(import (scheme base) (scheme file) (scheme write) (chicken read-syntax) (chicken process-context) (chicken irregex) (chicken format) (chicken process) (chicken file) (zilch zexpr)) +(import (scheme base) (scheme file) (scheme write) (chicken read-syntax) (chicken process-context) (chicken irregex) (chicken format) (chicken process) (chicken file) (zilch zexpr) (srfi 132)) ;; Return a string version of the passed-in val, but properly quoted as s-expression. (define (quotify val) @@ -72,6 +72,35 @@ (call-with-port (open-output-string) (lambda (p) (render-lambda-doc p doc) (get-output-string p)))) +(define table-of-contents + '(((zilch lang rust) . ()) + ((zilch lang ninja) . ()) + ((zilch lang go) . ()) + ((zilch nix) . ()) + ((zilch lib) . ()) + ((zilch) . ()))) + +; Returns whether `left` is a prefix of `right`. +(define (is-prefix left right) + (define + res + (let loop ((left left) (right right)) + (cond + ((and (null? left) (null? right)) #t) + ((null? left) #t) + ((null? right) #f) + ((equal? (car left) (car right)) (loop (cdr left) (cdr right))) + (else #f)))) + res) + +(define (add-to-toc name path) + (let loop ((item table-of-contents)) + (cond + ((null? item) (error "Unknown TOC root" name)) + ((equal? name (caar item)) (set-cdr! (car item) (cons (sprintf "** xref:generated:~A[++~A++]\n" path name) (cdar item)))) + ((is-prefix (caar item) name) (set-cdr! (car item) (cons (sprintf "*** xref:generated:~A[++~A++]\n" path name) (cdar item)))) + (else (loop (cdr item)))))) + ;; Iterate over the contents of a define-library, and collect comments on certain defines. (define (parse-library-contents fname contents lib-comments) (define comments '()) @@ -141,10 +170,12 @@ (cdr j))))) contents) - (define out-path (string-append root "/docs/modules/generated/pages")) (define first #t) - (for-each (lambda (l) (set! out-path (string-append out-path (if first "/" ".") (symbol->string l))) (set! first #f)) (car contents)) - (set! out-path (string-append out-path ".adoc")) + (define file-path "") + (for-each (lambda (l) (set! file-path (string-append file-path (if first "" ".") (symbol->string l))) (set! first #f)) (car contents)) + (set! file-path (string-append file-path ".adoc")) + (define out-path (string-append root "/docs/modules/generated/pages/" file-path)) + (add-to-toc (car contents) file-path) (define out-file (open-output-file out-path)) ; Print out the comments @@ -178,3 +209,14 @@ (find-files root #:test ".*\\.(sld|scm)" #:action process-file) + +(call-with-output-file (string-append root "/docs/modules/generated/partials/nav.adoc") + (lambda (p) + (for-each + (lambda (chunk) + (define sorted (list-sort string