From 616f48c5c4f37c47ae86a0d1650745b38459b141 Mon Sep 17 00:00:00 2001 From: Emmanuel Rosa Date: Sat, 24 Oct 2015 15:17:50 -0400 Subject: [PATCH 1/4] modify ResoureMapper to allow customizing the blog post URLs ResourceMapper.postUrlBasePath - The base path used for all posts ResourceMapper.createPostUrl - a Closure which returns the post URL relative to postUrlBasePath Signed-off-by: Emmanuel Rosa --- .../octopress/mapping/ResourceMapper.groovy | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy b/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy index dfc7b63..dbf77d0 100644 --- a/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy +++ b/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy @@ -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) @@ -41,6 +47,21 @@ class ResourceMapper { 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/" + } + /** * Excludes resources with published property set to false, * unless it is allowed to show unpublished resources in SiteConfig. @@ -72,7 +93,7 @@ class ResourceMapper { update.url = getFingerprintUrl(resource) break case ~/\/blog\/.*/: - update.url = getPostUrl('/blog/', resource) + update.url = getPostUrl(postUrlBasePath, resource) break } @@ -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 @@ -175,7 +196,8 @@ class ResourceMapper { } /** - * Creates URL for a post page. The URL format is '{base path}/{year}/{month}/{day}/{post name}'. + * 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 @@ -183,9 +205,7 @@ class ResourceMapper { * @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)}" } /** From 63dfe3a92032efcdffc22380f175574e077e420b Mon Sep 17 00:00:00 2001 From: Emmanuel Rosa Date: Sat, 24 Oct 2015 15:22:26 -0400 Subject: [PATCH 2/4] add a guard to the ResourceMapper to check for duplicate URLs After the models are customized, all of the resource URLs are checked to ensure they are unique. If a duplicate is found, a RuntimeException is thrown containing the duplicate URL in the message. Signed-off-by: Emmanuel Rosa --- .../octopress/mapping/ResourceMapper.groovy | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy b/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy index dbf77d0..055aa3e 100644 --- a/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy +++ b/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy @@ -44,7 +44,7 @@ class ResourceMapper { resource }.sort { -it.date.time } - customizeModels << addSiteMenu << customizeAsides << refinedResources + checkForDuplicateUrls << customizeModels << addSiteMenu << customizeAsides << refinedResources } /** @@ -195,6 +195,24 @@ class ResourceMapper { } } + /** + * 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 -> + def urls = [:] + + resources.each { resource -> + if(urls.containsKey(resource.url)) throw new RuntimeException("Encountered duplicate resource URL: $resource.url") + else urls[resource.url] = null + } + + resources + } + /** * Creates URL for a post page. Delegates to createPostUrl() to provide the URL format * relative to the basePath. From 2b8d617516000b4412bb855d96f74ac2307a2bd0 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlenko Date: Mon, 2 Nov 2015 11:42:01 +0200 Subject: [PATCH 3/4] Updated the page URL validation. Signed-off-by: Dmitriy Pavlenko --- .../com/sysgears/octopress/mapping/ResourceMapper.groovy | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy b/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy index 055aa3e..312385f 100644 --- a/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy +++ b/theme/src/com/sysgears/octopress/mapping/ResourceMapper.groovy @@ -203,11 +203,8 @@ class ResourceMapper { * @return the resources unchanged. */ private def checkForDuplicateUrls = { List resources -> - def urls = [:] - - resources.each { resource -> - if(urls.containsKey(resource.url)) throw new RuntimeException("Encountered duplicate resource URL: $resource.url") - else urls[resource.url] = null + resources.groupBy { it.url }.find { it.value.size() > 1 }?.value*.url?.unique()?.each { + throw new RuntimeException("Encountered duplicate resource URL: $it") } resources From 818138fa5b1cbb3cf98a67b1664deeadf7fb2b8a Mon Sep 17 00:00:00 2001 From: Evgenij Olshanskij Date: Tue, 19 Jan 2016 13:27:41 +0200 Subject: [PATCH 4/4] Updated mainClassName variable initializing. Signed-off-by: Evgenij Olshanskij --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index a3a9d80..a7d6e00 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ version = '0.4.0' defaultTasks 'preview' ext { - mainClassName = [project.group, 'Main'].join('.') + mainClassName = 'com.sysgears.grain.Main' compatibilityVersion = JavaVersion.VERSION_1_5 }