@@ -133,26 +133,45 @@ pub type MarkdownEvents<'a> = Vec<Event<'a>>;
133
133
/// # exporter.run().unwrap();
134
134
/// ```
135
135
136
- pub type Postprocessor < ' f > =
137
- dyn Fn ( & mut Context , & mut MarkdownEvents ) -> PostprocessorResult + Send + Sync + ' f ;
138
136
type Result < T , E = ExportError > = std:: result:: Result < T , E > ;
139
137
140
- /// Postprocess is a trait form of the [Postprocessor] callback that can be passed to
141
- /// [Exporter::add_postprocessor_impl].
142
- pub trait Postprocess : Send + Sync {
138
+ /// Postprocessor that can be that can be passed to [Exporter::add_postprocessor_impl].
139
+ pub trait Postprocessor : Send + Sync {
143
140
fn postprocess ( & self , ctx : & mut Context , events : & mut MarkdownEvents ) -> PostprocessorResult ;
144
141
}
145
142
146
- /// EmbedPostprocess is a trait form of the [Postprocessor] callback that can be
147
- /// passed to [Exporter::add_embed_postprocessor_impl].
148
- pub trait EmbedPostprocess : Send + Sync {
143
+ /// Postprocessor is implemented for any callback function type that matches the
144
+ /// signature.
145
+ impl < F : Fn ( & mut Context , & mut MarkdownEvents ) -> PostprocessorResult + Send + Sync > Postprocessor
146
+ for F
147
+ {
148
+ fn postprocess ( & self , ctx : & mut Context , events : & mut MarkdownEvents ) -> PostprocessorResult {
149
+ self ( ctx, events)
150
+ }
151
+ }
152
+
153
+ /// EmbedPostprocessor is like [Postprocessor] but for note embeds, and it is passed to
154
+ /// [Exporter::add_embed_postprocessor_impl].
155
+ pub trait EmbedPostprocessor : Send + Sync {
149
156
fn embed_postprocess (
150
157
& self ,
151
158
ctx : & mut Context ,
152
159
events : & mut MarkdownEvents ,
153
160
) -> PostprocessorResult ;
154
161
}
155
162
163
+ impl < F : Fn ( & mut Context , & mut MarkdownEvents ) -> PostprocessorResult + Send + Sync >
164
+ EmbedPostprocessor for F
165
+ {
166
+ fn embed_postprocess (
167
+ & self ,
168
+ ctx : & mut Context ,
169
+ events : & mut MarkdownEvents ,
170
+ ) -> PostprocessorResult {
171
+ self ( ctx, events)
172
+ }
173
+ }
174
+
156
175
const PERCENTENCODE_CHARS : & AsciiSet = & CONTROLS . add ( b' ' ) . add ( b'(' ) . add ( b')' ) . add ( b'%' ) . add ( b'?' ) ;
157
176
const NOTE_RECURSION_LIMIT : usize = 10 ;
158
177
@@ -232,23 +251,6 @@ pub enum PostprocessorResult {
232
251
StopAndSkipNote ,
233
252
}
234
253
235
- #[ derive( Clone ) ]
236
- enum PostprocessorRef < ' p > {
237
- Function ( & ' p Postprocessor < ' p > ) ,
238
- Trait ( & ' p dyn Postprocess ) ,
239
- EmbedTrait ( & ' p dyn EmbedPostprocess ) ,
240
- }
241
-
242
- impl < ' p > PostprocessorRef < ' p > {
243
- fn call ( & ' p self , ctx : & mut Context , events : & mut MarkdownEvents ) -> PostprocessorResult {
244
- match self {
245
- PostprocessorRef :: Function ( f) => f ( ctx, events) ,
246
- PostprocessorRef :: Trait ( t) => t. postprocess ( ctx, events) ,
247
- PostprocessorRef :: EmbedTrait ( t) => t. embed_postprocess ( ctx, events) ,
248
- }
249
- }
250
- }
251
-
252
254
#[ derive( Clone ) ]
253
255
/// Exporter provides the main interface to this library.
254
256
///
@@ -264,8 +266,8 @@ pub struct Exporter<'a> {
264
266
vault_contents : Option < Vec < PathBuf > > ,
265
267
walk_options : WalkOptions < ' a > ,
266
268
process_embeds_recursively : bool ,
267
- postprocessors : Vec < PostprocessorRef < ' a > > ,
268
- embed_postprocessors : Vec < PostprocessorRef < ' a > > ,
269
+ postprocessors : Vec < & ' a dyn Postprocessor > ,
270
+ embed_postprocessors : Vec < & ' a dyn EmbedPostprocessor > ,
269
271
}
270
272
271
273
impl < ' a > fmt:: Debug for Exporter < ' a > {
@@ -347,34 +349,38 @@ impl<'a> Exporter<'a> {
347
349
}
348
350
349
351
/// Append a function to the chain of [postprocessors][Postprocessor] to run on exported Obsidian Markdown notes.
350
- pub fn add_postprocessor ( & mut self , processor : & ' a Postprocessor ) -> & mut Exporter < ' a > {
351
- self . postprocessors
352
- . push ( PostprocessorRef :: Function ( processor) ) ;
352
+ pub fn add_postprocessor (
353
+ & mut self ,
354
+ processor : & ' a ( impl Fn ( & mut Context , & mut MarkdownEvents ) -> PostprocessorResult + Send + Sync ) ,
355
+ ) -> & mut Exporter < ' a > {
356
+ self . postprocessors . push ( processor) ;
353
357
self
354
358
}
355
359
356
- /// Append a trait implementation of [Postprocess] to the chain of [postprocessors] to run on
357
- /// Obsidian Markdown notes.
358
- pub fn add_postprocessor_impl ( & mut self , processor : & ' a dyn Postprocess ) -> & mut Exporter < ' a > {
359
- self . postprocessors . push ( PostprocessorRef :: Trait ( processor) ) ;
360
+ /// Append a trait object to the chain of [postprocessors] to run on Obsidian Markdown notes.
361
+ pub fn add_postprocessor_impl (
362
+ & mut self ,
363
+ processor : & ' a dyn Postprocessor ,
364
+ ) -> & mut Exporter < ' a > {
365
+ self . postprocessors . push ( processor) ;
360
366
self
361
367
}
362
368
363
- /// Append a function to the chain of [postprocessors][Postprocessor] for embeds.
364
- pub fn add_embed_postprocessor ( & mut self , processor : & ' a Postprocessor ) -> & mut Exporter < ' a > {
365
- self . embed_postprocessors
366
- . push ( PostprocessorRef :: Function ( processor) ) ;
369
+ /// Append a function to the chain of [postprocessors][EmbedPostprocessor] for embeds.
370
+ pub fn add_embed_postprocessor (
371
+ & mut self ,
372
+ processor : & ' a ( impl Fn ( & mut Context , & mut MarkdownEvents ) -> PostprocessorResult + Send + Sync ) ,
373
+ ) -> & mut Exporter < ' a > {
374
+ self . embed_postprocessors . push ( processor) ;
367
375
self
368
376
}
369
377
370
- /// Append a trait implementation of [EmbedPostprocess] to the chain of [postprocessors] for
371
- /// embeds.
378
+ /// Append a trait object to the chain of [postprocessors] for embeds.
372
379
pub fn add_embed_postprocessor_impl (
373
380
& mut self ,
374
- processor : & ' a dyn EmbedPostprocess ,
381
+ processor : & ' a dyn EmbedPostprocessor ,
375
382
) -> & mut Exporter < ' a > {
376
- self . embed_postprocessors
377
- . push ( PostprocessorRef :: EmbedTrait ( processor) ) ;
383
+ self . embed_postprocessors . push ( processor) ;
378
384
self
379
385
}
380
386
@@ -454,7 +460,7 @@ impl<'a> Exporter<'a> {
454
460
let ( frontmatter, mut markdown_events) = self . parse_obsidian_note ( src, & context) ?;
455
461
context. frontmatter = frontmatter;
456
462
for processor in & self . postprocessors {
457
- match processor. call ( & mut context, & mut markdown_events) {
463
+ match processor. postprocess ( & mut context, & mut markdown_events) {
458
464
PostprocessorResult :: StopHere => break ,
459
465
PostprocessorResult :: StopAndSkipNote => return Ok ( ( ) ) ,
460
466
PostprocessorResult :: Continue => ( ) ,
@@ -659,7 +665,7 @@ impl<'a> Exporter<'a> {
659
665
for processor in & self . embed_postprocessors {
660
666
// Postprocessors running on embeds shouldn't be able to change frontmatter (or
661
667
// any other metadata), so we give them a clone of the context.
662
- match processor. call ( & mut child_context, & mut events) {
668
+ match processor. embed_postprocess ( & mut child_context, & mut events) {
663
669
PostprocessorResult :: StopHere => break ,
664
670
PostprocessorResult :: StopAndSkipNote => {
665
671
events = vec ! [ ] ;
0 commit comments