Template:TagDirectory

Page last modified 20:05, 12 May 2009 by Liraz | Page History
Table of contents
No headers
/***
    USAGE:

    TagDirectory() 
        build a two-column tag directory for the immediate sub-pages of the current page.

    TagDirectory(PATH)
        build a two-column tag directory for the page at PATH.
***/

var basepath = $0 ?? $path;
var base = basepath ? wiki.getpage(basepath) : page;

// build map of all tags in subpages
var tagmap = { }; 
foreach(var p in base.subpages) { 
    var tags = p.tags; 

    // check if page has no tags; if so make up a default list
    if(!#tags) {
        let tags = [ { name: '(unclassified)', type: 'text' } ];
    }

    // foreach tag on the page, append the page to that tag's list
    foreach(var t in tags where t.type == 'text') {
        let tagmap ..= { (t.name) : tagmap[t.name] .. [ p ] };
    } 
}
if(#tagmap) {

    // count how many pages each tag has
    var tag_count = [ ]; 
    foreach(var tag in map.keys(tagmap)) { 
        let tag_count ..= [ { tag: tag, count: #tagmap[tag] } ]; 
    } 

    // balance the left and right columns so that the columns are as equal in height as possible
    var left_tags = []; 
    var left_tags_sum = 0; 
    var right_tags = []; 
    var right_tags_sum = 0; 
    foreach(var t in list.sort(tag_count, 'count', true)) { 
        if(right_tags_sum > left_tags_sum) { 
            let left_tags_sum += t.count; 
            let left_tags ..= [ t.tag ]; 
        } else { 
            let right_tags_sum += t.count; 
            let right_tags ..= [ t.tag ]; 
        } 
    }

    // emit the table with the two columns
    <table width="100%" cellspacing="0" cellpadding="5" border="0" style="table-layout: fixed;">
        <tr valign="top">
            <td style="padding-right: 20px;">
                foreach(var tag in list.sort(left_tags)) {
                    <h5>string.tocamelcase(tag)</h5>
                    var pages = list.sort(tagmap[tag], 'viewcount', _, '$right - $left');
                    <ul>
                        foreach(var p in pages) {
                            <li>
                                <span style="font-size: small;">web.link(p.uri, string.startswith(p.title, 'How do I... ', true) ? string.substr(p.title, 12) : p.title)</span>
                                <span style="color: rgb(128, 128, 128); font-size: small;">' (' .. num.format(p.viewcount, '#,##0') .. ' views)'</span>
                            </li>
                        }
                    </ul>
                }
            </td>
            <td style="padding-right: 20px;">
                foreach(var tag in list.sort(right_tags)) {
                    <h5>string.tocamelcase(tag)</h5>
                    var pages = list.sort(tagmap[tag], 'viewcount', _, '$right - $left');
                    <ul>
                        foreach(var p in pages) {
                            <li>
                                <span style="font-size: small;">web.link(p.uri, string.startswith(p.title, 'How do I... ', true) ? string.substr(p.title, 12) : p.title)</span>
                                <span style="color: rgb(128, 128, 128); font-size: small;">' (' .. num.format(p.viewcount, '#,##0') .. ' views)'</span>
                            </li>
                        }
                    </ul>
                }
            </td>
        </tr>
    </table>
}

 

Tag page
Page statistics
1213 view(s), 2 edit(s), and 3617 character(s)

Comments

You must login to post a comment.

Attach file

Attachments