From d14aa36dfa1334f3fb8abe59f1a95a2e1d2493d6 Mon Sep 17 00:00:00 2001 From: Ronan Hennessy Date: Fri, 6 Oct 2023 17:00:43 +0100 Subject: [PATCH] 592: checking for correct assembly metadata --- .../.vale.ini | 5 ++ .../testinvalid.adoc | 4 ++ .../testvalid.adoc | 8 ++++ .../AssemblyContainsRequiredMetadata.yml | 37 +++++++++++++++ .../AssemblyContainsRequiredMetadata.tengo | 46 +++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 .vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/.vale.ini create mode 100644 .vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testinvalid.adoc create mode 100644 .vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testvalid.adoc create mode 100644 .vale/styles/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata.yml create mode 100644 tengo-rule-scripts/AssemblyContainsRequiredMetadata.tengo diff --git a/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/.vale.ini b/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/.vale.ini new file mode 100644 index 000000000..67f96b1cd --- /dev/null +++ b/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/.vale.ini @@ -0,0 +1,5 @@ +; Vale configuration file to test the `AssemblyContainsRequiredMetadata` rule +StylesPath = ../../../styles +MinAlertLevel = suggestion +[*.adoc] +OpenShiftAsciiDoc.AssemblyContainsRequiredMetadata = YES diff --git a/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testinvalid.adoc b/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testinvalid.adoc new file mode 100644 index 000000000..3c78205eb --- /dev/null +++ b/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testinvalid.adoc @@ -0,0 +1,4 @@ +//vale-fixture +:context: += title + diff --git a/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testvalid.adoc b/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testvalid.adoc new file mode 100644 index 000000000..2e2afcbdd --- /dev/null +++ b/.vale/fixtures/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata/testvalid.adoc @@ -0,0 +1,8 @@ +//vale-fixture +:_content-type: ASSEMBLY +[id=""] += Assembly title +include::_attributes/input-attributes-file +:context: some-context + +toc::[] \ No newline at end of file diff --git a/.vale/styles/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata.yml b/.vale/styles/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata.yml new file mode 100644 index 000000000..0929d3de8 --- /dev/null +++ b/.vale/styles/OpenShiftAsciiDoc/AssemblyContainsRequiredMetadata.yml @@ -0,0 +1,37 @@ +--- +extends: script +message: "Assembly is missing required metadata. Ensure you include metadata for attributes, TOC and context." +level: error +link: https://github.com/openshift/openshift-docs/blob/main/contributing_to_docs/doc_guidelines.adoc#assembly-file-metadata +scope: raw +script: | + text := import("text") + matches := [] + + //trim extra whitespace + scope = text.trim_space(scope) + //add a newline, it might be missing + scope += "\n" + + attribute_regex := "include::_attributes/" + toc_regex := "toc::\\[\\]" + context_regex := ":context:" + matchAttribute := false + matchToc := false + matchContext := false + + //Check if context declaration is on any line + for line in text.split(scope, "\n") { + if text.re_match(attribute_regex, line){ + matchAttribute = true + } else if text.re_match(toc_regex, line){ + matchToc = true + } else if text.re_match(context_regex, line){ + matchContext = true + } + } + + //Highlight first line if context declaration not found in file + if !matchAttribute || !matchToc || !matchContext { + matches = append(matches, {begin: 1, end: 10}) + } \ No newline at end of file diff --git a/tengo-rule-scripts/AssemblyContainsRequiredMetadata.tengo b/tengo-rule-scripts/AssemblyContainsRequiredMetadata.tengo new file mode 100644 index 000000000..eee7032af --- /dev/null +++ b/tengo-rule-scripts/AssemblyContainsRequiredMetadata.tengo @@ -0,0 +1,46 @@ +/* +Tengo Language +Checks that lines are not hard-wrapped. +$ tengo HardWrappedLines.tengo +*/ + +fmt := import("fmt") +os := import("os") +text := import("text") + +input := os.args() +scope := os.read_file(input[2]) +matches := [] + + //trim extra whitespace + scope = text.trim_space(scope) + //add a newline, it might be missing + scope += "\n" + + attribute_regex := "include::_attributes/" + toc_regex := "toc::\\[\\]" + context_regex := ":context:" + matchAttribute := false + matchToc := false + matchContext := false + + //Check if context declaration is on any line + for line in text.split(scope, "\n") { + if text.re_match(attribute_regex, line){ + matchAttribute = true + fmt.println("Found attribute declaration.") + } else if text.re_match(toc_regex, line){ + matchToc = true + fmt.println("Found toc declaration") + } else if text.re_match(context_regex, line){ + matchContext = true + fmt.println("Found context declaration") + } + } + + //Highlight first line if context declaration not found in file + if !matchAttribute || !matchToc || !matchContext { + matches = append(matches, {begin: 1, end: 10}) + fmt.println("Did not find correct metadata") + fmt.println(matches) + } \ No newline at end of file