GureWork > espacios de Coworking topalekuak :)
Template:@neilw > PrettyPrint

PrettyPrint
PrettyPrintEdit

    Documentation

    This box includes basic usage information for this template.  When calling the template, this documentation will not appear.  Functional template code should be placed outside the dotted box.

    Summary

    Template Description Pretty-print DekiScript data structures.  This is useful for debugging your scripts (makes a much more readable way to see your lists and maps) and also exploring the wiki data structures.
    Requirements Mindtouch 10.0
    Documentation URL http://developer.mindtouch.com/App_C...ata_structures
    Discussion URL  

    Version History

    Place newest version at the top of the table.

    Version Date Author Description
    1.0.0 1-Oct-2010 Neil Weinstock Pretty basic, but usable and useful.
    0.0.0a 1-Oct-2010 Neil Weinstock Just getting started

    Template Parameters

    Name Type Default Description
    data anything required DekiScript data structure to be displayed.  Anything is fair game.
    depth num 3 Number of levels of maps or lists to be expanded.  3 is the max supported for now.
    collapse bool true Collapse maps or lists.  Requires CollapseItem.

    Template Code

    // Arg processing
    var errors = [];
    
    // Data
    var data = $0 ?? $data;
    
    // Depth
    var depth = $1 ?? $depth ?? 3;
    if (depth is not num || depth < 0 || depth > 3) {
        let errors ..= [ "DEPTH must be a number in the range [0,3]" ];
        let depth = 0;
    }
                
    // Collapse
    var collapse = $2 ?? $collapse ?? true;
    if (collapse && !wiki.template("Template:@neilw/CollapseItem")) {
    //if (collapse && !template.collapseitem) {
        let errors ..= [ "COLLAPSE option requires the CollapseItem template to be installed" ];
        let collapse = false;
    }
                    
    // Print errors 
    if (#errors) <div style="color:red; padding:10px; border:1px dotted red;">
        <strong> "PrettyPrint errors:" </strong>;
        <ul> foreach (var e in errors) <li> e </li>; </ul>;
    </div>;
                            
    // PrettyPrint!
    var enclosers = { list: [ "[", "]" ], map: [ "{", "}" ] };
    var d1 = data;
    var display = "display: "..(collapse ? "none" : "block");
                        
    // Level 1
    var d1 = data;
    <div>
        if (depth >= 1 && ((d1 is list || d1 is map) && #d1)) {
            var id1 = @id;
            var e1 = enclosers[typeof(d1)];
            e1[0];            
            if (collapse) wiki.template("Template:@neilw/CollapseItem", { item: "id1", effect: "slide" } );
            //if (collapse) CollapseItem(id1,_,_,"slide");
          <div style=(display) id=(id1)>
            var size1 = #d1-1;
            var set1 = (d1 is map ? list.sort(map.keys(d1)) : num.series(0, size1));
            foreach (var k1 in set1, var d2 = d1[k1], var last1=(__index==size1)) <div style="margin-left: 20px">
                var id2 = id1 .. "_" .. __index;
                if (d1 is map) k1..": ";
                if (depth >= 2 && ((d2 is list || d2 is map) && #d2)) {
                    var e2 = enclosers[typeof(d2)];
                    e2[0];
                    if (collapse) wiki.template("Template:@neilw/CollapseItem", { item: "id2", effect: "slide" } );                
                    //if (collapse) CollapseItem(id2,_,_,"slide");
                  <div style=(display) id=(id2)>
                    var size2 = #d2-1;
                    var set2 = (d2 is map ? list.sort(map.keys(d2)) : num.series(0,size2));
                    foreach (var k2 in set2, var d3 = d2[k2], var last2=(__index==size2)) <div style="margin-left: 20px">
                        var id3 = id2 .. "_" .. __index;
                        if (d2 is map) k2..": ";
                        if (depth >= 3 && ((d3 is list || d3 is map) && #d3)) {
                            var e3 = enclosers[typeof(d3)];
                            e3[0];
                            if (collapse) wiki.template("Template:@neilw/CollapseItem", { item: "id3", effect: "slide" } );
                            //if (collapse) CollapseItem(id3,_,_,"slide");
                          <div style=(display) id=(id3)>
                            var size3 = #d3 - 1;
                            var set3 = (d3 is map ? list.sort(map.keys(d3)) : num.series(0,size3));
                            foreach (var k3 in set3, var d4 = d3[k3], var last3=(__index==size3)) <div style="margin-left: 20px">
                                if (d3 is map) k3..": ";
                                json.emit(d4);
                                if (!last3) ",";
                            </div>;
                          </div>;
                            e3[1];
                        }
                        else json.emit(d3);
                        if (!last2) ",";
                    </div>;
                  </div>;
                    e2[1];
                }
                else json.emit(d2);
                if (!last1) ",";
            </div>;
          </div>;
            e1[1];
        }
        else json.emit(d1);
    </div>;
    
    Powered by MindTouch Core (Expired)