From ce213dff18890d57a996254aa0900548d4ec611a Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Mon, 20 Jan 2020 14:41:19 +0100
Subject: [PATCH] WIP: script for merging xsl files into one

---
 makefile               |  7 +++--
 misc/merge_xsl.sh      | 69 ++++++++++++++++++++++++++++++++++++++++++
 src/core/webcaosdb.xsl |  2 ++
 src/core/xsl/main.xsl  |  2 --
 4 files changed, 76 insertions(+), 4 deletions(-)
 create mode 100755 misc/merge_xsl.sh

diff --git a/makefile b/makefile
index 22a274eb..0b74a07e 100644
--- a/makefile
+++ b/makefile
@@ -49,9 +49,9 @@ LIBS_SUBDIRS = $(addprefix $(LIBS_DIR)/, js css fonts)
 
 ALL: install
 
-install: clean cp-src cp-ext cp-conf $(addprefix $(PUBLIC_DIR)/, $(LIBS)) build_properties
+install: clean cp-src cp-ext cp-conf $(addprefix $(PUBLIC_DIR)/, $(LIBS)) build_properties merge_xsl
 
-test: clean cp-src cp-ext cp-ext-test cp-conf $(addprefix $(PUBLIC_DIR)/, $(TEST_LIBS)) build_properties
+test: clean cp-src cp-ext cp-ext-test cp-conf $(addprefix $(PUBLIC_DIR)/, $(TEST_LIBS)) build_properties merge_xsl
 	@for f in $(shell find $(TEST_EXT_DIR) -type f -iname *.js) ; do \
 		sed -i "/EXTENSIONS/a \<script src=\"$${f#$(TEST_EXT_DIR)/}\" ></script>" $(PUBLIC_DIR)/index.html ; \
 		echo include $$f; \
@@ -61,6 +61,9 @@ test: clean cp-src cp-ext cp-ext-test cp-conf $(addprefix $(PUBLIC_DIR)/, $(TEST
 		echo include $$f; \
 	done
 
+merge_xsl:
+	misc/merge_xsl.sh
+
 build_properties:
 	@set -a -e ; \
 	for f in $$(ls build.properties.d/) ; do source "build.properties.d/$$f" ; done ; \
diff --git a/misc/merge_xsl.sh b/misc/merge_xsl.sh
new file mode 100755
index 00000000..07cc6d7c
--- /dev/null
+++ b/misc/merge_xsl.sh
@@ -0,0 +1,69 @@
+#!/bin/bash
+#
+# ** header v3.0
+# This file is a part of the CaosDB Project.
+#
+# Copyright (C) 2020 IndiScale GmbH
+# Copyright (C) 2020 Timm Fitschen (t.fitschen@indiscale.com)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+# ** end header
+
+# This file can be used to merge xsl files together. It replaces include
+# directives in the webcaosdb.xsl with the content of the included xsl file,
+# resulting in one, big(ish) stand-alone xsl file.
+#
+# The resulting file can be tested (least for syntax) with
+# `xsltproc public/webcaosdb.xsl test/core/xml/test_case_preview_entities.xml`.
+
+
+SOURCE_DIR=public/
+MERGE="xsl/footer.xsl xsl/filesystem.xsl xsl/entity.xsl xsl/query.xsl xsl/messages.xsl xsl/navbar.xsl xsl/main.xsl xsl/welcome.xsl xsl/common.xsl"
+TARGET=public/webcaosdb.xsl
+
+
+function _merge () {
+    _HREF=$1
+    _SOURCE=$2
+    _TARGET=$3
+
+    echo "merging ${_SOURCE} into ${_TARGET} by replacing xsl:include directive with href=${_HREF} by the content of ${_SOURCE}."
+
+    # inject bash-style env-var name into xsl
+    _PLACEHOLDER="  <!-- COPIED FROM ${_HREF} (START)-->\n  \$\{_CONTENT_XSL\}\n  <!-- COPIED FROM ${_HREF} (END)-->"
+    sed "s|^.*xsl:include.*${_HREF}.*|${_PLACEHOLDER}|g" -i ${_TARGET}
+
+    # remove header from source file
+    csplit -f ".tmp_source" "${_SOURCE}" /xsl:stylesheet/
+    rm .tmp_source00
+
+    # remove root and other things
+    sed "/xsl:stylesheet/d" -i .tmp_source01
+    sed "/xsl:output/d" -i .tmp_source01
+
+    # replace env-var in tmp results
+    export _CONTENT_XSL=$(cat .tmp_source01)
+    envsubst '$_CONTENT_XSL' < "${_TARGET}" > .tmp_results.xsl
+    rm .tmp_source01
+
+    # replace target file with newly generated one
+    mv .tmp_results.xsl ${_TARGET}
+}
+
+for HREF in $MERGE ; do
+    _SOURCE=${SOURCE_DIR}${HREF}
+    _merge $HREF ${_SOURCE} $TARGET
+done
+
diff --git a/src/core/webcaosdb.xsl b/src/core/webcaosdb.xsl
index ff12c424..f7e10836 100644
--- a/src/core/webcaosdb.xsl
+++ b/src/core/webcaosdb.xsl
@@ -34,6 +34,8 @@
   <xsl:include href="xsl/entity.xsl" />
   <xsl:include href="xsl/filesystem.xsl" />
   <xsl:include href="xsl/footer.xsl" />
+  <xsl:include href="xsl/common.xsl"/>
+  <xsl:include href="xsl/welcome.xsl"/>
 
   <xsl:template match="/">
     <html lang="en">
diff --git a/src/core/xsl/main.xsl b/src/core/xsl/main.xsl
index 038b9765..6932d247 100644
--- a/src/core/xsl/main.xsl
+++ b/src/core/xsl/main.xsl
@@ -26,8 +26,6 @@
 -->
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html"/>
-  <xsl:include href="common.xsl"/>
-  <xsl:include href="welcome.xsl"/>
   <xsl:variable name="basepath">
     <xsl:call-template name="uri_ends_with_slash">
       <xsl:with-param name="uri">
-- 
GitLab