Contents

ptrkstr/slab

Swift package to convert a HTML table into an array of dictionaries.

Example

GIVEN I have the following table <table> <tbody> <tr> <th>A </th> <th>B </th> </tr> <tr> <td>1 </td> <td>2 </td> </tr> <tr> <td>3 </td> <td>4 </td> </tr> </tbody> </table>

WHEN I use Slab

let array: [[String: String]] = try Slab().convert(htmlString)

THEN I see an array of dictionaries

[
    ["A": "1", "B": "2"],
    ["A": "3", "B": "4"]
]

Features

Colspan Support

<table> <tbody> <tr> <th>A </th> <th>B </th> </tr> <tr> <td colspan="2">1 </td> </tr> <tr> <td>3 </td> <td>4 </td> </tr> </tbody> </table>

[
    ["A": "1", "B": "1"],
    ["A": "3", "B": "4"]
]

Rowspan Support

<table> <tbody> <tr> <th>A </th> <th>B </th> </tr> <tr> <td>1 </td> <td rowspan="2">2 </td> </tr> <tr> <td>4 </td> </tr> </tbody> </table>

[
    ["A": "1", "B": "2"],
    ["A": "4", "B": "2"]
]

Linebreak Support

<table> <tbody> <tr> <th>A<br>A </th> <th>B </th> </tr> <tr> <td>1 </td> <td>2 </td> </tr> <tr> <td>3<br>3 </td> <td>4 </td> </tr> </tbody> </table>

[
    ["A\\nA": "1", "B": "2"],
    ["A\\nA": "3\\n3", "B": "2"]
]

Modification

Prior to row data being entered, you have the opportunity to modify the SwiftSoup.Element.

<table> <tbody> <tr> <th>A<sup>[1]</sup> </th> <th>B </th> </tr> <tr> <td>1 </td> <td>2<sup>[2]</sup> </td> </tr> </tbody> </table>

Configuration(modify: { element, row, column in
    try element.select("sup").remove()
    return element
})
[
    ["A": "1", "B": "2"]
]

Notes

  • Empty <td></td> is converted into an empty String

Installation

SPM

Add the following to your project:

https://github.com/ptrkstr/Slab

Package Metadata

Repository: ptrkstr/slab

Default branch: master

README: README.md