-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfwl.mjs
More file actions
736 lines (681 loc) · 19.6 KB
/
Copy pathfwl.mjs
File metadata and controls
736 lines (681 loc) · 19.6 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/* This commentaries are made by hand to be processed by https://github.com/StringManolo/docu */
/* lang -> javascript
* name -> fwl
* title -> FWL - Documentation
* filetype -> ES6 module
* description -> FWL (Function Web Language) is a javascript ES6 module code geneator. Is intended to write full featured documentation and generate html, markdown and bbcode.
* summary -> Library to create documents in html, markdown and bbcode.
* sourceLink -> https://github.com/StringManolo/docu/blob/master/fwl.mjs
*/
/* function -> fwl
* summary -> Bind the library methods to the object
* param -> obj -> Object -> Object to bind the methods to
* return -> undefined
* example -> import fwl from "./fwl.mjs";
fwl(global);
create(
h1 `Example`,
p `This is a paragraph`
hr ``
text(`This is a template using javascript variables ${new Date()}`)
)
*/
const fwl = obj => {
/* property -> obj.documentType
* summary -> Specify what document to make
* values -> "html","markdown","bbcode"
* example -> obj.documentType = "html" // create outputs html
obj.documentType = "markdown" // create outputs markdown instead
obj.documentType = "bbcode" // create outputs bbcode instead
*/
obj.documentType = "html";
/* method -> obj.create
* summary -> Print and return the generated code for the chosen document
* param -> (...s) -> String -> String/s to print
* return -> str -> String -> All the strings
* example -> create (
html `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">`,
title `FWL`,
html `</head>
<body>`,
h1 `FWL (Function Web Language)`,
h3 `FWL is a simple "language" designed to build webpages and documents in html, markdown, bbcode...`,
html `</body>
</html>`
);
*/
obj.create = (...s) => {
let str = "";
for (let i in s) {
if (!/REMOVETHISLINE/g.test(s[i])) {
str += `${s[i]}\n`;
}
}
console.log(str);
return str;
}
/* method -> obj.html
* summary -> Only add the string to generated html if HTML document.
* param -> s -> String -> HTML code to include
* return -> s -> String -> Html code
* example -> html `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
`
const startingBodyTag = "<body>";
const endingBodyTag = "</body>";
create (
html (startingBodyTag),
h1 `Hello`,
html (endingBodyTag)
`);
// This will generate only the h1 tag in markdown and bbcode
// and will add the body tags if the document is html
*/
obj.html = s => {
switch(obj.documentType) {
case "html" : return s;
case "markdown" : return "REMOVETHISLINE";
case "bbcode" : return "REMOVETHISLINE";
}
}
/* method -> obj.title
* summary -> Add title tag to generated html
* param -> s -> String -> Document title
* return -> code -> String -> String between title tags (html)
* example -> title `Documentation`
*/
obj.title = s => {
switch(obj.documentType) {
case "html" : return `<title>${s}</title>`;
case "markdown" : return "REMOVETHISLINE";
case "bbcode" : return "REMOVETHISLINE";
}
}
/* method -> obj.text
* summary -> Add text to the document
* param -> s -> String -> Text to add.
* return -> s -> String -> Text code
* example -> text `This text will be shown in generated documents`
*/
obj.text = s => {
switch(obj.documentType) {
case "html" : return `<span>${s}</span>`;
case "markdown" : return `${s}`;
case "bbcode" : return `${s}`;
}
}
/* method -> obj.p
* summary -> Add text in a paragraph to the document
* param -> s -> String -> Text to insert
* return -> s -> String -> Paragraph code
* example -> p `My text`
*/
obj.p = s => {
switch(obj.documentType) {
case "html" : return `<p>${s}</p>`;
case "markdown" : return ` \n${s} \n`;
case "bbcode" : return `\n${s}\n`;
}
}
/* method -> obj.br
* summary -> Add a linebreak
* param -> s -> String -> Text to prepend the linebreak
* return -> s -> String -> Linebreak code
* example -> br `This text is line break terminated`
*/
obj.br = (s="") => {
switch(obj.documentType) {
case "html" : return `${s}<br />`;
case "markdown" : return `${s} \n`;
case "bbcode" : return `${s}\n`;
}
}
/* method -> obj.h1
* summary -> Add text as h1
* param -> s -> String -> Heading text
* return -> s -> String -> Heading code
* example -> h1 `Documentation`
*/
obj.h1 = s => {
switch(obj.documentType) {
case "html" : return `<h1>${s}</h1>`;
case "markdown" : return `# ${s}`;
case "bbcode" : return `[size=18pt][b]${s}[/b][/size]\n`; //`[h1]${s}[/h1]`;
}
}
/* method -> obj.h2
* summary -> Add text as h2
* param -> s -> String -> Heading text
* return -> s -> String -> Heading code
* example -> h2 `Articles`
*/
obj.h2 = s => {
switch(obj.documentType) {
case "html" : return `<h2>${s}</h2>`;
case "markdown" : return `## ${s}`;
case "bbcode" : return `[size=16pt][b]${s}[/b][/size]\n`; //`[h2]${s}[/h2]`;
}
}
/* method -> obj.h3
* summary -> Add text as h1
* param -> s -> String -> Heading text
* return -> s -> String -> Heading code
* example -> h3 `Article Title`
*/
obj.h3 = s => {
switch(obj.documentType) {
case "html" : return `<h3>${s}</h3>`;
case "markdown" : return `### ${s}`;
case "bbcode" : return `[size=15pt][b]${s}[/b][/size]\n`; //`[h3]${s}[/h3]`;
}
}
/* method -> obj.h4
* summary -> Add text as h1
* param -> s -> String -> Heading text
* return -> s -> String -> Heading code
* example -> h4 `Article Content Title`
*/
obj.h4 = s => {
switch(obj.documentType) {
case "html" : return `<h4>${s}</h4>`;
case "markdown" : return `#### ${s}`;
case "bbcode" : return `[size=13pt][b]${s}[/b][/size]\n`; //`[h4]${s}[/h4]`;
}
}
/* method -> obj.h5
* summary -> Add text as h1
* param -> s -> String -> Heading text
* return -> s -> String -> Heading code
* example -> h5 `Part of the article`
*/
obj.h5 = s => {
switch(obj.documentType) {
case "html" : return `<h5>${s}</h5>`;
case "markdown" : return `##### ${s}`;
case "bbcode" : return `[size=11pt][b]${s}[/b][/size]`; //`[h5]${s}[/h5]`;
}
}
/* method -> obj.h6
* summary -> Add text as h1
* param -> s -> String -> Heading text
* return -> s -> String -> Heading code
* example -> h6 `Small title`
*/
obj.h6 = s => {
switch(obj.documentType) {
case "html" : return `<h6>${s}</h6>`;
case "markdown" : return `###### ${s}`;
case "bbcode" : return `[size=9pt][b]${s}[/b][/size]`; //`[h6]${s}[/h6]`;
}
}
/* method -> obj.link
* summary -> Create a clickable link
* param -> s -> String -> Link text + space + url
* return -> s -> String -> Clickable link code
* example -> link `StringManolo Github Account Link https://github.com/StringManolo`
*/
obj.link = s => {
let s1, s2;
if (typeof(s) == "string") {
s = [s];
}
if (/ /g.test(s[0])) {
let aux = s[0].split(" ");
s1 = aux.splice(aux.length - 1);
s2 = aux.join(" ");
} else {
s1 = s[0];
}
switch(obj.documentType) {
case "html" : return `<a href="${s1}">${s2}</a>`;
case "markdown" : return `[${s2}](${s1})`;
case "bbcode" : return `[url=${s1}]${s2}[/url]`;
}
}
/* method -> obj.image
* summary -> Create a image
* param -> s -> String -> Alt text + space + url
* return -> s -> String -> Image code
* example -> image `google favicon https://google.com/favicon.ico`
*/
obj.image = s => {
let s1, s2;
if (/ /g.test(s[0])) {
let aux = s[0].split(" ");
s1 = aux.splice(aux.length - 1);
s2 = aux.join(" ");
} else {
s1 = s[0];
}
switch(obj.documentType) {
case "html" : return `<img src="${s1}" alt="${s2}">`;
case "markdown" : return ``;
case "bbcode" : return `[img alt=${s2}]${s1}[/img]`;
}
}
/* method -> obj.code
* summary -> Create a highlighted code block
* param -> s -> String -> Code. First line is only the language name
* return -> s -> String -> Code block.
* example -> code `#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}`
*/
obj.code = s => {
let s1, s2;
if (typeof(s) != "string") {
s = s[0];
}
if (/(\r\n|\r|\n)/g.test(s)) {
let aux = s.split("\n");
s1 = aux.splice(0, 1);
s2 = aux.join("\n");
} else {
if (/ /g.test(s)) {
let aux = s.split(" ");
s1 = aux.splice(aux.length - 1);
s2 = aux.join(" ");
} else {
s1 = s;
}
}
switch(obj.documentType) {
case "html" : return `<pre><code lang="${s1}">${htmlEntities(s2)}</code></pre>`;
case "markdown" : return "```" + s1 + "\n" + s2 + "\n```";
case "bbcode" : return `[code=${s1}]${s2}[/code]`;
}
}
/* method -> obj.code
* summary -> Create a highlighted code block
* param -> s -> String -> Code. First line is only the language name
* return -> s -> String -> Code block.
* example -> code `#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}`
*/
obj.rawCode = s => {
let s1, s2;
if (typeof(s) != "string") {
s = s[0];
}
if (/(\r\n|\r|\n)/g.test(s)) {
let aux = s.split("\n");
s1 = aux.splice(0, 1);
s2 = aux.join("\n");
} else {
if (/ /g.test(s)) {
let aux = s.split(" ");
s1 = aux.splice(aux.length - 1);
s2 = aux.join(" ");
} else {
s1 = s;
}
}
switch(obj.documentType) {
case "html" : return `<pre><code lang="${s1}">${s2}</code></pre>`;
case "markdown" : return "```" + s1 + "\n" + s2 + "\n```";
case "bbcode" : return `[code=${s1}]${s2}[/code]`;
}
}
/* method -> obj.bold
* summary -> Make text bold
* param -> s -> String -> Text
* return -> s -> String -> Bold text
* example -> bold `important`;
*/
obj.bold = s => {
switch(obj.documentType) {
case "html" : return `<b>${s}</b>`;
case "markdown" : return `**${s}**`;
case "bbcode" : return `[b]${s}[/b]`;
}
}
/* method -> obj.italic
* summary -> Make text italic
* param -> s -> String -> Text
* return -> s -> String -> Italized text
* example -> italic `cool`
*/
obj.italic = s => {
switch(obj.documentType) {
case "html" : return `<i>${s}</i>`;
case "markdown" : return `_${s}_`;
case "bbcode" : return `[i]${s}[/i]`;
}
}
/* method -> obj.underline
* summary -> Underline the text
* param -> s -> String -> Text
* return -> s -> String -> Underlined text (markdown returns normal text)
* example -> underline `underlined text`
*/
obj.underline = s => {
switch(obj.documentType) {
case "html" : return `<u>${s}</u>`;
case "markdown" : return `${s}`;
case "bbcode" : return `[u]${s}[/u]`;
}
}
/* method -> obj.list
* summary -> Create a dotted list of items
* param -> s -> String -> Comma separated list of items
* return -> s -> String -> List code
* example -> list `car,bike,plane,truck,bus`
*/
obj.list = s => {
if (typeof(s) == "string") {
s = [s];
}
/* TODO escapedcomma not working as intended */
let aux = replaceAll(s[0], "\\\\,", "ESCAPEDCOMMA").split(",");
for (let i in aux) {
aux[i] = replaceAll(aux[i], "ESCAPEDCOMMA", ",");
}
let str = "";
switch(obj.documentType) {
case "html": {
str += "<ul>\n";
for(let i in aux) {
str += `<li>${aux[i]}</li>\n`;
}
str += "</ul>";
}
return str;
case "markdown": {
for(let i in aux) {
str += `- ${aux[i]}\n`;
}
}
return str.substr(0, str.length-1);
case "bbcode": {
str += "[list]\n";
for(let i in aux) {
str += `[li]${aux[i]}[/li]\n`;
}
str += "[/list]";
}
return str;
}
}
/* method -> obj.olist
* summary -> Create a numeric list of items
* param -> s -> String -> Comma separated list of items
* return -> s -> String -> List code
* example -> olist `Press the button,Wait 5 seconds,Press the button again,done`
*/
obj.olist = s => {
if (typeof(s) == "string") {
s = [s];
}
let aux = replaceAll(s[0], "\\\\,", "ESCAPEDCOMMA").split(",");
for (let i in aux) {
aux[i] = replaceAll(aux[i], "ESCAPEDCOMMA", ",");
}
let str = "";
switch(obj.documentType) {
case "html": {
str += "<ol>\n";
for(let i in aux) {
str += `<li>${aux[i]}</li>\n`;
}
str += "</ol>";
}
return str;
case "markdown": {
for(let i = 0; i < aux.length; ++i) {
str += `${+i + 1}. ${aux[i]}\n`;
}
}
return str.substr(0, str.length-1);
case "bbcode": {
str += "[list]\n";
for(let i in aux) {
str += `[li]${aux[i]}[/li]\n`;
}
str += "[/list]";
}
return str;
}
}
/* method -> obj.hr
* summary -> Add a horizontal line separator
* param -> ()
* return -> s -> String -> Horizontal separator code
* example -> hr ``
*/
obj.hr = () => {
switch(obj.documentType) {
case "html" : return `<hr>`;
case "markdown" : return `\n--- \n`;
case "bbcode" : return `\n[hr]\n`;
}
}
/* method -> obj.quote
* summary -> Create a quoted block
* param -> s -> String -> Text block
* return -> s -> String -> Quoted block
* example -> quote `"This text is being quoted"`
*/
obj.quote = s => {
let aux = "";
if (obj.documentType == "markdown") {
s = s[0].split("\n");
for (let i in s) {
aux += "> " + s[i] + "\n";
}
aux.substr(0, aux.length - 1);
}
switch(obj.documentType) {
case "html" : return `<blockquote>${s}</blockquote>`;
case "markdown" : return aux;
case "bbcode" : return `[quote]${s}[/quote]`;
}
}
/* method -> obj.table
* summary -> Create a table
* param -> s -> String -> | separated list
* return -> s -> String -> Table code
* example -> table `item | price
bag | 12€
shoes | 3€`
*/
obj.table = s => {
s = s[0].split("\n");
let aux = [];
for (let i = 0; i < s.length; ++i) {
aux.push(s[i].split(" | "));
}
let str = "";
switch(obj.documentType) {
case "html": {
str += "<table style='border:1px solid #555'>\n";
for (let i = 0; i < aux.length; ++i) {
if (i == 0) {
str += "<tr>";
for (let j in aux[i]) {
str += `<th>${aux[i][j]}</th>`;
}
str += "</tr>\n";
} else {
str += "<tr>";
for (let j in aux[i]) {
str += `<td>${aux[i][j]}</td>`;
}
str += "</tr>\n";
}
}
str += "</table>";
return str;
}
case "markdown": {
for (let i = 0; i < aux.length; ++i) {
for (let j in aux[i]) {
str += `| ${aux[i][j]} `;
}
str += "| \n";
if (i == 0) {
for (let j in aux[i]) {
str += `| --- `;
}
str += "|\n";
}
}
return str;
}
case "bbcode": {
str += "[table]\n";
for (let i = 0; i < aux.length; ++i) {
if (i == 0) {
str += "[tr]";
for (let j in aux[i]) {
//str += `[th]${aux[i][j]}[/th]`;
str += `[td]${aux[i][j]}[/td]`;
}
str += "[/tr]\n";
} else {
str += "[tr]";
for (let j in aux[i]) {
str += `[td]${aux[i][j]}[/td]`;
}
str += "[/tr]\n";
}
}
str += "[/table]";
return str;
}
}
}
/* method -> obj.style_tables
* summary -> Add tables css if generating a html document
* param -> ()
* return -> s -> String -> CSS style code
* example -> style_tables ``
*/
obj.style_tables = () => {
switch(obj.documentType) {
case "html" : return `<style>
table {
margin-bottom: 24px;
border-spacing: 0;
border-bottom: 2px solid black;
border-top: 2px solid black;
} table th {
padding: 3px 10px;
background-color: #ddd;
border-top: none;
border-left: none;
border-right: none;
border-bottom: 1px solid black;
} table td {
padding: 3px 10px;
border-top: none;
border-left: 1px solid black;
border-bottom: none;
border-right: 1px solid black;
}
</style>`;
case "markdown" : return "REMOVETHISLINE";
case "bbcode" : return "REMOVETHISLINE";
}
}
/* method -> obj.style_default
* summary -> Add document css if generating a html document
* param -> ()
* return -> s -> String -> CSS style code
* example -> style_default``
*/
obj.style_default = () => {
switch(obj.documentType) {
case "html" : return `<style>
html {
margin: 0;
width: 100%;
height: 100%;
text-align: middle;
text-size-adjust: none; /* Fix Android Big Text */
touch-action: manipulation; /* Disable double tap zoom on touch devices to allow fast touch actions */
} body {
padding-bottom: 16px; /* Some Android screens cover bottom viewport */
background-color: #eee;
color: #555;
} code {
display: block;
color: #ccc;
font-family: "Courier New", monospace;
white-space: pre;
overflow: auto;
margin-bottom: 8px;
padding: 10px;
opacity: 0.9;
color: #f82;
background-color: #222;
font-weight: 600;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
border: 1px solid black;
} blockquote {
background-color: #eee;
color: #333;
border-top: 2px solid black;
padding: 4px;
} table {
margin-left: 4px;
border-spacing: 0;
border-bottom: 2px solid black;
border-top: 2px solid black;
border-radius: 2px;
} table th {
padding: 3px 10px;
background-color: #ddd;
border-top: none;
border-left: none;
border-right: none;
border-bottom: 1px solid black;
} p {
margin: 0;
} h5 {
margin: 18px 0 -14px 0;
}
</style>`;
case "markdown" : return "REMOVETHISLINE";
case "bbcode" : return "REMOVETHISLINE";
}
}
}
/* function -> replaceAll
* summary -> Replace all ocurrences of a string from the text
* param -> str -> String -> Text to replace findings from
* param -> pattern -> String -> Text (coverted to regular expression) to replace
* param -> newStr -> String -> New text to replaced findings for
* return -> s -> String -> Text with all ocurrences replaced
* example -> replaceAll("hello Manolo, hello", "hello", "bye");
*/
const replaceAll = (str, pattern, newStr) => {
const reg = new RegExp(pattern, "g");
while (reg.test(str)) {
str = str.replace(reg, newStr);
}
return str;
}
/* function -> htmlEntities
* summary -> Convert all characters to HTML entities to avoid text being interpretated as code
* param -> s -> String -> Code to covert to HTML entities
* return -> a -> String -> HTML entities
* example -> htmlEntities("My favourite tag is <iframe src='javascript:alert(1337)'></iframe>");
*/
const htmlEntities = s => {
const r = "replace";
const a = s[r](/ /g," ")[r](/!/g,"!")[r](/"/g,""")[r](/%/g,"%")[r](/'/g,"'")[r](/\(/g,"(")[r](/\)/g,")")[r](/</g,"<")[r](/>/g,">")[r](/`/g,"`")[r](/a/g,"a")[r](/A/g,"A")[r](/e/g,"e")[r](/E/g,"E")[r](/i/g,"i")[r](/I/g,"I")[r](/o/g,"o")[r](/O/g,"O")[r](/u/g,"u")[r](/U/g,"U")[r](/{/g,"{")[r](/}/g,"}")[r](/‘/g,"‘")[r](/’/g,"’")[r](/‚/g,"‚")[r](/“/g,"“")[r](/”/g,"”")[r](/„/g,"„")[r](/′/g,"′")[r](/″/g,"‴")[r](/‹/g,"‹")[r](/›/g,"›")[r](/s/g,"s")[r](/S/g,"S")[r](/\./g,".");
return a;
}
export default fwl;