Skip to content

Commit

Permalink
Rebased with master
Browse files Browse the repository at this point in the history
  • Loading branch information
manikmagar committed Jun 8, 2017
1 parent 5c8fd60 commit d602719
Show file tree
Hide file tree
Showing 21 changed files with 238 additions and 106 deletions.
32 changes: 20 additions & 12 deletions src/main/java/org/jbake/app/ContentStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,27 @@
*/
package org.jbake.app;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
import com.orientechnologies.orient.core.db.OPartitionedDatabasePoolFactory;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentPool;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import org.jbake.model.DocumentAttributes;
import org.jbake.model.DocumentTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author jdlee
Expand Down Expand Up @@ -157,6 +156,15 @@ public DocumentList getPublishedDocumentsByTag(String tag) {
}
return documents;
}

public DocumentList getPublishedDocumentsByCategory(String category) {
final DocumentList documents = new DocumentList();
for (final String docType : DocumentTypes.getDocumentTypes()) {
DocumentList documentsByTag = query("select * from " + docType + " where status='published' and ? in categories order by date desc", category);
documents.addAll(documentsByTag);
}
return documents;
}

public DocumentList getPublishedPages() {
return getPublishedContent("page");
Expand Down
39 changes: 24 additions & 15 deletions src/main/java/org/jbake/template/model/AllCategoriesExtractor.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.jbake.template.model;

import java.util.Collections;
import java.util.HashSet;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ContentStore;
import org.jbake.app.Crawler;
import org.jbake.app.DBUtil;
import org.jbake.app.DocumentList;
import org.jbake.template.ModelExtractor;

Expand All @@ -16,20 +14,31 @@
*
* This extractor model will list of categories from all published content.
*
* @author Manik MAgar <[email protected]>
* @author Manik Magar <[email protected]>
*
*/
public class AllCategoriesExtractor implements ModelExtractor<Set<String>> {
public class AllCategoriesExtractor implements ModelExtractor<DocumentList> {

@Override
public Set<String> get(ContentStore db, Map model, String key) {
DocumentList query = db.getAllCategoriesFromPublishedPosts();
Set<String> result = new HashSet<String>();
for (Map<String, Object> document : query) {
String[] categories = DBUtil.toStringArray(document.get(Crawler.Attributes.CATEGORIES));
Collections.addAll(result, categories);
}
return result;
public DocumentList get(ContentStore db, Map model, String key) {
DocumentList dl = new DocumentList();
Map<String, Object> config = (Map<String, Object>) model.get("config");

String categoryPath = config.get(Keys.CATEGORY_PATH.replace(".", "_")).toString();

for (String category : db.getCategories()){
Map<String, Object> newCategory = new HashMap<String, Object>();
String tagName = category;
newCategory.put("name",tagName);

String uri = categoryPath + File.separator + category + config.get(Keys.OUTPUT_EXTENSION.replace(".", "_")).toString();

newCategory.put("uri", uri);
newCategory.put("posts", db.getPublishedPostsByCategories(category));
newCategory.put("documents", db.getPublishedDocumentsByCategory(category));
dl.push(newCategory);
}
return dl;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jbake.template.model;

import java.util.Map;

import org.jbake.app.ContentStore;
import org.jbake.app.Crawler;
import org.jbake.app.DocumentList;
import org.jbake.template.ModelExtractor;


public class CategoryDocumentsExtractor implements ModelExtractor<DocumentList> {

@Override
public DocumentList get(ContentStore db, Map model, String key) {
String category = null;
if (model.get(Crawler.Attributes.CATEGORY) != null) {
category = model.get(Crawler.Attributes.CATEGORY).toString();
}
DocumentList query = db.getPublishedDocumentsByCategory(category);
return query;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ org.jbake.template.model.DBExtractor=db
org.jbake.template.model.TagPostsExtractor=tag_posts
org.jbake.template.model.TaggedDocumentsExtractor=tagged_documents
org.jbake.template.model.CategoryPostsExtractor=category_posts
org.jbake.template.model.AllCategoriesExtractor=all_categories
org.jbake.template.model.CategoryDocumentsExtractor=category_documents
org.jbake.template.model.AllCategoriesExtractor=categories
2 changes: 1 addition & 1 deletion src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ template.page.file=page.ftl
# filename of page template file
template.category.file=category.ftl
# file name of page template for {site.url}/categories
template.categories.file=categories.ftl
template.categories.file=categories-index.ftl
# folder that contains all content files
content.folder=content
# folder that contains all asset files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ private void setupExpectedOutputStrings() {
outputStrings.put("sitemap", Arrays.asList("blog/2013/second-post.html",
"blog/2012/first-post.html",
"papers/published-paper.html"));


outputStrings.put("category", Arrays.asList("blog/2012/first-post.html"));

outputStrings.put("categories", Arrays.asList("categories/Technology.html"));

}

Expand Down Expand Up @@ -272,6 +277,23 @@ protected List<String> getOutputStrings(String type) {

@Test
public void renderCategories() throws Exception {


renderer.renderCategories("categories");

// verify
File outputFile = new File(destinationFolder + File.separator + "categories" + File.separator + "Technology.html");
Assert.assertTrue(outputFile.exists());
String output = FileUtils.readFileToString(outputFile);
for (String string : outputStrings.get("category")) {
assertThat(output).contains(string);
}

// verify index.html file
File indexFile = new File(destinationFolder + File.separator + "categories" + File.separator + "index.html");
Assert.assertTrue(indexFile.exists());
String indexData = FileUtils.readFileToString(indexFile);
for (String string : outputStrings.get("categories")) {
assertThat(indexData).contains(string);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,56 +85,4 @@ public void shouldFallbackToRenderSingleIndexIfNoPostArePresent() throws Excepti
assertTrue("index file exists",indexFile.exists());

}


@Test
public void renderCategoriesNoExtension() throws Exception {
config.setProperty(Keys.URI_NO_EXTENSION, true);
config.setProperty(Keys.URI_NO_EXTENSION_PREFIX, "blog");
Crawler crawler = new Crawler(db, sourceFolder, config);
crawler.crawl(new File(sourceFolder.getPath() + File.separator + "content"));
Renderer renderer = new Renderer(db, destinationFolder, templateFolder, config);
renderer.renderCategories("categories");

// verify
File outputFile = new File(destinationFolder + File.separator + "categories" + File.separator + "Technology" + File.separator + "index.html");
Assert.assertTrue(outputFile.exists());
String output = FileUtils.readFileToString(outputFile);
for (String string : outputStrings.get("categories_noextension")) {
assertThat(output).contains(string);
}

// verify index.html file
File indexFile = new File(destinationFolder + File.separator + "categories" + File.separator + "index.html");
Assert.assertTrue(indexFile.exists());
String indexData = FileUtils.readFileToString(indexFile);
for (String string : outputStrings.get("categories_index_noextension")) {
assertThat(indexData).contains(string);
}
}
@Test
@Override
public void renderCategories() throws Exception {
config.setProperty(Keys.URI_NO_EXTENSION, false);
Crawler crawler = new Crawler(db, sourceFolder, config);
crawler.crawl(new File(sourceFolder.getPath() + File.separator + "content"));
Renderer renderer = new Renderer(db, destinationFolder, templateFolder, config);
renderer.renderCategories("categories");

// verify
File outputFile = new File(destinationFolder + File.separator + "categories" + File.separator + "Technology.html");
Assert.assertTrue(outputFile.exists());
String output = FileUtils.readFileToString(outputFile);
for (String string : outputStrings.get("categories")) {
assertThat(output).contains(string);
}

// verify index.html file
File indexFile = new File(destinationFolder + File.separator + "categories" + File.separator + "index.html");
Assert.assertTrue(indexFile.exists());
String indexData = FileUtils.readFileToString(indexFile);
for (String string : outputStrings.get("categories_index")) {
assertThat(indexData).contains(string);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.jbake.app.template;

import java.util.Arrays;

public class JadeTemplateEngineRenderingTest extends AbstractTemplateEngineRenderingTest {

public JadeTemplateEngineRenderingTest() {
super("jadeTemplates", "jade");
}

@Override
public void renderCategories() throws Exception {

}
}
6 changes: 4 additions & 2 deletions src/test/java/org/jbake/template/ModelExtractorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.rules.ExpectedException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;

public class ModelExtractorsTest {

Expand All @@ -31,10 +30,13 @@ public void shouldLoadExtractorsOnInstantiation() {
"alltags",
"db",
"tag_posts",
"category_posts",
"category_documents",
"categories"
};

for (String aKey : expectedKeys) {
assertThat(ModelExtractors.getInstance().containsKey(aKey)).isTrue();
assertThat(ModelExtractors.getInstance().containsKey(aKey)).as("Extractor with key %s to exist", aKey).isTrue();
}
}

Expand Down
16 changes: 0 additions & 16 deletions src/test/resources/content/blog/2012/first-post.html

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
type=post
tags=blog
status=published
categories=Technology
~~~~~~

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel diam purus. Curabitur ut nisi lacus.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</div>

<ul>
<#list content.categories?keys as category>
<li><a href="${content.categories[category]}"/>${category}</a></li>
<#list categories as category>
<li><a href="${category.uri}"/>${category.name}</a></li>
</#list>
</ul>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fixture.groovyMarkupTemplates

layout 'layout/main.tpl',
bodyContents: contents {
div(class:"row-fluid marketing"){
div(class:"span12"){
h1("Category List")
div{
categories.each { cat ->
a(href:"${cat.uri}","${cat.name}")
}

}
}
}

hr()
}
32 changes: 32 additions & 0 deletions src/test/resources/fixture/groovyMarkupTemplates/category.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fixture.groovyMarkupTemplates

layout 'layout/main.tpl',
bodyContents: contents {
div(class:"row-fluid marketing"){
div(class:"span12"){
h2('Category')
def last_month
category_posts.each { post ->
if (last_month) {
if (post.date.format("MMMM yyyy") != last_month) {
h3("${post.date.format("MMMM yyyy")}")
}
}
else {
h3("${post.date.format("MMMM yyyy")}")
}

h4 {
yield "${post.date.format("dd MMMM")} - "
a(href:"${post.uri}","${post.title}")
}
last_month = post.date.format("MMMM yyyy")
}
}
}

hr()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</div>
<div class="category-posts">
<ul>
<%content.categories.each {category,path ->%>
<li><a href="${path}"/>${category}</a></li>
<%categories.each {category ->%>
<li><a href="${category.uri}"/>${category.name}</a></li>
<%}%>
</ul>
</div>
Expand Down
Loading

0 comments on commit d602719

Please sign in to comment.