Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add annotations expression object for thymeleaf #3076

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package run.halo.app.theme.dialect;

import java.util.Set;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.expression.IExpressionObjectFactory;
import run.halo.app.theme.dialect.expression.Annotations;

/**
* Builds the expression objects to be used by Halo dialects.
*
* @author guqing
* @since 2.0.0
*/
public class HaloExpressionObjectFactory implements IExpressionObjectFactory {

public static final String ANNOTATIONS_EXPRESSION_OBJECT_NAME = "annotations";

protected static final Set<String> ALL_EXPRESSION_OBJECT_NAMES = Set.of(
ANNOTATIONS_EXPRESSION_OBJECT_NAME);

private static final Annotations ANNOTATIONS = new Annotations();

@Override
public Set<String> getAllExpressionObjectNames() {
return ALL_EXPRESSION_OBJECT_NAMES;
}

@Override
public Object buildObject(IExpressionContext context, String expressionObjectName) {
if (ANNOTATIONS_EXPRESSION_OBJECT_NAME.equals(expressionObjectName)) {
return ANNOTATIONS;
}
return null;
}

@Override
public boolean isCacheable(String expressionObjectName) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.HashSet;
import java.util.Set;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.dialect.IExpressionObjectDialect;
import org.thymeleaf.expression.IExpressionObjectFactory;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;

Expand All @@ -12,9 +14,13 @@
* @author guqing
* @since 2.0.0
*/
public class HaloProcessorDialect extends AbstractProcessorDialect {
public class HaloProcessorDialect extends AbstractProcessorDialect implements
IExpressionObjectDialect {
private static final String DIALECT_NAME = "haloThemeProcessorDialect";

private static final IExpressionObjectFactory HALO_EXPRESSION_OBJECTS_FACTORY =
new HaloExpressionObjectFactory();

public HaloProcessorDialect() {
// We will set this dialect the same "dialect processor" precedence as
// the Standard Dialect, so that processor executions can interleave.
Expand All @@ -31,4 +37,9 @@ public Set<IProcessor> getProcessors(String dialectPrefix) {
processors.add(new CommentElementTagProcessor(dialectPrefix));
return processors;
}

@Override
public IExpressionObjectFactory getExpressionObjectFactory() {
return HALO_EXPRESSION_OBJECTS_FACTORY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package run.halo.app.theme.dialect.expression;

import java.util.Map;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.theme.finders.vo.ExtensionVoOperator;

/**
* <p>Expression Object for performing annotations operations inside Halo Extra Expressions.</p>
* An object of this class is usually available in variable evaluation expressions with the name
* <code>#annotations</code>.
*
* @author guqing
* @since 2.0.2
*/
public class Annotations {

/**
* Get annotation value from extension vo.
*
* @param extension extension vo
* @param key the key of annotation
* @return annotation value if exists, otherwise null
*/
@Nullable
public String get(ExtensionVoOperator extension, String key) {
Map<String, String> annotations = extension.getMetadata().getAnnotations();
if (annotations == null) {
return null;
}
return annotations.get(key);
}

/**
* Returns the value to which the specified key is mapped, or defaultValue if
* <code>extension</code> contains no mapping for the key.
*
* @param extension extension vo
* @param key the key of annotation
* @return annotation value if exists, otherwise defaultValue
*/
@NonNull
public String getOrDefault(ExtensionVoOperator extension, String key, String defaultValue) {
Map<String, String> annotations = extension.getMetadata().getAnnotations();
if (annotations == null) {
return defaultValue;
}
return annotations.getOrDefault(key, defaultValue);
}

/**
* Check if the extension has the specified annotation.
*
* @param extension extension vo
* @param key the key of annotation
* @return true if the extension has the specified annotation, otherwise false
*/
public boolean contains(ExtensionVoOperator extension, String key) {
Map<String, String> annotations = extension.getMetadata().getAnnotations();
if (annotations == null) {
return false;
}
return annotations.containsKey(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Builder
@ToString
@EqualsAndHashCode
public class CategoryTreeVo implements VisualizableTreeNode<CategoryTreeVo> {
public class CategoryTreeVo implements VisualizableTreeNode<CategoryTreeVo>, ExtensionVoOperator {

private MetadataOperator metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Value
@Builder
@EqualsAndHashCode
public class CategoryVo {
public class CategoryVo implements ExtensionVoOperator {

MetadataOperator metadata;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/theme/finders/vo/CommentVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Value
@Builder
@EqualsAndHashCode
public class CommentVo {
public class CommentVo implements ExtensionVoOperator {

@Schema(required = true)
MetadataOperator metadata;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package run.halo.app.theme.finders.vo;

import org.springframework.lang.NonNull;
import run.halo.app.extension.MetadataOperator;

/**
* An operator for extension value object.
*
* @author guqing
* @since 2.0.0
*/
public interface ExtensionVoOperator {

@NonNull
MetadataOperator getMetadata();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SuperBuilder
@ToString
@EqualsAndHashCode
public class ListedPostVo {
public class ListedPostVo implements ExtensionVoOperator {

private MetadataOperator metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SuperBuilder
@ToString
@EqualsAndHashCode
public class ListedSinglePageVo {
public class ListedSinglePageVo implements ExtensionVoOperator {

private MetadataOperator metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Data
@ToString
@Builder
public class MenuItemVo implements VisualizableTreeNode<MenuItemVo> {
public class MenuItemVo implements VisualizableTreeNode<MenuItemVo>, ExtensionVoOperator {

MetadataOperator metadata;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/theme/finders/vo/MenuVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Value
@ToString
@Builder
public class MenuVo {
public class MenuVo implements ExtensionVoOperator {

MetadataOperator metadata;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/theme/finders/vo/ReplyVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Builder
@ToString
@EqualsAndHashCode
public class ReplyVo {
public class ReplyVo implements ExtensionVoOperator {

@Schema(required = true)
MetadataOperator metadata;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/theme/finders/vo/TagVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
@Value
@Builder
public class TagVo {
public class TagVo implements ExtensionVoOperator {

MetadataOperator metadata;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/theme/finders/vo/ThemeVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Value
@Builder
@ToString
public class ThemeVo {
public class ThemeVo implements ExtensionVoOperator {

MetadataOperator metadata;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/theme/finders/vo/UserVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Value
@Builder
public class UserVo {
public class UserVo implements ExtensionVoOperator {
MetadataOperator metadata;

User.UserSpec spec;
Expand Down
Loading