Skip to content

Commit

Permalink
Merge branch 'master' into 0.4.x
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Yermolaiev <[email protected]>
  • Loading branch information
oermolaev committed Sep 21, 2016
2 parents 5a85834 + 818138f commit 09b2548
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = '0.4.5'
defaultTasks 'preview'

ext {
mainClassName = [project.group, 'Main'].join('.')
mainClassName = 'com.sysgears.grain.Main'
compatibilityVersion = JavaVersion.VERSION_1_5
}

Expand Down
49 changes: 42 additions & 7 deletions theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class ResourceMapper {
*/
private TweetsFetcher tweetsFetcher

/**
* The blog post URL base path. Change the value to customize.
* Defaults to /blog/
*/
public String postUrlBasePath = '/blog/'

public ResourceMapper(Site site) {
this.site = site
tweetsFetcher = new TweetsFetcher(site)
Expand All @@ -38,7 +44,22 @@ class ResourceMapper {
resource
}.sort { -it.date.time }

customizeModels << addSiteMenu << customizeAsides << refinedResources
checkForDuplicateUrls << customizeModels << addSiteMenu << customizeAsides << refinedResources
}

/**
* Creates URL for a post page relative to postUrlBasePath.
* Replace with your own implementation to customize.
* The default URL format is '{year}/{month}/{day}/{post name}'.
*
* @param resource the blog post resource
*
* @return formatted url to the post page
*/
def createPostUrl = { Map resource ->
def date = resource.date.format('yyyy/MM/dd/')
def title = resource.title.encodeAsSlug()
"$date$title/"
}

/**
Expand Down Expand Up @@ -72,7 +93,7 @@ class ResourceMapper {
update.url = getFingerprintUrl(resource)
break
case ~/\/blog\/.*/:
update.url = getPostUrl('/blog/', resource)
update.url = getPostUrl(postUrlBasePath, resource)
break
}

Expand Down Expand Up @@ -141,7 +162,7 @@ class ResourceMapper {
page + [url: feedUrl, tag: tag, posts: postsByCategory(tag).take(maxRss)]
}
break
case ~/\/blog\/.*/:
case { it.startsWith(postUrlBasePath) }:
def post = posts.find { it.url == page.url }
def index = posts.indexOf(post)
def prev = index > 0 ? posts[index - 1] : null
Expand Down Expand Up @@ -175,17 +196,31 @@ class ResourceMapper {
}

/**
* Creates URL for a post page. The URL format is '{base path}/{year}/{month}/{day}/{post name}'.
* Ensures all resources have unique URLs.
*
* @param resources to validate.
* @throw RuntimeException when a duplicate URL is found.
* @return the resources unchanged.
*/
private def checkForDuplicateUrls = { List resources ->
resources.groupBy { it.url }.find { it.value.size() > 1 }?.value*.url?.unique()?.each {
throw new RuntimeException("Encountered duplicate resource URL: $it")
}

resources
}

/**
* Creates URL for a post page. Delegates to createPostUrl() to provide the URL format
* relative to the basePath.
*
* @param basePath base path to the page
* @param location location of the file
*
* @return formatted url to the post page
*/
private String getPostUrl(String basePath, Map resource) {
def date = resource.date.format('yyyy/MM/dd/')
def title = resource.title.encodeAsSlug()
"$basePath$date$title/"
"$basePath${createPostUrl(resource)}"
}

/**
Expand Down

0 comments on commit 09b2548

Please sign in to comment.