+def hugoTemplateSubstitutions(String input, Map extras=null) {
+ def replacements = [
+ DATE: getDate("yyyy-MM-dd"),
+ CHANNEL: propertiesChannelName,
+ APPLICATION_NAME: applicationName,
+ GIT_HASH: gitHash,
+ GIT_BRANCH: gitBranch,
+ VERSION: JALVIEW_VERSION,
+ JAVA_VERSION: JAVA_VERSION,
+ VERSION_UNDERSCORES: JALVIEW_VERSION_UNDERSCORES,
+ DRAFT: "false",
+ JVL_HEADER: ""
+ ]
+ def output = input
+ if (extras != null) {
+ extras.each{ k, v ->
+ output = output.replaceAll("__${k}__", ((v == null)?"":v))
+ }
+ }
+ replacements.each{ k, v ->
+ output = output.replaceAll("__${k}__", ((v == null)?"":v))
+ }
+ return output
+}
+
+def mdFileComponents(File mdFile, def dateOnly=false) {
+ def map = [:]
+ def content = ""
+ if (mdFile.exists()) {
+ def inFrontMatter = false
+ def firstLine = true
+ mdFile.eachLine { line ->
+ if (line.matches("---")) {
+ def prev = inFrontMatter
+ inFrontMatter = firstLine
+ if (inFrontMatter != prev)
+ return false
+ }
+ if (inFrontMatter) {
+ def m = null
+ if (m = line =~ /^date:\s*(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/) {
+ map["date"] = new Date().parse("yyyy-MM-dd HH:mm:ss", m[0][1])
+ } else if (m = line =~ /^date:\s*(\d{4}-\d{2}-\d{2})/) {
+ map["date"] = new Date().parse("yyyy-MM-dd", m[0][1])
+ } else if (m = line =~ /^channel:\s*(\S+)/) {
+ map["channel"] = m[0][1]
+ } else if (m = line =~ /^version:\s*(\S+)/) {
+ map["version"] = m[0][1]
+ } else if (m = line =~ /^\s*([^:]+)\s*:\s*(\S.*)/) {
+ map[ m[0][1] ] = m[0][2]
+ }
+ if (dateOnly && map["date"] != null) {
+ return false
+ }
+ } else {
+ if (dateOnly)
+ return false
+ content += line+"\n"
+ }
+ firstLine = false
+ }
+ }
+ return dateOnly ? map["date"] : [map, content]
+}
+
+task hugoTemplates {
+ group "website"
+ description "Create partially populated md pages for hugo website build"
+
+ def hugoTemplatesDir = file("${jalviewDir}/${hugo_templates_dir}")
+ def hugoBuildDir = "${jalviewDir}/${hugo_build_dir}"
+ def templateFiles = fileTree(dir: hugoTemplatesDir)
+ def releaseMdFile = file("${jalviewDir}/${releases_dir}/release-${JALVIEW_VERSION_UNDERSCORES}.md")
+ def whatsnewMdFile = file("${jalviewDir}/${whatsnew_dir}/whatsnew-${JALVIEW_VERSION_UNDERSCORES}.md")
+ def oldJvlFile = file("${jalviewDir}/${hugo_old_jvl}")
+ def jalviewjsFile = file("${jalviewDir}/${hugo_jalviewjs}")
+
+ doFirst {
+ // specific release template for version archive
+ def changes = ""
+ def whatsnew = null
+ def givenDate = null
+ def givenChannel = null
+ def givenVersion = null
+ if (CHANNEL == "RELEASE") {
+ def (map, content) = mdFileComponents(releaseMdFile)
+ givenDate = map.date
+ givenChannel = map.channel
+ givenVersion = map.version
+ changes = content
+ if (givenVersion != null && givenVersion != JALVIEW_VERSION) {
+ throw new GradleException("'version' header (${givenVersion}) found in ${releaseMdFile} does not match JALVIEW_VERSION (${JALVIEW_VERSION})")
+ }
+
+ if (whatsnewMdFile.exists())
+ whatsnew = whatsnewMdFile.text
+ }
+
+ def oldJvl = oldJvlFile.exists() ? oldJvlFile.collect{it} : []
+ def jalviewjsLink = jalviewjsFile.exists() ? jalviewjsFile.collect{it} : []
+
+ def changesHugo = null
+ if (changes != null) {
+ changesHugo = '<div class="release_notes">\n\n'
+ def inSection = false
+ changes.eachLine { line ->
+ def m = null
+ if (m = line =~ /^##([^#].*)$/) {
+ if (inSection) {
+ changesHugo += "</div>\n\n"
+ }
+ def section = m[0][1].trim()
+ section = section.toLowerCase()
+ section = section.replaceAll(/ +/, "_")
+ section = section.replaceAll(/[^a-z0-9_\-]/, "")
+ changesHugo += "<div class=\"${section}\">\n\n"
+ inSection = true
+ } else if (m = line =~ /^(\s*-\s*)<!--([^>]+)-->(.*?)(<br\/?>)?\s*$/) {
+ def comment = m[0][2].trim()
+ if (comment != "") {
+ comment = comment.replaceAll('"', """)
+ def issuekeys = []
+ comment.eachMatch(/JAL-\d+/) { jal -> issuekeys += jal }
+ def newline = m[0][1]
+ if (comment.trim() != "")
+ newline += "{{<comment>}}${comment}{{</comment>}} "
+ newline += m[0][3].trim()
+ if (issuekeys.size() > 0)
+ newline += " {{< jal issue=\"${issuekeys.join(",")}\" alt=\"${comment}\" >}}"
+ if (m[0][4] != null)
+ newline += m[0][4]
+ line = newline
+ }
+ }
+ changesHugo += line+"\n"
+ }
+ if (inSection) {
+ changesHugo += "\n</div>\n\n"
+ }
+ changesHugo += '</div>'
+ }
+
+ templateFiles.each{ templateFile ->
+ def newFileName = string(hugoTemplateSubstitutions(templateFile.getName()))
+ def relPath = hugoTemplatesDir.toPath().relativize(templateFile.toPath()).getParent()
+ def newRelPathName = hugoTemplateSubstitutions( relPath.toString() )
+
+ def outPathName = string("${hugoBuildDir}/$newRelPathName")
+
+ copy {
+ from templateFile
+ rename(templateFile.getName(), newFileName)
+ into outPathName
+ }
+
+ def newFile = file("${outPathName}/${newFileName}".toString())
+ def content = newFile.text
+ newFile.text = hugoTemplateSubstitutions(content,
+ [
+ WHATSNEW: whatsnew,
+ CHANGES: changesHugo,
+ DATE: givenDate == null ? "" : givenDate.format("yyyy-MM-dd"),
+ DRAFT: givenDate == null ? "true" : "false",
+ JALVIEWJSLINK: jalviewjsLink.contains(JALVIEW_VERSION) ? "true" : "false",
+ JVL_HEADER: oldJvl.contains(JALVIEW_VERSION) ? "jvl: true" : ""
+ ]
+ )
+ }
+
+ }
+
+ inputs.file(oldJvlFile)
+ inputs.dir(hugoTemplatesDir)
+ inputs.property("JALVIEW_VERSION", { JALVIEW_VERSION })
+ inputs.property("CHANNEL", { CHANNEL })
+}
+
+def getMdDate(File mdFile) {
+ return mdFileComponents(mdFile, true)
+}
+
+def getMdSections(String content) {
+ def sections = [:]
+ def sectionContent = ""
+ def sectionName = null
+ content.eachLine { line ->
+ def m = null
+ if (m = line =~ /^##([^#].*)$/) {
+ if (sectionName != null) {
+ sections[sectionName] = sectionContent
+ sectionName = null
+ sectionContent = ""
+ }
+ sectionName = m[0][1].trim()
+ sectionName = sectionName.toLowerCase()
+ sectionName = sectionName.replaceAll(/ +/, "_")
+ sectionName = sectionName.replaceAll(/[^a-z0-9_\-]/, "")
+ } else if (sectionName != null) {
+ sectionContent += line+"\n"
+ }
+ }
+ if (sectionContent != null) {
+ sections[sectionName] = sectionContent
+ }
+ return sections
+}