From e40d6a73d57835fa99de928098a0af0b6a8e723e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2020 23:32:00 -0400 Subject: [PATCH] oeis topic string and code segment update --- lib/adapter/cmd.py | 7 +- share/adapters/oeis.sh | 141 +++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 73 deletions(-) mode change 100644 => 100755 share/adapters/oeis.sh diff --git a/lib/adapter/cmd.py b/lib/adapter/cmd.py index db365e2..30795b8 100644 --- a/lib/adapter/cmd.py +++ b/lib/adapter/cmd.py @@ -131,13 +131,10 @@ class AdapterOeis(CommandAdapter): # cut oeis/ off if topic.startswith("oeis/"): - topic = topic[4:] + topic = topic[5:] return cmd + [topic] - def _get_list(self, prefix=None): - return list("oeis/%s" % x for x in range(1, 8649)) - def is_found(self, topic): return True @@ -159,7 +156,7 @@ class AdapterChmod(CommandAdapter): # cut chmod/ off if topic.startswith("chmod/"): - topic = topic[4:] + topic = topic[6:] return cmd + [topic] diff --git a/share/adapters/oeis.sh b/share/adapters/oeis.sh old mode 100644 new mode 100755 index f2594eb..b727384 --- a/share/adapters/oeis.sh +++ b/share/adapters/oeis.sh @@ -3,108 +3,113 @@ # Written by Erez Binyamin (github.com/ErezBinyamin) # Search for an integer sequence -oeis() { +# USAGE: +# oeis +# oeis +# oeis +oeis() ( local URL='https://oeis.org' local TMP=/tmp/oeis local DOC=/tmp/oeis/doc.html + local MAX_TERMS=10 mkdir -p $TMP + # Get short description of a sequence + get_desc() { + grep -A 1 '' $DOC \ + | sed '//d; /--/d; s/^[ \t]*//; s/<[^>]*>//g;' \ + | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</.*, .*[0-9]' $DOC \ + | sed 's/<[^>]*>//g' \ + | grep -v '[a-z]' \ + | grep -v ':' \ + | cut -d ',' -f 1-${MAX_TERMS} + return $? + } + # Sample code parser INPUT arg: is grep regex + parse_code() { + local GREP_REGEX="${1}" + cat $DOC \ + | tr '\n' '`' \ + | grep -o "${GREP_REGEX}" \ + | tr '`' '\n' \ + | sed 's/^[ \t]*//; s/<[^>]*>//g; /^\s*$/d;' \ + | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/<//dev/null > $DOC - # ID + # Print ID printf "ID: ${ID}\n" - # Description - grep -A 1 '' $DOC \ - | sed '//d; /--/d; s/^[ \t]*//; s/<[^>]*>//g;' \ - | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</.*, .*[0-9]' $DOC \ - | sed 's/<[^>]*>//g' \ - | grep -v '[a-z]' \ - | grep -v ':' + # Print Sequence sample limited by $MAX_TERMS + get_seq ${MAX_TERMS} printf "\n" - # Code - if grep -q 'MAPLE' $DOC + # Print Code Sample + if [[ ${LANG^^} == 'MAPLE' ]] && grep -q 'MAPLE' $DOC then - GREP_REGEX='MAPLE.*CROSSREFS' - grep -q 'PROG' $DOC && GREP_REGEX='MAPLE.*PROG' - grep -q 'MATHEMATICA' $DOC && GREP_REGEX='MAPLE.*MATHEMATICA' - cat $DOC \ - | tr '\n' '`' \ - | grep -o "${GREP_REGEX}" \ - | tr '`' '\n' \ - | sed 's/^[ \t]*//; s/<[^>]*>//g; /^\s*$/d;' \ - | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</]*>//g; /^\s*$/d;' \ - | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</]*>//g; /^\s*$/d;' \ - | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</ ${TMP}/lang - langs=("Axiom" "MAGMA" "PARI" "Python" "Sage" "Haskell" "Julia" "GAP" "Scala") - for L in ${langs[@]} - do - echo "foo" | pygmentize -l ${L,,} &>/dev/null && PYG="${L,,}" || PYG="c" - if grep -q "(${L})" $DOC - then - awk -v tgt="${L}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/lang \ - | pygmentize -f terminal256 -g -P style=monokai -l ${PYG} - printf "\n" - fi - done + # PROG section contains more code samples (Non Mathematica or Maple) + parse_code "PROG.*CROSSREFS" \ + | 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 + cat ${TMP}/code_snippet # Search unknown sequence else # Build URL - URL+="/search?q=signed%3A$(echo $@ | tr ' ' ',')" + URL+="/search?q=signed%3A$(echo $@ | grep -v [a-z] | grep -v [A-Z] | tr ' ' ',')" curl $URL 2>/dev/null > $DOC # Sequence IDs grep -o '=id:.*&' $DOC \ | sed 's/=id://; s/&//' > $TMP/id # Descriptions - grep -A 1 '' $DOC \ - | sed '//d; /--/d; s/^[ \t]*//; s/<[^>]*>//g' \ - | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</ $TMP/nam + get_desc > $TMP/desc # Sequences - grep -o '.*' $DOC \ - | sed 's/<[^>]*>//g' > $TMP/seq + get_seq ${MAX_TERMS} > $TMP/seq # Print data for all readarray -t ID < $TMP/id - readarray -t NAM < $TMP/nam + readarray -t DESC < $TMP/desc readarray -t SEQ < $TMP/seq for i in ${!ID[@]} do - printf "${ID[$i]}: ${NAM[$i]}\n" + printf "${ID[$i]}: ${DESC[$i]}\n" echo ${SEQ[$i]} printf "\n" done fi -} +) oeis $@