From 2a50a2625228d46056765300476b78b5f072ae88 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Thu, 30 May 2019 15:09:45 -0400 Subject: [PATCH] Build: check external asciidoc links * Check external links have a description * Use Groovy regex instead of Java * Add source file name to exception message * Add 'checkAsciiDocLinks' to Travis build --- .travis.yml | 2 +- build.gradle | 39 ++++++++++++++++++++++++++++- src/reference/asciidoc/samples.adoc | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 815c5397e5..f276da88b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,4 +15,4 @@ install: true env: - TERM=dumb SI_FATAL_WHEN_NO_BEANFACTORY=true NO_REFERENCE_TASK=true script: - - ./gradlew check --refresh-dependencies --no-daemon + - ./gradlew check checkAsciiDocLinks --refresh-dependencies --no-daemon diff --git a/build.gradle b/build.gradle index f5f3c9012f..df64251245 100644 --- a/build.gradle +++ b/build.gradle @@ -855,8 +855,45 @@ task prepareAsciidocBuild(type: Sync) { into "$buildDir/asciidoc" } -task('makePDF', type: org.asciidoctor.gradle.AsciidoctorTask) { +task checkAsciidocLinks { dependsOn prepareAsciidocBuild + inputs.dir("$buildDir/asciidoc") + doLast { + inputs.files.filter{ f -> f.path.endsWith('.adoc') }.each { file -> + def doc = file.text + Set anchors = new HashSet<>() + def matcher = (doc =~ /\[\[([^]]+)\]\]/) + while (matcher.find()) { + anchors.add(matcher.group(1)) + } + matcher = (doc =~ /<<([^>]+)>>/) + while (matcher.find()) { + String anchor = matcher.group(1); + def hasComma = anchor.contains(",") + if (!anchor.startsWith("./")) { + if (hasComma) { + anchor = anchor.substring(0, anchor.indexOf(",")); + } + if (!anchors.contains(anchor)) { + throw new InvalidUserDataException("Anchor '" + anchor + + "' not found in '" + file.name + + "', if in another file, it needs to be qualified with './fileName.adoc#'") + } + } + else { + if (!hasComma) { + throw new InvalidUserDataException("External anchor '" + anchor + + "' in '" + file.name + + "' should have a textual description (,...)") + } + } + } + } + } +} + +task('makePDF', type: org.asciidoctor.gradle.AsciidoctorTask) { + dependsOn checkAsciidocLinks backends 'pdf' sourceDir "$buildDir/asciidoc" sources { diff --git a/src/reference/asciidoc/samples.adoc b/src/reference/asciidoc/samples.adoc index 0962424105..e668951877 100644 --- a/src/reference/asciidoc/samples.adoc +++ b/src/reference/asciidoc/samples.adoc @@ -23,7 +23,7 @@ https://github.com/SpringSource/spring-integration-samples[https://github.com/Sp In order to check out or clone the samples, you must have a Git client installed on your system. There are several GUI-based products available for many platforms (such as https://eclipse.org/egit/[EGit] for the Eclipse IDE). A simple Google search can help you find them. -You can also use the command line interface for <>. +You can also use the command line interface for https://git-scm.com/[Git]. NOTE: If you need more information on how to install or use Git, visit: https://git-scm.com/[https://git-scm.com/].