Search in sources :

Example 1 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class ThemeServiceTest method setUp.

@BeforeEach
public void setUp() {
    themes = new HashSet<>();
    theme1 = mock(Theme1.class);
    theme2 = mock(Theme2.class);
    when(theme1.getKey()).thenReturn("theme1");
    when(theme2.getKey()).thenReturn("theme2");
    when(theme1.toString()).thenReturn("theme1.toString()");
    when(theme2.toString()).thenReturn("theme2.toString()");
    themes.add(theme1);
    adminThemes = new HashSet<>();
    adminTheme1 = mock(AdminTheme1.class);
    adminTheme2 = mock(AdminTheme2.class);
    when(adminTheme1.getKey()).thenReturn("adminTheme1");
    when(adminTheme2.getKey()).thenReturn("adminTheme2");
    when(adminTheme1.toString()).thenReturn("adminTheme1.toString()");
    when(adminTheme2.toString()).thenReturn("adminTheme2.toString()");
    adminThemes.add(adminTheme1);
    assetManager = mock(AssetManager.class);
    themeContextOptions = new JSONObject();
    theme2ContextOptions = new JSONObject();
    adminThemeContextOptions = new JSONObject();
    adminTheme2ContextOptions = new JSONObject();
    // Mock Injector
    // Mock Context
    context = mock(OrchidContext.class);
    when(context.query("theme")).thenReturn(new JSONElement(themeContextOptions));
    when(context.query("theme2")).thenReturn(new JSONElement(theme2ContextOptions));
    when(context.query("adminTheme")).thenReturn(new JSONElement(adminThemeContextOptions));
    when(context.query("adminTheme2")).thenReturn(new JSONElement(adminTheme2ContextOptions));
    when(context.resolve((Class<Theme>) theme1.getClass())).thenReturn(theme1);
    when(context.resolve((Class<Theme>) theme2.getClass())).thenReturn(theme2);
    when(context.resolve((Class<AdminTheme>) adminTheme1.getClass())).thenReturn(adminTheme1);
    when(context.resolve((Class<AdminTheme>) adminTheme2.getClass())).thenReturn(adminTheme2);
    // Create instance of Service Implementation
    ThemeServiceImpl service = new ThemeServiceImpl(assetManager, () -> themes, "theme1", () -> adminThemes, "adminTheme1");
    service.initialize(context);
    // Create wrapper around the Implementation to verify it works in composition
    underTest = new ThemeService() {

        public void initialize(OrchidContext context) {
        }

        public <T extends OrchidService> T getService(Class<T> serviceClass) {
            return (T) service;
        }
    };
}
Also used : AssetManager(com.eden.orchid.api.theme.assets.AssetManager) OrchidContext(com.eden.orchid.api.OrchidContext) JSONObject(org.json.JSONObject) JSONElement(com.eden.common.json.JSONElement) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class ContentTagParser method render.

