<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "http://commons.omniupdate.com/dtd/standard.dtd">
<!--
	Implementations Skeleton - 2017/10/10

	BREADCRUMBS
	Assumes that a section properties files is being used to extract section titles.
	If there aren't any props files, the xsl skips the section. However, if the current page is the index page, the breadcrumb value the page is used.
	If the user leaves the breadcrumb value empty or enters the value of "$skip", the build out will skip the breacrumb for that folder on build out

	Example:
	<xsl:call-template name="breadcrumb">
		<xsl:with-param name="path" select="$dirname"/>
	</xsl:call-template>

	Dependencies:
	- variables.xsl
	- functions.xsl

	Contributors: Your Name Here
	Last Updated: Enter Date Here
-->
<xsl:stylesheet version="3.0" expand-text="yes"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:ou="http://omniupdate.com/XSL/Variables"
	xmlns:fn="http://omniupdate.com/XSL/Functions"
	xmlns:ouc="http://omniupdate.com/XSL/Variables"
	exclude-result-prefixes="ou xsl xs fn ouc">

	<xsl:variable name="breadcrumb-element">li</xsl:variable>
	<xsl:variable name="breadcrumb-delim"></xsl:variable>  <!-- a predefined delimiter to place in between crumbs -->
	<xsl:variable name="index-filename" select="concat($index-file, '.' , $extension)"/>

	<!-- The generic breadcrumb template. Override this template to match the implementation design. -->
	<xsl:template name="breadcrumb-general">
		<xsl:param name="href" required="yes" as="xs:string"/>
		<xsl:param name="text" required="yes" as="xs:string"/>
		<xsl:param name="class" as="xs:string" select="''"/>
		<xsl:param name="delim" as="item()" select="$breadcrumb-delim"/>

		<xsl:copy-of select="$delim" />
		<xsl:element name="{$breadcrumb-element}">
			<xsl:if test="normalize-space($class)">
				<xsl:attribute name="class">{$class}</xsl:attribute>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="normalize-space($href)">
					<a href="{$href}">{$text}</a>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$text"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>

	<!-- The current breadcrumb template. Override this template to match the implementation design if it is different from the generic breadcrumb template. -->
	<xsl:template name="breadcrumb-current">
		<xsl:param name="text" required="yes" />
		<xsl:param name="class" as="xs:string">current</xsl:param>
		<xsl:param name="delim" select="$breadcrumb-delim"/>

		<xsl:if test="normalize-space($text)">
			<xsl:call-template name="breadcrumb-general">
				<xsl:with-param name="href"></xsl:with-param>
				<xsl:with-param name="text" select="$text"/>
				<xsl:with-param name="class" select="$class"/>
				<xsl:with-param name="delim" select="$delim"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<!-- The root/home breadcrumb template. Override this template to match the implementation design if it is different from the generic breadcrumb template. -->
	<xsl:template name="breadcrumb-root">
		<xsl:param name="text" select="ou:pcf-param('breadcrumb', document($ou:root || $ou:site || '/' || $props-file))"/>

		<xsl:choose>
			<xsl:when test="normalize-space($text)">
				<xsl:call-template name="breadcrumb-general">
					<xsl:with-param name="href" select="$ou:httproot"/>
					<xsl:with-param name="text" select="$text"/>
					<xsl:with-param name="delim"></xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="breadcrumb-general">
					<xsl:with-param name="href" select="$ou:httproot"/>
					<xsl:with-param name="text">Home</xsl:with-param>
					<xsl:with-param name="delim"></xsl:with-param>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="breadcrumb">
		<xsl:param name="path" select="$dirname" /> <!-- defined in the vars xsl as $ou:dirname with a trailing '/' -->
		<xsl:param name="title" select="ou:pcf-param('breadcrumb')" /> <!-- originally defined as $page-title from vars -->

		<ul class="breadcrumb">

			<xsl:if test="$path != '/' or $ou:filename != $index-filename">
				<xsl:call-template name="breadcrumb-root"/>
			</xsl:if>
			<!-- begin the recursive template for the crumbs (below) -->

			<!-- check for valid breadcrumb-start to prevent infinite recursion -->
			<xsl:choose>
				<xsl:when test="contains($path, $breadcrumb-start)">
					<xsl:call-template name="breadcrumb-iterate">
						<xsl:with-param name="staging-path" select="$path"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="$is-edt">
					<xsl:call-template name="breadcrumb-general">
						<xsl:with-param name="href"></xsl:with-param>
						<xsl:with-param name="text">System Message: Bad Breadcrumb Start Directory Variable</xsl:with-param>
					</xsl:call-template>
				</xsl:when>
			</xsl:choose>

			<xsl:variable name="section-breadcrumb" as="xs:string" select="normalize-space(ou:pcf-param('breadcrumb', document($props-path)))"/>
			<xsl:if test="$ou:filename != $index-filename or $section-breadcrumb = '' or $section-breadcrumb = '$skip'">
				<xsl:call-template name="breadcrumb-current">
					<xsl:with-param name="text" select="$title"/>
				</xsl:call-template>
			</xsl:if>

		</ul>
	</xsl:template>

	<xsl:template name="breadcrumb-iterate">
		<xsl:param name="staging-path" required="yes" as="xs:string"/>

		<xsl:if test="$staging-path != $breadcrumb-start">
			<!-- begin recursive function if the current path doesn't match the root or breadcrumb-start directory variable -->
			<xsl:call-template name="breadcrumb-iterate">
				<xsl:with-param name="staging-path" select="ou:parent-path($staging-path)"/>
			</xsl:call-template>
		</xsl:if>

		<xsl:choose>
			<xsl:when test="$staging-path != '/'">
				<xsl:call-template name="breadcrumb-output">
					<xsl:with-param name="staging-path" select="$staging-path"/>
				</xsl:call-template>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="breadcrumb-output">
		<xsl:param name="staging-path" required="yes" as="xs:string"/>

		<xsl:variable name="this-props-path" select="concat($ou:root, $ou:site, $staging-path, $props-file)"/>	<!-- props-file is defined in vars xsl -->
		<xsl:variable name="text" as="xs:string?" select="normalize-space(ou:pcf-param('breadcrumb', document($this-props-path)))"/>

		<!-- check to see if the breadcrumb needs to be skipped to do to user input -->
		<xsl:choose>
			<xsl:when test="$text and ($text != '$skip')">
				<xsl:choose>
					<xsl:when test="$staging-path = $dirname and $ou:filename = $index-filename">
						<xsl:call-template name="breadcrumb-current">
							<xsl:with-param name="text" select="$text"/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="breadcrumb-general">
							<xsl:with-param name="href" select="$domain || $staging-path"/>
							<xsl:with-param name="text" select="$text"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="not(doc-available($this-props-path)) and $is-edt">
				<xsl:call-template name="breadcrumb-general">
					<xsl:with-param name="href"></xsl:with-param>
					<xsl:with-param name="text">System Message: Props File Not Found</xsl:with-param>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$is-edt">
				<xsl:call-template name="breadcrumb-general">
					<xsl:with-param name="href"></xsl:with-param>
					<xsl:with-param name="text">System Message: Props File <xsl:value-of select="if ($text = '$skip') then 'Skipped' else 'Empty'" /></xsl:with-param>
				</xsl:call-template>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
