Skip to content
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

Add a smart excerpt/summary generator. #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 78 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@
<thymeleaf.version>2.1.3.RELEASE</thymeleaf.version>
<thymeleaf.extras.version>2.1.1.RELEASE</thymeleaf.extras.version>
<json-simple.version>1.1.1</json-simple.version>
<tika.version>1.9</tika.version><!-- Version 1.9 of Tika is the last one working with Java 6 -->
<tagsoup.version>1.2.1</tagsoup.version>
<sis.version>0.6</sis.version><!-- 0.5, 0.6 -->
<asm.version>5.0.4</asm.version><!-- 4.1, 5.0.4 -->
<guava.version>18.0</guava.version><!-- 11.0.2, 18.0 -->
<hamcrest.version>1.3</hamcrest.version>
<htmlcompressor.version>1.5.2</htmlcompressor.version>
</properties>

<build>
Expand Down Expand Up @@ -288,6 +295,7 @@
</resource>
</resources>
</build>

<dependencies>
<dependency>
<groupId>commons-io</groupId>
Expand All @@ -298,6 +306,12 @@
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>${commons.configuration.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
Expand Down Expand Up @@ -332,6 +346,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -348,6 +368,12 @@
<artifactId>pegdown</artifactId>
<version>${pegdown.version}</version>
<optional>true</optional>
<exclusions>
<exclusion><!-- provided by tika -->
<groupId>org.ow2.asm</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.asciidoctor</groupId>
Expand Down Expand Up @@ -391,25 +417,66 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<optional>true</optional>
</dependency>


<!-- excerpt -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
<optional>true</optional>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>${tika.version}</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${tika.version}</version>
</dependency>
<dependency>
<groupId>org.ccil.cowan.tagsoup</groupId>
<artifactId>tagsoup</artifactId>
<version>${tagsoup.version}</version>
</dependency>
<dependency>
<groupId>org.apache.sis.core</groupId>
<artifactId>sis-utility</artifactId>
<version>${sis.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-debug-all</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.htmlcompressor</groupId>
<artifactId>htmlcompressor</artifactId>
<version>${htmlcompressor.version}</version>
</dependency>

</dependencies>
</project>
61 changes: 51 additions & 10 deletions src/main/java/org/jbake/app/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,57 @@ public static interface Keys {
*/
static final String DRAFT_SUFFIX = "draft.suffix";

/**
* Output filename for feed file, is only used when {@link #RENDER_FEED} is true
*/
static final String FEED_FILE = "feed.file";

/**
* Output filename for index, is only used when {@link #RENDER_INDEX} is true
*/
static final String INDEX_FILE = "index.file";

/**
* Output filename for feed file, is only used when {@link #RENDER_FEED} is true
*/
static final String FEED_FILE = "feed.file";

/**
* Flag indicating if index page shows excerpts or full bodies,
* is only used when {@link #RENDER_INDEX} is true
*/
static final String INDEX_SUMMERY = "index.summery";

/**
* Flag indicating if feed page shows excerpts or full bodies,
* is only used when {@link #RENDER_INDEX} is true
*/
static final String FEED_SUMMERY = "feed.summery";

/**
* The max length of an excerpt used as summary.
*
* By default: -1, i.e. no limit.
*/
static final String SUMMERY_MAX_LENGTH = "summary.max.length";

/*
* The unit used to truncate the body, either character, Unicode code point or word.
*
* By default: world
*/
static final String SUMMERY_LENGTH_UNIT = "summary.length.unit";

/**
* The ellipsis used with an excerpt shorter than the page/post body content.
*
* By default: ...
*/
static final String SUMMERY_ELLIPSIS = "summary.ellipsis";

/**
* The readmore label used with an excerpt shorter than the page/post body content.
*
* If a pattern is provided then it will be interpolated with the post/page link.
* By default: empty string
*/
static final String SUMMERY_READMORE = "summary.readmore";

/**
* Output filename for index, is only used when {@link #RENDER_INDEX} is true
*/
static final String INDEX_FILE = "index.file";

/**
* File extension to be used for all output files
*/
Expand Down
178 changes: 178 additions & 0 deletions src/main/java/org/jbake/app/Content.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package org.jbake.app;

import static org.jbake.app.ContentTag.*;

import java.util.HashMap;
import java.util.Map;

/**
* Content model used by jbake.
*/
public class Content {

private final Map<String, Object> content;

public Map<String, Object> getContentAsMap() {
return content;
}

public Content() {
this.content = new HashMap<String, Object>();
}

public Content(final Map<String, Object> contents) {
this.content = new HashMap<String, Object>(contents);
}

public void putAll(final Content content) {
this.content.putAll(content.content);
}

public void putAll(final Map<String, Object> content) {
this.content.putAll(content);
}

public void put(final ContentTag key, final Object value) {
put(key.name(), value);
}

public void put(final String key, final Object value) {
if (value == null) {
throw new IllegalArgumentException("Content doesn't accept key with a null value, key here is: " + key);
}

content.put(key, value);
}

public Object get(final ContentTag key) {
return get(key.name());
}

public Object get(final String key) {
return content.get(key);
}

public boolean containsKey(final ContentTag key) {
return containsKey(key.name());
}

public boolean containsKey(final String key) {
return content.containsKey(key);
}

public String getString(final ContentTag key, final String defaultValue)
{
return getString(key.name(), defaultValue);
}

public String getString(final String key, final String defaultValue)
{
Object value = content.get(key);

if (value instanceof String)
{
return (String) value;
}
else if (value == null)
{
return defaultValue;
}
else
{
throw new IllegalArgumentException('\'' + key + "' doesn't map to a String object");
}
}

public int getInt(final ContentTag key, final int defaultValue)
{
return getInt(key.name(), defaultValue);
}

public int getInt(final String key, final int defaultValue)
{
Integer i = getInteger(key, null);

if (i == null)
{
return defaultValue;
}

return i.intValue();
}

public Integer getInteger(final String key, final Integer defaultValue)
{
Object value = content.get(key);

if (value == null)
{
return defaultValue;
}
return Integer.valueOf(value.toString());
}

public boolean getBoolean(final ContentTag key, final boolean defaultValue) {
return getBoolean(key.name(), defaultValue);
}

public boolean getBoolean(final String key, final boolean defaultValue) {
Object value = content.get(key);

if (value == null)
{
return defaultValue;
}
else
return Boolean.valueOf(key);
}

public void setStatus(final String s) {
put(status, s);
}

public void setStatus(final ContentStatus s) {
setStatus(s.key());
}

public ContentStatus getStatus() {
try {
return ContentStatus.valueOf(getString(status, null));
} catch (IllegalArgumentException e) {
throw new IllegalStateException("A content status must have a valid value.", e);
} catch (NullPointerException e) {
throw new IllegalStateException("A content status must be provided.", e);
}
}

public void tryToSetupStatusIfNeededWithDefaultValue(final ContentStatus defaultStatus) {
if (containsKey(status)) {
return; // source is providing a status so no need to use default
}
if (defaultStatus == null) {
return; // the status stays null: it should be catch lately
}
setStatus(defaultStatus);
return;
}

public boolean isWithValidHeader() {
return get(type) != null && get(status) != null;
}

@Override
public int hashCode() {
return content.hashCode();
}

@Override
public boolean equals(final Object obj) {
return content.equals(obj);
}

@Override
public String toString() {
return content.toString();
}


}
Loading