Skip to content

Commit a63bcbf

Browse files
author
Malte Legenhausen
committed
hr support added
1 parent fb29815 commit a63bcbf

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/html-to-text.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ var _s = require('underscore.string');
66
var htmlparser = require("htmlparser");
77

88
function htmlToText(html, options) {
9+
options = options || {};
10+
_.defaults(options, {
11+
wordwrap: 80,
12+
tables: []
13+
});
14+
915
var handler = new htmlparser.DefaultHandler(function (error, dom) {
1016

1117
}, {
1218
verbose: true,
1319
ignoreWhitespace: true
1420
});
15-
var parser = new htmlparser.Parser(handler);
16-
parser.parseComplete(html);
21+
new htmlparser.Parser(handler).parseComplete(html);
22+
1723
var result = buildText(filterBody(handler.dom), options);
1824
return _s.strip(result);
1925
}
@@ -89,6 +95,10 @@ function formatAnchor(elem, fn) {
8995
return elem.attribs.href;
9096
}
9197

98+
function formatHorizontalLine(elem, fn, options) {
99+
return '\n' + _s.repeat('-', options.wordwrap || 80) + '\n';
100+
}
101+
92102
function tableToString(table) {
93103
// Determine space width per column
94104
// Convert all rows to lengths
@@ -153,8 +163,6 @@ function formatTable(elem, fn) {
153163
}
154164

155165
function buildText(dom, options) {
156-
options = options || {};
157-
var tables = options.tables || [];
158166
function walk(dom) {
159167
var result = '';
160168
_.each(dom, function(elem) {
@@ -176,8 +184,11 @@ function buildText(dom, options) {
176184
case 'br':
177185
result += formatBreak(elem, walk);
178186
break;
187+
case 'hr':
188+
result += formatHorizontalLine(elem, walk, options);
189+
break;
179190
case 'table':
180-
if (elem.attribs && elem.attribs.class && _.include(tables, elem.attribs.class)) {
191+
if (elem.attribs && elem.attribs.class && _.include(options.tables, elem.attribs.class)) {
181192
result += formatTable(elem, walk);
182193
break;
183194
}

0 commit comments

Comments
 (0)