-
Notifications
You must be signed in to change notification settings - Fork 328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented Categories Support #293
base: master
Are you sure you want to change the base?
Changes from 4 commits
c56e6c8
78ef260
5c8fd60
d602719
f07b85b
f6cd76d
8415647
27f41eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
import org.apache.commons.configuration.Configuration; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.jbake.app.ConfigUtil.Keys; | ||
import org.jbake.app.Crawler; | ||
import org.json.simple.JSONValue; | ||
|
@@ -140,6 +141,31 @@ public Map<String, Object> parse(Configuration config, File file, String content | |
content.put(Crawler.Attributes.TAGS, tags); | ||
} | ||
|
||
// If categories are not disabled then add a default category | ||
if(config.getBoolean(Keys.CATEGORIES_ENABLE)){ | ||
if (content.get("categories") != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here it would be better to use |
||
String[] categories = (String[]) content.get("categories"); | ||
for( int i=0; i<categories.length; i++ ) { | ||
categories[i]=categories[i].trim(); | ||
if (config.getBoolean(Keys.CATEGORY_SANITIZE)) { | ||
categories[i]=categories[i].replace(" ", "-"); | ||
} | ||
} | ||
content.put("categories", categories); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here it would be better to use |
||
} else { | ||
String defaultCategory = config.getString(Keys.CATEGORY_DEFAULT).trim(); | ||
if (config.getBoolean(Keys.CATEGORY_SANITIZE,false)) { | ||
defaultCategory = defaultCategory.replace(" ", "-"); | ||
} | ||
content.put("categories", ArrayUtils.toArray(defaultCategory)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here it would be better to use Crawler.Attributes.CATEGORIES instead of the string value "categories" |
||
} | ||
content.put("primary_category", ((String[])content.get("categories"))[0]); | ||
} else { | ||
if (content.get("categories") != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here it would be better to use Crawler.Attributes.CATEGORIES instead of the string value "categories" |
||
LOGGER.warn("categories.enable is set as false but post {} has categories specifid. Categories will be ignored.", file); | ||
} | ||
} | ||
|
||
// TODO: post parsing plugins to hook in here? | ||
|
||
return content; | ||
|
@@ -233,7 +259,13 @@ private void processHeader(Configuration config, List<String> contents, final Ma | |
} | ||
} else if (key.equalsIgnoreCase(Crawler.Attributes.TAGS)) { | ||
content.put(key, getTags(value)); | ||
} else if (isJson(value)) { | ||
} else if (key.equalsIgnoreCase("categories")){ | ||
List<String> categories = new ArrayList<String>(); | ||
for (String category : parts[1].split(",")){ | ||
categories.add(category.trim()); | ||
} | ||
content.put(parts[0], categories.toArray(new String[0])); | ||
} else if (isJson(value)) { | ||
content.put(key, JSONValue.parse(value)); | ||
} else { | ||
content.put(key, value); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.jbake.render; | ||
|
||
import java.io.File; | ||
|
||
import org.apache.commons.configuration.CompositeConfiguration; | ||
import org.jbake.app.ConfigUtil.Keys; | ||
import org.jbake.app.ContentStore; | ||
import org.jbake.app.Renderer; | ||
import org.jbake.template.RenderingException; | ||
|
||
/** | ||
* | ||
* This class renders Post Categories. | ||
* | ||
* @author Manik Magar <[email protected]> | ||
* | ||
*/ | ||
public class CategoriesRenderer implements RenderingTool { | ||
|
||
@Override | ||
public int render(Renderer renderer, ContentStore db, File destination, File templatesPath, CompositeConfiguration config) throws RenderingException { | ||
if (config.getBoolean(Keys.RENDER_CATEGORIES)) { | ||
try { | ||
return renderer.renderCategories(config.getString(Keys.CATEGORY_PATH)); | ||
} catch (Exception e) { | ||
throw new RenderingException(e); | ||
} | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.jbake.template.model; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.jbake.app.ConfigUtil.Keys; | ||
import org.jbake.app.ContentStore; | ||
import org.jbake.app.DocumentList; | ||
import org.jbake.template.ModelExtractor; | ||
|
||
|
||
/** | ||
* | ||
* This extractor model will list of categories from all published content. | ||
* | ||
* @author Manik Magar <[email protected]> | ||
* | ||
*/ | ||
public class AllCategoriesExtractor implements ModelExtractor<DocumentList> { | ||
|
||
@Override | ||
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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to reference the crawler attribute instead of using a hard coded string value:
Crawler.Attributes.CATEGORIES
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @GeorgHenkel, Thanks. I will make the changes.