Skip to content

Commit

Permalink
Version 2.1.6
Browse files Browse the repository at this point in the history
ECMA48 CUD works as intended with trailing colored text.
Renamed charSetFind() returns.
Text info header now highlights unknown document.characterSets in use by
the browser.
  • Loading branch information
Ben Garrett committed Mar 1, 2017
1 parent a409992 commit 55166db
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 52 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ web-ext-artifacts
style.txt

# Visual Studio Code
.vscode
.vscode

# Windows
.xcopyignore
5 changes: 5 additions & 0 deletions css/retrotxt.css
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ header {
width: 100%;
}

header .unknown {
/* note: header filter: invert(100%); */
color: rgb(0, 170, 170);
}

main {
display: inline-block;
order: 2;
Expand Down
22 changes: 9 additions & 13 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function ListCharacterSets()
// 8 Backspace, 9 Horizontal tab, 10 Line feed (line break), 12 Form feed (page break)
// 13 Carriage return, 26 End of file (not a C0 standard but used in MS-DOS)
this.C0common = [8, 9, 10, 12, 13, 26]
this.sets = [`US_ASCII`, `CP437`, `8859_5`, `CP1252`, `8859_1`, `8859_15`, `UTF8`, `UTF_ERR`]
this.sets = [`out_8859_1`, `out_8859_15`, `out_CP1252`, `out_US_ASCII`, `out_UTF8`, `src_8859_5`, `src_CP1252`]
}

