[][src]Trait equt_md_ext::MarkdownExt

#[must_use = "iterators are lazy and do nothing unless consumed"]pub trait MarkdownExt<T>: Iterator<Item = T> + Sized {
    fn frontmatter(&mut self) -> &mut Share<RefCell<Option<FrontMatter>>>;

    fn inspect_frontmatter(
        self,
        frontmatter: &mut Option<FrontMatter>
    ) -> Raw<Self, T> { ... }
fn after<P, F, S, G>(self, after: P, f: F) -> After<Self, S, G, F, P, T>
    where
        P: Fn(&T) -> bool,
        S: Iterator<Item = T>,
        G: IntoIterator<Item = T, IntoIter = S>,
        F: Fn(Ref<Option<FrontMatter>>) -> G
, { ... }
fn before<P, F, S, G>(self, before: P, f: F) -> Before<Self, S, G, F, P, T>
    where
        P: Fn(&T) -> bool,
        S: Iterator<Item = T>,
        G: IntoIterator<Item = T, IntoIter = S>,
        F: Fn(Ref<Option<FrontMatter>>) -> G
, { ... }
fn link<E>(self, other: E) -> Link<Self, E>
    where
        E: MarkdownExt<T>
, { ... }
fn iter<I>(self, iter: I) -> Link<Self, Raw<I, T>>
    where
        I: Iterator<Item = T>
, { ... }
fn head<F, G, H>(self, f: F) -> Head<Self, H, F, G, T>
    where
        H: Iterator<Item = T>,
        G: IntoIterator<Item = T, IntoIter = H>,
        F: Fn(Ref<Option<FrontMatter>>) -> G
, { ... }
fn tail<F, G, H>(self, f: F) -> Tail<Self, H, F, G, T>
    where
        H: Iterator<Item = T>,
        G: IntoIterator<Item = T, IntoIter = H>,
        F: Fn(Ref<Option<FrontMatter>>) -> G
, { ... }
fn within<P, Q, F>(self, start: P, end: Q, f: F) -> Within<Self, P, Q, F, T>
    where
        P: Fn(&T) -> bool,
        Q: Fn(&T) -> bool,
        F: Fn(Ref<Option<FrontMatter>>, T) -> Option<T>
, { ... } }

Extending the original markdown events iterator

All provided methods in this trait will keep the frontmatter data internally.

Any closures that needs frontmatter as an argument are lazy. With the laziness, the frontmatter doesn't have to be presented first, which means it could be updated or get replaced during the process.

Required methods

fn frontmatter(&mut self) -> &mut Share<RefCell<Option<FrontMatter>>>

Different from a normal iterator, MarkdownExt must own the frontmatter data.

Loading content...

Provided methods

fn inspect_frontmatter(
    self,
    frontmatter: &mut Option<FrontMatter>
) -> Raw<Self, T>

Inspect the current frontmatter

Example

let mut fm: Option<FrontMatter> = None;
events.inspect_frontmatter(&mut fm)

The result would be a clone so it's still available even after the whole MarkdownExt chain has been consumed.

fn after<P, F, S, G>(self, after: P, f: F) -> After<Self, S, G, F, P, T> where
    P: Fn(&T) -> bool,
    S: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = S>,
    F: Fn(Ref<Option<FrontMatter>>) -> G, 

Place events after the first occurence of a certain event.

Example

let outdate = events.after(|e| match &e {
    Event::End(Tag::Heading(1)) => true,
    _ => false,
}, |frontmatter| days_since_last_update(frontmatter));

Assume the days_since_last_update could help create a series of events that notify the reader of the number of the days since the last update. The after could place the new events right after the first heading.

fn before<P, F, S, G>(self, before: P, f: F) -> Before<Self, S, G, F, P, T> where
    P: Fn(&T) -> bool,
    S: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = S>,
    F: Fn(Ref<Option<FrontMatter>>) -> G, 

Place events before the first occurence of a certain event.

Example

let official = events.before(|e| match &e {
    Event::Start(Tag::Heading(1)) => true,
    _ => false,
}, |frontmatter| disclaimer(frontmatter));

