├── LICENSE ├── README.md └── shiki.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jorrit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | 3 | ![shiki in action](http://i.imgur.com/xHzSwVM.png) 4 | 5 | This is `shiki`, a neat little script that allows you to browse Wikipedia articles right in your Terminal. 6 | Imagine you're in the zone, don't recognize something and don't want to break your flow by tabbing to your browser: Shiki has you covered! 7 | 8 | ## Usage 9 | 10 | `shiki NASCAR` will give you the article overview/intro to NASCAR, in English. 11 | 12 | Furthermore `shiki` has some useful options. These are: 13 | * `--full/-f`: requests the full article 14 | * `--simple/-s`: requests the simple article. This is especially handy if you want a quick understanding of something! 15 | * `NASCAR --language fr`: requests the article in French 16 | 17 | So, if you would want to read the full article about 'The Netherlands' in 'Dutch', you would `shiki -f "The Netherlands" -l nl` 18 | 19 | Note that 'simple' is a Wikipedia language, and thus can't be combined with the `--language` option. 20 | 21 | ## Installation 22 | 23 | Copy the code into your '.bash_profile', '.zshrc' or '.functions'. 24 | Another (cleaner) way is to clone this repository and add `source "${HOME}/shiki/shiki.sh"` to one of the aforementioned files. 25 | 26 | ## With help from… 27 | 28 | [Konsolebox](git.io/konsolebox) 29 | [#bash](http://webchat.freenode.net/?channels=bash) on freenode 30 | -------------------------------------------------------------------------------- /shiki.sh: -------------------------------------------------------------------------------- 1 | # Wikipedia in the shell 2 | # © Jorrit Visser 3 | # Major props to konsolebox 4 | 5 | # Usage: `shiki unix`, `shiki -f "Ghost in the Shell" --language fr` 6 | # Options: `-s/--simple`, `-f/--full` and `$article -l/--language` 7 | # '--simple' and '--language' can't be used together 8 | 9 | shiki() { 10 | local article="" encoded_article lang="" url full_mode=false simple_mode=false 11 | 12 | while [ "${#}" -gt 0 ]; do 13 | case "${1}" in # Check if first argument is an option 14 | -f|--full) 15 | full_mode=true # request full article 16 | ;; 17 | -s|--simple) 18 | simple_mode=true # request simple article 19 | ;; 20 | -fs|-sf|--simple-full|--full-simple) 21 | full_mode=true 22 | simple_mode=true 23 | ;; 24 | -l|--language) 25 | lang="${2}" 26 | shift # Use shift to eat argument 27 | ;; 28 | --) 29 | shift 30 | if [ "${#}" -gt 0 ] && [ -n "${article}" ]; then 31 | printf "You can't request multiple articles!\n" 32 | return 1 33 | fi 34 | article="${1}" 35 | break # break out of `case` to prevent 'article' reassignment 36 | ;; 37 | -*) 38 | printf "Invalid option: ${1}!\n" 39 | return 1 40 | ;; 41 | *) 42 | if [ -n "${article}" ]; then 43 | printf "You can't request multiple articles!\n" 44 | return 1 45 | fi 46 | article="${1}" 47 | ;; 48 | esac 49 | 50 | shift 51 | done 52 | 53 | # Check if article string is set 54 | if [ -z "${article}" ]; then 55 | printf "You must specify an article!\n" 56 | return 1 57 | fi 58 | 59 | # URL-encode article string 60 | encoded_article="$(python2 -c "import urllib, sys; print urllib.quote(sys.argv[1])" "${article}")" || return 1 61 | 62 | if [ -n "${lang}" ] && [ "${simple_mode}" = true ]; then 63 | printf "'--simple' and '--language' can't be used together!\n" # 'simple' is considered a language 64 | return 1 65 | elif [ "${simple_mode}" = true ]; then 66 | lang="simple" 67 | elif [ -z "${lang}" ]; then 68 | lang="en" # if language is not set, default to English 69 | fi; 70 | 71 | # https://www.mediawiki.org/wiki/API:Main_page 72 | # https://www.mediawiki.org/wiki/Extension:TextExtracts 73 | url="https://${lang}.wikipedia.org/w/api.php?action=query&titles=${encoded_article}&prop=extracts&exlimit=max&explaintext&format=json&formatversion=2&redirects=" 74 | [ "${full_mode}" = true ] || url="${url}&exintro" # Full or short article 75 | 76 | # Remove all markup cruft at article boundaries, with `sed` 77 | # For `formatversion=1` use `sed 's/"}}}.*//'` 78 | # Literal newline `sed` hack for POSIX compatibility 79 | curl -fsSL "${url}" | sed 's/^.*"extract":"//; s/"}\]}.*//; s/\\n/\ 80 | /g' | fmt -w "$(tput cols)" | less -m +Gg # Pipe into `less` for cleaner article browsing 81 | } 82 | --------------------------------------------------------------------------------