Search in sources :

Example 1 with EdenPair

use of com.eden.common.util.EdenPair in project Orchid by JavaEden.

the class PostsGenerator method startIndexing.

@Override
public List<? extends OrchidPage> startIndexing() {
    // Map category names to pairs of PostPages and PostArchivePages
    Map<String, EdenPair<List<PostPage>, List<PostArchivePage>>> categories = new HashMap<>();
    List<String> categoryNames = new ArrayList<>();
    if (OrchidUtils.elementIsArray(context.query("options.posts.categories"))) {
        JSONArray categoriesArray = (JSONArray) context.query("options.posts.categories").getElement();
        for (int i = 0; i < categoriesArray.length(); i++) {
            if (!EdenUtils.isEmpty(categoriesArray.getString(i))) {
                categoryNames.add(categoriesArray.getString(i));
            }
        }
    } else {
        categoryNames.add(null);
    }
    for (String category : categoryNames) {
        List<PostPage> posts = getPostsList(category);
        List<PostArchivePage> archive = buildArchive(category, posts);
        categories.put(category, new EdenPair<>(posts, archive));
    }
    List<OrchidPage> allPages = new ArrayList<>();
    for (String key : categories.keySet()) {
        allPages.addAll(categories.get(key).first);
        allPages.addAll(categories.get(key).second);
    }
    return allPages;
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) EdenPair(com.eden.common.util.EdenPair)

Example 2 with EdenPair

use of com.eden.common.util.EdenPair in project Orchid by JavaEden.

the class FrontMatterPrecompiler method parseFrontMatter.

private EdenPair<JSONObject, Integer> parseFrontMatter(String input) {
    if (input.startsWith("---")) {
        Matcher m = Pattern.compile("^---(\\w+)?$", Pattern.MULTILINE).matcher(input);
        int matches = 0;
        int fmStart = 0;
        int fmEnd = 0;
        int contentStart = 0;
        // if we find a match, get the group
        while (m.find()) {
            if (matches == 0) {
                fmStart = m.end();
                matches++;
            } else if (matches == 1) {
                fmEnd = m.start();
                contentStart = m.end();
                matches++;
                break;
            }
        }
        if (matches == 2) {
            String parsedType = input.substring(0, fmStart).replaceAll("---", "");
            List<String> extensions = new ArrayList<>();
            if (!EdenUtils.isEmpty(parsedType)) {
                extensions.add(parsedType);
            }
            if (!EdenUtils.isEmpty(context.query("options.frontMatterExt"))) {
                extensions.add(context.query("options.frontMatterExt").toString());
            }
            extensions.add(defaultType);
            final String frontMatterText = input.substring(fmStart, fmEnd);
            JSONObject frontMatter = extensions.stream().map(ext -> context.getTheme().parse(ext, frontMatterText)).filter(OrchidUtils::elementIsObject).map(el -> (JSONObject) el.getElement()).findFirst().orElseGet(JSONObject::new);
            return new EdenPair<>(frontMatter, contentStart);
        }
    }
    return new EdenPair<>(null, 0);
}
Also used : JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) EdenPair(com.eden.common.util.EdenPair)

Aggregations

EdenPair (com.eden.common.util.EdenPair)2 ArrayList (java.util.ArrayList)2 OrchidPage (com.eden.orchid.api.theme.pages.OrchidPage)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1