whitespace

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2024 License: GPL-3.0 Imports: 4 Imported by: 2

README

Whitespace

golang GoDoc Go Report Issues Size Tag License


Purpose

Whitespace (TABulators, NewLines, and SPaces) are characters in e.g. HTML pages which don't have syntactic significance but are just supposed to ease the reading by humans. Whether you put one space between </p> and the following <p> doesn't make a difference as far as parsing and rendering the HTML page is concerned – it just increases the filesize and therefore the amount of bandwidth needed, the time used for transfer and interpretation, the amount of memory used in both the sending server and the receiving user/browser. In the end one could say: the more whitespace there is in your HTML pages the more expensive it is for all parties involved.

When writing Nele and Kaliber (both of which are essentially web-servers) I realised that I basically implemented the same code for removing superfluous whitespace before delivering the HTML pages to the remote user. So I extracted that code and refactored it to become the simple middleware plugin you can see here.

Installation

You can use Go to install this package for you:

go get -u github.com/mwat56/whitespace

Usage

There are two ways to use this library:

  1. As a middleware plugin which then will be used automatically.
  2. By calling the Remove() function from your code whenever it suits you.

In both cases you can use this library by

import "github.com/mwat56/whitespace"

To use it as middleware for your web-server you call

whitespace.Wrap(aHandler http.Handler) http.Handler

where aHandler is the page-handler you implemented to handle (generate and send) your web-pages. The function's return value can then be used to set up your http.Server instance.

If instead you want to use the library manually you can call

whitespace.Remove(aPage []byte) []byte

where aPage is the HTML page you prepared and the function's return value is that very page with all superfluous whitespace removed.

Runtime

You can de-/activate the removal behaviour at runtime by setting the boolean UseRemoveWhitespace flag. If it is true (i.e. the default) both the Remove() and Wrap() functions work as expected (see above). However, if you set that flag to false no whitespace removal takes place and the HTML pages produced by your server will be send to the remote user as they were generated (including the whitespace you and/or your tools put there).

Libraries

No external libraries were used building whitespace.

Licence

    Copyright © 2020, 2024  M.Watermann, 10247 Berlin, Germany
                    All rights reserved
                EMail : <support@mwat.de>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should have received a copy of the GNU General Public License along with this program. If not, see the GNU General Public License for details.


GFDL

Documentation

Overview

Package whitespace implements a middleware plugin to remove whitespace from web-pages generated by your web-server.

Copyright © 2020, 2022 M.Watermann, 10247 Berlin, Germany
                All rights reserved
            EMail : <support@mwat.de>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should have received a copy of the GNU General Public License along with this program. If not, see the [GNU General Public License](http://www.gnu.org/licenses/gpl.html) for details.

Index

Constants

This section is empty.

Variables

View Source
var (
	// `UseRemoveWhitespace` (default: `true`) determines whether the
	// removal of whitespace is actually run.
	// If set to `false` the `Remove()` function becomes basically a NoOp.
	//
	// This flag allows you to include the `Wrap()` and/or `Remove()`
	// functions in your code and change your program's actual behaviour
	// according to some configuration setting or commandline option:
	// You'd just change this flag accordingly at runtime without changing
	// your source code at all.
	UseRemoveWhitespace = true
)

Functions

func Remove

func Remove(aPage []byte) []byte

`Remove` returns `aPage` with HTML comments and unnecessary whitespace removed.

Depending of the global `UseRemoveWhitespace` flag this function removes all unneeded/redundant whitespace and HTML comments from the given `aPage`. This can reduce significantly the amount of data to send to the remote user agent thus saving both bandwidth and transfer time.

Parameters:

  • `aPage`: The web page's HTML markup to process.

Returns:

  • `[]byte`: The cleansed page.

func Wrap

func Wrap(aNext http.Handler) http.Handler

`Wrap` returns a handler function that removes superfluous whitespace, wrapping the given `aHandler` and calling it internally.

Parameters:

  • `aNext`: responds to the actual HTTP request.

Returns:

  • `http.Handler`: The handler function to use by the application.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL