https://github.com/mwat56/whitespace
Go web-server middleware plugin to remove superfluous whitespace from HTML pages.
https://github.com/mwat56/whitespace
golang golang-library golang-module middleware web-server whitespaces
Last synced: 4 months ago
JSON representation
Go web-server middleware plugin to remove superfluous whitespace from HTML pages.
- Host: GitHub
- URL: https://github.com/mwat56/whitespace
- Owner: mwat56
- License: gpl-3.0
- Created: 2020-03-02T20:02:56.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-07-26T11:49:30.000Z (almost 2 years ago)
- Last Synced: 2025-08-15T09:37:51.283Z (10 months ago)
- Topics: golang, golang-library, golang-module, middleware, web-server, whitespaces
- Language: Go
- Size: 40 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Whitespace
[](https://golang.org/)
[](https://godoc.org/github.com/mwat56/whitespace)
[](https://goreportcard.com/report/github.com/mwat56/whitespace)
[](https://github.com/mwat56/whitespace/issues?q=is%3Aopen+is%3Aissue)
[](https://github.com/mwat56/whitespace/)
[](https://github.com/mwat56/whitespace/tags)
[](https://github.com/mwat56/whitespace/blob/main/LICENSE)
- [Whitespace](#whitespace)
- [Purpose](#purpose)
- [Installation](#installation)
- [Usage](#usage)
- [Runtime](#runtime)
- [Libraries](#libraries)
- [Licence](#licence)
----
## 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 `` and the following `
` 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](https://github.com/mwat56/Nele/blob/main/README.md) and [Kaliber](https://github.com/mwat56/kaliber/blob/main/README.md) (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 :
> 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.
----
[](http://www.gnu.org/copyleft/fdl.html)