JSFiddle - React, Tailwind, and code Playground
Research outcomes extractor
by timmah
HTML
<table class="tbl-row-bdr margintop" id="table_of_pubs">
<thead>
<tr>
<th class="bg-uni25">Author(s)</th>
<th class="bg-uni25">Year</th>
<th class="bg-uni25">Title</th>
<th class="bg-uni25 tbl20">Type</th>
</tr>
</thead>
<tbody>
<tr>
<td class="padright padbottom nowrap">Adcock, Fleur</td>
<td class="padright"><em>forth-<br>
coming</em></td>
<td class="padright">Leveraging the UN Special Procedures to implement the UN Declaration on the Rights of Indigenous peoples.
<p class="padtop padbottom"> <a id="descLink1931">» description</a> <a id="docLink1931" class="nounderline docLink">» <span class="small">(unavailable)</span></a> </p>
<div class="description" id="descr1931">
<p class="marginbottom"></p>
</div>
<p class="padbottom"><a id="citLink1931">» citation</a></p>
<div class="citation" id="cit1931">
<p>Adcock, Fleur, 'Leveraging the UN Special Procedures to implement the UN Declaration on the Rights of Indigenous peoples', in: A. Erueti (ed.), <em>Aotearoa and Implementation of the UN Declaration on the Rights of Indigenous Peoples – Theory and Practice</em>,
Victoria University Press, Wellington (forthcoming) pp:xxx.</p>
</div>
</td>
<td>Book chapter</td>
</tr>
<tr>
<td class="padright padbottom nowrap">Mitchell, Myles</td>
<td class="padright"><em>forth-<br>
coming</em></td>
<td class="padright">Stone arrangements as symbols: a theoretical and methodological approach in Esperance Nyungar country, Western Australia.
<p class="padtop padbottom"> <a id="descLink3063">» description</a> <a id="docLink3063" class="nounderline docLink"...
CSS
table {
padding: 0;
}
th {
background: #eaeaea;
}
tr {}
td {
border: solid 1px grey;
}
JavaScript
/* This extracts the data from each table cell of the Research outcomes database,
* and restructures it into a more user-friendly CSV format for importing into
* Drupal via Feed importers
*
*/
// Create a container to hold the final file
var csvArray = [["authors", "year", "title", "description", "citation", "link", "linktext", "research_type", "id"]];
console.clear();
//console.log(csv);
$('table tbody tr').each(function() {
// Define all cells
var $this = $(this),
$cell1 = $this.children('td:nth-child(1)'),
$cell2 = $this.children('td:nth-child(2)'),
$cell3 = $this.children('td:nth-child(3)'),
$cell4 = $this.children('td:nth-child(4)');
// For each row, create containers to hold each field of data
var $container = [],
$authors = [],
$year = '',
$title = '',
$desc = $cell3.find('.description').html().trim().replace('\n', '').replace(/\s+/g, ' '),
$cite = $cell3.find('.citation').html().trim().replace('\n', '').replace(/\s+/g, ' '),
$link = $cell3.find('a[href][id^="docLink"]').attr('href') || '',
$linkText = $cell3.find('a[href][id^="docLink"]').text(),
$type = $cell4.text().trim() || '',
$id = $cell3.find('.description').attr('id').replace('descr', '');
// Crudely strip the description if the container is empty
if ($desc === '<p class="marginbottom"></p>') {
$desc = '';
}
// For each cell extract the data and place it in the relevant container
function getTitle() {
$cell3.remove('*');
$title = $cell3.text().split('\n');
$title = $title[0];
}
function getAuthor() {
$cell1.contents().filter('br').remove();
var arr = $cell1.text().split('\n'),
authors = [];
for (var i = 0; i < arr.length; i++) {
var e = arr[i];
...