1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-12-02 07:50:04 +03:00

Merge pull request #208 from ErezBinyamin/master

more resilliant cmd.py Adapter parsing
This commit is contained in:
Igor Chubin 2020-06-07 19:22:37 +02:00 committed by GitHub
commit 98c630c62a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 19 deletions

View File

@ -130,9 +130,10 @@ class AdapterOeis(CommandAdapter):
cmd[0] = _get_abspath(cmd[0])
# cut oeis/ off
# Space delimiter for args to oeis.sh
# Replace all non (numeric, '-') chars with Spaces to delimit args to oeis.sh
if topic.startswith("oeis/"):
topic = topic[5:].replace('+',' ')
topic = topic[5:]
topic = re.sub('[^0-9-]', ' ', topic)
return cmd + [topic]
@ -154,10 +155,13 @@ class AdapterChmod(CommandAdapter):
cmd = self._command[:]
# cut chmod/ off
# remove all non (alphanumeric, '-') chars
if topic.startswith("chmod/"):
topic = topic[6:]
topic = re.sub('[^a-z^A-Z^0-9-]', '', topic)
return cmd + [topic]
def is_found(self, topic):
return True
return True

View File

@ -13,14 +13,17 @@ oeis() (
local DOC=/tmp/oeis/doc.html
local MAX_TERMS=10
mkdir -p $TMP
# Get short description of a sequence
# -- get_desc --
# @return print description of OEIS sequence
get_desc() {
grep -A 1 '<td valign=top align=left>' $DOC \
| sed '/<td valign=top align=left>/d; /--/d; s/^[ \t]*//; s/<[^>]*>//g;' \
| sed 's/&nbsp;/ /g; s/\&amp;/\&/g; s/&gt;/>/g; s/&lt;/</g; s/&quot;/"/g'
return $?
}
# Print out the first MAX_TERMS terms of a sequence
# -- get_seq --
# @param MAX_TERMS
# @return Print the first MAX_TERMS terms of a sequence
get_seq() {
local MAX_TERMS=${1}
grep -o '<tt>.*, .*[0-9]</tt>' $DOC \
@ -30,7 +33,9 @@ oeis() (
| cut -d ',' -f 1-${MAX_TERMS}
return $?
}
# Sample code parser INPUT arg: is grep regex <FROM*TO>
# -- parse_code --
# @param GREP_REGEX
# @return Code snippet that corresponds to GREP_REGEX
parse_code() {
local GREP_REGEX="${1}"
cat $DOC \
@ -41,27 +46,35 @@ oeis() (
| sed 's/&nbsp;/ /g; s/\&amp;/\&/g; s/&gt;/>/g; s/&lt;/</g; s/&quot;/"/g'
return $?
}
# Search sequence by ID
# -- MAIN --
# Search sequence by ID (optional language arg)
# . oeis <SEQ_ID>
# . oeis <SEQ_ID> <LANGUAGE>
# . oeis <LANGUAGE> <SEQ_ID>
if [ $# -lt 3 ]
then
# Arg-Parse ID, Generate URL
echo $1 | grep -q -e [a-z] -e [B-Z] && ID=$2 || ID=$1
echo $1 | grep -q -e [a-z] -e [B-Z] && LANG=$1 || LANG=$2
if echo ${1^^} | grep -q '[B-Z]'
then
ID=$2
LANGUAGE=$1
else
ID=$1
LANGUAGE=$2
fi
[[ ${ID:0:1} == 'A' ]] && ID=${ID:1}
ID=$(bc <<< "$ID")
ID="A$(printf '%06d' ${ID})"
URL+="/${ID}"
curl $URL 2>/dev/null > $DOC
# Print ID
# Print ID, description, and sequence
printf "ID: ${ID}\n"
# Print Description
get_desc
printf "\n"
# Print Sequence sample limited by $MAX_TERMS
get_seq ${MAX_TERMS}
printf "\n"
# Print Code Sample
if [[ ${LANG^^} == 'MAPLE' ]] && grep -q 'MAPLE' $DOC
if [[ ${LANGUAGE^^} == 'MAPLE' ]] && grep -q 'MAPLE' $DOC
then
GREP_REGEX='MAPLE.*CROSSREFS'
grep -q 'PROG' $DOC && GREP_REGEX='MAPLE.*PROG'
@ -69,7 +82,7 @@ oeis() (
parse_code "${GREP_REGEX}" \
| sed 's/MAPLE/(MAPLE)/; /MATHEMATICA/d; /PROG/d; /CROSSREFS/d'
fi
if [[ ${LANG^^} == 'MATHEMATICA' ]] && grep -q 'MATHEMATICA' $DOC
if [[ ${LANGUAGE^^} == 'MATHEMATICA' ]] && grep -q 'MATHEMATICA' $DOC
then
GREP_REGEX='MATHEMATICA.*CROSSREFS'
grep -q 'PROG' $DOC && GREP_REGEX='MATHEMATICA.*PROG'
@ -81,11 +94,11 @@ oeis() (
| sed '/PROG/d; /CROSSREFS/d' > ${TMP}/prog
# Print out code sample for specified language
rm -f ${TMP}/code_snippet
awk -v tgt="${LANG^^}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet
L="${LANG:0:1}"
LANG="${LANG:1}"
LANG="${L^^}${LANG,,}"
[ $(wc -c < $TMP/code_snippet) -eq 0 ] && awk -v tgt="${LANG}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet
awk -v tgt="${LANGUAGE^^}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet
L="${LANGUAGE:0:1}"
LANGUAGE="${LANGUAGE:1}"
LANGUAGE="${L^^}${LANGUAGE,,}"
[ $(wc -c < $TMP/code_snippet) -eq 0 ] && awk -v tgt="${LANGUAGE}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet
cat ${TMP}/code_snippet
# Search unknown sequence
else