Skip to content

Commit bc68cce

Browse files
copoerjaytaylor
authored andcommitted
Added TextOnly Filter
1 parent 90c08c5 commit bc68cce

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

html2text.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Options struct {
1818
PrettyTables bool // Turns on pretty ASCII rendering for table elements.
1919
PrettyTablesOptions *PrettyTablesOptions // Configures pretty ASCII rendering for table elements.
2020
OmitLinks bool // Turns on omitting links
21+
TextOnly bool // Returns only plain text
2122
}
2223

2324
// PrettyTablesOptions overrides tablewriter behaviors
@@ -157,6 +158,9 @@ func (ctx *textifyTraverseContext) handleElement(node *html.Node) error {
157158
}
158159

159160
str := subCtx.buf.String()
161+
if ctx.options.TextOnly {
162+
return ctx.emit(str + ".\n\n")
163+
}
160164
dividerLen := 0
161165
for _, line := range strings.Split(str, "\n") {
162166
if lineLen := len([]rune(line)); lineLen-1 > dividerLen {
@@ -177,7 +181,9 @@ func (ctx *textifyTraverseContext) handleElement(node *html.Node) error {
177181

178182
case atom.Blockquote:
179183
ctx.blockquoteLevel++
180-
ctx.prefix = strings.Repeat(">", ctx.blockquoteLevel) + " "
184+
if !ctx.options.TextOnly {
185+
ctx.prefix = strings.Repeat(">", ctx.blockquoteLevel) + " "
186+
}
181187
if err := ctx.emit("\n"); err != nil {
182188
return err
183189
}
@@ -190,7 +196,9 @@ func (ctx *textifyTraverseContext) handleElement(node *html.Node) error {
190196
return err
191197
}
192198
ctx.blockquoteLevel--
193-
ctx.prefix = strings.Repeat(">", ctx.blockquoteLevel)
199+
if !ctx.options.TextOnly {
200+
ctx.prefix = strings.Repeat(">", ctx.blockquoteLevel)
201+
}
194202
if ctx.blockquoteLevel > 0 {
195203
ctx.prefix += " "
196204
}
@@ -213,8 +221,10 @@ func (ctx *textifyTraverseContext) handleElement(node *html.Node) error {
213221
return err
214222

215223
case atom.Li:
216-
if err := ctx.emit("* "); err != nil {
217-
return err
224+
if !ctx.options.TextOnly {
225+
if err := ctx.emit("* "); err != nil {
226+
return err
227+
}
218228
}
219229

220230
if err := ctx.traverseChildren(node); err != nil {
@@ -230,6 +240,9 @@ func (ctx *textifyTraverseContext) handleElement(node *html.Node) error {
230240
return err
231241
}
232242
str := subCtx.buf.String()
243+
if ctx.options.TextOnly {
244+
return ctx.emit(str + ".")
245+
}
233246
return ctx.emit("*" + str + "*")
234247

235248
case atom.A:
@@ -254,7 +267,7 @@ func (ctx *textifyTraverseContext) handleElement(node *html.Node) error {
254267
if attrVal := getAttrVal(node, "href"); attrVal != "" {
255268
attrVal = ctx.normalizeHrefLink(attrVal)
256269
// Don't print link href if it matches link element content or if the link is empty.
257-
if !ctx.options.OmitLinks && attrVal != "" && linkText != attrVal {
270+
if (!ctx.options.OmitLinks && attrVal != "" && linkText != attrVal) || !ctx.options.TextOnly {
258271
hrefLink = "( " + attrVal + " )"
259272
}
260273
}

0 commit comments

Comments
 (0)