From 382910c64279ac64f2872b9f9d20087b8ac61427 Mon Sep 17 00:00:00 2001
From: Andrei Gherzan <andrei.gherzan@huawei.com>
Date: Thu, 18 Mar 2021 17:37:13 +0000
Subject: [PATCH] CONTRIBUTING.sh: Add tool to generate markdown CONTRIBUTING
 file

Signed-off-by: Andrei Gherzan <andrei.gherzan@huawei.com>
---
 contributing/CONTRIBUTING.lua |  18 ++++++
 contributing/CONTRIBUTING.sh  | 112 ++++++++++++++++++++++++++++++++++
 contributing/Makefile         |   9 +++
 contributing/project.rst      |  11 ++++
 4 files changed, 150 insertions(+)
 create mode 100644 contributing/CONTRIBUTING.lua
 create mode 100755 contributing/CONTRIBUTING.sh
 create mode 100644 contributing/Makefile
 create mode 100644 contributing/project.rst

diff --git a/contributing/CONTRIBUTING.lua b/contributing/CONTRIBUTING.lua
new file mode 100644
index 0000000..215cff1
--- /dev/null
+++ b/contributing/CONTRIBUTING.lua
@@ -0,0 +1,18 @@
+-- SPDX-FileCopyrightText: Huawei Inc.
+--
+-- SPDX-License-Identifier: Apache-2.0
+
+--- Filter out toctree and contents directives
+-- This function is a element-transforming function to be used as a lua-filter
+-- for pandoc. You can find it passed to pandoc as part of CONTRIBUTING.sh
+-- tool. This is needed when converting a set of reST documents to a markdown
+-- one where directives like `toctree` and `contents` are not supported and end
+-- up translated literally.
+function Div(el)
+  local class = el["c"][1][2][1]
+  if class == "toctree" or class == "contents" then
+    return {}
+  else
+    return nil
+  end
+end
diff --git a/contributing/CONTRIBUTING.sh b/contributing/CONTRIBUTING.sh
new file mode 100755
index 0000000..d096bdf
--- /dev/null
+++ b/contributing/CONTRIBUTING.sh
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+# SPDX-FileCopyrightText: Huawei Inc.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+set -e
+
+DEFAULT_COPYRIGHT="Huawei Inc."
+DOCS_LICENSE="CC-BY-4.0"
+TOOL_DEPS="\
+    pandoc \
+    reuse \
+    "
+
+help() {
+    cat << EOF
+CONTRIBUTING.sh
+
+Tool to populate a CONTRIBUTING.md file.
+
+Options:
+  -h, --help
+    Display this help information and exit.
+
+  -c, --copyright COPYRIGHT
+    Copyright statement.
+    Defaults to: "$DEFAULT_COPYRIGHT".
+
+  -o, --output FILE|DIR
+    Output file. If directory is given, the output will a CONTRIBUTING.md file
+    in the requested directory.
+    Required argument.
+
+Tool dependencies:
+EOF
+for tool in $TOOL_DEPS; do
+    echo "  - $tool"
+done
+}
+
+log() {
+    # Address log levels
+    case $1 in
+        ERROR)
+            loglevel=ERROR
+            shift
+            ;;
+        WARN)
+            loglevel=WARNING
+            shift
+            ;;
+        *)
+            loglevel=LOG
+            ;;
+    esac
+    printf "[%s] %s\n" "$loglevel" "$1"
+    [ "$loglevel" != "ERROR" ] || exit 1
+}
+
+# Parse arguments
+while [ "$#" -ge 1 ]; do
+    i="$1"
+    case $i in
+        -h|--help)
+            help
+            exit 0
+            ;;
+        -c|--copyright)
+            [ -n "$2" ] || log ERROR "\"$1\" argument needs a value. See help information."
+            COPYRIGHT="$2"
+            shift
+            ;;
+        -o|--output)
+            [ -n "$2" ] || log ERROR "\"$1\" argument needs a value. See help information."
+            OUT="$2"
+            [ ! -d "$OUT" ] || OUT="$OUT/CONTRIBUTING.md"
+            shift
+            ;;
+        *)
+            log ERROR "Unrecognized option $1."
+            ;;
+    esac
+    shift
+done
+[ -n "$OUT" ] || log ERROR "No output provided. See help information."
+[ -n "$COPYRIGHT" ] || COPYRIGHT="$DEFAULT_COPYRIGHT"
+
+# Check tool's depedencies
+for tool in $TOOL_DEPS; do
+    command -v "$tool" >/dev/null 2>&1 || log ERROR "\"$tool\" not found. Make \
+sure it is installed on your system and available in PATH. See help \
+information."
+done
+
+SCRIPT_PATH="$(dirname "$0")"
+mkdir -p "$(dirname "$OUT")" || log ERROR "Couldn't create path to $OUT."
+
+log "Converting to markdown..."
+pandoc -s --toc --markdown-headings=atx --wrap=none -t gfm \
+    --lua-filter="$SCRIPT_PATH/CONTRIBUTING.lua" \
+    "$SCRIPT_PATH/../definitions.rst" \
+    "$SCRIPT_PATH/gitlab.rst" \
+    "$SCRIPT_PATH/reuse.rst" \
+    "$SCRIPT_PATH/dco.rst" \
+    "$SCRIPT_PATH/project.rst" > "$OUT" || log ERROR "Failed to create the markdown export."
+
+log "Adding REUSE headers..."
+reuse addheader --license "$DOCS_LICENSE" --copyright "$COPYRIGHT" \
+    --exclude-year "$OUT" || log ERROR "Failed to add REUSE headers."
+
+log "Done. Output available in \"$OUT\"."
diff --git a/contributing/Makefile b/contributing/Makefile
new file mode 100644
index 0000000..8b199d7
--- /dev/null
+++ b/contributing/Makefile
@@ -0,0 +1,9 @@
+# SPDX-FileCopyrightText: Huawei Inc.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+.PHONY: all
+all: check
+
+check:
+	shellcheck CONTRIBUTING.sh
diff --git a/contributing/project.rst b/contributing/project.rst
new file mode 100644
index 0000000..78a5f4b
--- /dev/null
+++ b/contributing/project.rst
@@ -0,0 +1,11 @@
+.. SPDX-FileCopyrightText: Huawei Inc.
+..
+.. SPDX-License-Identifier: CC-BY-4.0
+
+.. This is a boilerplate to be used by the CONTRIBUTING.sh tool. Do not include
+   it as part of building the documentation.
+
+``%PROJECTNAME%``-specific contributions process and guidelines
+###############################################################
+
+%POPULATEME%
-- 
GitLab