-
Notifications
You must be signed in to change notification settings - Fork 0
/
ment
executable file
·111 lines (105 loc) · 4 KB
/
ment
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
#!/bin/bash
( ####------------------------BEGIN-SUBSHELL-####
#--------------SETTINGS/VARS--------------------------------#
# https://www.xfree86.org/current/xmessage.1.html
ULIST=(
'www.gsp.com/cgi-bin/man.cgi?topic=%s§ion=%s'
'man.cx/%s(%s)'
'www.freebsd.org/cgi/man.cgi?query=%s&sektion=%s&format=ascii'
'sarata.com/manpages/%s.%s.html'
)
MODE='view'
RDIR="$HOME/.config/mentat"
URLG="${ULIST[ 0 ]}"
PAGES=( {1..9} )
OUTDIR="${RDIR}/mentatpages"
RECSDIR="${RDIR}/Recs/"
NOCACHE='0'
VIEW_PG='1'
#--------------INITILIZE_DIRECTORIES------------------------#
if ! [[ -d "${RDIR}" ]] ; then mkdir "${RDIR}" ; fi
if ! [[ -d "${OUTDIR}" ]] ; then mkdir "${OUTDIR}" ; fi
if ! [[ -d "${RECSDIR}" ]] ; then mkdir "${RECSDIR}" ; fi
#--------------VIEW_AND_DUMP_FUNCTIONS----------------------#
if command -v "lynx" &>/dev/null
then function mlink_view() { lynx -cmd_script="${RECSDIR}/cmd_script_lynx.txt" "${1}" ; }
function mlink_dump() { lynx -dump -nonumbers "${1}" ; }
elif command -v "links" &>/dev/null
then function mlink_dump() { links -dump -html-numbered-links 0 -no-g "${1}" ; }
function mlink_view() { links "${1}" ; }
elif command -v "curl" &>/dev/null
then function mlink_view() { curl --use-ascii "${1}" ; }
function mlink_dump() { curl "${1}" ; }
else echo "No web program found."
return '1'
fi
function mlink_nvim() {
nvim -c "$(printf 'vnew Web | call termopen(" %s ") | only'\
"lynx -cmd_script='${RECSDIR}/cmd_script_lynx.txt' '${1}'")"
}
#--------------HELPER_GET-----------------------------#
function get_subname() {
grep -Pio '^([a-zA-Z]*://)?([ \t]*www.)?\K[^./]*' <<< "${1}"
}
function get_url() {
tmpfile="$(mktemp)" ; echo "${tmpfile}"
mlink_dump "${1}" > "${tmpfile}"
if str_beg="$(grep -Pn --max-count=1 '^NAME[ \t]*$' "${tmpfile}" | grep -Po '^[0-9]+(?=[^0-9])')"
then str_end="$(grep -Pn '^[A-Z]+[ \t]*$' "${tmpfile}" | tail -1 | grep -Po '^[0-9]+(?=[^0-9])')"
sed -i -n "$((str_beg-1)),$((str_end+5))p" "${tmpfile}"
else
return "1"
fi
}
#--------------GET----------------------#
function get_man() {
cmg_fn="${OUTDIR}/$(get_subname "${URLG}").${CNAME}"
for i in "${PAGES[@]}" ; do
if ! [[ "${NOCACHE}" -eq "1" ]] && ls -1 "${cmg_fn}.${i}" &>/dev/null
then echo "${cmg_fn}.${i}"$'\n\t''has already been downloaded, use --nocache to ignore and redownload.'
else
mdurl="$(printf "https://${URLG}" "${CNAME}" "${i}")"
if ztop="$(get_url "${mdurl}")" ; then
if command -v "txt2man" &>/dev/null
then txt2man "${ztop}" > "${cmg_fn}.${i}"
else cat "${ztop}" > "${cmg_fn}.${i}"
fi ; fi
if [[ -f "${ztop}" ]] ; then rm "${ztop}"; fi
fi
done
}
function get_all() {
while [[ "${i:=0}" -lt ${#URL[@]} ]] ; do
get_man "${ULIST[ i++ ]}"
done
}
#--------------HANDLE_ARGS--------------#
function handle_args() {
case "${1,,}" in
--url) URLG="${2}" ; shift 1
;; --view-num) VIEW_PG="${2}" ; shift 1
;; --nocache) NOCACHE='1'
;; --all) MODE='all'
;; --view) MODE='view'
;; --dump) MODE='dump'
;; --get) MODE='get'
;; --nvim) MODE='nvim'
;; -*) echo "OPTION ${1} NOT FOUND" ; return "1"
;; "") echo "COMMAND NAME NOT PROVIDED" ; return "1"
;; *) CNAME="${*}" ; return "$?"
esac ; shift 1; handle_args "${@}"
}
#--------------MAIN---------------------#
function main() {
if ! handle_args "${@}" ; then return "1" ; fi
case "${MODE,,}" in
get) get_man "${URLG}"
;; all) get_all
;; view) mlink_view "$(printf "${URLG}" "${CNAME}" "${VIEW_PG}")"
;; dump) mlink_dump "$(printf "${URLG}" "${CNAME}" "${VIEW_PG}")"
;; nvim) mlink_nvim "$(printf "${URLG}" "${CNAME}" "${VIEW_PG}")"
;; *) echo "Error, mode is undefined"; return 1
esac
}
main "${@}"
) ####------------------------END-SUBSHELL-####