-
-
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.
feat: add annotations expression object for thymeleaf
- Loading branch information
Showing
16 changed files
with
250 additions
and
15 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/run/halo/app/theme/dialect/HaloExpressionObjectFactory.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,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; | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
src/main/java/run/halo/app/theme/dialect/expression/Annotations.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,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); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/run/halo/app/theme/finders/vo/ExtensionVoOperator.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,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(); | ||
} |
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
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.