Search in sources :

Example 1 with OrchidResource

use of com.eden.orchid.api.resources.resource.OrchidResource in project Orchid by JavaEden.

the class HomepageGenerator method startIndexing.

@Override
public List<? extends OrchidPage> startIndexing() {
    List<OrchidPage> pages = new ArrayList<>();
    OrchidResource resource = new StringResource(context, "index.twig", "");
    OrchidPage page = new OrchidPage(resource);
    page.getReference().setTitle("Home");
    page.setType("frontPage");
    page.addComponent("readme", ReadmeComponent.class);
    page.addComponent("license", LicenseComponent.class);
    pages.add(page);
    return pages;
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) StringResource(com.eden.orchid.api.resources.resource.StringResource) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) ArrayList(java.util.ArrayList)

Example 2 with OrchidResource

use of com.eden.orchid.api.resources.resource.OrchidResource in project Orchid by JavaEden.

the class IndexGenerator method generateIndexFiles.

private void generateIndexFiles() {
    OrchidRootInternalIndex internalIndex = context.getInternalIndex();
    Map<String, OrchidInternalIndex> mappedIndex = internalIndex.getAllIndexedPages();
    OrchidIndex indices = new OrchidInternalIndex("indices");
    // Render an page for each generator's individual index
    mappedIndex.keySet().forEach(key -> {
        OrchidResource resource = new JsonResource(new JSONElement(mappedIndex.get(key).toJSON()), new OrchidReference(context, "meta/" + key + ".index.json"));
        OrchidPage page = new OrchidPage(resource);
        page.renderRaw();
        indices.addToIndex(indices.getOwnKey() + "/" + page.getReference().getPath(), page);
    });
    // Render full composite index page
    OrchidResource compositeIndexResource = new JsonResource(new JSONElement(internalIndex.toJSON()), new OrchidReference(context, "meta/index.json"));
    OrchidPage compositeIndexPage = new OrchidPage(compositeIndexResource);
    compositeIndexPage.renderRaw();
    indices.addToIndex(indices.getOwnKey() + "/" + compositeIndexPage.getReference().getPath(), compositeIndexPage);
    // Render an index of all indices, so individual index pages can be found
    OrchidResource indicesIndexResource = new JsonResource(new JSONElement(indices.toJSON()), new OrchidReference(context, "meta/indices.json"));
    OrchidPage indicesPage = new OrchidPage(indicesIndexResource);
    indicesPage.renderRaw();
}
Also used : OrchidRootInternalIndex(com.eden.orchid.impl.indexing.OrchidRootInternalIndex) OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidReference(com.eden.orchid.api.theme.pages.OrchidReference) OrchidInternalIndex(com.eden.orchid.impl.indexing.OrchidInternalIndex) JSONElement(com.eden.common.json.JSONElement) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) JsonResource(com.eden.orchid.api.resources.resource.JsonResource) OrchidIndex(com.eden.orchid.api.indexing.OrchidIndex)

Example 3 with OrchidResource

use of com.eden.orchid.api.resources.resource.OrchidResource in project Orchid by JavaEden.

the class SassImporter method apply.

