Template:expTrevorh3

by 2 contributors:
<%
// path, depth, self, whitelist, blacklist 
// Needs to match ALL whitelist entries to be included
// Needs to only match one blacklist entry to be rejected

// NOTE: replace in code when moving to modules
var locale = env.locale

var output = "";
var str = "";

var pages = page.subpagesExpand($0,$1,($2 == true));



step1 = subPagesFilterTags(pages, $3, $4);
step2 = page.subPagesFlatten(step1);
output = page.subPagesSort(step2, "0");
str = page.subPagesFormatList(output,"0");


//step2 = subPagesFlatten(step1);
//output = subPagesSort(step2, "0");
//str = subPagesFormatList(output,"0");

    // Filter subPages list
    // listWhite/listBlack, comma seperated list of tags
    // listWhite - Page must contain all entries to be included
    // listBlack - Page must contain one entry to be excluded
    function subPagesFilterTags (pages, listWhite, listBlack) {

        var arrWhite = '' + listWhite.toLowerCase().split(",");
        var arrBlack = '' + listBlack.toLowerCase().split(",");

        return process_array(pages);

        function checkLists (tags) {
            // Return false if there are not enough tags to match the whitelist
            // Return true if there is no whitelist and no tags (no need to check blacklist)
            if (tags.length < arrWhite.length) {
                return false;
            } else if (arrWhite.length == 0 && tags.length == 0) {
                return true;
            }
            
            // Check blacklist first, it is quicker
            for (var i=0; i<tags.length; i++) {
                if (arrBlack.indexOf(tags[i].toLowerCase()) > -1) {
                    return false;
                }
            }
            
            // Check whitelist
            for (var i=0; i<arrWhite.length; i++) {
                var found = false;
                for (var j=0; j<tags.length; j++) {
                    if (arrWhite[i] == tags[j].toLowerCase()) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    return false;
                }
            }
            return true;
        }

        function process_array(arr) {
            if(arr.length) {
                var arrNew = [];
                arr.forEach(function(item) {
                    if (!item) { return; }
                    if (item.locale != locale) { return; }
                    var subpages = process_array(item.subpages || []) || [];
                    item.subpages = subpages || [];
                    if (!checkLists(item.tags)) {
                        if (!subpages.length) {
                            return
                        } else {
                            item.url = "";
                        }
                    }
                    arrNew.push(item);
                });
                return arrNew;
            }
        }
    }
/*
    // Flatten subPages list
    function subPagesFlatten (pages) {
        
        var output = [];
        
        process_array(pages);

        return output;

        function process_array(arr) {
            if(arr.length) {
                arr.forEach(function(item) {
                    if (!item) { return; }
                    process_array(item.subpages || []);
                    // If only a header for a branch
                    if (item.url == "") {
                        return
                    }
                    item.subpages =[];
                    output.push(item);
                });
            }
        }
    }

    // If reverse is non-zero, the sort is backward
    function subPagesSort (pages, reverse) {
        if (reverse == 0) {
            pages.sort(alphanumForward);
        } else {
            pages.sort(alphanumBackward);
        }

        return pages;

        function chunkify(t) {
			var tz = [], x = 0, y = -1, n = 0, i, j;

			while (i = (j = t.charAt(x++)).charCodeAt(0)) {
                var m = (i == 46 || (i >=48 && i <= 57));
				if (m !== n) {
					tz[++y] = "";
	    			n = m;
				}
				tz[y] += j;
			}
			return tz;
		}

        function alphanumForward(a, b) {

			var aa = chunkify(a.title);
			var bb = chunkify(b.title);

			for (x = 0; aa[x] && bb[x]; x++) {
				if (aa[x] !== bb[x]) {
					var c = Number(aa[x]), d = Number(bb[x]);
					if (c == aa[x] && d == bb[x]) {
						return c - d;
					} else return (aa[x] > bb[x]) ? 1 : -1;
				}
			}
			return aa.length - bb.length;
		}

        function alphanumBackward(a, b) {

    		var bb = chunkify(a.title);
			var aa = chunkify(b.title);

			for (x = 0; aa[x] && bb[x]; x++) {
				if (aa[x] !== bb[x]) {
					var c = Number(aa[x]), d = Number(bb[x]);
					if (c == aa[x] && d == bb[x]) {
						return c - d;
					} else return (aa[x] > bb[x]) ? 1 : -1;
				}
			}
			return aa.length - bb.length;
		}
    }
    
    // If ordered is true, the output is an <ol> instead of <ul>
    //
    // Special note: If ordered is true, pages whose locale differs from
    // the current page's locale are omitted, to work around misplaced
    // localizations showing up in navigation.
    function subPagesFormatList (pages, ordered) {
        return process_array(pages, ordered != 0);
        
        function process_array(arr, ordered) {
            var result = '';
            var openTag = '<ul>';
            var closeTag = '</ul>';
            
            if (ordered) {
                openTag = '<ol>';
                closeTag = '</ol>';
            }
            
            if(arr.length) {
                result += openTag;
                arr.forEach(function(item) {
                    if (!item) { return; }
                    if (ordered && (item.locale != locale)) { return; }
//                    result += '<li><a href="' + item.url + '">' + kuma.htmlEscape(item.title) + '</a>' +
                    result += '<li>';
                    if (item.url != '') {
                        result += '<a href="' + item.url + '">' + item.title + '</a>';
                    } else {
                        result += item.title;
                    }
                    result += process_array(item.subpages || [], ordered) + '</li>';
                    
                });
                result += closeTag;
            }
            return result;
        }
        
    }
*/
%>
<%- str %>
Search for pages that use Template:expTrevorh3 to see example use cases and how many pages use this macro.

Document Tags and Contributors

Contributors to this page: trevorh, openjck
Last updated by: trevorh,