Fallback on TextLexer of no other options is found
This commit is contained in:
parent
6cf6009b19
commit
8b6efc4add
1 changed files with 12 additions and 4 deletions
16
hypo.hy
16
hypo.hy
|
@ -17,8 +17,9 @@
|
||||||
|
|
||||||
(import web sys os hashlib datetime
|
(import web sys os hashlib datetime
|
||||||
[pygments [highlight]]
|
[pygments [highlight]]
|
||||||
[pygments.lexers [guess-lexer-for-filename]]
|
[pygments.lexers [get-lexer-by-name guess-lexer-for-filename]]
|
||||||
[pygments.formatters [HtmlFormatter]])
|
[pygments.formatters [HtmlFormatter]]
|
||||||
|
[pygments.util [ClassNotFound]])
|
||||||
|
|
||||||
(try (import [config [*]])
|
(try (import [config [*]])
|
||||||
(catch [ImportError]
|
(catch [ImportError]
|
||||||
|
@ -62,6 +63,14 @@
|
||||||
{"where" "shash = $shash"})))
|
{"where" "shash = $shash"})))
|
||||||
(if res (car res))))
|
(if res (car res))))
|
||||||
|
|
||||||
|
(defun get-lexer [filename content]
|
||||||
|
"Try to guess the correct lexer by FILENAME and CONTENT.
|
||||||
|
|
||||||
|
If no lexer is found fallback onto the text lexer."
|
||||||
|
(try (guess-lexer-for-filename filename content)
|
||||||
|
(catch [ClassNotFound]
|
||||||
|
(get-lexer-by-name "text"))))
|
||||||
|
|
||||||
(defclass raw []
|
(defclass raw []
|
||||||
[[GET (lambda [self name]
|
[[GET (lambda [self name]
|
||||||
(let ((filename (+ "files/" name))
|
(let ((filename (+ "files/" name))
|
||||||
|
@ -90,8 +99,7 @@
|
||||||
((= hfile.type "text")
|
((= hfile.type "text")
|
||||||
(progn
|
(progn
|
||||||
(let ((content (read-file filename))
|
(let ((content (read-file filename))
|
||||||
(lexer (guess-lexer-for-filename
|
(lexer (get-lexer hfile.filename content))
|
||||||
hfile.filename content))
|
|
||||||
(formatter (HtmlFormatter)))
|
(formatter (HtmlFormatter)))
|
||||||
(kwapply (render.main)
|
(kwapply (render.main)
|
||||||
{"content" (highlight content lexer
|
{"content" (highlight content lexer
|
||||||
|
|
Loading…
Reference in a new issue