@Override
public void render(PebbleTemplateImpl self, Writer writer, EvaluationContextImpl context) throws IOException {
    OrchidContext orchidContext = contextProvider.get();
    TemplateTag freshTag = orchidContext.resolve(tagClass);
    Map<String, Object> evaluatedParamExpressionMap = evaluateParams(paramExpressionMap, self, context);
    Object pageVar = context.getVariable("page");
    final OrchidPage actualPage;
    if (pageVar instanceof OrchidPage) {
        actualPage = (OrchidPage) pageVar;
    } else {
        actualPage = null;
    }
    freshTag.extractOptions(orchidContext, evaluatedParamExpressionMap);
    String bodyContent = StringUtils.toString(tagBodyExpression.evaluate(self, context)).trim();
    TemplateTag.Tab tab = new TemplateTag.SimpleTab(null, null, bodyContent);
    freshTag.setContent(tab);
    freshTag.onRender(orchidContext, actualPage);
    if (freshTag.rendersContent()) {
        writer.append(freshTag.renderContent(orchidContext, actualPage));
    }
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidContext(com.eden.orchid.api.OrchidContext) TemplateTag(com.eden.orchid.api.compilers.TemplateTag) PebbleWrapperTemplateTag(com.eden.orchid.impl.compilers.pebble.PebbleWrapperTemplateTag)

Example 3 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class SimpleTagParser method render.

@Override
public void render(PebbleTemplateImpl self, Writer writer, EvaluationContextImpl context) throws IOException {
    OrchidContext orchidContext = contextProvider.get();
    TemplateTag freshTag = orchidContext.resolve(tagClass);
    Map<String, Object> evaluatedParamExpressionMap = evaluateParams(paramExpressionMap, self, context);
    Object pageVar = context.getVariable("page");
    final OrchidPage actualPage;
    if (pageVar instanceof OrchidPage) {
        actualPage = (OrchidPage) pageVar;
    } else {
        actualPage = null;
    }
    freshTag.extractOptions(orchidContext, evaluatedParamExpressionMap);
    freshTag.onRender(orchidContext, actualPage);
    if (freshTag.rendersContent()) {
        writer.append(freshTag.renderContent(orchidContext, actualPage));
    }
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidContext(com.eden.orchid.api.OrchidContext) TemplateTag(com.eden.orchid.api.compilers.TemplateTag) PebbleWrapperTemplateTag(com.eden.orchid.impl.compilers.pebble.PebbleWrapperTemplateTag)

Example 4 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class CompilerServiceTest method setUp.

@BeforeEach
public void setUp() {
    // target outputs
    mockInput = "input";
    compiledOutput = "compiled";
    parsedOutput = new HashMap<>();
    precompilerOutput = "precompiled";
    precompilerElement = new HashMap<>();
    precompilerEmbeddedData = new EdenPair<>(precompilerOutput, precompilerElement);
    Set<OrchidCompiler> compilers = new HashSet<>();
    mockCompiler = mock(OrchidCompiler.class);
    compilers.add(mockCompiler);
    when(mockCompiler.getSourceExtensions()).thenReturn(new String[] { "md", "markdown" });
    when(mockCompiler.getOutputExtension()).thenReturn("html");
    // when(mockCompiler.compile("md", mockInput, null)).thenReturn(compiledOutput);
    // when(mockCompiler.compile("markdown", mockInput, null)).thenReturn(compiledOutput);
    // when(mockCompiler.compile("mkdwn", mockInput, null)).thenReturn(compiledOutput);
    doAnswer(invocation -> {
        invocation.callRealMethod();
        return null;
    }).when(mockCompiler).compile(any(), any(), any(), any(), any());
    Set<OrchidParser> parsers = new HashSet<>();
    mockParser = mock(OrchidParser.class);
    parsers.add(mockParser);
    when(mockParser.getSourceExtensions()).thenReturn(new String[] { "yml", "yaml" });
    when(mockParser.parse("yml", "input")).thenReturn(parsedOutput);
    when(mockParser.parse("yaml", "input")).thenReturn(parsedOutput);
    mockPrecompiler = mock(OrchidPrecompiler.class);
    when(mockPrecompiler.getEmbeddedData("", mockInput)).thenReturn(precompilerEmbeddedData);
    // test the service directly
    context = mock(OrchidContext.class);
    when(context.getSiteData(any())).thenReturn(null);
    when(context.resolveSet(OrchidCompiler.class)).thenReturn(compilers);
    when(context.resolveSet(OrchidParser.class)).thenReturn(parsers);
    when(context.resolve(OrchidPrecompiler.class)).thenReturn(mockPrecompiler);
    service = new CompilerServiceImpl();
    service.initialize(context);
    service.allConfig = new HashMap<>();
    // add custom compiler extensions
    service.customCompilerExtensions = new JSONObject();
    service.customCompilerExtensions.put("mkdwn", "md");
    service.onPostExtraction();
    // test that the default implementation is identical to the real implementation
    underTest = new CompilerService() {

        public void initialize(OrchidContext context) {
        }

        public <T extends OrchidService> T getService(Class<T> serviceClass) {
            return (T) service;
        }
    };
}
Also used : OrchidContext(com.eden.orchid.api.OrchidContext) JSONObject(org.json.JSONObject) HashSet(java.util.HashSet) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with OrchidContext

use of com.eden.orchid.api.OrchidContext in project Orchid by JavaEden.

the class OrchidModule method bindKnownSet.

private void bindKnownSet(final Class<?> setClass) {
    Provider<OrchidContext> contextProvider = getProvider(OrchidContext.class);
    if (!knownSets.contains(setClass)) {
        Multibinder<AdminList> binder = Multibinder.newSetBinder(binder(), AdminList.class);
        binder.addBinding().toInstance(new AdminList() {

            @Override
            public Class<?> getListClass() {
                return setClass;
            }

            @Override
            public String getKey() {
                return setClass.getSimpleName();
            }

            @Override
            public Collection<Class<?>> getItems() {
                return contextProvider.get().resolveSet(setClass).stream().map(Object::getClass).collect(Collectors.toList());
            }

            @Override
            public boolean isImportantType() {
                return setClass.isAnnotationPresent(ImportantModularType.class);
            }
        });
        knownSets.add(setClass);
    }
}
Also used : AdminList(com.eden.orchid.api.server.admin.AdminList) OrchidContext(com.eden.orchid.api.OrchidContext) Collection(java.util.Collection) ImportantModularType(com.eden.orchid.api.server.annotations.ImportantModularType)

Aggregations

OrchidContext (com.eden.orchid.api.OrchidContext)14 OrchidPage (com.eden.orchid.api.theme.pages.OrchidPage)6 TemplateTag (com.eden.orchid.api.compilers.TemplateTag)4 PebbleWrapperTemplateTag (com.eden.orchid.impl.compilers.pebble.PebbleWrapperTemplateTag)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Map (java.util.Map)3 OrchidResource (com.eden.orchid.api.resources.resource.OrchidResource)2 StringResource (com.eden.orchid.api.resources.resource.StringResource)2 ModularList (com.eden.orchid.api.theme.components.ModularList)2 OrchidReference (com.eden.orchid.api.theme.pages.OrchidReference)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 JSONObject (org.json.JSONObject)2 Clog (com.caseyjbrooks.clog.Clog)1 JSONElement (com.eden.common.json.JSONElement)1 OrchidSecurityManager (com.eden.orchid.api.OrchidSecurityManager)1 OrchidRootIndex (com.eden.orchid.api.indexing.OrchidRootIndex)1 OptionsExtractor (com.eden.orchid.api.options.OptionsExtractor)1 ImpliedKey (com.eden.orchid.api.options.annotations.ImpliedKey)1