aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2013-08-11 00:11:56 +0200
committerGravatar Tom Willemse2013-08-11 00:13:06 +0200
commit8b6efc4add1acccb62584c88ba956c54c919f986 (patch)
tree3641499589765c0c1dde801f09afcc9259ac05ef
parent6cf6009b199e03b67864649b9e2ea6a704094ccc (diff)
downloadhypo-8b6efc4add1acccb62584c88ba956c54c919f986.tar.gz
hypo-8b6efc4add1acccb62584c88ba956c54c919f986.zip
Fallback on TextLexer of no other options is found
-rwxr-xr-xhypo.hy16
1 files changed, 12 insertions, 4 deletions
diff --git a/hypo.hy b/hypo.hy
index b055869..6cac809 100755
--- a/hypo.hy
+++ b/hypo.hy
@@ -17,8 +17,9 @@
(import web sys os hashlib datetime
[pygments [highlight]]
- [pygments.lexers [guess-lexer-for-filename]]
- [pygments.formatters [HtmlFormatter]])
+ [pygments.lexers [get-lexer-by-name guess-lexer-for-filename]]
+ [pygments.formatters [HtmlFormatter]]
+ [pygments.util [ClassNotFound]])
(try (import [config [*]])
(catch [ImportError]
@@ -62,6 +63,14 @@
{"where" "shash = $shash"})))
(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 []
[[GET (lambda [self name]
(let ((filename (+ "files/" name))
@@ -90,8 +99,7 @@
((= hfile.type "text")
(progn
(let ((content (read-file filename))
- (lexer (guess-lexer-for-filename
- hfile.filename content))
+ (lexer (get-lexer hfile.filename content))
(formatter (HtmlFormatter)))
(kwapply (render.main)
{"content" (highlight content lexer