Skip to content

Commit

Permalink
Make improvements and consistency changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alrra committed Jul 29, 2016
1 parent 7f8798a commit f62a8a3
Show file tree
Hide file tree
Showing 29 changed files with 457 additions and 297 deletions.
5 changes: 3 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# For more information about the configurations used
# in this file, please see the EditorConfig documentation:
# in this file, please see the EditorConfig documentation.
#
# http://editorconfig.org

root = true
Expand All @@ -18,7 +19,7 @@ trim_trailing_whitespace = true

[*.md]

# Markdown uses the trailing whitespaces
# Markdown uses the trailing whitespaces.
# https://daringfireball.net/projects/markdown/syntax#block

trim_trailing_whitespace = false
Expand Down
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Automatically normalize line endings for all text-based files
# Automatically normalize line endings for all text-based files.
# https://git-scm.com/docs/gitattributes#_end_of_line_conversion

* text=auto
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# For more information about the configurations used
# in this file, please see the Travis CI documentation:
# in this file, please see the Travis CI documentation.
#
# https://docs.travis-ci.com

before_install:
Expand Down
2 changes: 1 addition & 1 deletion src/bin/clean-whiteboard-photos
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ clean_whiteboard_photos() {

main() {

# Check if ImageMagick's convert command-line tool is installed
# Check if ImageMagick's convert command-line tool is installed.

if ! command -v "convert" $> /dev/null; then
printf "Please install ImageMagick's 'convert' command-line tool!"
Expand Down
18 changes: 9 additions & 9 deletions src/bin/get-compression-heatmap
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ create_heatmap_for_webpage() {
--header "Accept-Encoding: gzip, deflate"
--header "Cache-Control: no-cache" # Prevent intermediate
# proxies from caching
# the response
# the response.

--location # If the page was moved
# to a different location,
# redo the request
# redo the request.
--max-time 150
--show-error
--silent
--user-agent "Mozilla/5.0 Gecko" # Send a fake UA string
# for sites that sniff
# it instead of using the
# Accept-Encoding header
# Accept-Encoding header.
)

local tmpFile="$(create_tmp_file)"
Expand All @@ -73,7 +73,7 @@ create_heatmap_for_webpage() {

# Try to get the compressed version of the page, and if the
# page isn't served compressed, `create_heatmap_for_file` will
# take care of compressing it
# take care of compressing it.

curl "${CURL_DEFAULT_OPTIONS[@]}" "$1" &> /dev/null 1> "$tmpFile" \
&& create_heatmap_for_file "$tmpFile" "$2"
Expand Down Expand Up @@ -117,8 +117,8 @@ get_script_path() {

open_image() {

# The order of the following checks matters
# as on Ubuntu there is also a utility called `open`
# The order of the following checks matters as
# on Ubuntu there is also a utility called `open`.

if cmd_exists "xdg-open"; then
xdg-open "$1"
Expand Down Expand Up @@ -175,7 +175,7 @@ main() {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Check if the current command
# line argument is a local file
# line argument is a local file.

if [ -f "$1" ]; then

Expand All @@ -185,7 +185,7 @@ main() {
create_heatmap_for_png "$1" "$imageFile"
fi

# If it's not, assume it is a URL
# If it's not, assume it is a URL.

else
create_heatmap_for_webpage "$1" "$imageFile"
Expand All @@ -199,7 +199,7 @@ main() {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Opening too many images really
# quickly makes macOS' Preview crash
# quickly makes macOS' Preview crash.

sleep 0.2

Expand Down
39 changes: 21 additions & 18 deletions src/bin/get-content-encoding
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ declare -r -a CURL_DEFAULT_OPTIONS=(
--connect-timeout 30
--header "Accept-Encoding: br, gzip, deflate, lzma, sdch"
--header "Cache-Control: no-cache" # Prevent intermediate proxies
# from caching the response
# from caching the response.

--location # If the page was moved to a
# different location, redo the
# request
# request.
--max-time 150
--show-error
--silent
--user-agent "Mozilla/5.0 Gecko" # Send a fake UA string for sites
# that sniff it instead of using
# the Accept-Encoding header
# the Accept-Encoding header.
)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -48,21 +48,23 @@ check_for_sdch() {
declare i=""
declare url="$2"

# Check if the server advertised any dictionaries
# Check if the server advertised any dictionaries.

dicts="$( printf "%s" "$currentHeaders" |
grep -i "Get-Dictionary:" |
cut -d':' -f2 |
sed s/,/\ /g )"

# If it does, check to see if it really supports SDCH
# If it does, check to see if it really supports SDCH.

if [ -n "$dicts" ]; then
for i in $dicts; do

dict=""

# Check if the dictionary location is specified as a path,
# and if it is, construct it's URL from the host name of the
# referrer URL
# Check if the dictionary location is specified as
# a path, and if it is, construct it's URL from the
# host name of the referrer URL.

[[ "$i" != http* ]] \
&& dict="$( printf "%s" "$url" |
Expand All @@ -71,7 +73,7 @@ check_for_sdch() {
dict="$dict$i"

# Request the dictionaries from the server and
# construct the `Avail-Dictionary` header value
# construct the `Avail-Dictionary` header value.
#
# [ The user agent identifier for a dictionary is defined
# as the URL-safe base64 encoding (as described in RFC
Expand All @@ -90,7 +92,7 @@ check_for_sdch() {
done

# Redo the request (advertising the available dictionaries)
# and replace the old resulted headers with the new ones
# and replace the old resulted headers with the new ones.

printf "%s" "$( curl "${CURL_DEFAULT_OPTIONS[@]}" \
-H "Avail-Dictionary: $availDicts" \
Expand Down Expand Up @@ -118,26 +120,26 @@ get_content_encoding() {
"$url" )" \
&& ( \

# Iterate over the headers of all redirects
# Iterate over the headers of all redirects.
while [ -n "$headers" ]; do

# Get headers for the "current" URL
# Get headers for the "current" URL.
currentHeaders="$( printf "%s" "$headers" | sed -n '1,/^HTTP/p' )"

# Remove the headers for the "current" URL
# Remove the headers for the "current" URL.
headers="${headers/"$currentHeaders"/}"

currentHeaders="$(check_for_sdch "$currentHeaders" "$url")"

# Get the value of the `Content-Encoding` header
# Get the value of the `Content-Encoding` header.
encoding="$( printf "%s" "$currentHeaders" |
grep -i "Content-Encoding:" |
cut -d' ' -f2 |
tr "\r" "," |
tr -d "\n" |
sed 's/,$//' )"

# Print the output for the "current" URL
# Print the output for the "current" URL.
[ -n "$encoding" ] && encoding="[$encoding]"

if [ "$url" != "$1" ]; then
Expand All @@ -148,15 +150,16 @@ get_content_encoding() {
indent=""
fi

# Get the next URL from the series of redirects

# Get the next URL from the series of redirects.
tmp="$url"
url="$( printf "%s" "$currentHeaders" |
grep -i "Location" |
sed -e 's/Location://' |
sed 's/^ *//' |
tr -d '\r' )"

# In case the `Location` header is specified as a path
# In case the `Location` header is specified as a path.
[[ "$url" != http* ]] && url="$tmp$url"

done
Expand All @@ -167,7 +170,7 @@ get_content_encoding() {

main() {

# Check if `cURL` is installed
# Check if `cURL` is installed.

if ! command -v "curl" &> /dev/null; then
printf "cURL is required, please install it!\n"
Expand Down
8 changes: 4 additions & 4 deletions src/bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cmd_exists() {

open_url() {

# The order of the following checks matters
# as on Ubuntu there is also a utility called `open`
# The order of the following checks matters as
# on Ubuntu there is also a utility called `open`.

if cmd_exists "xdg-open"; then
xdg-open "$1"
Expand All @@ -28,7 +28,7 @@ main() {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Wait for the server to be available, and once
# it is, open its address in the default browser
# it is, open its address in the default browser.

while [ $i -lt $MAX_NUMBER_OF_TRIES ]; do
if [ "$(lsof -i -nP | grep "$port" | grep -i "python")" != "" ]; then
Expand All @@ -41,7 +41,7 @@ main() {

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Start server
# Start server.

"$(dirname "${BASH_SOURCE[0]}")/server.py" "$port"

Expand Down
12 changes: 6 additions & 6 deletions src/bin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
except ImportError:

# In Python 3 the 'SimpleHTTPServer'
# module has been merged into 'http.server'
# module has been merged into 'http.server'.

import http.server as server
import socketserver
Expand All @@ -21,23 +21,23 @@
map = handler.extensions_map
port = int(sys.argv[1])

# Set default Content-Type to 'text/plain'
# Set default Content-Type to 'text/plain'.
map[""] = "text/plain"

# Serve everything as UTF-8 (although not technically
# correct, this doesn't break anything for binary files)
# correct, this doesn't break anything for binary files).
for key, value in map.items():
map[key] = value + "; charset=utf-8"

# Create but don't automatically bind socket
# (the 'allow_reuse_address' option needs to be set first)
# (the 'allow_reuse_address' option needs to be set first).
httpd = socketserver.ThreadingTCPServer(("localhost", port), handler, False)

# Prevent 'cannot bind to address' errors on restart
# Prevent 'cannot bind to address' errors on restart.
# https://brokenbad.com/address-reuse-in-pythons-socketserver/
httpd.allow_reuse_address = True

# Manually bind socket and start the server
# Manually bind socket and start the server.
httpd.server_bind()
httpd.server_activate()
print("Serving content on port: ", port)
Expand Down
2 changes: 1 addition & 1 deletion src/git/gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Automatically normalize line endings for all text-based files
# Automatically normalize line endings for all text-based files.
# https://git-scm.com/docs/gitattributes#_end_of_line_conversion
#
# * text=auto
Loading

0 comments on commit f62a8a3

Please sign in to comment.