UNPKG

4.38 kBJavaScriptView Raw
1/*
2
3 miniwrite
4
5 https://github.com/Bartvds/miniwrite
6
7 Copyright (c) 2013 Bart van der Schoor
8
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without
12 restriction, including without limitation the rights to use,
13 copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the
15 Software is furnished to do so, subject to the following
16 conditions:
17
18 The above copyright notice and this permission notice shall be
19 included in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32
33(function () {
34 'use strict';
35
36 var core = require('./core');
37
38 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39
40 function getAttributes(attributes) {
41 var t = typeof attributes;
42 if (t === 'string') {
43 return attributes;
44 }
45 if (t === 'object') {
46 return Object.keys(attributes).reduce(function (memo, key) {
47 memo.push(key + '="' + attributes[key] + '"');
48 return memo;
49 }, []).join(' ');
50 }
51 return '';
52 }
53
54 function getHTMLWrap(tag, attributes, linebreak) {
55 tag = (tag || 'span');
56 attributes = getAttributes(attributes);
57 linebreak = (typeof linebreak !== 'undefined' ? linebreak : '\n');
58
59 var pre = '<' + tag + (attributes.length > 0 ? ' ' + attributes : '') + '>';
60 var post = '</' + tag + '>' + linebreak;
61
62 return function (str) {
63 return pre + str + post;
64 };
65 }
66
67 function getHTMLSpanWrap() {
68 return getHTMLWrap('span', 'style="white-space:pre;font-family:monospace;"', '\n');
69 }
70
71 function getHTMLDomAppend(tag, attributes) {
72 tag = (tag || 'span');
73 attributes = getAttributes(attributes);
74
75 var keys = Object.keys(attributes);
76 if (keys.length === 0) {
77 return function (str) {
78 var elem = document.createElement(tag);
79 elem.appendChild(document.createTextNode(str));
80 return elem;
81 };
82 }
83
84 return function (str) {
85 var elem = document.createElement(tag);
86 elem.appendChild(document.createTextNode(str));
87 for (var i = 0, ii = keys.length; i < ii; i++) {
88 var atr = document.createAttribute(keys[i]);
89 atr.nodeValue = attributes[keys[i]];
90 elem.setAttributeNode(atr);
91 }
92 return elem;
93 };
94 }
95
96 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97
98 function htmlString(target, tag, attributes, linebreak) {
99 var wrap = getHTMLWrap((tag || 'span'), (attributes || {style: 'white-space:pre;font-family:monospace;'}), linebreak);
100 var mw = core.base();
101 mw.enabled = true;
102 mw.target = target;
103 mw.writeln = function (line) {
104 if (mw.enabled) {
105 mw.target.writeln(wrap(line));
106 }
107 };
108 mw.toString = function () {
109 return '<miniwrite-html-string>';
110 };
111 return mw;
112 }
113
114 function htmlAppend(parent, tag, attributes) {
115 var wrap = getHTMLDomAppend((tag || 'span'), (attributes || {style: 'white-space:pre;font-family:monospace;'}));
116 var mw = core.base();
117 mw.enabled = true;
118 mw.parent = parent;
119 mw.writeln = function (line) {
120 if (mw.enabled) {
121 mw.parent.appendChild(wrap(line));
122 }
123 };
124 mw.toString = function () {
125 return '<miniwrite-html-append>';
126 };
127 return mw;
128 }
129
130 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131
132 // assemble exports
133 var html = {
134 getHTMLWrap: getHTMLWrap,
135 getHTMLSpanWrap: getHTMLSpanWrap,
136 getHTMLDomAppend: getHTMLDomAppend,
137
138 htmlString: htmlString,
139 htmlAppend: htmlAppend
140 };
141
142 module.exports = html;
143
144}).call();
145