@Override
public Collection<Import> apply(String url, Import previous) {
    EdenPair<String, String> thisItem = splitPath(url);
    String[] availableFiles = new String[] { thisItem.first + "/" + thisItem.second + ".scss", thisItem.first + "/" + thisItem.second + ".sass", thisItem.first + "/" + "_" + thisItem.second + ".scss", thisItem.first + "/" + "_" + thisItem.second + ".sass" };
    for (String availableFile : availableFiles) {
        String absoluteUri = OrchidUtils.normalizePath(splitPath(previous.getAbsoluteUri().getPath()).first + "/" + availableFile);
        if (absoluteUri.contains("//")) {
            absoluteUri = absoluteUri.replaceAll("//", "/");
        }
        if (absoluteUri.startsWith("/")) {
            absoluteUri = absoluteUri.substring(1);
        }
        OrchidResource importedResource = resources.get().getResourceEntry("assets/css/" + absoluteUri);
        if (importedResource != null) {
            String content = importedResource.getContent();
            if (importedResource.shouldPrecompile()) {
                content = context.getTheme().precompile(content, importedResource.getEmbeddedData());
            }
            try {
                String newURI = "" + OrchidUtils.normalizePath(absoluteUri);
                Import newImport = new Import(newURI, newURI, content);
                return Collections.singletonList(newImport);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : Import(io.bit3.jsass.importer.Import) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource)

Example 4 with OrchidResource

use of com.eden.orchid.api.resources.resource.OrchidResource in project Orchid by JavaEden.

the class DefaultResourceSource method getResourceEntries.

@Override
public List<OrchidResource> getResourceEntries(String path, String[] fileExtensions, boolean recursive) {
    List<OrchidResource> entries = new ArrayList<>();
    JarFile jarFile = jarForClass(this.getClass());
    if (jarFile == null) {
        return entries;
    }
    Enumeration<JarEntry> jarEntries = jarFile.entries();
    while (jarEntries.hasMoreElements()) {
        JarEntry jarEntry = jarEntries.nextElement();
        // we are checking a file in the jar
        if (OrchidUtils.normalizePath(jarEntry.getName()).startsWith(path + "/") && !jarEntry.isDirectory()) {
            if (EdenUtils.isEmpty(fileExtensions) || FilenameUtils.isExtension(jarEntry.getName(), fileExtensions)) {
                if (shouldAddEntry(jarEntry.getName())) {
                    entries.add(new JarResource(context, jarFile, jarEntry));
                }
            }
        }
    }
    return entries;
}
Also used : JarResource(com.eden.orchid.api.resources.resource.JarResource) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 5 with OrchidResource

use of com.eden.orchid.api.resources.resource.OrchidResource in project Orchid by JavaEden.

the class PostsGenerator method getPostsList.

private List<PostPage> getPostsList(String category) {
    List<OrchidResource> resourcesList;
    if (EdenUtils.isEmpty(category)) {
        resourcesList = resources.getLocalResourceEntries("posts", null, false);
    } else {
        resourcesList = resources.getLocalResourceEntries("posts/" + category, null, false);
    }
    List<PostPage> posts = new ArrayList<>();
    for (OrchidResource entry : resourcesList) {
        Matcher matcher = pageTitleRegex.matcher(entry.getReference().getFileName());
        if (matcher.matches()) {
            JSONObject pageData = (OrchidUtils.elementIsObject(entry.getEmbeddedData())) ? (JSONObject) entry.getEmbeddedData().getElement() : new JSONObject();
            int year = Integer.parseInt(matcher.group(1));
            int month = Integer.parseInt(matcher.group(2));
            int day = Integer.parseInt(matcher.group(3));
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            int second = calendar.get(Calendar.SECOND);
            String title = matcher.group(4);
            if (!EdenUtils.isEmpty(entry.queryEmbeddedData("title"))) {
                title = entry.queryEmbeddedData("title").toString();
            } else {
                title = Arrays.stream(title.split("-")).map(word -> word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase()).collect(Collectors.joining(" "));
            }
            if (entry.queryEmbeddedData("date") != null) {
                String date = entry.queryEmbeddedData("date").toString();
                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
                    Date dateTime = formatter.parse(date);
                    calendar = Calendar.getInstance();
                    calendar.setTime(dateTime);
                    year = calendar.get(Calendar.YEAR);
                    month = calendar.get(Calendar.YEAR);
                    day = calendar.get(Calendar.YEAR);
                    hour = calendar.get(Calendar.HOUR_OF_DAY);
                    minute = calendar.get(Calendar.MINUTE);
                    second = calendar.get(Calendar.SECOND);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            pageData.put("year", year);
            pageData.put("month", month);
            pageData.put("day", day);
            pageData.put("hour", hour);
            pageData.put("minute", minute);
            pageData.put("second", second);
            if (EdenUtils.isEmpty(category)) {
                entry.getReference().stripFromPath("blog");
            } else {
                pageData.put("category", category);
                entry.getReference().stripFromPath("blog/" + category);
            }
            pageData.put("title", title);
            setPermalink(entry.getReference(), pageData);
            PostPage post = new PostPage(entry);
            post.setType("post");
            post.setData(pageData);
            String excerpt = excerptStrategy.getExcerpt(post);
            post.getData().put("excerpt", excerpt);
            posts.add(post);
        }
    }
    posts.sort((o1, o2) -> {
        String[] criteria = new String[] { "year", "month", "day", "hour", "minute", "second" };
        for (String item : criteria) {
            if (o1.getData().has(item) && o1.getData().has(item)) {
                int result = o2.getData().optInt(item) - o1.getData().optInt(item);
                if (result != 0) {
                    return result;
                }
            }
        }
        return 0;
    });
    int i = 0;
    for (PostPage post : posts) {
        if (next(posts, i) != null) {
            post.setNext(next(posts, i));
        }
        if (previous(posts, i) != null) {
            post.setPrevious(previous(posts, i));
        }
        i++;
    }
    return posts;
}
Also used : Matcher(java.util.regex.Matcher) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Date(java.util.Date) JSONObject(org.json.JSONObject) OrchidResource(com.eden.orchid.api.resources.resource.OrchidResource) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

OrchidResource (com.eden.orchid.api.resources.resource.OrchidResource)18 OrchidPage (com.eden.orchid.api.theme.pages.OrchidPage)7 ArrayList (java.util.ArrayList)7 StringResource (com.eden.orchid.api.resources.resource.StringResource)6 JSONObject (org.json.JSONObject)6 OrchidReference (com.eden.orchid.api.theme.pages.OrchidReference)4 OrchidResponse (com.eden.orchid.api.server.OrchidResponse)3 File (java.io.File)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 OrchidContext (com.eden.orchid.api.OrchidContext)2 OrchidResponse (com.eden.orchid.server.api.OrchidResponse)2 Get (com.eden.orchid.server.api.methods.Get)2 Document (org.jsoup.nodes.Document)2 Element (org.jsoup.nodes.Element)2 Clog (com.caseyjbrooks.clog.Clog)1 JSONElement (com.eden.common.json.JSONElement)1 OrchidIndex (com.eden.orchid.api.indexing.OrchidIndex)1 OrchidRootIndex (com.eden.orchid.api.indexing.OrchidRootIndex)1