function ListDefaults()
Expand Down Expand Up @@ -279,7 +279,7 @@ function findControlSequences(s = ``)
const t = s.slice(0, 5).toUpperCase() // only need the first 5 characters
let a, b, c
// ECMA-48 control sequences (4/Feb/2017: despite the performance hit, need to run this first to avoid false detections)
if (t.trim().charCodeAt(0) === 27 && t.trim().charCodeAt(1) === 91) return `ecma48` // (16/Feb/2017: trim is needed for some ANSIs)
if (s.trim().charCodeAt(0) === 27 && s.trim().charCodeAt(1) === 91) return `ecma48` // (16/Feb/2017: trim is needed for some ANSIs)
c = s.indexOf(`${String.fromCharCode(27)} ${String.fromCharCode(91)} `) // indexOf is the fastest form of string search
if (c > 0) return `ecma48`
// make sure first char is an @-code
Expand Down Expand Up @@ -322,32 +322,28 @@ function HumaniseCP(code = ``)
{
let text = ``, title = ``
switch (code) {
case `CP437`:
case `8859_5`:
case `src_CP1252`:
case `src_8859_5`:
text = `CP-437`
title = `IBM/MS-DOS Code Page 437`
break
case `CP1252`:
case `out_CP1252`:
text = `Windows-1252`
title = `Code Page 1252 commonly used in legacy Microsoft Windows systems`
break
case `8859_1`:
case `out_8859_1`:
text = `ISO-8859-1`
title = `ISO-8859 Part 1: Latin alphabet No. 1 alternatively known as ECMA-94`
break
case `8859_15`:
case `out_8859_15`:
text = `ISO-8859-15`
title = `ISO-8859 Part 15: Latin alphabet No. 9`
break
case `UTF_ERR`:
text = `Unsupported UTF-8 4-bit encoding`
title = `Currently RetroTxt only supports Unicode characters between 0-65535(0000-FFFF) `
break
case `UTF8`:
case `out_UTF8`:
text = `UTF-8`
title = `Universal Coded Character Set 8-bit`
break
case `US_ASCII`:
case `out_US_ASCII`:
text = `US-ASCII`
title = `Plain text, alternatively known as ASA X3.4, ANSI X3.4, ECMA-6, ISO/IEC 646`
break
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"name": "RetroTxt",
"description": "Turn many pieces of ANSI text art and ASCII/NFO plain text into HTML5 text.",
"version": "2.1.1",
"version": "2.1.6",
"version_name": "2",
"minimum_chrome_version": "49",
"default_locale": "en_US",
Expand Down
48 changes: 30 additions & 18 deletions retrotxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,20 +271,29 @@ function changeLineHeight(lh = `normal`, dom = new FindDOM())
}

function charSetFind(c = ``, dom = {})
// determine character set
// Return the source text character set
// @c Code page cases used by the context menus
// @dom A HTML DOM Object that will be modified
{
if (typeof c !== `string` && c !== null) checkArg(`c`, `string`, c)
if (typeof dom !== `object`) checkArg(`dom`, `object`, dom)

switch (c) {
case `codeMsDos0`: return `CP437`
case `codeMsDos1`: return `8859_5`
case `codeWindows`: return `CP1252`
case `codeLatin9`: return `8859_15`
case `codeNone`: return `US_ASCII`
default: return new BuildCharSet(dom.preProcess).guess
switch (c) { // user overrides
case `codeMsDos0`: return `src_CP1252`
case `codeMsDos1`: return `src_8859_5`
case `codeWindows`: return `out_CP1252`
case `codeLatin9`: return `out_8859_15`
case `codeNone`: return `out_US_ASCII`
default: { // force returns based on browser tab character set
//console.log(`document.characterSet ${document.characterSet.toUpperCase()}`)
switch (document.characterSet.toUpperCase()) {
case `WINDOWS-1252`:
case `UTF-8`: return `src_CP1252`
case `ISO-8859-5`: return `src_8859_5`
default: { // unknown/unsupported encodings, we so guess but the output is most-likely to be incorrect
return new BuildCharSet(dom.preProcess).guess
}
}
}
}
}

Expand All @@ -297,14 +306,15 @@ function charSetRebuild(c = ``, dom = {})
if (typeof dom !== `object`) checkArg(`dom`, `object`, dom)

switch (c) {
case `CP437`:
case `8859_5`: return new BuildCPDos(dom.preProcess, c).text
case `CP1252`: return new BuildCP1252(dom.preProcess).text
case `8859_1`: return new BuildCP88591(dom.preProcess).text
case `8859_15`: return new BuildCP885915(dom.preProcess).text
case `UTF8`: return new BuildCPUtf8(dom.preProcess).text
case `US_ASCII`:
case `UTF_ERR`: return new BuildCPUtf16(dom.preProcess).text
case `src_CP1252`:
case `src_8859_5`: return new BuildCPDos(dom.preProcess, c).text
case `out_CP1252`: return new BuildCP1252(dom.preProcess).text
case `out_8859_1`: return new BuildCP88591(dom.preProcess).text
case `out_8859_15`: return new BuildCP885915(dom.preProcess).text
case `out_UTF8`: return new BuildCPUtf8(dom.preProcess).text
case `out_US_ASCII`: return new BuildCPUtf16(dom.preProcess).text
default:
checkErr(`'${c}' is not a valid charSetRebuild() identifier`, true)
}
}

Expand Down Expand Up @@ -871,9 +881,11 @@ function runRetroTxt(tabId = 0, pageEncoding = `unknown`)
// code page details for text font info.
if (srcMeta.chrSet !== null) {
let dcp = srcMeta.chrSet.replace(`-`, ``).toUpperCase()
let dcpAttr = ``
rev1Text.codePage.text = rev1Text.codePage.text.replace(`CP-`, `CP`)
dcp = dcp.replace(`WINDOWS`, `CP`) // abbreviate WINDOWS1252 to CP1252 etc.
tfi.body = `${tfi.body} <span title="Document encoding set by the browser">${dcp}</span> → \
if ([`CP1252`, `ISO8859-5`, `UTF8`, `UTF16LE`, `UTF16BE`].includes(dcp) === false) dcpAttr = `class="unknown"` // note: header has CSS filter: invert(100%); applied
tfi.body = `${tfi.body} <span title="Document encoding set by the browser"${dcpAttr}>${dcp}</span> → \
<span title="Unicode ≈ ${rev1Text.codePage.title}">${rev1Text.codePage.text}</span>`
}
// font name
Expand Down
10 changes: 4 additions & 6 deletions test/example_files/ecma-48.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ The start of the file MUST begin with an ECMA-48 escape sequence.
FramedRed textBlue on greymagentanot Framed
EncircledRed textBlue on greymagentanot Encircled
OverlinedRed textBlue on greymagentanot Overlined

 Multiple effects that we abort here

This line uses xterm-256 coloured text
And greyscale colours
 Multiple effects that we abort here
[?33h Standard background  iCE color background! [?33l
This line uses xterm-256 coloured textAnd greyscale colours
+ 1st alternative font+ 2nd alternative font
+ 3rd alternative font+ 4th alternative font
+ 5th alternative font+ 6th alternative font
Expand All @@ -29,7 +27,7 @@ The start of the file MUST begin with an ECMA-48 escape sequence.
Currently inline font switching breaks the monospace page layout
ECMA-48 cursor positional control sequences
����������������������������������ͻ
� [0][10] places right�
� [0][10] places right�
����������������������������������ͼ
3 places down and 10 right
This control should erase to the end of the line
Expand Down
1 change: 1 addition & 0 deletions test/example_files/iso-8859-1.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
To display correctly this file needs be saved with ISO 8859-1 encoding.
And RetroTxt, Transcode text > None

RetroTxt ISO 8859-1 Test Page

Expand Down
10 changes: 5 additions & 5 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ QUnit.module(`functions.js`)

QUnit.test(`ListCharacterSets`, function (assert) {
const content = new ListCharacterSets()
assert.equal(content.sets[0], `US_ASCII`, `Should be \`US_ASCII\``)
assert.equal(content.sets[0], `out_US_ASCII`, `Should be \`US_ASCII\``)
})

