### IndexMap Struct Definition and Example - Rust Source: https://docs.rs/parsoid/latest/parsoid/map/struct Defines the IndexMap struct, a hash table with independent iteration order, compatible with HashMap but with added features for order and indexing. Includes an example demonstrating its use for counting letter frequencies in a sentence. ```rust pub struct IndexMap { /* private fields */ } use indexmap::IndexMap; // count the frequency of each letter in a sentence. let mut letters = IndexMap::new(); for ch in "a short treatise on fungi".chars() { *letters.entry(ch).or_insert(0) += 1; } assert_eq!(letters[&'s'], 2); assert_eq!(letters[&'t'], 3); assert_eq!(letters[&'u'], 1); assert_eq!(letters.get(&'y'), None); ``` -------------------------------- ### Rust: Creating a Simple MediaWiki Template (No Parameters) Source: https://docs.rs/parsoid/latest/src/parsoid/template Provides an example of creating a MediaWiki template without any parameters using the `new_simple` function in parsoid-rs. This is a simplified constructor for templates that do not require arguments. ```rust pub fn new_simple(name: &str) -> Self { match Self::new(name, &IndexMap::new()) { Ok(temp) => temp, Err(e) => { // The only error condition currently reachable is if the parameters // can't be JSON serialized, which doesn't make sense for an empty map unreachable!( "Template::new_simple() errored with {}", e.to_string() ); } } } ``` -------------------------------- ### Import Parsoid Prelude in Rust Source: https://docs.rs/parsoid/latest/parsoid/prelude/index This snippet shows how to import the necessary traits and useful types from the parsoid crate's prelude. It's a common starting point for using the library's core functionalities. ```rust use parsoid::prelude.*; ``` -------------------------------- ### Add Link and Convert to Wikitext in Rust Source: https://docs.rs/parsoid/latest/index This example shows how to add a wiki link to a page's content and then convert the modified content back into wikitext format. It utilizes the 'parsoid' crate and requires asynchronous operations. ```rust use parsoid::prelude::*; let client = ParsoidClient::new("https://test.wikipedia.org/w/rest.php", "parsoid-rs demo")?; let code = client.get("Wikipedia:Sandbox").await?.into_mutable(); let link = WikiLink::new( "./Special:Random", &Wikicode::new_text("Visit a random page") ); code.append(&link); let wikitext = client.transform_to_wikitext(&code).await?; assert!(wikitext.ends_with("[[Special:Random|Visit a random page]]")); ``` -------------------------------- ### Rust: Getting All Template Parameters Source: https://docs.rs/parsoid/latest/parsoid/prelude/struct Details a Rust method that returns all parameters of a MediaWiki template, including both named and unnamed parameters, as a map. ```rust pub fn params(&self) -> IndexMap ``` -------------------------------- ### Rust Result Product Example Source: https://docs.rs/parsoid/latest/parsoid/type Demonstrates using the `product` method on an iterator of `Result`s. If any element fails to parse into the target type, the entire operation returns an `Err`. Otherwise, it returns the product of all successfully parsed elements. ```rust let nums = vec!["5", "10", "1", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert_eq!(total, Ok(100)); let nums = vec!["5", "10", "one", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert!(total.is_err()); ``` -------------------------------- ### Rust WikinodeIterator::filter_external_links Method Source: https://docs.rs/parsoid/latest/parsoid/trait Filters and returns a vector of all external links (e.g., `[https://example.org/ Example]`) from the current `WikinodeIterator` context in Rust. ```rust fn filter_external_links(&self) -> Vec { ... } ``` -------------------------------- ### Rust: Get Entry for Key (entry) Source: https://docs.rs/parsoid/latest/parsoid/map/struct Retrieves an `Entry` for a given key, allowing for in-place manipulation or insertion. Computes in amortized average O(1) time. ```Rust pub fn entry(&mut self, key: K) -> Entry<'_, K, V> ``` -------------------------------- ### Rust WikinodeIterator::filter_comments Method Source: https://docs.rs/parsoid/latest/parsoid/trait Filters and returns a vector of all HTML comments (e.g., ``) from the current `WikinodeIterator` context in Rust. ```rust fn filter_comments(&self) -> Vec { ... } ``` -------------------------------- ### Rust Wikicode: Get Redirect Information Source: https://docs.rs/parsoid/latest/parsoid/struct Retrieves redirect information for the Parsoid HTML document. Returns an `Option`, where `Redirect` is likely a struct containing details about a redirect. ```rust pub fn redirect(&self) -> Option ``` -------------------------------- ### Accessing MediaWiki Template Data with Rust Source: https://docs.rs/parsoid/latest/src/parsoid/template Demonstrates how to retrieve and inspect data from an existing MediaWiki template using the parsoid-rs library. It shows how to get the template's name, raw name, name in wikitext, and specific parameters. ```rust # use parsoid::Result; # use parsoid::prelude::*; # #[tokio::main] # async fn main() -> Result<()> { # let client = ParsoidClient::new("https://www.mediawiki.org/w/rest.php","parsoid-rs testing").unwrap(); let code = client.transform_to_html("{{1x|test}}").await?.into_mutable(); // Get the `Template` instance let template = code.filter_templates()?[0].clone(); assert_eq!(template.name(), "Template:1x".to_string()); assert_eq!(template.raw_name(), "./Template:1x".to_string()); assert_eq!(template.name_in_wikitext(), "1x".to_string()); assert_eq!(template.param("1"), Some("test".to_string())); # Ok(()) # } ``` -------------------------------- ### Rust: Get All Template Parameters Source: https://docs.rs/parsoid/latest/src/parsoid/template Retrieves all parameters of a wikitext template as an `IndexMap`, where keys are parameter names (or indices for unnamed parameters) and values are their wikitext representations. It converts the internal parameter representation to strings. ```rust /// Get a map of all parameters, named and unnamed pub fn params(&self) -> IndexMap { self.inner() .params .into_iter() .map(|(param, val)| (param, val.wt)) .collect() } ``` -------------------------------- ### Rust: Getting Parameter Name as in Wikitext Source: https://docs.rs/parsoid/latest/parsoid/prelude/struct Defines a Rust method to get the exact name of a parameter as it appears in the wikitext source, which might include comments or variations. For example, `paramname`. ```rust pub fn param_in_wikitext(&self, name: &str) -> Option ``` -------------------------------- ### Parsoid API Client Initialization Source: https://docs.rs/parsoid/latest/src/parsoid/api Demonstrates how to create a new instance of the Parsoid API client. The `base_url` should point to a MediaWiki REST API endpoint (e.g., `rest.php` or RESTBase), and `user_agent` is required for identification. ```APIDOC ## Client Initialization ### Description Creates a new Parsoid API client. ### Method `Client::new(base_url: &str, user_agent: &str) -> Result` ### Parameters * **base_url** (string) - Required - The base URL of the Parsoid API endpoint. This can be a MediaWiki core REST API (`https://wiki.example.org/w/rest.php`), a Parsoid extension endpoint (`https://wiki.example.org/w/rest.php/wiki.example.org/v3`), or a deprecated RESTBase API (`https://en.wikipedia.org/api/rest_v1`). Do not include a trailing slash. * **user_agent** (string) - Required - A string identifying the client application, used in the User-Agent header. ### Returns A `Result` containing a new `Client` instance on success, or an `Error` on failure. ``` -------------------------------- ### Get Section from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Attempts to get a clone of a `Section` node. Returns `Some(Section)` if the node is a section header, otherwise `None`. This is for parsing document structure. ```rust pub fn as_section(&self) -> Option
{ match self { Self::Section(section) => Some(section.clone()), _ => None, } } ``` -------------------------------- ### Get Reference from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Attempts to get a clone of a `Reference` node. Returns `Some(Reference)` if the node is a reference, otherwise `None`. This is for processing inline citations and footnotes. ```rust pub fn as_reference(&self) -> Option { match self { Self::Reference(reference) => Some(reference.clone()), _ => None, } } ``` -------------------------------- ### Initialize Parsoid Client with Reqwest Client (Non-Wasm32) Source: https://docs.rs/parsoid/latest/src/parsoid/api Constructs a Parsoid client using a provided `reqwest::Client`. This version is for non-`wasm32` targets and directly sets the user agent using `http.user_agent(ua)`. It then builds the HTTP client and creates a new `Client` instance. ```rust let ua = format!("parsoid-rs/{VERSION} {user_agent}"); #[cfg(not(target_arch = "wasm32"))] { http = http.user_agent(ua); } Self::new_with_client(base_url, http.build()?) ``` -------------------------------- ### Get Nowiki from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Attempts to get a clone of a `Nowiki` node. Returns `Some(Nowiki)` if the node is a nowiki tag, otherwise `None`. This is for preserving literal text. ```rust pub fn as_nowiki(&self) -> Option { match self { Self::Nowiki(nowiki) => Some(nowiki.clone()), _ => None, } } ``` -------------------------------- ### Initialize Parsoid Client with Reqwest Client (Wasm32) Source: https://docs.rs/parsoid/latest/src/parsoid/api Constructs a Parsoid client using a provided `reqwest::Client`. This version is specifically for `wasm32` targets, setting default headers including 'Api-User-Agent'. It then builds the HTTP client and creates a new `Client` instance. ```rust let ua = format!("parsoid-rs/{VERSION} {user_agent}"); #[cfg(target_arch = "wasm32")] { let mut headers = header::HeaderMap::new(); headers .insert("Api-User-Agent", header::HeaderValue::from_str(&ua)?); http = http.default_headers(headers); } Self::new_with_client(base_url, http.build()?) ``` -------------------------------- ### Get Indicator from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Attempts to get a clone of an `Indicator` node. Returns `Some(Indicator)` if the node is an indicator, otherwise `None`. Used for processing specific wikicode indicators. ```rust pub fn as_indicator(&self) -> Option { match self { Self::Indicator(indicator) => Some(indicator.clone()), _ => None, } } ``` -------------------------------- ### parsoid: ReferenceList Methods for Reference Management Source: https://docs.rs/parsoid/latest/parsoid/cite/struct Provides methods to interact with individual references within a ReferenceList. These include getting all references, finding a specific reference by ID, and managing grouping properties. ```rust pub fn references(&self) -> Vec pub fn find(&self, id: &str) -> Option pub fn group(&self) -> Result> pub fn set_group(&self, group: String) -> Result<()> pub fn remove_group(&self) -> Result<() pub fn is_auto_generated(&self) -> Result ``` -------------------------------- ### Get HtmlEntity from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Attempts to get a clone of an `HtmlEntity` node. Returns `Some(HtmlEntity)` if the node represents an HTML entity, otherwise `None`. Useful for resolving character entities. ```rust pub fn as_html_entity(&self) -> Option { match self { Self::HtmlEntity(entity) => Some(entity.clone()), _ => None, } } ``` -------------------------------- ### Add Link and Convert to Wikitext (Rust) Source: https://docs.rs/parsoid/latest/parsoid/index Fetches a Wikipedia sandbox page, adds a new wiki link to it, and then converts the modified content back to wikitext. This demonstrates adding and transforming wiki markup. Requires an async runtime. ```rust use parsoid::prelude::*; async fn add_link_and_transform() -> Result<(), Box> { let client = ParsoidClient::new("https://test.wikipedia.org/w/rest.php", "parsoid-rs demo")?; let mut code = client.get("Wikipedia:Sandbox").await?.into_mutable(); let link = WikiLink::new( "./Special:Random", &Wikicode::new_text("Visit a random page") ); code.append(&link); let wikitext = client.transform_to_wikitext(&code).await?; assert!(wikitext.ends_with("[[Special:Random|Visit a random page]]")); Ok(()) } ``` -------------------------------- ### Creating and Inserting a New MediaWiki Template with Rust Source: https://docs.rs/parsoid/latest/src/parsoid/template Illustrates the process of programmatically creating a new MediaWiki template with parameters and inserting it into existing wikitext using parsoid-rs. It covers parameter preparation and conversion to wikitext. ```rust # use parsoid::Result; # use parsoid::prelude::*; # #[tokio::main] # async fn main() -> Result<()> { # let client = ParsoidClient::new("https://www.mediawiki.org/w/rest.php","parsoid-rs testing").unwrap(); let mut params = map::IndexMap::new(); params.insert("1".to_string(), "test".to_string()); let template = Template::new("1x", ¶ms)?; let code = Wikicode::new(""); code.append(&template); let wikitext = client.transform_to_wikitext(&code).await?; assert_eq!(wikitext, "{{1x|test}}".to_string()); # Ok(()) # } ``` -------------------------------- ### Rust: Getting Raw Template Name Source: https://docs.rs/parsoid/latest/parsoid/prelude/struct Defines a Rust function to get the full, raw name of a MediaWiki template, such as './Template:Foo_bar', or the name of a parser function like 'ifeq'. ```rust pub fn raw_name(&self) -> String ``` -------------------------------- ### Rust: Programmatically Constructing a MediaWiki Template Source: https://docs.rs/parsoid/latest/src/parsoid/template Details the process of programmatically creating a new MediaWiki template using the `Template::new` function. It shows how to define template parameters and serialize them into the required `data-mw` attribute for HTML. Dependencies: `serde_json`, `kuchikikiki`. ```rust pub fn new(name: &str, params: &IndexMap) -> Result { let params = params .iter() .map(|(key, val)| (key.to_string(), Param::new(val))) .collect(); let transclusion = Transclusion { parts: vec![TransclusionPart::Template { template: TransclusionTemplate { target: TransclusionTarget::new(name), params, i: 0, }, }], errors: None, }; let element = NodeRef::new_element( crate::build_qual_name(local_name!("span")), vec![ ( ExpandedName::new(ns!(), "typeof"), Attribute { prefix: None, value: Self::TYPEOF.to_string(), }, ), ( ExpandedName::new(ns!(), "data-mw"), Attribute { prefix: None, value: serde_json::to_string(&transclusion)?, }, ), ], ); Ok(Self { element, part: 0, siblings: vec![], }) } ``` -------------------------------- ### Get ExtLink from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Attempts to get a clone of an external link node. Returns `Some(ExtLink)` if the node is an external link, otherwise `None`. This is used for processing links to external resources. ```rust pub fn as_extlink(&self) -> Option { match self { Self::ExtLink(extlink) => Some(extlink.clone()), _ => None, } } ``` -------------------------------- ### Get Default Headers for HTTP Requests Source: https://docs.rs/parsoid/latest/src/parsoid/api Provides a clone of a static `HeaderMap` containing default headers, primarily the 'Accept' header set to 'ACCEPT_2_8_0'. This ensures consistent headers across requests without re-initializing the map. ```rust fn default_headers(&self) -> HeaderMap { lazy_static! { static ref HEADERMAP: HeaderMap = { let mut headers = header::HeaderMap::new(); headers.insert( header::ACCEPT, ACCEPT_2_8_0 .parse() .expect("Unable to parse Accept header"), ); headers }; } (*HEADERMAP).clone() } ``` -------------------------------- ### Rust: Get Inner Transclusion Template Source: https://docs.rs/parsoid/latest/src/parsoid/template Extracts and returns the `TransclusionTemplate` from the current transclusion part. It performs a type check to ensure the part is indeed a template, otherwise, it panics. This is used to get mutable access to template data. ```rust fn inner(&self) -> TransclusionTemplate { let transclusion = self.transclusion(); if let TransclusionPart::Template { template: temp } = &transclusion.parts[self.part] { temp.clone() } else { unreachable!("Template part {} is the wrong type", self.part) } } ``` -------------------------------- ### Rust: Creating a Template with Parameters Source: https://docs.rs/parsoid/latest/parsoid/prelude/struct Shows the Rust function signature for creating a MediaWiki template with specified parameters. It takes the template name and a map of parameter names to their values. ```rust pub fn new(name: &str, params: &IndexMap) -> Result ``` -------------------------------- ### Creating and Inserting MediaWiki Templates in Rust Source: https://docs.rs/parsoid/latest/parsoid/prelude/struct Illustrates the process of constructing a new MediaWiki template with parameters and inserting it into a Wikicode object. The resulting Wikitext is then generated using the parsoid crate. ```rust let mut params = map::IndexMap::new(); params.insert("1".to_string(), "test".to_string()); let template = Template::new("1x", ¶ms)?; let code = Wikicode::new(""); code.append(&template); let wikitext = client.transform_to_wikitext(&code).await?; assert_eq!(wikitext, "{{1x|test}}".to_string()); ``` -------------------------------- ### Rust Wikicode: Traversal Iterators Source: https://docs.rs/parsoid/latest/parsoid/struct Offers methods for iterating over the entire subtree, including the node itself and its descendants. `traverse_inclusive` includes the start and end edges of the node and its descendants. `traverse` includes only the start and end edges of the descendants. ```rust pub fn traverse_inclusive(&self) -> Traverse pub fn traverse(&self) -> Traverse ``` -------------------------------- ### Rust Result Sum Example Source: https://docs.rs/parsoid/latest/parsoid/type Illustrates the `sum` method for iterators yielding `Result`s. The summation proceeds until an `Err` is encountered, at which point the `Err` is returned. If all elements are `Ok`, their sum is returned. This example shows filtering for negative numbers. ```rust let f = |&x: &i32| if x < 0 { Err("Negative element found") } else { Ok(x) }; let v = vec![1, 2]; let res: Result = v.iter().map(f).sum(); assert_eq!(res, Ok(3)); let v = vec![1, -2]; let res: Result = v.iter().map(f).sum(); assert_eq!(res, Err("Negative element found")); ``` -------------------------------- ### IndexMap Creation Methods - Rust Source: https://docs.rs/parsoid/latest/parsoid/map/struct Provides methods for creating new IndexMap instances. Includes options for creating an empty map, a map with a specified initial capacity, and maps with custom hash builders, all while managing allocation and computational complexity. ```rust pub fn new() -> IndexMap pub fn with_capacity(n: usize) -> IndexMap pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> IndexMap pub const fn with_hasher(hash_builder: S) -> IndexMap ``` -------------------------------- ### Filter Wiki Onlyinclude Tags in Rust Source: https://docs.rs/parsoid/latest/src/parsoid/iter Extracts content within `` tags from the Parsoid document. Similar to `filter_noinclude`, it uses 'typeof' attributes on meta elements to detect the start and end of the tag, collecting the enclosed content. This function assumes the presence of corresponding start and end markers. ```Rust fn filter_onlyinclude(&self) -> Vec { let mut start: Option = None; let mut inner = vec![]; let mut nodes = vec![]; for node in self.inclusive_descendants() { if let Wikinode::Generic(node) = &node { if let Some(element) = node.as_element() { if element.name.local == local_name!("meta") { if let Some(typeof_) = element.attributes.borrow().get("typeof") { if attribute_contains_word(typeof_, OnlyInclude::TYPEOF_START) { start = Some(node.as_node().clone()); inner = vec![]; continue; } else if attribute_contains_word(typeof_, OnlyInclude::TYPEOF_END) { // TODO: Implement logic to create OnlyInclude object similar to filter_noinclude // This part of the code is incomplete in the provided snippet. continue; } } } } } // Add logic here to collect inner content if start is Some } nodes } ``` -------------------------------- ### IndexMap TypeId Source: https://docs.rs/parsoid/latest/parsoid/map/struct Provides a method to get the TypeId of an IndexMap, as part of the Any trait implementation. ```Rust impl Any for T where T: 'static + ?Sized, fn type_id(&self) -> TypeId ``` -------------------------------- ### Add Link and Transform to Wikitext with ParsoidClient Source: https://docs.rs/parsoid/latest/src/parsoid/lib Shows how to use ParsoidClient to fetch a page, add a new wiki link to its content, and then transform the modified content back into wikitext format. This involves creating a WikiLink object and using the transform_to_wikitext method. Requires 'parsoid' and 'tokio'. ```rust use parsoid::Result; use parsoid::prelude::*; #[tokio::main] async fn main() -> Result<()> { let client = ParsoidClient::new("https://test.wikipedia.org/w/rest.php", "parsoid-rs demo")?; let code = client.get("Wikipedia:Sandbox").await?.into_mutable(); let link = WikiLink::new( "./Special:Random", &Wikicode::new_text("Visit a random page") ); code.append(&link); let wikitext = client.transform_to_wikitext(&code).await?; assert!(wikitext.ends_with("[[Special:Random|Visit a random page]]")); Ok(()) } ``` -------------------------------- ### Rust WikiLink Constructor and Getters Source: https://docs.rs/parsoid/latest/parsoid/node/struct Provides functions for creating and retrieving information from a WikiLink. It includes methods to create a new link, get its raw target (prefixed with './'), and retrieve the link's display target (page title). ```rust pub fn new(target: &str, text: &NodeRef) -> Self pub fn raw_target(&self) -> String pub fn target(&self) -> String ``` -------------------------------- ### First and Last Element Access Source: https://docs.rs/parsoid/latest/parsoid/map/struct Methods to get the first and last key-value pairs or entries from the map. ```APIDOC ## first ### Description Get the first key-value pair. Computes in O(1) time. ### Method N/A (Method of a struct) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) N/A (Returns Option<(&K, &V)>) #### Response Example None ## first_mut ### Description Get the first key-value pair, with mutable access to the value. Computes in O(1) time. ### Method N/A (Method of a struct) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) N/A (Returns Option<(&K, &mut V)>) #### Response Example None ## first_entry ### Description Get the first entry in the map for in-place manipulation. Computes in O(1) time. ### Method N/A (Method of a struct) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) N/A (Returns Option>) #### Response Example None ## last ### Description Get the last key-value pair. Computes in O(1) time. ### Method N/A (Method of a struct) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) N/A (Returns Option<(&K, &V)>) #### Response Example None ## last_mut ### Description Get the last key-value pair, with mutable access to the value. Computes in O(1) time. ### Method N/A (Method of a struct) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) N/A (Returns Option<(&K, &mut V)>) #### Response Example None ``` -------------------------------- ### Rust: Get Template Name in Wikitext Source: https://docs.rs/parsoid/latest/src/parsoid/template Returns the name of the template as it appears directly in wikitext. This is accessed from the inner `TransclusionTemplate` structure. ```rust /// Get the name of the template as it appears in wikitext pub fn name_in_wikitext(&self) -> String { self.inner().target.wt } ``` -------------------------------- ### Construct Full Link - Rust Source: https://docs.rs/parsoid/latest/src/parsoid/lib Constructs a full link string by replacing spaces with underscores and ensuring the string is URL-decoded. It then formats the link with a leading './'. ```rust fn full_link(title: &str) -> String { let title = title.replace(' ', "_"); let title = urlencoding::decode(&title) .map(|s| s.to_string()) .unwrap_or(title); format!("./{{title}}") } ``` -------------------------------- ### Rust: Creating a Simple Template Source: https://docs.rs/parsoid/latest/parsoid/prelude/struct Provides a Rust function signature for creating a new MediaWiki template that has no parameters. This is a convenience function for templates with only a name. ```rust pub fn new_simple(name: &str) -> Self ``` -------------------------------- ### Rust Wikicode: Get Title Source: https://docs.rs/parsoid/latest/parsoid/struct Retrieves the title associated with the Parsoid HTML document, if available. Returns an `Option`. ```rust pub fn title(&self) -> Option ``` -------------------------------- ### Get Last Entry in IndexMap Source: https://docs.rs/parsoid/latest/parsoid/map/struct Retrieves the last entry in the IndexMap for in-place manipulation. This operation has a time complexity of O(1). ```rust pub fn last_entry(&mut self) -> Option> ``` -------------------------------- ### Create New Parsoid Client with Existing Reqwest Client Source: https://docs.rs/parsoid/latest/src/parsoid/api Creates a new `Client` instance using an existing `reqwest::Client`. It handles different base URL formats to detect the Parsoid endpoint kind (Core, Extension, RESTBase) and adjusts the base URL if necessary, especially for converting RESTBase to the Core REST API format. ```rust pub fn new_with_client(base_url: &str, http: HttpClient) -> Result { #[cfg(not(feature = "restbase"))] let base_url = if base_url.contains("/api/rest_v1") { // convert Wikimedia RESTBase API to Core REST API base_url.replace("/api/rest_v1", "/w/rest.php") } else { base_url.to_string() }; #[cfg(feature = "restbase")] let base_url = base_url.to_string(); let endpoint_kind = if base_url.contains("/api/rest_v1") { #[cfg(not(feature = "restbase"))] unreachable!(); #[cfg(feature = "restbase")] { warn!("RESTBase is deprecated, Use rest.php instead."); ParsoidEndpointKind::RESTBase } } else if base_url.contains("/rest.php") && base_url.ends_with("v3") { ParsoidEndpointKind::ParsoidExtension } else if base_url.contains("/rest.php") { ParsoidEndpointKind::Core } else { return Err(Error::UnrecognizedParsoidEndpoint()); }; Ok(Client { http, base_url, endpoint_kind, retry_limit: 10, semaphore: Arc::new(Semaphore::new(10)), }) } ``` -------------------------------- ### Rust Wikicode: Get Revision ID Source: https://docs.rs/parsoid/latest/parsoid/struct Retrieves the revision ID associated with the Parsoid HTML, if it exists. Returns an `Option`. ```rust pub fn revision_id(&self) -> Option ``` -------------------------------- ### Content Negotiation Source: https://docs.rs/parsoid/latest/src/parsoid/api Details the `Accept` header used for content negotiation, specifically for requesting HTML with a specific profile version. ```APIDOC ## Content Negotiation ### Description Parsoid API supports content negotiation to request specific versions of HTML output. ### Header * **Accept**: `text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.8.0"` This header is used to request HTML conforming to the 2.8.0 specification. ``` -------------------------------- ### Fetch and Extract Template Parameter with ParsoidClient Source: https://docs.rs/parsoid/latest/src/parsoid/lib Demonstrates fetching a Wikipedia page using ParsoidClient, converting it to mutable Wikicode, and iterating through templates to extract a specific parameter ('birth_name') from an infobox. Requires the 'parsoid' and 'tokio' crates. ```rust use parsoid::Result; use parsoid::prelude::*; #[tokio::main] async fn main() -> Result<()> { let client = ParsoidClient::new("https://en.wikipedia.org/w/rest.php", "parsoid-rs demo")?; let code = client.get("Taylor_Swift").await?.into_mutable(); for template in code.filter_templates()? { if template.name() == "Template:Infobox person" { let birth_name = template.param("birth_name").unwrap(); assert_eq!(birth_name, "Taylor Alison Swift"); } } Ok(()) } ``` -------------------------------- ### Generic Type Identity (Rust) Source: https://docs.rs/parsoid/latest/parsoid/node/enum Includes the implementation of the `Any` trait for generic types, which provides a `type_id` method to get the `TypeId` of the type. ```rust impl Any for T where T: 'static + ?Sized, Source§ fn type_id(&self) -> TypeId Gets the `TypeId` of `self`. Read more Source§ ``` -------------------------------- ### Fetch Page HTML from Parsoid API (Rust) Source: https://docs.rs/parsoid/latest/src/parsoid/api Constructs and fetches the HTML content for a given wiki page from the Parsoid API. It supports fetching by page title or revision ID and adapts to different Parsoid endpoint kinds (Core, Extension, RESTBase). It handles potential 404 errors by returning a PageDoesNotExist error. ```rust async fn page_html( &self, page: &str, revid: Option, ) -> Result { let url = match self.endpoint_kind { ParsoidEndpointKind::Core => { if let Some(revid) = revid { format!( "{}/v1/revision/{}/html?redirect=no&flavor=edit", self.base_url, revid ) } else { format!( "{}/v1/page/{}/html?redirect=no&flavor=edit", self.base_url, encode(page) ) } } ParsoidEndpointKind::ParsoidExtension => { if let Some(revid) = revid { format!( "{}/page/html/{}/{}?redirect=false", self.base_url, encode(page), revid ) } else { format!( "{}/page/html/{}?redirect=false", self.base_url, encode(page) ) } } #[cfg(feature = "restbase")] ParsoidEndpointKind::RESTBase => { if let Some(revid) = revid { format!( "{}/page/html/{}/{}?redirect=false", self.base_url, encode(page), revid ) } else { format!( "{}/page/html/{}?redirect=false", self.base_url, encode(page) ) } } }; let resp = self.request(self.http.get(url)).await?; if resp.status() == 404 { Err(Error::PageDoesNotExist(page.to_string())) } else { Ok(resp.error_for_status()?) } } ``` -------------------------------- ### Type ID Retrieval (Rust) Source: https://docs.rs/parsoid/latest/parsoid/cite/struct Demonstrates how to get the `TypeId` of a static object using the `Any` trait implementation. This is useful for runtime type identification. ```rust impl Any for T where T: 'static + ?Sized, Source§ fn type_id(&self) -> TypeId Gets the `TypeId` of `self`. Read more Source§ ``` -------------------------------- ### Rust: Create New Parameter Source: https://docs.rs/parsoid/latest/src/parsoid/template A constructor function for the Param struct. It initializes a Param with a given wikitext string and sets the key to None. This is used for creating parameter objects. ```rust fn new(wikitext: impl Into) -> Self { Param { wt: wikitext.into(), key: None, } } ``` -------------------------------- ### Get Category Name in Rust Source: https://docs.rs/parsoid/latest/src/parsoid/node Retrieves the full name of the category, including the 'Category:' prefix. This is a direct accessor to the stored category name. ```rust pub fn category(&self) -> &str { // Implementation omitted for brevity unimplemented!(); } ``` -------------------------------- ### Get TypeId of Any Type in Rust Source: https://docs.rs/parsoid/latest/parsoid/node/struct Provides a method to retrieve the `TypeId` of any type that implements `'static`. This is fundamental for runtime type introspection and dynamic dispatch. ```rust impl Any for T where T: 'static + ?Sized, fn type_id(&self) -> TypeId ``` -------------------------------- ### Rust: Create and Manipulate IncludeOnly Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Demonstrates creating an IncludeOnly node with initial wikitext, retrieving the wikitext, and updating it. It uses the `parsoid` crate and requires `Result` and `NodeRef` types. ```rust use parsoid::Result; use parsoid::prelude::*; fn main() -> Result<()> { let mut includeonly = IncludeOnly::new("foo bar")?; assert_eq!( &includeonly.to_string(), "foo bar"}">" ); includeonly.set_wikitext("bar foo")?; assert_eq!( &includeonly.to_string(), "bar foo"}">" ); Ok(()) } ``` -------------------------------- ### Rust Wikicode: Get Spec Version Source: https://docs.rs/parsoid/latest/parsoid/struct Retrieves the HTML specification version associated with the Parsoid HTML document, if available. Returns an `Option`. ```rust pub fn spec_version(&self) -> Option ``` -------------------------------- ### Rust: Use ParsoidClient to Transform to HTML Source: https://docs.rs/parsoid/latest/src/parsoid/inclusion Demonstrates how to use the `ParsoidClient` to transform a Wikitext string into HTML, specifically handling `onlyinclude` tags. The example shows fetching an `OnlyInclude` instance from the resulting HTML and asserting its content. This showcases a practical application of the Parsoid library for web content processing. ```rust # use parsoid::Result; # use parsoid::prelude::*; # #[tokio::main] # async fn main() -> Result<()> { # let client = ParsoidClient::new("https://www.mediawiki.org/w/rest.php","parsoid-rs testing").unwrap(); let code = client.transform_to_html("foobarbaz").await?.into_mutable(); // Get the `OnlyInclude` instance let onlyinclude = code.filter_onlyinclude()[0].clone(); assert_eq!(&onlyinclude.inclusive_descendants()[0].text_contents(), "bar"); # Ok(()) # } ``` -------------------------------- ### Get External Link Target in Rust Source: https://docs.rs/parsoid/latest/src/parsoid/node Retrieves the target URL of an external link. This function extracts the value from the 'href' attribute of the anchor element. ```rust pub fn target(&self) -> String { self.as_element() .unwrap() .attributes .borrow() .get("href") .unwrap() .to_string() } ``` -------------------------------- ### From and Into Conversions (Rust) Source: https://docs.rs/parsoid/latest/parsoid/cite/struct Demonstrates the `From` and `Into` traits for type conversions. `From for T` allows creating a type from itself, while `Into for T` allows converting `T` into `U` if `U` implements `From`. ```rust impl From for T Source§ fn from(t: T) -> T Returns the argument unchanged. Source§ ``` ```rust impl Into for T where U: From, Source§ fn into(self) -> U Calls `U::from(self)`. That is, this conversion is whatever the implementation of `From for U` chooses to do. Source§ ``` -------------------------------- ### Generic Cloning and Ownership (Rust) Source: https://docs.rs/parsoid/latest/parsoid/node/enum Illustrates blanket implementations for `CloneToUninit` and `ToOwned`. `CloneToUninit` is a nightly-only experimental API for copying to uninitialized memory, while `ToOwned` provides methods for creating owned data from borrowed data. ```rust impl CloneToUninit for T where T: Clone, Source§ unsafe fn clone_to_uninit(&self, dest: *mut u8) 🔬This is a nightly-only experimental API. (`clone_to_uninit`) Performs copy-assignment from `self` to `dest`. Read more Source§ impl ToOwned for T where T: Clone, Source§ type Owned = T The resulting type after obtaining ownership. Source§ fn to_owned(&self) -> T Creates owned data from borrowed data, usually by cloning. Read more Source§ fn clone_into(&self, target: &mut T) Uses borrowed data to replace owned data, usually by cloning. Read more Source§ ``` -------------------------------- ### Fetch and Extract Template Parameter (Rust) Source: https://docs.rs/parsoid/latest/parsoid/index Fetches a Wikipedia page, finds a specific template ('Template:Infobox person'), and extracts the value of the 'birth_name' parameter. Requires the 'parsoid' crate and an async runtime. ```rust use parsoid::prelude::*; async fn extract_birth_name() -> Result<(), Box> { let client = ParsoidClient::new("https://en.wikipedia.org/w/rest.php", "parsoid-rs demo")?; let code = client.get("Taylor_Swift").await?.into_mutable(); for template in code.filter_templates()? { if template.name() == "Template:Infobox person" { let birth_name = template.param("birth_name").unwrap(); assert_eq!(birth_name, "Taylor Alison Swift"); } } Ok(()) } ``` -------------------------------- ### Cloning and Debugging ReferenceList in Rust Source: https://docs.rs/parsoid/latest/parsoid/cite/struct Demonstrates implementations for `Clone` and `Debug` traits for the `ReferenceList` type. The `clone` method creates a duplicate, while `fmt` allows for formatted output. ```rust impl Clone for ReferenceList fn clone(&self) -> ReferenceList fn clone_from(&mut self, source: &Self) impl Debug for ReferenceList fn fmt(&self, f: &mut Formatter<'_>) -> Result ``` -------------------------------- ### Get First Key-Value Pair in IndexMap (Rust) Source: https://docs.rs/parsoid/latest/parsoid/map/struct Retrieves an immutable reference to the first key-value pair in the IndexMap, preserving insertion order. This operation is O(1). ```rust pub fn first(&self) -> Option<(&K, &V)> ``` -------------------------------- ### Rust: Create New Transclusion Target Source: https://docs.rs/parsoid/latest/src/parsoid/template A constructor function for TransclusionTarget. It initializes the target with a given name, setting the 'wt' field and leaving 'href' and 'function' as None. This is used when converting HTML to wikitext. ```rust fn new(name: &str) -> Self { Self { wt: name.to_string(), // html -> wikitext doesn't need `href` href: None, function: None, } } ``` -------------------------------- ### Rust Result And Source: https://docs.rs/parsoid/latest/parsoid/type Demonstrates the `and` method, which returns the second `Result` if the first is `Ok`, otherwise returns the `Err` from the first `Result`. Arguments are eagerly evaluated. ```Rust let x: Result = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); ``` -------------------------------- ### Rust: Get Wikitext from IncludeOnly Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Retrieves the wikitext content enclosed within an IncludeOnly node. This function is part of the `IncludeOnly` struct in the `parsoid` crate. ```rust pub fn wikitext(&self) -> Result { let data: IncludeOnlyDataMw = inner_data(self)?; Ok(data .src .strip_prefix("") .unwrap() .strip_suffix("") .unwrap() .to_string()) } ``` -------------------------------- ### Create New Wikicode Node - Rust Source: https://docs.rs/parsoid/latest/src/parsoid/lib Constructs a new HTML element node with a specified tag. This is useful for building HTML structures programmatically. It returns a `Wikicode` instance representing the new node. ```rust /// Create a new HTML node with the given tag /// ``` /// # use parsoid::prelude::*; /// let node = Wikicode::new_node("b"); /// // Append your list items /// node.append(&Wikicode::new_text("bolded text")); /// assert_eq!(&node.to_string(), "bolded text") /// ``` pub fn new_node(tag: &str) -> Self { let document = NodeRef::new_element(crate::build_qual_name(tag.into()), vec![]); Self { document, title: None, etag: None, } } ``` -------------------------------- ### Get Heading from Section in Rust Source: https://docs.rs/parsoid/latest/src/parsoid/node Attempts to retrieve the associated `Heading` element from a `Section`. It returns `None` for pseudo-sections and uses a selector to find the heading otherwise. ```rust pub fn heading(&self) -> Option { if !self.is_pseudo_section() { self.select_first(Heading::SELECTOR) .map(|node| Heading::new_from_node(&node)) } else { None } } ``` -------------------------------- ### Parsoid Endpoint Kinds Source: https://docs.rs/parsoid/latest/src/parsoid/api Explains the different types of Parsoid endpoints the client can interact with, including Core, ParsoidExtension, and the deprecated RESTBase. ```APIDOC ## ParsoidEndpointKind Enum ### Description Represents the different types of Parsoid API endpoints supported by the client. ### Variants * **Core**: MediaWiki Core REST API (new in MW 1.42). * **ParsoidExtension**: Provided by the Parsoid MediaWiki extension. * **RESTBase**: Deprecated RESTBase API (only available on WMF projects). This variant requires the `restbase` feature to be enabled. ``` -------------------------------- ### Get Redirect from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Safely extracts a `Redirect` node from a generic node. Returns `Some(Redirect)` if the node is a redirect, otherwise `None`. This is for processing wiki redirects. ```rust pub fn as_redirect(&self) -> Option { match self { Self::Redirect(redirect) => Some(redirect.clone()), _ => None, } } ``` -------------------------------- ### Get IncludeOnly from Node Source: https://docs.rs/parsoid/latest/src/parsoid/node Safely extracts an `IncludeOnly` node from a generic node. Returns `Some(IncludeOnly)` if the node matches, otherwise `None`. This is relevant for template inclusions. ```rust pub fn as_includeonly(&self) -> Option { match self { Self::IncludeOnly(includeonly) => Some(includeonly.clone()), _ => None, } } ``` -------------------------------- ### Rust: Setting a Template Parameter Source: https://docs.rs/parsoid/latest/parsoid/prelude/struct Presents a Rust function for setting or updating a parameter within a MediaWiki template. It accepts the parameter name and its new wikitext value, returning the old value if one was present. ```rust pub fn set_param(&self, name: &str, wikitext: &str) -> Result> ``` -------------------------------- ### Type Conversion for ReferenceList in Rust Source: https://docs.rs/parsoid/latest/parsoid/cite/struct Shows how to convert a `ReferenceList` into a `Wikinode` using the `From` trait. This allows for seamless integration when a `Wikinode` is expected. ```rust impl From for Wikinode fn from(node: ReferenceList) -> Self ``` -------------------------------- ### Get Plain Text Contents - Rust Source: https://docs.rs/parsoid/latest/src/parsoid/lib Extracts and returns a plain text representation of the Parsoid HTML, with all markup stripped. It utilizes the body_element() method to ensure content is from the body. ```rust pub fn text_contents(&self) -> String { self.body_element().text_contents() } ```