Template:PageWordCloud

    Table of contents
    No headers
    // First release using 1.1.0 SiteTagCloud template, by neilw and maphew, 2009
    //
    // USAGE: PageWordCloud( order, max, color, size, sizeUnits, exclude, showCount, align );
    //    order: str (valid values: "name" or "count") (default "count")
    //        Specifies how the tags are sorted in the list
    //    max: num (default:50)
    //        Specifies the maximum number of tags to be displayed.  0 ==> show all tags
    //    color: list (default: [ [50,200,50], [50,100,50] ])
    //        Specifies the color range for displayed tags.  May be either a single list of [r,g,b]
    //        (in which case all tags will be that color) or a list of two [r,b,g] lists, where tags
    //        with the fewest occurrences will be shown in the first color, and tags with the most
    //        occurrences will be shown with the second color.  Other numbers of occurrences will be
    //        interpolated between the two.
    //    size: num or list (default: [80,240])
    //        Specifies the size range for displayed tags.  If a single number is specified, all tags
    //        will shown in that size.  If a list of two numbers is specified, then the tags with fewest
    //        occurrences will be shown in the first size, and those with the most will be shown in the 
    //        second.  Others will be interpolated in between.
    //    sizeUnits: str (default: "%")
    //        CSS units for the size argument.
    //    exclude: str or list (default: [])
    //        Tag(s) to exclude from the cloud.  May be either a single tag given as a string, or a
    //        list of tags given as a list of strings.
    //    align: str (default: "left")
    //        Specifies the text alignment for the tag cloud.  Standard CSS values are available.
    
    var errors = [];
    
    //**************
    // Process Args
    //**************
    
    // order
    var defaultOrder = "name";
    var tagOrder = string.tolower($0 ?? $order ?? defaultOrder);
    if (tagOrder != "name" && tagOrder != "count") {
        let errors ..= [ "ORDER must be either 'name' or 'count' (using default)" ];
        let tagOrder = defaultOrder;
    }
    // max
    var defaultMax = 50;
    var tagsLimit = $1 ?? $max ?? defaultMax;
    if (tagsLimit is not num || tagsLimit < 0) {
        let errors ..= [ "MAX must be a number >= 0 (using default)" ];
        let tagsLimit = defaultMax;
    }
    // color
    var defaultColor = [ [50,200,50], [50,100,50] ];
    var color = $2 ?? $color ?? defaultColor;
    if (color is list && #color == 3) let color = [ color,color ];
    if (color is not list || #color != 2 ||
        color[0] is not list || #color[0] != 3 || color[1] is not list || #color[1] != 3 ||
        #list.select(color[0]..color[1], "$ is not num || $ < 0 || $ > 255"))
    {
        let errors ..= [ "COLOR must be a list of 3 numbers, or list of two lists of three numbers;" ..
                       " each number must be in the range 0-255 (using default)" ];
        let color = defaultColor;
    }
    // size and sizeUnits (processing these together)
    var defaultSize = [ 80,240 ];
    var defaultUnits = "%";
    var sizeMinMax = $3 ?? $size      ?? defaultSize ;
    var sizeUnits  = $4 ?? $sizeUnits ?? defaultUnits;
    if (sizeMinMax is num) let sizeMinMax = [ sizeMinMax, sizeMinMax ];
    if (sizeMinMax is not list || sizeMinMax[0] is not num ||sizeMinMax[1] is not num) {
        let errors ..= [ "SIZE must be a number or list of two numbers (using default)" ];
        let sizeMinMax = defaultSize;
        let sizeUnits  = defaultUnits;
    }
    // exclude
    var defaultExclude = [];
    var tagsToExclude = $5 ?? $exclude ?? defaultExclude;
    if (tagsToExclude is str) let tagsToExclude = [ tagsToExclude ];
    if (tagsToExclude is not list) {
        let errors ..= [ "EXCLUDE must be a string or list of strings (using default)" ];
        let tagsToExclude = defaultExclude;
    }
    // showcount
    var hitShow = $6 ?? $showCount ?? false;
    // align
    var align = $7 ?? $align ?? "left";
    
    //New parameters
    var sPageUrl= $pageUrl ?? ".";
    var charsToRemove = $charsToRemove ?? [',','.',':','"','+','/','!','?','(',')','\'','=','<','>','{','}',';','|','_'];
    var textToReplace = $textToReplace ?? [[]];
    
    // bail out if errors, to give quick feedback
    if (#errors)
        <div style="color:red">
            <strong> "SiteTagCloud encountered errors:" </strong>;
            <ul> foreach (var e in errors) <li> e </li>; </ul>;
        </div>;
    else {
    //****************************************
    // Pre-process font size and color values
    //****************************************
    var minSize = sizeMinMax[0];
    var sizeRange = sizeMinMax[1] - minSize;
    var colorMin =   { r:color[0][0], g:color[0][1], b:color[0][2] };
    var colorRange = { r:color[1][0]-colorMin.r, g:color[1][1]-colorMin.g, b:color[1][2]-colorMin.b };
    //******************
    // Process the tags
    //******************
    // convert list of excluded tags into a map (easier to check if tag is excluded)
    //let tagsToExclude = { (t): true foreach var t in tagsToExclude };
    
    // compute map of tags based on site data and excluded tags
    
    var netPageText=Wiki.Text(sPageUrl);
    //web.text('Page ('..string.Length(sPageUrl)..','..string.Length(netPageText)..'): ');web.text(netPageText);web.html('<br><br>');
    
    var sNetPageClean=netPageText;
    foreach (var cToRemove in charsToRemove) {
        let sNetPageClean=string.Replace(sNetPageClean,cToRemove,' ');
    }
    foreach (var txtToRep in textToReplace) {
        let sNetPageClean=string.Replace(sNetPageClean,txtToRep[0],txtToRep[1]);
    }
    
    var tagMapPage = {};
    var netPageSplits=string.Split(sNetPageClean,' ');
    //var netPageSplits=string.Split(netPageText,' ');    
    foreach (var sTag in netPageSplits) {
        var iCount=1;
        let sTag=string.Trim(sTag);
        let sTag=string.Replace(sTag,"-","");
        var iTagLen=string.Length(sTag);
        if (iTagLen>0) {
            if (string.EndsWith(sTag,"s")) {
                var sSingTag=string.SubStr(sTag,0,iTagLen-1);
                if (tagMapPage[sSingTag]) {
                    let sTag=sSingTag;
                }
            }
            else
            {
                var sPlurTag=sTag.."s";
                if (tagMapPage[sPlurTag]) {
                    let iCount=tagMapPage[sPlurTag];    
                }
            }
            let tagMapPage ..= { (sTag):iCount+(tagMapPage[sTag] ?? 0) };
        }
    }
    
    foreach (var t in tagsToExclude) {
      let tagMapPage ..= { (t):0 };
    }
    
    var tagListPage = [];
    foreach (var t in map.keys(tagMapPage)) {
      if (tagMapPage[t] > 0) {
        let tagListPage ..= [ { name:t, count:tagMapPage[t] } ];
     }
    }
    
    let tagListPage = list.sort(tagListPage, 'count', true);
    if (tagsLimit > 0) {   let tagListPage = list.splice(tagListPage, tagsLimit); }   var tagMapRed = {}; foreach (var mTagRed in tagListPage) {     let tagMapRed ..= { (mTagRed.name):(mTagRed.count) };         //let tagMapRed ..= { (mTagRed.name):(tagMapPage[mTagRed.name] ?? 0) };     }                 var tagMap = {}; foreach (var sTag in map.Keys(tagMapRed)) {     let tagMap ..= { (sTag):((tagMap[sTag] ?? 0)+(tagMapRed[sTag] ?? 0)) }; } foreach (var t in tagsToExclude) { // web.text(" t: "..t.." count:"..tagMap[t]); if (tagMap[t]>0)   let tagMap ..= { (t):0 }; } // compute list of tags sorted by occurrence count var tagListofMaps = [ { name: string.tocamelcase(t), count: tagMap[t] }     foreach var t in map.keys(tagMap) where tagMap[t] > 0 ]; var tagsTotal = #tagListofMaps; let tagListofMaps = list.sort(tagListofMaps, 'count', true); if ((tagsLimit > 0) && !__request.args.alltags)     let tagListofMaps = list.splice(tagListofMaps, tagsLimit); // pre-calculate some shortcut variables var maxValue = tagListofMaps[0].count; var minValue = tagListofMaps[#tagListofMaps-1].count; var minLog = num.log(minValue); var logRange = num.log(maxValue) - minLog; // sort display of tags by frequency or name let tagListofMaps = list.sort(tagListofMaps, tagOrder, (tagOrder == "count")); //**************** // emit tag cloud //**************** <div id="tagCloud">     <div style=("text-align:" .. align)>         foreach (var tm in tagListofMaps) {             var tagURI = sPageUrl.."?highlight="..tm.name.."+"..tm.name.."s";             //var tagURI = site.uri & "Special:Tags" & { tag:tm.name };             var weight = ( num.log(tm.count) - minLog ) / logRange;             var size = minSize + num.round(sizeRange * weight);             var color = "color:rgb(" ..                 num.min(colorMin.r + num.round(weight*colorRange.r),255) .. "," ..                 num.min(colorMin.g + num.round(weight*colorRange.g),255) .. "," ..                 num.min(colorMin.b + num.round(weight*colorRange.b),255) .. ");";             <span style=("padding-right:5px; font-size:" .. size .. sizeUnits)>                 <a href=(tagURI) style=(color)> tm.name </a>;                 if (hitShow)                     <sup style=(color.."font-size: x-small")> "(" .. tm.count .. ")" </sup>;                 " ";             </span>;         }     </div>;     if (!__request.args.alltags && (tagsLimit > 0) && (tagsTotal > tagsLimit)) {         "Showing top " .. tagsLimit .. " tags. ";         <a href=((page.uri & { alltags: "_" }).."#tagCloud")> "See all "..tagsTotal.." tags »" </a>;     } </div>; }
    Tag page (Edit tags)
    • No tags
    Page statistics
    201 view(s), 34 edit(s) and 9720 character(s)

    Comments

    You must login to post a comment.

    Attach file

    Attachments