parsoid

Struct Template

Source
pub struct Template { /* private fields */ }
Expand description

Represents a MediaWiki template ({{foo}})

How to access values from an existing template:

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()));

How to create and insert a new template:

let mut params = map::IndexMap::new();
params.insert("1".to_string(), "test".to_string());
let template = Template::new("1x", &params)?;
let code = Wikicode::new("");
code.append(&template);
let wikitext = client.transform_to_wikitext(&code).await?;
assert_eq!(wikitext, "{{1x|test}}".to_string());

You can also use Template::new_simple() if there are no parameters to pass.

Implementations§

Source§

impl Template

Source

pub fn new_simple(name: &str) -> Self

Create a new template with no parameters

Source

pub fn new(name: &str, params: &IndexMap<String, String>) -> Result<Self>

Create a new template

Source

pub fn remove_param(&self, name: &str) -> Result<Option<String>>

Remove the specified parameter from the template. If it was set, the previous value will be returned.

Source

pub fn set_param(&self, name: &str, wikitext: &str) -> Result<Option<String>>

Set the specified wikitext as a template parameter with the given name

The previous value, if one was set, will be returned.

Source

pub fn set_params( &self, map: impl Into<IndexMap<String, String>>, ) -> Result<IndexMap<String, String>>

Override all parameters with the specified map. A map of all previously set parameters and their values will be returned.

Source

pub fn name_in_wikitext(&self) -> String

Get the name of the template as it appears in wikitext

Source

pub fn set_name(&self, name: String) -> Result<()>

Change the name of the template or parser function. Note that the name should be how the name in wikitext. For example, "1x" for a Template:1x,

Source

pub fn raw_name(&self) -> String

Get the full name of the template, e.g. ./Template:Foo_bar or the parser function, e.g. ifeq.

Source

pub fn name(&self) -> String

Get a pretty normalized name of a template, e.g. Template:Foo bar or the parser function, e.g. ifeq

Source

pub fn params(&self) -> IndexMap<String, String>

Get a map of all parameters, named and unnamed

Source

pub fn param(&self, name: &str) -> Option<String>

Get the wikitext value of a specific parameter if it exists

Source

pub fn param_in_wikitext(&self, name: &str) -> Option<String>

Get the name of the parameter as it appears in the wikitext. For example given {{1x|param<!--comment-->name=value}} looking up paramname would return Some("param<!--comment->name").

Source

pub fn is_template(&self) -> bool

Whether it’s a template (as opposed to a parser function)

Source

pub fn is_parser_function(&self) -> bool

Whether it’s a parser function (as opposed to a template)

Trait Implementations§

Source§

impl Clone for Template

Source§

fn clone(&self) -> Template

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Template

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Template

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl WikiMultinode for Template

Source§

fn as_nodes(&self) -> Vec<NodeRef>

Source§

fn as_wikinodes(&self) -> WikinodeMap<IntoIter<NodeRef>>

Source§

fn detach(&self)

Remove this from the document

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T