Implemented Unflow for plaintext content

This commit is contained in:
Luxferre
2023-04-01 15:08:55 +03:00
parent 5ce5069a70
commit 28359eb889
4 changed files with 34 additions and 4 deletions
+23 -1
View File
@@ -10,6 +10,24 @@ Hi01379 = (function(psGopherRequest) {
return str.replace(/[&<>]/g, function(tag) {return tagsToReplace[tag] || tag})
}
function unphlow(str) { // Unphlow algorithm implementation
var lines=str.split('\n'), line, l = lines.length, i, buf = '', out = []
for(i=0;i<l;i++) {
line = lines[i].trim() // remove all leading/trailing whitespace-class chars
if(line.length) // if the line is not empty, just append it and a whitespace to the buffer
buf += line + ' '
else { // output logic
out.push(buf, '')
buf = ''
}
}
if(buf.length) // process the remaining output
out.push(buf)
return out.map(function(s) { // final whitespace sanitation
return s.replace(/\s+/g, ' ').trim()
}).join('\n')
}
function gophermapToHTML(gmap, chost, cport) {
if(!cport) cport = 70
if(!chost) chost = 'localhost'
@@ -98,13 +116,17 @@ Hi01379 = (function(psGopherRequest) {
})
}
else { // assuming text content otherwise
var output = (new TextDecoder).decode(rawdata), ctype = 'text/plain' // defaulting to type 0
var output = (new TextDecoder).decode(rawdata), ctype = 'text/plain', // defaulting to type 0
wo = '' // wrapper-friendly output
if(type == '1' || type == '7') { // gophermap
output = gophermapToHTML(output, resource[2], resource[3])
ctype = 'text/html'
wo = output
}
else wo = unphlow(output)
successCb({
content: output,
contentWf: wo,
contentType: ctype,
serviceMsg: type + ' ' + resource[1]
})