Skip to content

Go web-server middleware plugin to remove superfluous whitespace from HTML pages.

License

Notifications You must be signed in to change notification settings

mwat56/whitespace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 have to 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 : <[email protected]>

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.


About

Go web-server middleware plugin to remove superfluous whitespace from HTML pages.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Languages