gen_dw_format

HTML table formatting

Provides WET-related options for formatting cells in the specified rows and columns of the specified tables in an HTML document.

Click here to access the table formatter.

Inputs

The inputs are as follows:

Running the tool multiple times

If you would like to perform actions across varying rows/columns, dimensions, or tables in a single document, you can chain the actions together by using the output from one run of the tool as the input for another. For example, if you want to:

Then you could just run the tool three times, using the output of the first run as the input for the second run, and the output of the second run as the input for the third.

The tool increments the name of the output file from “formatted_tables” to “formatted_tables_0” to “formatted_tables_1” and so on, in case it needs to be run multiple times, to keep track of how many times it has been run.

Implementation details

The tables in the HTML document are stored in an array. Each table is an object containing the following properties:

This makes the overall array of tables -> rows -> cells a triple nested array, e.g. a single array might be represented as so:

Each table array accounts for rowspan and colspan. For example, suppose that in the 2nd row, (2, y), the 1st cell spans three columns, so (2, 1) contains a value, but (2, 2) and (2, 3) are just spans of (2, 1). Then, if you apply a function on the 3rd column, (x, 3), the function will see that the 3rd column in the 2nd row (2, 3) is just a placeholder, so it will NOT edit any cells in the 2nd row.

New captions are inserted immediately after the opening <table> tag where applicable.

Assumptions

This tool makes some assumptions about the HTML document’s formatting when reading the tables into an array. Some major assumptions are as follows (this is NOT a comprehensive list):

Adding actions

The process to add an action is as follows:

  1. In table_formatter.html: Add the action as an option under “Actions to perform”.
  2. In table_format_helpers.js:
    • At the bottom of the script, create a helper function for the action.
    • In format_table(), refer to the new helper function when the action is selected.
  3. In README.md (this file): If necessary, add a mention of the action to the Inputs section at the beginning of the document.

Helper functions for actions

Helper functions for each action are placed at the bottom of table_format_helpers.js.

Feel free to add more sections if an action does not neatly belong in one of the following categories.

Actions applied by cell

Most of the actions are to edit individual cells, so they are in the Functions for actions to be applied on the cell of a table in the table array section.

Each of these action functions should take in the following as input:

These action functions should return the table array given in the first input, but with the cell value changed.

Each of these functions is called using the apply_on_cells() function, which loops through each cell of each table to apply the action function on, and calls the action function on the cell.

Actions for setting thead/tbody/tfoot

These actions are in the Function for actions to be applied on thead/tbody/tfoot row index for a table in the table array section. They just set the locations of the opening and closing thead/tbody/tfoot tags relative to the table rows using the set_t_inds() function. Since doing this only needs two row indices, one for the row of the opening tag and one for the row of the closing tag, set_t_inds() does not require as many arguments as apply_on_cells().

Actions involving tags immediately outside of the table

These actions are in the Functions that involve tags outside of the table section. They require both the original html document and the table array. As such, in format_table(), the table array edits are implemented twice: once into the original html document before running these functions so that the html document is up to date, and once after running them to implement their changes.

For consistency, I have tried to format these action functions as so: