Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recursive custom formatters #154

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ function formatAnchor(elem, fn, options) {

options.lineCharCount = storedCharCount;

return formatText({ data: result || href, trimLeadingSpace: elem.trimLeadingSpace }, options);
return Object.assign({}, exports, options.format).text({
data: result || href,
trimLeadingSpace: elem.trimLeadingSpace
}, options);
}

function formatHorizontalLine(elem, fn, options) {
Expand Down Expand Up @@ -128,7 +131,7 @@ function formatUnorderedList(elem, fn, options) {
return child.type !== 'text' || !whiteSpaceRegex.test(child.data);
});
nonWhiteSpaceChildren.forEach(function(elem) {
result += formatListItem(prefix, elem, fn, options);
result += Object.assign({}, exports, options.format).listItem(prefix, elem, fn, options);
});
return result + '\n';
}
Expand Down Expand Up @@ -163,7 +166,7 @@ function formatOrderedList(elem, fn, options) {
// Calculate the needed spacing for nice indentation.
var spacing = maxLength - index.toString().length;
var prefix = ' ' + index + '. ' + ' '.repeat(spacing);
result += formatListItem(prefix, elem, fn, options);
result += Object.assign({}, exports, options.format).listItem(prefix, elem, fn, options);
});
}
return result + '\n';
Expand Down Expand Up @@ -220,22 +223,22 @@ function formatTable(elem, fn, options) {
if (elem.type === 'tag') {
switch (elem.name.toLowerCase()) {
case 'th':
tokens = formatHeading(elem, fn, options).split('\n');
rows.push(compact(tokens));
tokens = Object.assign({}, exports, options.format).heading(elem, fn, options).split('\n');
break;

case 'td':
tokens = fn(elem.children, options).split('\n');
rows.push(compact(tokens));
// Fill colspans with empty values
if (elem.attribs && elem.attribs.colspan) {
count = elem.attribs.colspan - 1 || 0;
times(count, function() {
rows.push(['']);
});
}
break;
}
if (tokens) {
rows.push(compact(tokens));
// Fill colspans with empty values
if (elem.attribs && elem.attribs.colspan) {
count = elem.attribs.colspan - 1 || 0;
times(count, function() {
rows.push(['']);
});
}
}
}
});
rows = helper.arrayZip(rows);
Expand Down
42 changes: 42 additions & 0 deletions test/html-to-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,31 @@ describe('html-to-text', function() {
var result = htmlToText.fromString(html, { tables: true });
expect(result).to.equal(resultExpected);
});
it('does handle colspan on th elements correctly', function () {
var html = '\
<table> \
<tr> \
<th>header column 1</th> \
<th colspan="2">header columns 2 and 3</th> \
<th>header column 4</th> \
</tr> \
<tbody> \
<tr> \
<td>column 1</td> \
<td>column 2</td> \
<td>column 3</td> \
<td>column 4</td> \
</tr> \
</center> \
</tbody> \
</table> \
';
var resultExpected = ' HEADER COLUMN 1 HEADER COLUMNS 2 AND 3 HEADER COLUMN 4 \n\
column 1 column 2 column 3 column 4';
var result = htmlToText.fromString(html, { tables: true });
expect(result).to.equal(resultExpected);
});

});

describe('a', function () {
Expand Down Expand Up @@ -377,6 +402,23 @@ describe('html-to-text', function() {
});
expect(result).to.equal('====\ntest\n====');
});

it('should use custom formatting functions when nested elements are being parsed', function () {
var result = htmlToText.fromString('<ul><li>one</li><li>two</li></ul>', {
format: {
listItem: function (prefix, elem, fn, options) {
options = Object.assign({}, options);
if (options.wordwrap) {
options.wordwrap -= prefix.length;
}
var text = fn(elem.children, options);
text = text.replace(/\n/g, '\n' + ' '.repeat(prefix.length));
return prefix + text.toUpperCase() + '\n';
}
}
});
expect(result).to.equal(' * ONE\n * TWO');
});
});

describe('Base element', function () {
Expand Down
7 changes: 7 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ <h2>Pretty printed table</h2>
<thead>
<tr>
<th>Article</th>
<th colspan="2">Notes</th>
<th>Price</th>
<th>Taxes</th>
<th>Amount</th>
Expand All @@ -44,24 +45,30 @@ <h2>Pretty printed table</h2>
<span style="font-size:0.8em">Contains: 1x Product 1</span>
</p>
</td>
<td align="right" valign="top">one</td>
<td align="right" valign="top">two</td>
<td align="right" valign="top">6,99&euro;</td>
<td align="right" valign="top">7%</td>
<td align="right" valign="top">1</td>
<td align="right" valign="top">6,99€</td>
</tr>
<tr>
<td>Shipment costs</td>
<td align="right" valign="top"></td>
<td align="right" valign="top">two</td>
<td align="right">3,25€</td>
<td align="right">7%</td>
<td align="right">1</td>
<td align="right">3,25€</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="3">to pay: 10,24€</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td colspan="3">Taxes 7%: 0,72€</td>
Expand Down
12 changes: 6 additions & 6 deletions test/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ takimata sanctus est Lorem ipsum dolor sit amet.
--------------------------------------------------------------------------------

PRETTY PRINTED TABLE
ARTICLE PRICE TAXES AMOUNT TOTAL
Product 1 6,99€ 7% 1 6,99€
Contains: 1x Product 1
Shipment costs 3,25€ 7% 1 3,25€
to pay: 10,24€
Taxes 7%: 0,72€
ARTICLE NOTES PRICE TAXES AMOUNT TOTAL
Product 1 one two 6,99€ 7% 1 6,99€
Contains: 1x Product 1
Shipment costs two 3,25€ 7% 1 3,25€
to pay: 10,24€
Taxes 7%: 0,72€


--------------------------------------------------------------------------------
Expand Down