diff --git a/stdlib/doc.lsp b/stdlib/doc.lsp new file mode 100644 index 0000000..6ee6b1a --- /dev/null +++ b/stdlib/doc.lsp @@ -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") diff --git a/utils/local_install.sh b/utils/local_install.sh index eab6061..a79d3af 100755 --- a/utils/local_install.sh +++ b/utils/local_install.sh @@ -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