This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
130 lines (117 loc) · 3.26 KB
/
parser.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
const { getColor } = require('./colors.js');
function parseTitle(str) {
return {
data: {
title: str,
}
};
}
function parseDescription(str) {
return {
data: {
description: str
}
}
}
function parseColor(str) {
return {
data: {
color: getColor(str)
}
}
}
function parseImage(str) {
return {
data: {
image: {
url: str
}
}
}
}
function parseFooter(str) {
return {
data: {
footer: {
text: str
}
}
}
}
function parseField(str) {
return {
key: 'fields',
data: [{
name: str.substring(0, str.indexOf("\n")),
value: str.substring(str.indexOf("\n") + 1)
}]
};
}
const commands = {
"t": parseTitle,
"d": parseDescription,
"c": parseColor,
"i": parseImage,
"f": parseField,
"if": parseField,
"fo": parseFooter
};
function handleCommand(currentCommand, currentScope, outputEmbed) {
const output = commands[currentCommand](currentScope.trim());
if (typeof output.key === 'undefined') {
outputEmbed = {...outputEmbed, ...output.data};
} else {
if (Array.isArray(output.data)) {
if (typeof outputEmbed[output.key] === 'undefined')
outputEmbed[output.key] = [];
outputEmbed[output.key] = [...outputEmbed[output.key], ...output.data];
} else if (typeof output.data === 'object') {
if (typeof outputEmbed[output.key] === 'undefined')
outputEmbed[output.key] = {};
outputEmbed[output.key] = {...outputEmbed[output.key], ...output.data};
}
}
return (outputEmbed);
}
function getEmbedsFromBody(body) {
let parsingChangelog = false
let currentCommand = "";
let currentScope = "";
let outputEmbeds = [];
let outputEmbed = {};
body = body.replace(/\r*/g, "");
body.split("\n").forEach(line => {
if (line === "[changelog]") {
if (parsingChangelog) {
if (currentCommand.length !== 0)
outputEmbed = handleCommand(currentCommand, currentScope, outputEmbed);
outputEmbeds.push(outputEmbed);
currentScope = "";
currentCommand = "";
outputEmbed = {};
}
parsingChangelog = true;
return;
}
if (!parsingChangelog) return;
if (line.startsWith(";")) {
let command = line.substring(1, line.indexOf(" "));
let arg = line.substring(line.indexOf(" ") + 1);
if (currentCommand.length !== 0) {
outputEmbed = handleCommand(currentCommand, currentScope, outputEmbed);
currentScope = "";
}
currentCommand = command;
currentScope += currentScope.length === 0 ? arg : "\n" + arg;
}
else
currentScope += currentScope.length === 0 ? line : "\n" + line;
});
if (currentCommand.length !== 0)
outputEmbed = handleCommand(currentCommand, currentScope, outputEmbed);
outputEmbeds.push(outputEmbed);
return outputEmbeds;
}
module.exports = {
getEmbedsFromBody,
}