rrg92 commited on
Commit
0781f58
1 Parent(s): 458ff76

Added Parser

Browse files
Files changed (2) hide show
  1. README.md +6 -1
  2. index.html +209 -19
README.md CHANGED
@@ -8,4 +8,9 @@ pinned: false
8
  license: apache-2.0
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
8
  license: apache-2.0
9
  ---
10
 
11
+ Parse Community Highlights
12
+
13
+ Steps:
14
+
15
+ 1. Copy content from Discord Web
16
+ 1. Paste into "Content" textarea
index.html CHANGED
@@ -1,19 +1,209 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <html>
2
+ <head>
3
+
4
+ <script type="text/javascript">
5
+ let CURRENT_CONTENT;
6
+ let parser = new DOMParser()
7
+
8
+ function FormatDiscordMessage(html){
9
+ let dom = parser.parseFromString(html, "text/html");
10
+ CURRENT_CONTENT.dom = dom;
11
+
12
+ let allChilds = dom.querySelectorAll("body > *");
13
+
14
+ let FullContent = [];
15
+
16
+ let buffContent = []
17
+ let buffLinks = []
18
+ let authorName= [];
19
+ let edition;
20
+
21
+ let flushContent = function(){
22
+ console.log("line break found!");
23
+
24
+ let AllText = buffContent.join("").trim();
25
+ let links = buffLinks.join(",");
26
+
27
+ if(!links)
28
+ return;
29
+
30
+ FullContent.push({
31
+ text: AllText
32
+ ,links
33
+ ,authors: authorName.join(",")
34
+ ,edition
35
+ });
36
+
37
+
38
+
39
+ buffContent = []
40
+ buffLinks = [];
41
+ authorName = []
42
+ }
43
+
44
+ for(let c of allChilds){
45
+ let text;
46
+
47
+ if(c.tagName == "A")
48
+ buffLinks.push(c.href);
49
+
50
+
51
+ if(c.tagName.at(0) == 'H'){
52
+
53
+ let editionMatch = c.textContent.match(/#\d+/g);
54
+
55
+ if(editionMatch){
56
+ edition = parseInt( editionMatch[0].replace('#',''))
57
+ }
58
+
59
+ continue;
60
+ }
61
+
62
+ if(c.classList.contains("mention"))
63
+ authorName.push(c.textContent);
64
+
65
+ text = c.textContent;
66
+
67
+ if(text)
68
+ buffContent.push(text);
69
+
70
+ if(/\!?\s*\n+/.test(text)){
71
+ flushContent();
72
+ }
73
+ }
74
+
75
+
76
+ // last
77
+ if(buffContent){
78
+ flushContent();
79
+ }
80
+
81
+
82
+ return { dom, content: FullContent };
83
+ }
84
+
85
+ function ProcessPastedMessage(){
86
+ let res = FormatDiscordMessage(CURRENT_CONTENT.html)
87
+
88
+ CURRENT_CONTENT.result = res;
89
+
90
+ let out = document.querySelector("#result");
91
+
92
+ let xDoc = document.implementation.createDocument(null, "highlights");
93
+ let rootDoc = xDoc.querySelector("highlights");
94
+
95
+ let Stats = {
96
+ total: 0
97
+ ,edition: null
98
+ };
99
+
100
+ Stats.edition = res.content[0].edition;
101
+
102
+ for(let [i,high] of res.content.entries()){
103
+ let xHigh = xDoc.createElement("highlight");
104
+
105
+ xHigh.setAttribute("autor", high.authors);
106
+ xHigh.setAttribute("links", high.links);
107
+ xHigh.setAttribute("edition", high.edition);
108
+ xHigh.textContent = high.text;
109
+
110
+ rootDoc.appendChild(xHigh)
111
+
112
+ Stats.total++
113
+
114
+ }
115
+
116
+
117
+ document.querySelector("#stats").innerHTML = `Stats: total = ${Stats.total}, edition = ${Stats.edition}`
118
+
119
+ let serializer = new XMLSerializer();
120
+ out.innerHTML = serializer.serializeToString(xDoc);
121
+ }
122
+
123
+
124
+ function ProcessPasted(content){
125
+
126
+ navigator.clipboard.read(["text/html"])
127
+ .then( async (content) => {
128
+
129
+ console.log("content", content[0].types)
130
+
131
+ let contentTypes = content[0].types;
132
+ let plainText = await (await content[0].getType("text/plain")).text();
133
+ let html = null;
134
+
135
+ if(contentTypes.includes("text/html")){
136
+ htmlContent = await content[0].getType("text/html");
137
+ console.log("html:", htmlContent);
138
+
139
+ html = await htmlContent.text();
140
+
141
+ CURRENT_CONTENT = {
142
+ html: await htmlContent.text()
143
+ ,text: plainText
144
+ }
145
+
146
+ } else {
147
+ console.log("NotContainsHtml");
148
+ }
149
+
150
+
151
+ CURRENT_CONTENT = {
152
+ html
153
+ ,text: plainText
154
+ }
155
+
156
+ setTimeout(ProcessPastedMessage, 100)
157
+
158
+ })
159
+
160
+ return false;
161
+ }
162
+
163
+ addEventListener("paste", ProcessPasted);
164
+
165
+
166
+ </script>
167
+ <style>
168
+ textarea {
169
+ width: 100%;
170
+ }
171
+
172
+ .container {
173
+ display: flex;
174
+ flex-direction: row;
175
+ }
176
+
177
+ .container > div {
178
+ width: 50%;
179
+ height: 70vh;
180
+ padding: 5px;
181
+ }
182
+
183
+ .container textarea {
184
+ height: 100%;
185
+ }
186
+ </style>
187
+
188
+ </head>
189
+ <body>
190
+ <div>
191
+ <p id="stats"></p>
192
+ </div>
193
+ <div class="container">
194
+ <div>
195
+ <p>Content</p>
196
+ <textarea></textarea>
197
+ </div>
198
+
199
+ <div>
200
+ <p>XML</p>
201
+ <textarea readonly id="result"></textarea>
202
+ </div>
203
+ </div>
204
+
205
+
206
+
207
+
208
+ </body>
209
+ </html>