Skip to main content

Parser

Struct Parser 

Source
#[non_exhaustive]
pub struct Parser { pub max_elems_to_parse: usize, pub n_top_candidates: usize, pub char_threshold: usize, pub classes_to_preserve: Vec<String>, pub keep_classes: bool, pub tags_to_score: Vec<String>, pub disable_jsonld: bool, pub allowed_video_regex: Option<Regex>, /* private fields */ }
Expand description

Port of Parser — the core readability extraction engine.

Create with Parser::new(), configure public fields as needed, then call parse() to extract an article.

A single Parser can be reused for multiple documents — internal state is fully reset at the start of each parse call. However, Parser is not thread-safe: it requires &mut self for parsing, so it cannot be shared across threads without external synchronization.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§max_elems_to_parse: usize

Max DOM nodes to process. 0 = unlimited. Port of MaxElemsToParse.

§n_top_candidates: usize

Number of top candidates to compare during scoring. Port of NTopCandidates.

§char_threshold: usize

Minimum character count for accepted article content. Port of CharThreshold.

§classes_to_preserve: Vec<String>

CSS class names to preserve when keep_classes is false. Port of ClassesToPreserve.

§keep_classes: bool

If true, keep all class attributes. Port of KeepClasses.

§tags_to_score: Vec<String>

Tag names eligible for content scoring. Port of TagsToScore.

§disable_jsonld: bool

Disable JSON-LD metadata extraction. Port of DisableJSONLD.

§allowed_video_regex: Option<Regex>

Optional regex for video URLs to allow. Port of AllowedVideoRegex.

Implementations§

Source§

impl Parser

Source

pub fn parse( &mut self, html: &str, page_url: Option<&Url>, ) -> Result<Article, Error>

Port of Parse — parse an HTML string and return the article.

Source

pub fn check_html(&self, html: &str) -> bool

Convenience wrapper: parse html and check readability without a pre-parsed Document.

Equivalent to CheckDocument(html.Parse(html)) in Go tests.

Source§

impl Parser

Source

pub fn new() -> Self

Port of NewParser — construct a parser with default settings.

Source

pub fn with_max_elems_to_parse(self, n: usize) -> Self

Set the maximum number of DOM elements to parse. 0 = unlimited.

Source

pub fn with_n_top_candidates(self, n: usize) -> Self

Set the number of top candidates to compare during scoring.

Source

pub fn with_char_threshold(self, n: usize) -> Self

Set the minimum character count for accepted article content.

Source

pub fn with_classes_to_preserve( self, classes: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Set CSS class names to preserve when keep_classes is false.

Source

pub fn with_keep_classes(self, keep: bool) -> Self

If true, keep all class attributes on extracted content.

Source

pub fn with_tags_to_score( self, tags: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Set tag names eligible for content scoring.

Source

pub fn with_disable_jsonld(self, disable: bool) -> Self

Disable JSON-LD metadata extraction.

Source

pub fn with_allowed_video_regex(self, re: Regex) -> Self

Set a regex for video URLs to allow during cleaning.

Trait Implementations§

Source§

impl Default for Parser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Parser

§

impl !RefUnwindSafe for Parser

§

impl !Send for Parser

§

impl !Sync for Parser

§

impl Unpin for Parser

§

impl UnsafeUnpin for Parser

§

impl UnwindSafe for Parser

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.