From 2c216ae8939d3ea85498cb227bfce1cf326f3a7f Mon Sep 17 00:00:00 2001 From: Sorawee Porncharoenwase Date: Tue, 13 Nov 2018 02:29:55 -0800 Subject: [PATCH] Sync changes from Frog's pygments Upstream code: https://github.com/greghendershott/frog/blob/master/frog/private/enhance-body/syntax-highlight/pygments.rkt In particular, this commit allows users to supply a Python executable which is needed if, for example, Pygments is installed for Python 3, but `python` is an alias for Python 2. --- pollen/unstable/pygments.rkt | 75 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/pollen/unstable/pygments.rkt b/pollen/unstable/pygments.rkt index 9204767..fec8927 100644 --- a/pollen/unstable/pygments.rkt +++ b/pollen/unstable/pygments.rkt @@ -6,9 +6,9 @@ racket/runtime-path racket/system racket/string + racket/format rackjure/threading - rackjure/str - xml + xml (only-in html read-html-as-xml) "../private/log.rkt" "../private/splice.rkt") @@ -68,17 +68,6 @@ if zero is False: ;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;; -;; from frog/params - -(define current-pygments-linenos? (make-parameter #t)) -(define current-pygments-cssclass (make-parameter "source")) - -;; end frog/params -;;;;;;;;;;;;;;;;;; - - - ;;;;;;;;;;;;;;;;;; ;; from frog/pygments @@ -90,26 +79,27 @@ if zero is False: (define start (let ([start-attempted? #f]) - (λ () + (λ (python-executable line-numbers? css-class) (unless start-attempted? (set! start-attempted? #t) - (prn0 "Launching python pipe.py") - (match (process (str "python -u " pipe.py - (if (current-pygments-linenos?) " --linenos" "") - " --cssclass " (current-pygments-cssclass))) - [(list in out pid err proc) - (set!-values (pyg-in pyg-out pyg-pid pyg-err pyg-proc) - (values in out pid err proc)) - (file-stream-buffer-mode out 'line) - (match (read-line pyg-in 'any) ;; consume "ready" line or EOF - [(? eof-object?) (say-no-pygments)] - [_ (say-pygments)])] - [_ (say-no-pygments)]))))) - -(define (say-pygments) - (prn1 "Using Pygments.")) -(define (say-no-pygments) - (prn1 "Pygments not found. Using plain `pre` blocks.")) + (define pre " Using plain `pre` blocks.") + (match (find-executable-path python-executable) + [(? path? python) + (prn1 (~a "Launching `" python " " pipe.py "` to use Pygments.")) + (match (process (~a python + " -u " pipe.py + (if line-numbers? " --linenos" "") + " --cssclass " css-class)) + [(list in out pid err proc) + (set!-values (pyg-in pyg-out pyg-pid pyg-err pyg-proc) + (values in out pid err proc)) + (file-stream-buffer-mode out 'line) + (match (read-line pyg-in 'any) ;; consume "ready" line or EOF + [(? eof-object?) (prn0 (~a "Pygments pipe.py not responding." pre))] + [_ (void)])] + [_ (prn0 (~a "`" python " " pipe.py "` failed." pre))])] + [#f + (prn0 (~a "Pygments executable `" python-executable "` not found." pre))]))))) (define (running?) (and pyg-proc @@ -130,11 +120,15 @@ if zero is False: (stop) (old-exit-handler v)))) -(define (pygmentize code lang [hl-lines null]) ;; string? string? (listof number?) -> (listof xexpr?) +(define (pygmentize code lang + #:python-executable python-executable + #:line-numbers? line-numbers? + #:css-class css-class + #:hl-lines hl-lines) (define (default code) `((pre () (code () ,code)))) (unless (running?) - (start)) + (start python-executable line-numbers? css-class)) (cond [(running?) ;; order of writing arguments is significant: cooperates with pipe.py (displayln lang pyg-out) @@ -144,7 +138,7 @@ if zero is False: (let loop ([s ""]) (match (read-line pyg-in 'any) ["__END__" (with-input-from-string s read-html-as-xexprs)] - [(? string? v) (loop (str s v "\n"))] + [(? string? v) (loop (~a s v "\n"))] [_ (copy-port pyg-err (current-output-port)) ;echo error msg (default code)]))] [else (default code)])) @@ -153,11 +147,18 @@ if zero is False: ;;;;;;;;;;;;;;;;;; -(define (highlight #:lines [hl-lines null] +(define (highlight #:python-executable [python-executable "python"] + #:line-numbers? [line-numbers? #t] + #:css-class [css-class "source"] + #:lines [hl-lines null] lang . codelines) (define code (string-append* codelines)) `(div ((class "highlight")) - ,@(strip-empty-attrs (pygmentize code lang hl-lines)))) + ,@(strip-empty-attrs (pygmentize code lang + #:python-executable python-executable + #:line-numbers? line-numbers? + #:css-class css-class + #:hl-lines hl-lines)))) ;; Other CSS options available from http://richleland.github.io/pygments-css/ @@ -223,4 +224,4 @@ if zero is False: .highlight .vc { color: #19177C } /* Name.Variable.Class */ .highlight .vg { color: #19177C } /* Name.Variable.Global */ .highlight .vi { color: #19177C } /* Name.Variable.Instance */ -.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */")) \ No newline at end of file +.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */")) -- 2.25.1