Skip to main content

webd is a python package for developing and optimally deploying HTML.

Project description

webd


webd (pronounced "webbed") is a python package for developing and optimally deploying HTML.

Installation


Use pip to install this package:

pip install webd

Usage


Import webd to begin using it:

import webd
Dependencies:
  • requests
  • pandas

Functionalities


  • loc - download remote files and alter HTML to reference local (downloaded) files
  • unloc - undo loc, change HTML script elements to reference original remote files
  • indent - regularize indentation of HTML
  • formatter - general formatting and clean up for HTML
  • in_css - internalize external CSS
  • in_js - inline external JavaScript
  • minify - minify HTML and CSS
  • opt - perform formatter, unloc, in_js, in_css, and minify on HTML

Levels


For added flexibility, each of the functionalities listed above has a base level and a file level. The base level alters a python object (usually an HTML string), and the file level alters a file or a set of files in a directory (usually HTML files). For each functionality, there are two functions that correspond to the two levels. These functions have the following naming convention: functionality and functionality_file.

For example, the loc functionality's functions are loc and loc_file. The loc function takes an html string as input and returns an altered html string. The loc_file function reads an html file, passes the file's contents to the loc function, and then writes the returned html string to file for one file or for a set of files in a directory.

Base-Level Functions


webd.loc(html)


Localizes HTML by downloading remote files (e.g. CSS and JavaScript) referenced therein. The downloaded files are placed in ./downloaded. (If there is no downloaded directory in the current working directory, one will be created.) Then, the HTML string given as input is altered to reference the local files in the downloaded directory. Localizing HTML is particularly useful when working with limited data or a poor network connection.

For example, running

webd.loc(html)

with the value of html being the following string

<!doctype html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
  </head>
  <body></body>
</html> 

will return the following

<!doctype html>
<html>
  <head>
    <script src="downloaded/jquery-3.4.1.min.js"></script>
    <script src="downloaded/popper.min.js"></script>
    <script src="downloaded/bootstrap.min.js"></script>
  </head>
  <body></body>
</html>

webd.unloc(html)


Undoes loc. Referencs in the HTML to files in the downloaded directory are found and replaced with references to the original URLs. Data on local files and their corresponding URLs is stored in downloaded/register.csv.

The following function call

webd.unloc(html)

with html being the value returned from webd.loc(html) above

<!doctype html>
<html>
  <head>
    <script src="downloaded/jquery-3.4.1.min.js"></script>
    <script src="downloaded/popper.min.js"></script>
    <script src="downloaded/bootstrap.min.js"></script>
  </head>
  <body></body>
</html>

will undo loc and return the original string with references to remote files

<!doctype html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
  </head>
  <body></body>
</html> 

webd.indent(html, n_spaces=2)


Properly indents HTML. The n_spaces parameter determines the size of the indent (number of spaces) and has a default value of two.

Running

webd.indent(html, n_spaces=3)

with html being the following string with exaggerated improper indentation

        <!doctype html>
<html>
      <head>
  <script src="downloaded/jquery-3.4.1.slim.min.js"></script>
<script src="downloaded/popper.min.js"></script>
          <script src="downloaded/bootstrap.min.js"></script>
                            </head>
  <body>
    </body>
</html> 

Returns the following

<!doctype html>
<html>
   <head>
      <script src="downloaded/jquery-3.4.1.slim.min.js"></script>
      <script src="downloaded/popper.min.js"></script>
      <script src="downloaded/bootstrap.min.js"></script>
   </head>
   <body>
   </body>
</html> 

webd.formatter(html, indent=True, n_spaces=2 )


Applies lightweight formatting and general cleanup for HTML. The parameter indent specifies whether webd.indent should also be called on the HTML with the value of n_spaces from webd.formatter being passed to webd.indent.

Running

webd.formatter(html)

on the following html mess

        <!docTYpe hTml>
<HTML>
      <Head>
  <scrIPt src="downloaded/jquery-3.4.1.slim.min.js"><   /script    >
<SCRIPT       src="downloaded/popper.min.js"   ></scripT>
          <script src="downloaded/bootstrap.min.js">     < /SCRIPT>
                            </heAd>
  <body>
    </BODY>
</hTmL> 

will return clean and properly-indented HTML

<!doctype html>
<html>
  <head>
    <script src="downloaded/jquery-3.4.1.slim.min.js"></script>
    <script src="downloaded/popper.min.js"></script>
    <script src="downloaded/bootstrap.min.js"></script>
  </head>
  <body>
  </body>
</html>

webd.in_js(html)


Inlines JavaScript from external files whether they be local or remote. Deferred JavaScript is inserted right before the closing body tag. Cleans up the HTML using webd.formatter.

For example, calling webd.in_js

webd.in_js(html)

with html having the value

<!doctype html>
<html>
  <head>
    <script src="demo_in_js.js" defer></script>
  </head>
  <body>
    <script src="demo_in_js.js"></script>
  </body>
</html>

and demo_in_js.js containing the following JavaScript

var height; height = 2; console.log(height-1);

returns the following HTML with JavaScript inlined

<!doctype html>
<html>
  <head></head>
  <body>
    <script>var height; height=2; console.log(height-1);</script>
    <script>var height; height=2; console.log(height-1);</script>
  </body>
</html>

webd.in_css(html)


Inserts all css from links to external files into one style tag that is inserted right before the closing head tag. Removes the links referencing CSS files. Cleans up the HTML using webd.formatter.

Running the following

webd.in_css(html)

with the value of html being

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="demo_in_css.css">
  </head>
  <body>
    <link rel="stylesheet" href="demo_in_css.css">
  </body>
</html>

and demo_in_css.css containing the following CSS

body {color: black;}

Returns the following HTML with internalized CSS

<!doctype html>
<html>
  <head>
    <style>body {color: black;}body {color: black;}</style>
  </head>
  <body></body>
</html>

webd.minify(html)


Minifies HTML or CSS. Automatically detects which is present. If HTML, the CSS in any style tags is also minified.

In the case where HTML is to be minified, running

webd.minify(html)

with html as

<!doctype html>
<html>
  <head></head>
  <body></body>
</html> 

returns the following

<!doctype html><html><head></head><body></body></html>

webd.opt(html)


Applies the functions formatter, unloc, in_js, in_css, and minify to HTML in that order. Useful for deploying HTML.

For example, running

webd.opt(html)

with html being the following string

<!doctype html>
<html>
  <head>
    <script src="demo_in_js.js" defer></script>
  </head>
  <body>
    <link rel="stylesheet" href="demo_in_css.css">
  </body>
</html>

yields the following output

<!doctype html><html><head><style>body{color:black;}</style></head><body><script>var height; height = 2; console.log(height-1);</script></body></html>

File-Level Functions


File-level functions have one required parameter -- file_dir -- and at least one optional parameter -- out. file_dir is the path to either the file or the directory that the function should be applied to. out specficies where the output of the function should be written to. Files are overwritten by default: the default value for out is the value of file_dir. If a directory is specified for out, the files' names are conserved when writing to that directory.

Additional parameters for file-level functions are passed to their corresponding base-level functions. For example, the following indent_file function call passes the n_spaces parameter value to the indent base-level function.

webd.indent_file('file.html', n_spaces=3)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for webd, version 0.0.6
Filename, size File type Python version Upload date Hashes
Filename, size webd-0.0.6-py3-none-any.whl (16.5 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size webd-0.0.6.tar.gz (12.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page