Code coverage report for master/lib/jsdoc/tutorial.js

Statements: 100% (40 / 40)      Branches: 90.91% (10 / 11)      Functions: 100% (10 / 10)      Lines: 100% (40 / 40)      Ignored: none     

All files » master/lib/jsdoc/ » tutorial.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141              1 1   1             1 51 51 51                 1 147                           1 143 143 143     143 143           1   155 51     155 155 147             1 3           1 3           1 4     1       2 2         1                 1 36   36   1             1 315             1 97           1        
/**
    @overview
    @author RafaƂ Wrzeszcz <rafal.wrzeszcz@wrzasq.pl>
    @license Apache License 2.0 - See file 'LICENSE.md' in this project.
 */
'use strict';
 
var markdown = require('jsdoc/util/markdown');
var util = require('util');
 
var hasOwnProp = Object.prototype.hasOwnProperty;
 
/** Removes child tutorial from the parent. Does *not* unset child.parent though.
    @param {Tutorial} parent - parent tutorial.
    @param {Tutorial} child - Old child.
    @private
 */
function removeChild(parent, child) {
    var index = parent.children.indexOf(child);
    Eif (index !== -1) {
        parent.children.splice(index, 1);
    }
}
 
/** Adds a child to the parent tutorial. Does *not* set child.parent though.
    @param {Tutorial} parent - parent tutorial.
    @param {Tutorial} child - New child.
    @private
 */
function addChild(parent, child) {
    parent.children.push(child);
}
 
/**
    @module jsdoc/tutorial
 */
 
/**
    @class
    @classdesc Represents a single JSDoc tutorial.
    @param {string} name - Tutorial name.
    @param {string} content - Text content.
    @param {number} type - Source formating.
 */
exports.Tutorial = function(name, content, type) {
    this.title = this.name = this.longname = name;
    this.content = content;
    this.type = type;
 
    // default values
    this.parent = null;
    this.children = [];
};
 
/** Moves children from current parent to different one.
    @param {?Tutorial} parent - New parent. If null, the tutorial has no parent.
 */
exports.Tutorial.prototype.setParent = function(parent) {
    // removes node from old parent
    if (this.parent) {
        removeChild(this.parent, this);
    }
 
    this.parent = parent;
    if (parent) {
        addChild(parent, this);
    }
};
 
/** Removes children from current node.
    @param {Tutorial} child - Old child.
 */
exports.Tutorial.prototype.removeChild = function(child) {
    child.setParent(null);
};
 
/** Adds new children to current node.
    @param {Tutorial} child - New child.
 */
exports.Tutorial.prototype.addChild = function(child) {
    child.setParent(this);
};
 
/** Prepares source.
    @return {string} HTML source.
 */
exports.Tutorial.prototype.parse = function() {
    switch (this.type) {
        // nothing to do
        case exports.TYPES.HTML:
            return this.content;
 
        // markdown
        case exports.TYPES.MARKDOWN:
            var mdParse = markdown.getParser();
            return mdParse(this.content);
 
        // uhm... should we react somehow?
        // if not then this case can be merged with TYPES.HTML
        default:
            return this.content;
    }
};
 
/**
 * @class
 * @classdesc Represents the root tutorial.
 * @extends {module:jsdoc/tutorial.Tutorial}
 */
exports.RootTutorial = function() {
    exports.RootTutorial.super_.call(this, '', '');
 
    this._tutorials = {};
};
util.inherits(exports.RootTutorial, exports.Tutorial);
 
/**
 * Retrieve a tutorial by name.
 * @param {string} name - Tutorial name.
 * @return {module:jsdoc/tutorial.Tutorial} Tutorial instance.
 */
exports.RootTutorial.prototype.getByName = function(name) {
    return hasOwnProp.call(this._tutorials, name) && this._tutorials[name];
};
 
/**
 * Add a child tutorial to the root.
 * @param {module:jsdoc/tutorial.Tutorial} child - Child tutorial.
 */
exports.RootTutorial.prototype._addTutorial = function(child) {
    this._tutorials[child.name] = child;
};
 
/** Tutorial source types.
    @enum {number}
 */
exports.TYPES = {
    HTML: 1,
    MARKDOWN: 2
};