Skip to content

Commit

Permalink
Merge pull request #27 from deedy5/text
Browse files Browse the repository at this point in the history
[html2text] Response object: 1) add attribute `text_plain`, 2) rename attribute `plaintext` to `text_markdown`
  • Loading branch information
deedy5 authored Jul 23, 2024
2 parents 90562be + b184a6f commit bb48577
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
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)
}
}

0 comments on commit bb48577

Please sign in to comment.