rust-html2text
rust-html2text copied to clipboard
Return raw text instead of formatted table
Hello. I've been using your library for my RSS reader for a long time.
It has a couple of glitches. one of which is rendering of HTML tables. For example,
let description = "<table cellpadding='10'>\n<tr>\n<td valign='top' align='center'><a href='https://blog.malwarebytes.com/cybercrime/2022/01/software-engineer-hacked-webcams-to-spy-on-girls-heres-how-to-protect-yourself/' title='Software engineer hacked webcams to spy on girls—Here's how to protect yourself'><img src='https://blog.malwarebytes.com/wp-content/uploads/2022/01/GettyImages-1199960659.jpg' border='0' width='300px' /></a></td>\n</tr>\n<tr>\n<td valign='top' align='left'>Yes, hackers can and will use your webcams against you if they see an opportunity. Don't let them.</p>\n<p>Categories: <a href=\"https://blog.malwarebytes.com/category/cybercrime/\" rel=\"category tag\">Cybercrime</a></p>\n<p>Tags: <a href=\"https://blog.malwarebytes.com/tag/andrew-shorrock/\" rel=\"tag\">Andrew Shorrock</a><a href=\"https://blog.malwarebytes.com/tag/catfishing/\" rel=\"tag\">catfishing</a><a href=\"https://blog.malwarebytes.com/tag/hacker-jailed/\" rel=\"tag\">hacker jailed</a><a href=\"https://blog.malwarebytes.com/tag/national-crime-agency/\" rel=\"tag\">National Crime Agency</a><a href=\"https://blog.malwarebytes.com/tag/nca/\" rel=\"tag\">NCA</a><a href=\"https://blog.malwarebytes.com/tag/robert-davies/\" rel=\"tag\">Robert Davies</a><a href=\"https://blog.malwarebytes.com/tag/software-engineer-hacker/\" rel=\"tag\">software engineer hacker</a><a href=\"https://blog.malwarebytes.com/tag/voyuerism/\" rel=\"tag\">voyuerism</a><a href=\"https://blog.malwarebytes.com/tag/webcam-security/\" rel=\"tag\">webcam security</a></p>\n<table width='100%'>\n<tr>\n<td align=right>\n<p><b>(<a href='https://blog.malwarebytes.com/cybercrime/2022/01/software-engineer-hacked-webcams-to-spy-on-girls-heres-how-to-protect-yourself/' title='Software engineer hacked webcams to spy on girls—Here's how to protect yourself'>Read more...</a>)</b></p>\n</td>\n</tr>\n</table>\n</td>\n</tr>\n</table>\n<p>The post <a rel=\"nofollow\" href=\"https://blog.malwarebytes.com/cybercrime/2022/01/software-engineer-hacked-webcams-to-spy-on-girls-heres-how-to-protect-yourself/\">Software engineer hacked webcams to spy on girls—Here’s how to protect yourself</a> appeared first on <a rel=\"nofollow\" href=\"https://blog.malwarebytes.com\">Malwarebytes Labs</a>.</p>\n";
eprintln!(
"raw : {:?}",
html2text::from_read(description.as_bytes(), 2000)
.trim()
.to_string(),
);
result is
raw : "────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n[][1] \n \n[1] https://blog.malwarebytes.com/cybercrime/2022/01/software-engineer-hacked-webcams-to-spy-on-girls-heres-how-to-protect-yourself/ \n────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\nYes, hackers can and will use your webcams against you if they see an opportunity. Don't let them. \n \n \nCategories: [Cybercrime][1] \n \nTags: [Andrew Shorrock][2][catfishing][3][hacker jailed][4][National Crime Agency][5][NCA][6][Robert Davies][7][software engineer hacker][8][voyuerism][9][webcam security][10] \n \n────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n([Read more...][1]) \n \n[1] https://blog.malwarebytes.com/cybercrime/2022/01/software-engineer-hacked-webcams-to-spy-on-girls-heres-how-to-protect-yourself/ \n────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n \n[1] https://blog.malwarebytes.com/category/cybercrime/ \n[2] https://blog.malwarebytes.com/tag/andrew-shorrock/ \n[3] https://blog.malwarebytes.com/tag/catfishing/ \n[4] https://blog.malwarebytes.com/tag/hacker-jailed/ \n[5] https://blog.malwarebytes.com/tag/national-crime-agency/ \n[6] https://blog.malwarebytes.com/tag/nca/ \n[7] https://blog.malwarebytes.com/tag/robert-davies/ \n[8] https://blog.malwarebytes.com/tag/software-engineer-hacker/ \n[9] https://blog.malwarebytes.com/tag/voyuerism/ \n[10] https://blog.malwarebytes.com/tag/webcam-security/ \n────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n\nThe post [Software engineer hacked webcams to spy on girls—Here’s how to protect yourself][1] appeared first on [Malwarebytes Labs][2].\n\n[1] https://blog.malwarebytes.com/cybercrime/2022/01/software-engineer-hacked-webcams-to-spy-on-girls-heres-how-to-protect-yourself/\n[2] https://blog.malwarebytes.com"
Is it possible to return just text instead of this rudimentary table?
Hi - glad it's been useful!
Is there a reason you're giving it such a large width? The table output was originally designed to display in a fixed width terminal, where the table drawing is reasonable (given the terminal limitations).
I can see a few things that could be better:
- Don't use the full width for tables if it's not needed (hasn't been an issue on 80 column terminals :-) )
- Perhaps tweak the heuristics for column widths.
- There are sometimes empty duplicate full-width lines generated.
In general I'm not sure how well just returning the contents would work in the case of tables which do have multiple columns, but I can see that if you're not interested in the fixed-width formatting that might be useful (e.g. treat each cell as a paragraph). I'd be interested to hear how you use it.
I also just noticed that the HTML in your example is broken - there's an HTML attribute which has a single-quoted attribute containing single quotes (apostrophe) in the text. That might not help (but I don't think that's the problem in this case).
Is there a reason you're giving it such a large width? The table output was originally designed to display in a fixed width terminal, where the table drawing is reasonable (given the terminal limitations)
It's not my HTML, it comes from the RSS feed.
the same issue is happening with Reddit feeds . for example, this one
if you're not interested in the fixed-width formatting that might be useful (e.g. treat each cell as a paragraph).
I think it will be a good solution since I'm sending stripped of HTML messages to telegram chats, not terminals
A mode which converts to text but doesn't try to lay out in a fixed-width column does sound like a reasonable feature which I hadn't previously considered - thanks for the suggestion/request! I'm currently busy with some other things so won't get to it for a little while (but am open to PRs and happy to help anyone who's interested in doing it in the meantime of course!).