QUnit.test(`ListDefaults`, function (assert) {
Expand Down Expand Up @@ -186,7 +186,7 @@ QUnit.test(`findEngine`, function (assert) {
})

QUnit.test(`HumaniseCP`, function (assert) {
const content = new HumaniseCP(`CP437`)
const content = new HumaniseCP(`src_CP1252`)
assert.equal(content.text, `CP-437`, `Should be \`CP-437\``)
assert.equal(content.title, `IBM/MS-DOS Code Page 437`, `Should be \`IBM/MS-DOS Code Page 437\``)
})
Expand All @@ -200,11 +200,11 @@ QUnit.test(`BuildCharSet`, function (assert) {
let content = new BuildCharSet(`Hello ♕ world`)
assert.equal(content.countUsAscii, 5, `Should be \`5\``)
assert.equal(content.setPage, 6, `Should be \`6\``)
assert.equal(content.guess, `UTF8`, `Should be \`UTF8\``)
assert.equal(content.guess, `out_UTF8`, `Should be \`UTF8\``)
content = new BuildCharSet(`Hello world`)
assert.equal(content.countUsAscii, 10, `Should be \`5\``)
assert.equal(content.setPage, 0, `Should be \`0\``)
assert.equal(content.guess, `US_ASCII`, `Should be \`US_ASCII\``)
assert.equal(content.guess, `out_US_ASCII`, `Should be \`US_ASCII\``)
})

QUnit.test(`restoreDocument`, function (assert) {
Expand Down Expand Up @@ -893,7 +893,7 @@ QUnit.test(`BuildEcma48()`, function (assert) {

sample = `←[?33h←[47;5m←[B${inputText}` // start with iCE on
test = new BuildEcma48(sample).innerHTML
assert.equal(test, `<div id=\"row-1\"><i class=\"SGR37 SGR147\"></i></div><div id=\"row-2\"><i class=\"SGR37 SGR147\">Hello world.</i><span class=\"dos-cursor\">_</span></div>`, `'${sample}' ${reply}`)
assert.equal(test, `<div id=\"row-1\"><i class=\"SGR37 SGR147\"> </i></div><div id=\"row-2\"><i class=\"SGR37 SGR147\">Hello world.</i><span class=\"dos-cursor\">_</span></div>`, `'${sample}' ${reply}`)

sample = `←[?33l←[47;5m←[B${inputText}` // start with iCE off
test = new BuildEcma48(sample).innerHTML
Expand Down
11 changes: 5 additions & 6 deletions text_cp_dos.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ function List8859_5()
this.set_a[parseInt(`F`, 16)] = `\u00A4`
}

function BuildCPDos(s = ``, mapTo = `CP437`, verbose = false)
function BuildCPDos(s = ``, mapTo = `src_CP1252`, verbose = false)
// Converts a string of text to emulate a MS-DOS Code Page using UTF-16 encoded
// characters.
// @s String of Unicode UTF-16 text
// @mapTo The character encoding map to use, either CP437, 8859_5 or US_ASCII
// @mapTo The character encoding map to use, either src_CP1252, src_8859_5 or out_US_ASCII
// @verbose Display to the console each character that is handled
{
if (typeof s !== `string`) checkArg(`s`, `string`, s)
Expand All @@ -68,12 +68,12 @@ function BuildCPDos(s = ``, mapTo = `CP437`, verbose = false)
let i = t.length
let map0_127, map128_255 // build character maps
switch (mapTo) {
case `CP437`:
case `US_ASCII`:
case `src_CP1252`:
case `out_US_ASCII`:
map0_127 = mapCP437.set_0.concat(mapCP437.set_1)
map128_255 = mapCP437.set_8.concat(mapCP437.set_9, mapCP437.set_a, mapCP437.set_b, mapCP437.set_c, mapCP437.set_d, mapCP437.set_e, mapCP437.set_f)
break
case `8859_5`:
case `src_8859_5`:
map0_127 = mapCP437.set_0.concat(mapCP437.set_1)
map128_255 = mapCP437.set_8.concat(map8859_5.set_9, map8859_5.set_a, mapCP437.set_b, mapCP437.set_c, mapCP437.set_d, mapCP437.set_e, mapCP437.set_f)
break
Expand All @@ -97,7 +97,6 @@ function BuildCPDos(s = ``, mapTo = `CP437`, verbose = false)

// handle characters 129…255 [80…FF]
let cpa = 128 // character position adjustment
if (mapTo === `8859_5`) cpa = cpa + 864 // ISO-8859-5
i = map128_255.length
while (i--) {
if (verbose) console.log(`${i} ${String.fromCharCode(i + cpa)} => ${map128_255[i]}`)
Expand Down
6 changes: 4 additions & 2 deletions text_ecma48.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function BuildEcma48(text = ``, sauce = { version: null }, verbose = false, rule
const phs = `0`.repeat(phl - 1)
// regex for HTML modifications
const emptyTags = new RegExp(/<i class="SGR37 SGR40"><\/i><i id=/ig)
const insSpace = new RegExp(/<div id="row-(\d+)"><i class="SGR37 SGR40"><\/i><\/div>/ig)
const insSpace = new RegExp(/<div id="row-(\d+)"><i class="SGR(\d+) SGR(\d+)"><\/i><\/div>/ig)
let edLine = {}
let S = text
// Clean up string before converting it to decimal values
Expand Down Expand Up @@ -181,7 +181,7 @@ function BuildEcma48(text = ``, sauce = { version: null }, verbose = false, rule
// clean any empty tags
ecma48DOM.html = ecma48DOM.html.replace(emptyTags, `<i id=`)
// force the browsers to show the empty rows by injecting a single space character
ecma48DOM.html = ecma48DOM.html.replace(insSpace, `<div id="row-$1"><i class="SGR37 SGR40"> </i></div>`) // intentional empty space
ecma48DOM.html = ecma48DOM.html.replace(insSpace, `<div id="row-$1"><i class="SGR$2 SGR$3"> </i></div>`) // intentional empty space
// apply erase lines
for (let line of cursor.eraseLines) {
line++ // account for arrays starting at 0 but lines starting at 1
Expand Down Expand Up @@ -676,6 +676,7 @@ function findBackground(v)
{
let valid = false
if (v >= 40 && v <= 49 || v >= 480 && v <= 489 || v >= 4810 && v <= 4899 || v >= 48100 && v <= 48255) valid = true
if (valid === true && v >= 480 && typeof ecma48.colorDepth === `number`) ecma48.colorDepth = 8 // x-term 256 color found
return valid
}

Expand Down Expand Up @@ -845,6 +846,7 @@ function findForeground(v)
{
let valid = false
if (v >= 30 && v <= 39 || v >= 380 && v <= 389 || v >= 3810 && v <= 3899 || v >= 38100 && v <= 38255) valid = true
if (valid === true && v >= 380 && typeof ecma48.colorDepth === `number`) ecma48.colorDepth = 8 // x-term 256 color found
return valid
}

Expand Down

0 comments on commit 55166db

Please sign in to comment.