html-doc.lisp 20.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
;;;; -*- mode:lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE:               html-doc.lisp
;;;;LANGUAGE:           Common-Lisp
;;;;SYSTEM:             Common-Lisp
;;;;USER-INTERFACE:     NONE
;;;;DESCRIPTION
;;;;    
;;;;    Generate HTML L.S.E. documentation from the in-line documentation.
;;;;    
;;;;AUTHORS
;;;;    <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;MODIFICATIONS
;;;;    2012-03-14 <PJB> Created
;;;;BUGS
;;;;LEGAL
;;;;    AGPL3
;;;;    
19
;;;;    Copyright Pascal J. Bourguignon 2012 - 2014
20 21 22 23 24 25 26 27 28 29 30 31
;;;;    
;;;;    This program is free software: you can redistribute it and/or modify
;;;;    it under the terms of the GNU Affero General Public License as published by
;;;;    the Free Software Foundation, either version 3 of the License, or
;;;;    (at your option) any later version.
;;;;    
;;;;    This program is distributed in the hope that it will be useful,
;;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;;    GNU Affero General Public License for more details.
;;;;    
;;;;    You should have received a copy of the GNU Affero General Public License
32
;;;;    along with this program.  If not, see http://www.gnu.org/licenses/
33
;;;;**************************************************************************
34 35
(eval-when (:compile-toplevel :load-toplevel :execute)
  (setf *readtable* (copy-readtable nil)))
36 37 38 39 40 41 42 43 44 45 46 47
(in-package "COM.INFORMATIMAGO.LSE")

(defvar *html-doc-base-filename* "lse-"
  "Prefix to html file names (but the index.html).")


(defun html-doc-category-file-name (category)
  (format niL "~(~A~A.html~)"
          *html-doc-base-filename*
          (remove-accents category)))


