#!/bin/bash
# ©2017-2018 Michael Uplawski <michael.uplawski@uplawski.eu>
# Use ths script at your own risk, modify it as you please.
# But maybe leave the copyright-notice intact. Thank You.

# --------- SOME DEFINITIONS ----------
# The Name of the dictionary:
DICT_NAME='Meriam Webster http://www.meriam-webster.com'
# The part of the dictionary-url which PRECEDES AN EXPRESSION to search
DICT=https://www.merriam-webster.com/dictionary/
# The command to extract a fragment from a page
EXTR='puts $_.at_css("div#definition-wrapper")'

# other dictionary
#DICT_NAME='Conjugaison.com'
#DICT=http://www.conjugaison.com/conjuguer.php?verbe=
#EXTR='puts $_.at_xpath("//table")'

# The browser to pipe the result to
# BROWSER='/usr/bin/lynx'
BROWSER='/usr/bin/w3m'

# a temporary HTML-file
FL=`mktemp --suffix=.html`

# --------> ACTION <---------
# Extract the interesting part of the search result, make HTML.
echo "<html><body><h1>"$DICT_NAME"</h1>`curl -s "$DICT""$1" | nokogiri -e "$EXTR"`</body/></html>" > $FL
# <-------- END ACTION --------->

# Display 
$BROWSER $FL
# remove temporary file
rm -f "$FL"
