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

[html2text] Response object: 1) add attribute text_plain, 2) rename attribute plaintext to text_markdown #27

Merged
merged 4 commits into from
Jul 23, 2024
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ resp.cookies
resp.encoding
resp.headers
resp.json()
resp.plaintext # html is converted to markdown text
resp.status_code
resp.text
resp.text_markdown # html is converted to markdown text
resp.text_plain # html is converted to plain text
resp.url
```

Expand Down
13 changes: 10 additions & 3 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use encoding_rs::Encoding;
use html2text::from_read;
use html2text::{from_read, from_read_with_decorator, render::text_renderer::TrivialDecorator};
use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PyString};
Expand Down Expand Up @@ -66,9 +66,16 @@ impl Response {
}

#[getter]
fn plaintext(&mut self, py: Python) -> PyResult<String> {
fn text_markdown(&mut self, py: Python) -> PyResult<String> {
let raw_bytes = self.content.bind(py).as_bytes();
let text = from_read(raw_bytes, 1080);
let text = from_read(raw_bytes, usize::MAX);
Ok(text)
}

#[getter]
fn text_plain(&mut self, py: Python) -> PyResult<String> {
let raw_bytes = self.content.bind(py).as_bytes();
let text = from_read_with_decorator(raw_bytes, usize::MAX, TrivialDecorator::new());
Ok(text)
}
}