48
(defvar *documentation-directory* #P"/tmp/doc/")
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132




;;; Generate the grammar rules.

(defun noregexp (str)
  (flet ((noreg (str)
           (if (and (< 1 (length str))
                     (find-if (lambda (ch) (find ch "[]?+*$."))
                              str))
               nil
               str)))
   (if (prefixp "\\" str)
       (noreg (subseq str 1))
       (noreg str))))

(defun optpar (rhs formated-rhs)
  (if (and (listp rhs)
           (eql 'alt (first rhs))
           (rest (rest rhs)))
      (format nil "(~A)" formated-rhs)
      formated-rhs))


(defun format-rule (rule terminals &optional (ntwidth 20))
  (labels ((format-rhs (rhs)
             (if (atom rhs)
                 (cond
                   ((stringp rhs)
                    (format nil "'~A'"
                                          (if (find #\' rhs)
                                              (with-output-to-string (out)
                                                (loop :for ch :across rhs
                                                  :do (if (char= #\' ch)
                                                          (princ "''" out)
                                                          (princ ch out))))
                                              rhs)))
                   ((let ((entry (assoc rhs terminals)))
                      (when entry
                        (let ((str (noregexp (second entry))))
                          (if str
                              (format nil "'~A'" str)
                              (subseq (symbol-name rhs) 4))))))
                   (t
                    (format nil "~A" rhs)))
                 (ecase (first rhs)
                   ((seq)
                    (format nil "~{~A~^ ~}"
                            (mapcar (lambda (item) (optpar item (format-rhs item)))
                                    (rest rhs))))
                   ((rep)
                    (format nil "{~{~A~^ ~}}"
                            (mapcar (lambda (item) (optpar item (format-rhs item)))
                                    (rest rhs))))
                   ((opt)
                    (format nil "[~{~A~^ ~}]"
                                (mapcar (lambda (item) (optpar item (format-rhs item)))
                                        (rest rhs))))
                   ((alt)
                    (format nil "~{~A~^ | ~}"
                            (mapcar (function format-rhs)
                                    (rest rhs))))))))
    (format nil "~VA ::= ~A." ntwidth (first rule) (format-rhs (second rule)))))


(defun generate-lse-grammar ()
  (with-output-to-string (*standard-output*)
    (let* ((lse (grammar-named 'lse))
           (terminals (grammar-terminals lse)))
      (format t "~%Symboles terminaux : ~2%")
      (dolist (terminal (remove-if (function noregexp) terminals :key (function second)))
        (format t "    ~:@(~VA~) = /~A/.~2%"
                20 (subseq (string (first terminal)) 4) (second terminal)))
      (format t "~%Règles de grammaire : ~2%")
      (dolist (rule (grammar-rules (grammar-named 'lse)))
        (princ "    ")
        (write-line (format-rule (clean-up-rule rule) terminals))
        (terpri)))))



;;; Categories:

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
(defun categories ()
  "Returns a list of documentation categories.
NOTE: Unclassified chapters are in the category NIL."
  (let ((categories '()))
    (maphash (lambda (title chap-list)
               (dolist (chapter chap-list)
                 (pushnew (chapter-category chapter) categories
                          :test (function equalp))))
             *chapters*)
    categories))


(defun chapters-per-category ()
 "Returns an a-list mapping categories to a sorted list of chapter titles."
 (mapcar (lambda (category)
           (cons category
                 (sort (map 'list 'chapter-title (find-category category))
                       (function string-lessp))))
         (categories)))


;; (let ((results '()))
;;  (maphash (lambda (title chapters)
;;             (dolist (chapter chapters)
;;               (push
;;                (if (functionp (chapter-text chapter))
;;                    `(:functional ,(chapter-title chapter) ,(chapter-category chapter))
;;                    `(:textual ,(chapter-title chapter) ,(chapter-category chapter)))
;;                results)))
;;           *chapters*)
;;  (sort results (function string<) :key (function first)))




(defparameter *html-key-labels*
  '(("ESC"         . "[INTERRUPTION]")
    ("ÉCHAPEMENT"  . "[INTERRUPTION]")
    ("CTRL-A"      . "[ATTENTION]")
    ("XOFF"        . "[X-OFF]")
    ("DEL"         . "[EFFACEMENT]")
    ("RETURN"      . "[ENTRÉE]")))

(defun substitutions (word)
  (let ((under (position #\_ word))
        (caret (position #\^ word)))
    (if (or under caret)
        (with-output-to-string (out)
          (loop
            :with len = (length word)
            :with i = 0
            :while (< i len)
185
            ;; We escape only ^ and _ with \.  Other occurences of \ are literal.
186
            :do (let ((ch (aref word i)))
187 188 189 190 191 192 193 194 195 196 197
                  (case ch
                    ((#\\) (if (< (1+ i) len)
                               (let ((nextch (aref word (1+ i))))
                                 (case nextch
                                   ((#\^ #\_) (princ nextch out) (incf i))
                                   (otherwise (princ ch out))))
                               (princ ch out)))
                    ((#\^)     (princ *unicode-upwards-arrow*   out))
                    ((#\_)     (princ *unicode-leftwards-arrow* out))
                    (otherwise (princ ch out)))
                  (incf i))))
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        word)))


(defun generate-html-word (word)
  (cond
    ((or (prefixp "http://" word)
         (prefixp "https://" word)
         (prefixp "mailto:" word)
         (prefixp "news:" word)
         (prefixp "irc://" word))
     (html:a (:href word) (html:pcdata "~A" word)))
    ((prefixp "[" word)
     (let ((rcro (position #\] word)))
       (if rcro
           (let* ((key (subseq word 1 rcro))
                  (aft (subseq word (1+ rcro)))
                  (entry (assoc key *html-key-labels*
                                :test (function string=))))
             (if entry
                 (html:kbd - (html:pcdata "~A" (cdr entry)))
                 (html:kbd - (html:pcdata "[~A]" key)))
             (html:pcdata "~A" aft))
           (html:pcdata "~A" (substitutions word)))))
    (t
     (html:pcdata "~A" (substitutions word))))
  (html:pcdata " "))



(defun generate-html-text (text)
  (loop
    :with newline = (string #\Newline)
    :with paras = (split-text text)
    :while paras
    :do (let ((para (pop paras)))
          (cond
            ((null para))
            ((null (first para)))
            ((zerop (length (first para))))

            ;; Lists.
            ((char= #\- (aref (first para) 0))
             ;; (push (cons (subseq (first para) 1) (rest para)) paras)
             (push para paras)
             (html:ul -
               (loop
                 :for para = (first paras)
                 :while (and (first para)
                             (plusp (length (first para)))
                             (char= #\- (aref (first para) 0)))
                 :do (pop paras)
                 :do (html:li - (dolist (word (rest para))
                                  (generate-html-word word))))))

            ;; Example programs.
            ((or (digit-char-p (aref (first para) 0))
                 (char= #\?    (aref (first para) 0))
                 (char= #\|    (aref (first para) 0)))
             (push para paras)
             (html:pre -
               (loop
                 :for para = (first paras)
                 :while (and (first para) (or (digit-char-p (aref (first para) 0))
                                              (char= #\?    (aref (first para) 0))
                                              (char= #\|    (aref (first para) 0))))
                 :do (pop paras)
                 :do (dolist (word para (html:pcdata newline))
                       (if (char= #\| (aref word 0))
                           (generate-html-word (subseq word 1))
                           (generate-html-word word))))))
            
            ;; References.
            ((prefixp "Voir" (first para))
             (let ((sep nil))
              (dolist (item (split-sequence #\, (format nil "~{~A~^ ~}" para)))
                (let* ((dot (char= #\. (aref item (1- (length item)))))
                       (item (if dot
                                 (subseq item 0 (1- (length item)))
                                 item))
                       (pos (position-if 'lower-case-p item :from-end t))
                       (pos (if pos
                                (position-if 'upper-case-p item :start pos)
                                (position-if 'upper-case-p item)))
                       (pre (if pos (subseq item 0 pos) item))
                       (ref (if pos (subseq item pos) nil)))
                  (when sep (html:pcdata sep))
                  (setf sep ", ")
                  (if (and ref (every (lambda (ch) (or (find ch " '") (upper-case-p ch))) ref))
                      (progn
                        (when (plusp (length pre)) (html:pcdata "~A" pre))
                        (if (= 2 (length ref))
                            (let ((command (find-chapter ref "COMMANDES")))
                              (if command
291 292 293 294 295
                                  (html:code -
                                   (html:a (:href (format nil "~A#~A"
                                                          (html-doc-category-file-name "COMMANDES")
                                                          (chapter-title command)))
                                     (html:pcdata "~A" (chapter-title command))))
296
                               (html:pcdata "~A" ref)))
297 298 299
                            (html:code -
                             (html:a (:href (format nil "#~A" ref))
                               (html:pcdata "~A" ref)))))
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                      (html:pcdata "~A" item))
                  (when dot (html:pcdata "."))))))

            ;; Normal paragraphs.
            (t
             (html:p -
               (dolist (word para)
                 (generate-html-word word))))))))


(defmethod generate-chapter ((chapter chapter) (category t))
  (html:a (:name (chapter-title chapter)))
  (html:h3 - (html:pcdata "~A" (chapter-title chapter)))
  (when (and (chapter-oneliner chapter)
             (string/= (chapter-oneliner chapter)
                       (chapter-text chapter)))
   (html:p - (html:pcdata "~A" (chapter-oneliner chapter))))
  (generate-html-text (chapter-text chapter)))


(defmethod generate-chapter ((chapter chapter) (category (eql :fonctions)))
  (html:a (:name (chapter-title chapter)))
  (html:h3 - (html:pcdata "~A~@[ - ~A~]"
                          (chapter-title chapter)
                          (substitutions (chapter-oneliner chapter))))
  (let* ((syntax (first-line (chapter-text chapter)))
         (text   (subseq  (chapter-text chapter) (length syntax))))
    (html:code - (html:pcdata (substitutions syntax)))
    (generate-html-text text)))


(defmethod generate-chapter ((chapter chapter) (category (eql :commandes)))
  (let* ((text (string-trim #(#\Space #\Newline) (chapter-text chapter)))
         (synopsis (first-line text))
         (text (string-left-trim #(#\Space #\Newline) (subseq text (length synopsis)))))
335 336
    (html:a (:name (chapter-title chapter)))
    ;; (html:h3 - (html:pcdata "~A" (chapter-title chapter)))
337 338 339
    (html:code -
      (html:b - (html:pcdata "~A" (subseq synopsis 0 2)))
      (html:pcdata "~A" (subseq synopsis 2)))
340 341
    (html:blockquote -
      (generate-html-text text))))
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356


(defmethod generate-chapter ((chapter chapter) (category (eql :instructions)))
  (html:a (:name (chapter-title chapter)))
  (html:h3 - (html:pcdata "~A" (chapter-title chapter)))
  (let* ((syntax (first-line (chapter-text chapter)))
         (text   (subseq (chapter-text chapter) (length syntax))))
    (html:code - (generate-html-text syntax))
    (generate-html-text text)))


(defun generate-category (category prev up next)
  "Generate a category documentation html file."
  (let ((cat (intern category "KEYWORD")))
    (with-open-file (html:*html-output-stream*
357
                     (merge-pathnames (html-doc-category-file-name category) *documentation-directory* nil)
358 359 360 361
                     :direction :output
                     :if-exists :supersede
                     :if-does-not-exist :create
                     :external-format *external-format/utf-8*)
362 363
      (format *trace-output* "~&Generating ~A~%" (pathname html:*html-output-stream*))
      (force-output *trace-output*)
364 365 366 367 368 369
      (html:with-html-output (html:*html-output-stream*
                              :kind :html
                              :encoding :utf-8)
        (html:doctype :strict
          (html:html -
            (html:head -
370
              (html:title - (html:pcdata "NASIUM L.S.E. - ~:(~A~)" category))
371 372 373 374
              (html:link (:rel "shortcut icon" :href "http://www.ogamita.com/favicon.ico"))
              (html:link (:rel "icon"          :href "http://www.ogamita.com/favicon.ico" :type "image/vnd.microsoft.icon"))
              (html:link (:rel "icon"          :href "http://www.ogamita.com/favicon.png" :type "image/png"))
              (html:link (:rel "stylesheet"    :href "http://www.ogamita.com/default.css" :type "text/css"))
375 376 377 378
              (html:meta (:http-equiv "Content-Type"   :content "text/html; charset=utf-8"))
              (html:meta (:name "author"               :content "Pascal J. Bourguignon"))
              (html:meta (:name "Reply-To"             :content "pjb@informatimago.com"))
              (html:meta (:name "Keywords"             :content
379
                                (format nil "NASIUM, LSE, L.S.E, Langage Symbolique d'Enseignement, Langage de programmation, Français, ~(~A~)" category))))
380
            (html:body -
381 382 383
              (html:img (:src "../nasium-lse-2.png" :alt "NASIUM L.S.E."))
              ;; (html:h1 - (html:pcdata "NASIUM L.S.E."))
              (html:hr)
384
              (html:div (:class "navigation")
385 386
                (html:a (:href "../")   (html:pcdata "Maison"))
                (html:pcdata " | ")
387 388
                (when prev
                  (html:a (:href prev) (html:pcdata "Page précédente"))
389
                  (html:pcdata " | "))
390 391
                (html:a (:href up)   (html:pcdata "Index"))
                (when next
392
                  (html:pcdata " | ")
393 394 395 396 397 398 399
                  (html:a (:href next) (html:pcdata "Page suivante"))))
              (html:hr)
              (html:h2 - (html:pcdata "~:(~A~)" category))
              ;; introduction:
              (let ((intro (find-chapter "INTRODUCTION" category)))
                (when intro
                  (generate-html-text (chapter-text intro))))
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
              (let ((chapters (cdr (assoc category (chapters-per-category)
                                          :test (function string-equal)))))
                (when chapters
                  (when (rest chapters)
                    ;; table of contents:
                    (html:div (:class "toc")
                      (html:p - (html:pcdata "Table des ~(~A~) :" category))
                      (html:ul -
                        (dolist (chapter chapters)
                          (unless (string-equal "INTRODUCTION" chapter)
                            (html:li -
                              (html:a (:href (format nil "#~A" chapter))
                                (html:pcdata chapter))))))))
                  ;; sections:
                  (dolist (chapter chapters)
415
                    (unless (string-equal "INTRODUCTION" chapter)
416 417 418 419 420
                      (generate-chapter (find-chapter chapter category) cat))))
                (when (string= "GRAMMAIRE" category)
                  (html:pre (:class "grammar")
                    (html:pcdata (generate-lse-grammar))))))))))))

421 422


423 424
;;; Generate the documentation and index:

425 426
(defparameter *indexed-categories*
  '("GÉNÉRALITÉS" #+lse-unix "OPTIONS"
427
    "COMMANDES" "INSTRUCTIONS" "FONCTIONS" "GRAMMAIRE" "LÉGAL"))
428 429 430 431

(defun generate-index ()
  "Generate an index file for the catgory html files."
  (with-open-file (html:*html-output-stream*
432
                   (merge-pathnames "index.html" *documentation-directory* nil)
433 434 435 436
                   :direction :output
                   :if-exists :supersede
                   :if-does-not-exist :create
                   :external-format *external-format/utf-8*)
437 438
    (format *trace-output* "~&Generating ~A~%" (pathname html:*html-output-stream*))
    (force-output *trace-output*)
439 440 441 442 443 444
    (html:with-html-output (html:*html-output-stream*
                            :kind :html
                            :encoding :utf-8)
      (html:doctype :strict
        (html:html -
          (html:head -
445
            (html:title - (html:pcdata "NASIUM L.S.E. - Langage Symbolique d'Enseignement"))
446 447 448 449 450
            (html:link (:rel "shortcut icon" :href "http://www.ogamita.com/favicon.ico"))
            (html:link (:rel "icon"          :href "http://www.ogamita.com/favicon.ico" :type "image/vnd.microsoft.icon"))
            (html:link (:rel "icon"          :href "http://www.ogamita.com/favicon.png" :type "image/png"))
            (html:link (:rel "stylesheet"    :href "http://www.ogamita.com/default.css" :type "text/css"))
            
451 452 453 454
            (html:meta (:http-equiv "Content-Type"   :content "text/html; charset=utf-8"))
            (html:meta (:name "author"               :content "Pascal J. Bourguignon"))
            (html:meta (:name "Reply-To"             :content "pjb@informatimago.com"))
            (html:meta (:name "Keywords"             :content "NASIUM, LSE, L.S.E, Langage Symbolique d'Enseignement, Langage de programmation, Français")))
455
          (html:body -
456 457 458 459 460 461
            (html:img (:src "../nasium-lse-2.png" :alt "NASIUM L.S.E."))
            ;; (html:h1 - (html:pcdata "NASIUM L.S.E."))
            (html:hr)
            (html:div (:class "navigation")
              (html:a (:href "../")   (html:pcdata "Maison")))
            (html:hr)
462
            (html:pre -
463
              (html:pcdata *title-banner* (version) *copyright*))
464 465 466 467 468 469 470 471
            (html:ol -
              (let ((chapters (chapters-per-category)))
                (dolist (category *indexed-categories*)
                  (html:li ()
                    (html:a (:href (html-doc-category-file-name category))
                      (html:pcdata "~A" category))))))))))))


472 473 474 475 476 477
(defun generate-html-documentation (*documentation-directory*)
  (let ((*documentation-directory* (etypecase *documentation-directory*
                                       (pathname *documentation-directory*)
                                       (stream   (pathname *documentation-directory*))
                                       (string   (pathname *documentation-directory*)))))
    (ensure-directories-exist (merge-pathnames "file.test" *documentation-directory* nil))
478 479 480 481 482 483 484 485 486 487
    (generate-index)
    (loop
      :for (prev category next) :on (append '(nil) *indexed-categories* '(nil))
      :while category
      :do (generate-category category
                             (when prev (html-doc-category-file-name prev))
                             "index.html"
                             (when next (html-doc-category-file-name next))))))


488 489

;;;; THE END ;;;;