-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide ElementTagPostProcessor to handle element tag in plugin
Signed-off-by: JohnNiang <[email protected]>
- Loading branch information
Showing
9 changed files
with
339 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
api/src/main/java/run/halo/app/theme/dialect/ElementTagPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package run.halo.app.theme.dialect; | ||
|
||
import org.pf4j.ExtensionPoint; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.model.IProcessableElementTag; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* An extension point for post-processing element tag. | ||
* | ||
* @author johnniang | ||
* @since 2.20.0 | ||
*/ | ||
public interface ElementTagPostProcessor extends ExtensionPoint { | ||
|
||
/** | ||
* <p> | ||
* Execute the processor. | ||
* </p> | ||
* <p> | ||
* The {@link IProcessableElementTag} object argument is immutable, so all modifications to | ||
* this object or any | ||
* instructions to be given to the engine should be done through the specified | ||
* {@link org.thymeleaf.model.IModelFactory} model factory in context. | ||
* </p> | ||
* <p> | ||
* Don't forget to return the new tag after processing or | ||
* {@link reactor.core.publisher.Mono#empty()} if not processable. | ||
* </p> | ||
* | ||
* @param context the template context. | ||
* @param tag the event this processor is executing on. | ||
* @return a {@link reactor.core.publisher.Mono} that will complete when processing finishes | ||
* or empty mono if not support. | ||
*/ | ||
Mono<IProcessableElementTag> process( | ||
ITemplateContext context, | ||
final IProcessableElementTag tag | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
application/src/main/java/run/halo/app/theme/dialect/HaloPostTemplateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package run.halo.app.theme.dialect; | ||
|
||
import static org.thymeleaf.spring6.context.SpringContextUtils.getApplicationContext; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.util.CollectionUtils; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.engine.AbstractTemplateHandler; | ||
import org.thymeleaf.model.IOpenElementTag; | ||
import org.thymeleaf.model.IProcessableElementTag; | ||
import org.thymeleaf.model.IStandaloneElementTag; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.plugin.extensionpoint.ExtensionGetter; | ||
|
||
/** | ||
* Template post-handler. | ||
* | ||
* @author johnniang | ||
* @since 2.20.0 | ||
*/ | ||
public class HaloPostTemplateHandler extends AbstractTemplateHandler { | ||
|
||
private List<ElementTagPostProcessor> postProcessors = List.of(); | ||
|
||
@Override | ||
public void setContext(ITemplateContext context) { | ||
super.setContext(context); | ||
this.postProcessors = Optional.ofNullable(getApplicationContext(context)) | ||
.map(appContext -> appContext.getBeanProvider(ExtensionGetter.class).getIfUnique()) | ||
.map(extensionGetter -> extensionGetter.getExtensionList(ElementTagPostProcessor.class)) | ||
.orElseGet(List::of); | ||
} | ||
|
||
@Override | ||
public void handleStandaloneElement(IStandaloneElementTag standaloneElementTag) { | ||
var processedTag = handleElementTag(standaloneElementTag); | ||
super.handleStandaloneElement((IStandaloneElementTag) processedTag); | ||
} | ||
|
||
@Override | ||
public void handleOpenElement(IOpenElementTag openElementTag) { | ||
var processedTag = handleElementTag(openElementTag); | ||
super.handleOpenElement((IOpenElementTag) processedTag); | ||
} | ||
|
||
@NonNull | ||
private IProcessableElementTag handleElementTag( | ||
@NonNull IProcessableElementTag processableElementTag | ||
) { | ||
IProcessableElementTag processedTag = processableElementTag; | ||
if (!CollectionUtils.isEmpty(postProcessors)) { | ||
var tagProcessorChain = Mono.just(processableElementTag); | ||
var context = getContext(); | ||
for (ElementTagPostProcessor elementTagPostProcessor : postProcessors) { | ||
tagProcessorChain = tagProcessorChain.flatMap( | ||
tag -> elementTagPostProcessor.process(context, tag).defaultIfEmpty(tag) | ||
); | ||
} | ||
processedTag = | ||
Objects.requireNonNull(tagProcessorChain.defaultIfEmpty(processableElementTag) | ||
.block(Duration.ofMinutes(1))); | ||
} | ||
return processedTag; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.