summaryrefslogtreecommitdiff
path: root/external
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2014-04-14 16:57:32 -0400
committerTim Hatch <tim@timhatch.com>2014-04-14 16:57:32 -0400
commitf863059d2dd8e45eefc509c563ccacabbb4d8a06 (patch)
treeb3742541248df269e16d4d9c2bdc29b4c897afe5 /external
parent01faf9753ed8a008edd8c960cd42bc9a0594b780 (diff)
parentcc47b8d607e2ccc1b9bbcd13e7383fe69fbb14ad (diff)
downloadpygments-f863059d2dd8e45eefc509c563ccacabbb4d8a06.tar.gz
Merged in flyingsheep/pygments-main (pull request #209)
Conflicts: external/autopygmentize
Diffstat (limited to 'external')
-rwxr-xr-xexternal/autopygmentize116
1 files changed, 66 insertions, 50 deletions
diff --git a/external/autopygmentize b/external/autopygmentize
index 2df6d469..964c138f 100755
--- a/external/autopygmentize
+++ b/external/autopygmentize
@@ -1,65 +1,81 @@
-#!/bin/sh
+#!/bin/bash
# Best effort auto-pygmentization with transparent decompression
# (c) Reuben Thomas 2012-2013
# This program is in the public domain.
# Strategy: first see if pygmentize can find a lexer; if not, ask file; if that finds nothing, fail
-# Set the environment variable PYGMENTIZE_OPTS to configure pygments.
+# Set the environment variable PYGMENTIZE_OPTS or pass options before the file path to configure pygments.
# This program can be used as a .lessfilter for the less pager to auto-color less's output
-lexer=`pygmentize -N "$1"`
-file_common_opts="--brief --dereference --uncompress"
-if [ "$lexer" = "text" ]; then
- unset lexer
- case `file --mime-type $file_common_opts "$1"` in
- application/xml|image/svg+xml) lexer=xml;;
- text/html) lexer=html;;
- text/troff) lexer=nroff;;
- text/x-asm) lexer=nasm;;
- text/x-awk) lexer=awk;;
- text/x-c) lexer=c;;
- text/x-c++) lexer=cpp;;
- text/x-diff) lexer=diff;;
- text/x-fortran) lexer=fortran;;
- text/x-gawk) lexer=gawk;;
- text/x-java) lexer=java;;
- text/x-lisp) lexer=common-lisp;;
- text/x-lua) lexer=lua;;
- text/x-makefile) lexer=make;;
- text/x-msdos-batch) lexer=bat;;
- text/x-nawk) lexer=nawk;;
- text/x-pascal) lexer=pascal;;
- text/x-perl) lexer=perl;;
- text/x-php) lexer=php;;
- text/x-po) lexer=po;;
- text/x-python) lexer=python;;
- text/x-ruby) lexer=ruby;;
- text/x-shellscript) lexer=sh;;
- text/x-tcl) lexer=tcl;;
- text/x-tex|text/x-texinfo) lexer=latex;; # FIXME: texinfo really needs its own lexer
-
- # Types that file outputs which pygmentize didn't support as of file 5.14, pygments 1.6
- # text/calendar
- # text/inf
- # text/PGP
- # text/rtf
- # text/texmacs
- # text/vnd.graphviz
- # text/x-bcpl
- # text/x-info
- # text/x-m4
- # text/x-vcard
- # text/x-xmcd
- esac
+file="${!#}" # last argument
+options=${@:1:$(($#-1))} # handle others args as options to pass to pygmentize
+
+file_common_opts="--brief --dereference"
+
+lexer=$(pygmentize -N "$file")
+if [[ "$lexer" == text ]]; then
+ unset lexer
+ case $(file --mime-type --uncompress $file_common_opts "$file") in
+ application/xml|image/svg+xml) lexer=xml;;
+ text/html) lexer=html;;
+ text/troff) lexer=nroff;;
+ text/x-asm) lexer=nasm;;
+ text/x-awk) lexer=awk;;
+ text/x-c) lexer=c;;
+ text/x-c++) lexer=cpp;;
+ text/x-diff) lexer=diff;;
+ text/x-fortran) lexer=fortran;;
+ text/x-gawk) lexer=gawk;;
+ text/x-java) lexer=java;;
+ text/x-lisp) lexer=common-lisp;;
+ text/x-lua) lexer=lua;;
+ text/x-makefile) lexer=make;;
+ text/x-msdos-batch) lexer=bat;;
+ text/x-nawk) lexer=nawk;;
+ text/x-pascal) lexer=pascal;;
+ text/x-perl) lexer=perl;;
+ text/x-php) lexer=php;;
+ text/x-po) lexer=po;;
+ text/x-python) lexer=python;;
+ text/x-ruby) lexer=ruby;;
+ text/x-shellscript) lexer=sh;;
+ text/x-tcl) lexer=tcl;;
+ text/x-tex|text/x-texinfo) lexer=latex;; # FIXME: texinfo really needs its own lexer
+
+ # Types that file outputs which pygmentize didn't support as of file 5.11, pygments 1.6rc1
+ # text/calendar
+ # text/PGP
+ # text/rtf
+ # text/texmacs
+ # text/x-bcpl
+ # text/x-info
+ # text/x-m4
+ # text/x-vcard
+ # text/x-xmcd
+
+ text/plain) # special filenames. TODO: insert more
+ case $(basename "$file") in
+ .zshrc) lexer=sh;;
+ esac
+ ;;
+ esac
fi
-encoding=`file --mime-encoding $file_common_opts "$1"`
+encoding=$(file --mime-encoding --uncompress $file_common_opts "$file")
+if [[ $encoding == "us-asciibinarybinary" ]]; then
+ encoding="us-ascii"
+fi
-if [ -n "$lexer" ]; then
+if [[ -n "$lexer" ]]; then
+ concat=cat
+ case $(file $file_common_opts --mime-type "$file") in
+ application/x-gzip) concat=zcat;;
+ application/x-bzip2) concat=bzcat;;
+ application/x-xz) concat=xzcat;;
+ esac
# FIXME: Specify input encoding rather than output encoding https://bitbucket.org/birkenfeld/pygments-main/issue/800
- zcat "$1" | pygmentize -O encoding=$encoding,outencoding=UTF-8 $PYGMENTIZE_OPTS -l $lexer
- exit 0
+ exec $concat "$file" | pygmentize -f terminal256 -O style=native,encoding=$encoding,outencoding=UTF-8 $PYGMENTIZE_OPTS $options -l $lexer
fi
exit 1