-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.lessfilter
executable file
·123 lines (91 loc) · 3.83 KB
/
.lessfilter
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env bash
# exit 1 if no 1st argument present
[[ -z "${1}" ]] && exit 1
# Source clean-hash function from kdm-bash-env, or exit 2
# shellcheck disable=SC1091
. "${HOME}/.kdm/rc.d/00-base" || exit 2
# if pygmentize is not present, exit now
hash pygmentize || exit 0
_debugLog() {
# Comment below line to enable debug output
return 0
echo "1 : '${1}'"
echo "FILE_NAME : '${FILE_NAME}'"
echo "FILE_MIME_TYPE : '${FILE_MIME_TYPE}'"
echo "EXTENSION_DETECT : '${EXTENSION_DETECT}'"
echo "LEXER_OVERRIDE : '${LEXER_OVERRIDE}'"
echo
}
# Pygments formatter and style
PYG_FMT="terminal16m"
PYG_STYLE="monokai"
# Get base file name
FILE_NAME="$(basename "${1}")"
# Force lowercase filename
FILE_NAME="${FILE_NAME,,}"
LEXER_OVERRIDE=""
# If file type was detected based on file extension
EXTENSION_DETECT="false"
# Try to detect file type purely based on file name extension
case "${FILE_NAME}" in
# Fairly common file types normally supported by pygmentize
*.[ch]|*.[ch]pp|*.[ch]xx|*.ad[asb]|*.asm|*.awk|*.axp|*.bat|*.c|*.cc|*.cmd|*.cnf|*.conf|*.config|*.cpp|*.css|*.diff|*.ebuild|*.eclass|*.groff|*.h|*.hh|*.htm|*.html|*.inc|*.java|*.l|*.lsp|*.m4|*.md|*.p|*.pas|*.patch|*.pl|*.pm|*.pod|*.pov|*.ppd|*.rb|*.sass|*.scss|*.sh|*.sls|*.sql|*.svg|*.xml|*.xps|*.xsd|*.xsl|*.yaml|*.yml)
EXTENSION_DETECT="true"
;;
esac
# Call pygmentize without lexer set, to attempt auto-detection based on file extension
if [[ "${EXTENSION_DETECT}" == "true" ]]; then
_debugLog "${1}"
pygmentize -f "${PYG_FMT}" -O "style=${PYG_STYLE}" "${1}"
exit "${?}"
fi
# Normal file extension-based detection failed, add some manual effort
if [[ -z "${LEXER_OVERRIDE}" ]]; then
case "${FILE_NAME}" in
# .bash_logout, .bash_profile, .bashrc, .profile, etc
.bash*|.profile) LEXER_OVERRIDE="sh" ;;
# systemd files
*.mount|*.service|*.socket|*.target|*.timer) LEXER_OVERRIDE="dosini" ;;
*.ino) LEXER_OVERRIDE="c" ;;
*.json) LEXER_OVERRIDE="json" ;;
*.php) LEXER_OVERRIDE="php" ;;
*.plist) LEXER_OVERRIDE="xml" ;;
*.py) LEXER_OVERRIDE="python" ;;
*.xdf) LEXER_OVERRIDE="xml" ;;
esac
fi
# grep for specific shebang, if it passes, set lexer value
if [[ -z "${LEXER_OVERRIDE}" ]]; then
SHEBANG_LINE="$(head -n 1 "${1}")"
echo "${SHEBANG_LINE}" | grep -Eq '#!.*./bin.*.node' && LEXER_OVERRIDE="javascript"
echo "${SHEBANG_LINE}" | grep -Eq '#!.*./bin.*.php.*' && LEXER_OVERRIDE="php"
echo "${SHEBANG_LINE}" | grep -Eq '#!.*./bin.*.python.*' && LEXER_OVERRIDE="python"
echo "${SHEBANG_LINE}" | grep -Eq '#!.*./bin.*.sh' && LEXER_OVERRIDE="sh"
fi
# Shebang grep failed, try detecting based on mime type
if [[ -z "${LEXER_OVERRIDE}" ]]; then
FILE_MIME_TYPE="$(file --brief --dereference --no-pad --no-buffer --mime-type "${FILE_PATH}" 2> /dev/null | head -n 1)"
case "${FILE_MIME_TYPE}" in
application/javascript) LEXER_OVERRIDE="javascript" ;;
application/json) LEXER_OVERRIDE="json" ;;
text/html) LEXER_OVERRIDE="html" ;;
text/x-c) LEXER_OVERRIDE="c" ;;
text/x-php) LEXER_OVERRIDE="php" ;;
text/x-script.python) LEXER_OVERRIDE="python" ;;
text/x-shellscript) LEXER_OVERRIDE="sh" ;;
text/xml) LEXER_OVERRIDE="xml" ;;
esac
fi
_debugLog "${1}"
if [[ -n "${LEXER_OVERRIDE}" ]]; then
# JSON : Format with jq, sort output, use tab indendation
if [[ "${LEXER_OVERRIDE}" == "json" ]] && hash jq; then
jq -S --tab '.' "${1}" | pygmentize -f "${PYG_FMT}" -O "style=${PYG_STYLE}" -l "${LEXER_OVERRIDE}"
exit "${?}"
fi
pygmentize -f "${PYG_FMT}" -O "style=${PYG_STYLE}" -l "${LEXER_OVERRIDE}" "${1}"
exit "${?}"
fi
# Last-ditch effort
# -g = Guess the lexer from the file contents, or pass through as plain text if nothing can be guessed
pygmentize -g -f "${PYG_FMT}" -O "style=${PYG_STYLE}" "${1}"