Struct parsoid::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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

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

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

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,

§

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>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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