You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2017/11/23 11:51:47 UTC

[sling-site] branch master updated: Move common template utilities to their own file

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-site.git


The following commit(s) were added to refs/heads/master by this push:
     new e21451f  Move common template utilities to their own file
e21451f is described below

commit e21451fabcb1e7d36164b54f0393181b7f08d3f2
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Nov 23 12:51:34 2017 +0100

    Move common template utilities to their own file
---
 src/main/jbake/templates/includes/U.groovy | 75 ++++++++++++++++++++++++++++
 src/main/jbake/templates/page.tpl          | 78 ++----------------------------
 2 files changed, 80 insertions(+), 73 deletions(-)

diff --git a/src/main/jbake/templates/includes/U.groovy b/src/main/jbake/templates/includes/U.groovy
new file mode 100644
index 0000000..943891b
--- /dev/null
+++ b/src/main/jbake/templates/includes/U.groovy
@@ -0,0 +1,75 @@
+// Shared utilities
+package includes
+
+class U {
+    def processBody(content, config) {
+    	def str = content.body
+
+    	// Temporarily disable the TOC macro, replace with a comment
+    	def replacement ='<!-- TODO reactivate TOC once JBake moves to flexmark-java -->\n'
+    	str = str.replaceAll('\\[TOC\\]', replacement)
+
+    	// Temporarily disable the syntax markers (of which there are two flavors, for some reason)
+    	str = str.replaceAll('(::|#\\!)(java|jsp|xml|sh|javascript|html) *\\n', '<!-- TODO syntax marker ($1$2) disabled -->')
+
+    	// Optionally expand variables
+    	if("true".equals(content.expandVariables)) {
+    		str = expandVariables(str, config)
+    	}
+    	return str
+    }
+
+    def expandVariables(str, config) {
+        def pageVariables = [
+            sling_tagline : config.blog_subtitle,
+            sling_minJavaVersion : "8",
+            sling_minMavenVersion : "3.5.0",
+            sling_releaseVersion : "9"
+        ]
+
+    	// Use a closure to avoid exception on missing variable
+    	str = str.replaceAll(/\$\{(\w+)\}/) { key -> pageVariables[key[1]] ?: "MISSING_PAGE_VARIABLE:${key[0]}" }
+    }
+
+    // Is parent an ancestor of child?
+    def isAncestor(parent, child) {
+    	// assuming .html extension
+    	return child.uri[0..-6].contains(parent.uri[0..-6]) && !child.uri.equals(parent.uri)
+    }
+
+    // Return the parents of supplied content, sorted by
+    // their depth in the tree, root first
+    def getSortedParents(content, published_content) {
+    	def result = new TreeMap()
+    	result.put(0, [ uri:"", title:"Home" ])
+    	published_content.each { item ->
+    		if(isAncestor(item, content)) {
+    			result.put(item.uri.length(), item)
+    		}
+    	}
+    	return result
+    }
+
+    def exec(cmd, defaultText) {
+    	try {
+     	    def p = cmd.execute()
+    	    p.waitFor()
+      	    return p.text
+    	} catch(Exception e) {
+    		return defaultText
+    	}
+    }
+
+    def getRevisionInfo(filename) {
+        def lastCommit = "444eb637ff1ddcf11a0f37f02dd4b3fe89eb149f"
+    	def gitCmd = 'git log -1 --format=%h####%ad####%an####%s ' + filename
+    	def defaultText = "0####0000####<MISSING>####<MISSING>"
+    	def gitInfo = exec(gitCmd, defaultText).split("####")
+    	return [
+    		lastCommit : gitInfo[0],
+    		date : gitInfo[1],
+    		author : gitInfo[2],
+    		comment : gitInfo[3]
+    	]
+    }
+}
\ No newline at end of file
diff --git a/src/main/jbake/templates/page.tpl b/src/main/jbake/templates/page.tpl
index bffb6d6..be33887 100644
--- a/src/main/jbake/templates/page.tpl
+++ b/src/main/jbake/templates/page.tpl
@@ -1,80 +1,12 @@
-def expandVariables(str, config) {
-    def pageVariables = [
-        sling_tagline : config.blog_subtitle,
-        sling_minJavaVersion : "8",
-        sling_minMavenVersion : "3.5.0",
-        sling_releaseVersion : "9"
-    ]
-
-	// Use a closure to avoid exception on missing variable
-	str = str.replaceAll(/\$\{(\w+)\}/) { key -> pageVariables[key[1]] ?: "MISSING_PAGE_VARIABLE:${key[0]}" }
-}
-
-def processBody(content, config) {
-	def str = content.body
-
-	// Temporarily disable the TOC macro, replace with a comment
-	def replacement ='<!-- TODO reactivate TOC once JBake moves to flexmark-java -->\n'
-	str = str.replaceAll('\\[TOC\\]', replacement)
-
-	// Temporarily disable the syntax markers (of which there are two flavors, for some reason)
-	str = str.replaceAll('(::|#\\!)(java|jsp|xml|sh|javascript|html) *\\n', '<!-- TODO syntax marker ($1$2) disabled -->')
-
-	// Optionally expand variables
-	if("true".equals(content.expandVariables)) {
-		str = expandVariables(str, config)
-	}
-	return str
-}
-
-// Is parent an ancestor of child?
-def isAncestor(parent, child) {
-	// assuming .html extension
-	return child.uri[0..-6].contains(parent.uri[0..-6]) && !child.uri.equals(parent.uri)
-}
-
-// Return the parents of supplied content, sorted by
-// their depth in the tree, root first
-def getSortedParents(content, published_content) {
-	def result = new TreeMap()
-	result.put(0, [ uri:"", title:"Home" ])
-	published_content.each { item ->
-		if(isAncestor(item, content)) {
-			result.put(item.uri.length(), item)
-		}
-	}
-	return result
-}
-
-def exec(cmd, defaultText) {
-	try {
- 	    def p = cmd.execute()
-	    p.waitFor()
-  	    return p.text
-	} catch(Exception e) {
-		return defaultText
-	}
-}
-
-def getRevisionInfo(filename) {
-    def lastCommit = "444eb637ff1ddcf11a0f37f02dd4b3fe89eb149f"
-	def gitCmd = 'git log -1 --format=%h####%ad####%an####%s ' + filename
-	def defaultText = "0####0000####<MISSING>####<MISSING>"
-	def gitInfo = exec(gitCmd, defaultText).split("####")
-	return [
-		lastCommit : gitInfo[0],
-		date : gitInfo[1],
-		author : gitInfo[2],
-		comment : gitInfo[3]
-	]
-}
+// Include common utilities
+U = new includes.U()
 
 layout 'layout/main.tpl', true,
         projects: projects,
 		breadcrumbs : contents {
 			div(class:"breadcrumbs") {
 				def separator = "&nbsp;&raquo;&nbsp;"
-				getSortedParents(content, published_content).each { item ->
+				U.getSortedParents(content, published_content).each { item ->
 					a (href:"${config.site_contextPath}${item.value.uri}") {
 						yield item.value.title
 					}
@@ -94,7 +26,7 @@ layout 'layout/main.tpl', true,
             div(class:"row"){
                 div(class:"small-12 columns"){
                     section(class:"wrap"){
-                        yieldUnescaped processBody(content, config)
+                        yieldUnescaped U.processBody(content, config)
                     }
                 }
             }
@@ -113,7 +45,7 @@ layout 'layout/main.tpl', true,
 		},
 		lastModified: contents {
 			div(class:"revisionInfo") {
-				def info = getRevisionInfo(content.file);
+				def info = U.getRevisionInfo(content.file);
 				yield "Last modified by "
 				span(class:"author") { yield info.author }
 				yield " on "

-- 
To stop receiving notification emails like this one, please contact
['"commits@sling.apache.org" <co...@sling.apache.org>'].