Assume the disclaimer could create some events and disclaim something important. The before here places it before the first heading.

Just like the standard chain for iterator, the frontmatter will get transferred by the following rules.

  • If both have frontmatter, the latter one would be picked
  • If neither has frontmatter, it would be None
  • Otherwise, the only frontmatter would be used

Example

let new_events = events.link(Parser::new(text)?);

New events parsed from text will be placed after the original events.

fn iter<I>(self, iter: I) -> Link<Self, Raw<I, T>> where
    I: Iterator<Item = T>, 

Like link, but accept an Iterator.

For dynamically generating events, consider the tail method.

fn head<F, G, H>(self, f: F) -> Head<Self, H, F, G, T> where
    H: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = H>,
    F: Fn(Ref<Option<FrontMatter>>) -> G, 

Place events at the start.

Example

let with_heading = events.head(|frontmatter| heading(frontmatter));

Assume heading reads the Title section in the frontmatter and generate a heading event, the head will place it at the start of the current iterator.

fn tail<F, G, H>(self, f: F) -> Tail<Self, H, F, G, T> where
    H: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = H>,
    F: Fn(Ref<Option<FrontMatter>>) -> G, 

Place events at the end.

Example

let with_copyright = events.tail(|frontmatter| copyright(frontmatter));

Assume copyright reads the meta data in the frontmatter and generate copyright related event, the tail will place it at the end of the current iterator.

fn within<P, Q, F>(self, start: P, end: Q, f: F) -> Within<Self, P, Q, F, T> where
    P: Fn(&T) -> bool,
    Q: Fn(&T) -> bool,
    F: Fn(Ref<Option<FrontMatter>>, T) -> Option<T>, 

Maps function upon events within two specific events.

Example

let uppercase = events.within(|e| match &e {
    Event::Start(Tag::Heading(_)) => true,
    _ => false,
}, |e| match &e {
    Event::End(Tag::Heading(_)) => true,
    _ => false,
}, |_, e| Some(match e {
    Event::Text(s) => Event::Text(s.into_string().to_uppercase().into()),
    event => event,
}));

The above code will turn all text in heading to uppercase.

Loading content...

Implementors

impl<'e> MarkdownExt<Event<'e>> for Parser<'e>[src]

impl<A, B, T> MarkdownExt<T> for Link<A, B> where
    A: Iterator<Item = T>,
    B: Iterator<Item = T>, 
[src]

impl<I, H, F, G, T> MarkdownExt<T> for Head<I, H, F, G, T> where
    I: Iterator<Item = T>,
    H: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = H>,
    F: Fn(Ref<Option<FrontMatter>>) -> G, 
[src]

impl<I, H, F, G, T> MarkdownExt<T> for Tail<I, H, F, G, T> where
    I: Iterator<Item = T>,
    H: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = H>,
    F: Fn(Ref<Option<FrontMatter>>) -> G, 
[src]

impl<I, P, Q, F, T> MarkdownExt<T> for Within<I, P, Q, F, T> where
    I: Iterator<Item = T>,
    P: Fn(&T) -> bool,
    Q: Fn(&T) -> bool,
    F: FnMut(Ref<Option<FrontMatter>>, T) -> Option<T>, 
[src]

impl<I, S, G, F, P, T> MarkdownExt<T> for After<I, S, G, F, P, T> where
    I: Iterator<Item = T>,
    S: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = S>,
    F: Fn(Ref<Option<FrontMatter>>) -> G,
    P: Fn(&T) -> bool
[src]

impl<I, S, G, F, P, T> MarkdownExt<T> for Before<I, S, G, F, P, T> where
    I: Iterator<Item = T>,
    S: Iterator<Item = T>,
    G: IntoIterator<Item = T, IntoIter = S>,
    F: Fn(Ref<Option<FrontMatter>>) -> G,
    P: Fn(&T) -> bool
[src]

impl<I, T> MarkdownExt<T> for Raw<I, T> where
    I: Iterator<Item = T>, 
[src]

impl<T> MarkdownExt<T> for Bottom<T>[src]

Loading content...