doc::man added

This commit is contained in:
vaclavt 2022-01-22 23:05:58 +01:00
parent bd8f0773c6
commit fdebf01a8a
2 changed files with 100 additions and 0 deletions

98
stdlib/doc.lsp Normal file
View File

@ -0,0 +1,98 @@
(include "/usr/local/var/mlisp/terminal.lsp")
(defun doc::strip-backticks (s)
(if (and (> (string-len s) 2)
(= (string-substr s 0 1) "`")
(= (string-substr s -1 1) "`"))
(string-substr s 1 (- (string-len s) 2))
s))
(defun doc::read-doc-file ()
(do (define parse-line (lambda (ln)
(if (string-regex? ln "^|.*|")
(do (define tokens (tail (string-split (string-trim ln) "\|"))) ; first element is ""
(if (= (len tokens) 4)
(insert tokens 0 (string-replace-re (first (string-split (first tokens) "\s+|\)")) "`|\(" ""))
nil)
)
nil)
))
(define lines (string-split (read-file "doc/Doc.md") "\n") )
(define lines (map (lambda (ln) (parse-line ln)) lines))
(define lines (filter (lambda (e) (!= e nil)) lines))
(set! doc::doc_entries lines)
))
(defun doc::print (entry)
(do (print (term-green (doc::strip-backticks (second entry))) "-" (third entry))
(print (last entry) "\n")
(if (> (string-len (fourth entry)) 2)
(do (define examp (doc::strip-backticks (fourth entry)))
(define pos (string-find examp "=>" 0))
(if (> pos 0)
(print (term-magenta
(+ (string-substr examp 0 (- pos 1))
"\n => "
(string-substr examp (+ pos 3) (string-len examp)))
)))
)
)
))
(defun doc::man (what)
(do (define man (filter (lambda (x) (= (first x) what)) doc::doc_entries))
(if man
(doc::print (first man))
(print (term-red (+ "No entry for " what)))
)
))
(defun doc::lookup (what)
(do (define what_list (string-split (string-downcase what) "\s+"))
(define scores '()) ; ((score entry)..)
(for entry doc::doc_entries
; ("throw-exception" "`(throw-exception exp_desc)`" "Throws an exception with exp_desc describing what happened " "" "Exceptions"
(define entry_score 0)
(for kw what_list
; name matches
(if (= (string-downcase (first entry)) kw)
(set! entry_score (+ entry_score 100)))
; name contains kw
(if (string-find (string-downcase (first entry)) kw 0)
(set! entry_score (+ entry_score 10)))
; desc contains kw
(if (string-find (string-downcase (third entry)) kw 0)
(set! entry_score (+ entry_score 1)))
)
(if (> entry_score 0)
(set! scores (push scores (list entry_score entry))))
)
(define sorted (quick-sort-by scores (lambda (a b) (< (first a) (first b)))))
(for e (take sorted 10)
(define entry (second e))
(define call (doc::strip-backticks (second entry)))
(define desc (doc::strip-backticks (third entry)))
(print (term-red (first entry)) "-" (term-green (doc::strip-backticks (second entry))) "-" (third entry))
)
(if (> (len sorted) 10) (print "..."))
))
(defun doc::appropos (which)
(doc::lookup which))
(defun doc::section (which)
(print (term-red "implement me!")))
(define doc::doc_entries '()) ; must be here
; read doc into memory
(doc::read-doc-file "/usr/local/var/mlisp/Doc.md")
;;example
; (doc::man "first")
; (doc::lookup "string pad")
; (doc::lookup "list flat")

View File

@ -9,6 +9,8 @@ echo ""
echo "copying lsp files"
mkdir -p /usr/local/var/mlisp/
cp stdlib/*.lsp /usr/local/var/mlisp/
echo "copying doc files"
cp Doc/*.md /usr/local/var/mlisp/
echo "copying ml file"
cp ./build/ml /usr/local/bin/ml