summaryrefslogtreecommitdiff
path: root/external/autopygmentize
blob: 3a4aed7f150eec04902c8986b5b71fb649b0701c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# Best effort auto-pygmentization with transparent decompression
# by Reuben Thomas 2008-2022
# 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 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

file="${!#}"              # last argument
options=${@:1:$(($#-1))}  # handle others args as options to pass to pygmentize

file_common_opts="--brief --dereference"

case $(file --mime-type --uncompress $file_common_opts "$file") in
    application/xml|image/svg+xml) lexer=xml;;
    application/javascript) lexer=javascript;;
    application/json) lexer=json;;
    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-crystal) lexer=crystal;;
    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.20, pygments 2.0
    # 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

    text/plain)  # special filenames. TODO: insert more
        case $(basename "$file") in
            .zshrc) lexer=sh;;
        esac
        # pygmentize -N is much cheaper than file, but makes some bad guesses (e.g.
        # it guesses ".pl" is Prolog, not Perl)
        lexer=$(pygmentize -N "$file")
        ;;
esac

# Find a concatenator for compressed files
concat=cat
case $(file $file_common_opts --mime-type "$file") in
    application/gzip)    concat=zcat;;
    application/x-bzip2) concat=bzcat;;
    application/x-xz)    concat=xzcat;;
esac

# Find a suitable reader, preceded by a hex dump for binary files,
# or fmt for text with very long lines
prereader=""
reader=cat
encoding=$(file --mime-encoding --uncompress $file_common_opts "$file")
# FIXME: need a way to switch between hex and text view, as file often
# misdiagnoses files when they contain a few control characters
# if [[ $encoding == "binary" ]]; then
#     prereader="od -x" # POSIX fallback
#     if [[ -n $(which hd) ]]; then
#         prereader=hd # preferred
#     fi
#     lexer=hexdump
#     encoding=latin1
#el
# FIXME: Using fmt does not work well for system logs
# if [[ "$lexer" == "text" ]]; then
#    if file "$file" | grep -ql "text, with very long lines"; then
#        reader=fmt
#    fi
# fi
if [[ "$lexer" != "text" ]]; then
    reader="pygmentize -O inencoding=$encoding $PYGMENTIZE_OPTS $options -l $lexer"
fi

# Run the reader
if [[ -n "$prereader" ]]; then
    exec $concat "$file" | $prereader | $reader
else
    exec $concat "$file" | $reader
fi