UNPKG

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