Skip to content

Commit

Permalink
Switch to vendoring the codepage library as the npm package is defunct.
Browse files Browse the repository at this point in the history
  • Loading branch information
ILOVEPIE committed Apr 15, 2024
1 parent 0a7fde7 commit a820ee4
Show file tree
Hide file tree
Showing 11 changed files with 1,652 additions and 57 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ help:
@echo "Valid Targets:"
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#._]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs

test:
deps:
@echo "Update dependencies..."
@sh ./scripts/commands/update-dependencies.sh

test: deps
@echo "Testing..."
@sh ./scripts/commands/test.sh

Expand All @@ -33,7 +37,7 @@ clean:
@echo "Cleaning build directorys."
@sh ./scripts/commands/clean.sh

rebuild: clean local
rebuild: clean deps local

cleanup:
@echo "Cleaning up build tools."
Expand Down
4 changes: 0 additions & 4 deletions README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ Note: Version 2.0.0 of opentype.js is not yet released, in order to use this lib

How to include the library (from the jsdelivr CDN, this cdn is recommended as they publish usage statistics for each package):
```html
<script src="https://cdn.jsdelivr.net/npm/codepage@1.15.0/cptable.js"></script>
<script src="https://cdn.jsdelivr.net/npm/codepage@1.15.0/cputils.js"></script>
<script src="https://cdn.jsdelivr.net/npm/opentype.js@^2.0.0/dist/opentype.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@sabre-js/sabre@latest/dist/sabre.min.js"></script>
```
How to include the library (from the unpkg CDN, for the more privacy minded):
```html
<script src="https://unpkg.com/codepage@1.15.0/cptable.js"></script>
<script src="https://unpkg.com/codepage@1.15.0/cputils.js"></script>
<script src="https://unpkg.com/opentype.js@^2.0.0/dist/opentype.min.js"></script>
<script src="https://unpkg.com/@sabre-js/sabre@latest/dist/sabre.min.js"></script>
```
Expand Down
4 changes: 4 additions & 0 deletions include/lib/codepage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @type {{utils:{encode:function(number,string):Array<number>,decode:function(number,ByteArray):string}}}
*/
sabre.cptable = {utils: {encode: function () {},decode: function () {}}};
9 changes: 1 addition & 8 deletions include/shared.include.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,4 @@ opentype.parse = function () {};
/**
* @typedef {Array<number>|Uint8Array}
*/
let ByteArray;

/**
* @type {{utils:{encode:function(number,string):Array<number>,decode:function(number,ByteArray):string}}}
*/
global.cptable = {utils: {encode: function () {},decode: function () {}}};

let cptable = global.cptable;
let ByteArray;
1 change: 1 addition & 0 deletions scripts/commands/clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
find "$DEBUG_BIN_DIR/" -type f -not -name .gitignore | xargs rm -f
find "$BIN_DIR/" -type f -not -name .gitignore | xargs rm -f
find "$TEMP_DIR/" -type f -not -name .gitignore | xargs rm -f
rm -rf "$TEMP_DIR/codepage"
rm -f "$TOOL_DATA_DIR/changes.cfg"
19 changes: 19 additions & 0 deletions scripts/commands/update-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
. "$PWD/scripts/bootstrap.sh"

LOG_FILE="$PROJECT_ROOT/dependencies.log"
if [ ! -d "$TEMP_DIR/codepage" ]; then
rm -rf "$TEMP_DIR/codepage"
git clone "https://git.sheetjs.com/sheetjs/js-codepage.git" "$TEMP_DIR/codepage"
fi
pushd "$TEMP_DIR/codepage"
printf '%s\n' "Building codepage dependency..." | tee -a $LOG_FILE
git pull 2>&1 | tee -a $LOG_FILE
make 2>&1 | tee -a $LOG_FILE
bash make.sh "$TOOL_DATA_DIR/codepage.csv" "$TEMP_DIR/codepage.js" cptable 2>&1 | tee -a $LOG_FILE
printf '%s\n' '(function(){' > "$PROJECT_SOURCE_DIR/lib/codepage.js"
cat "$TEMP_DIR/codepage.js" "$TEMP_DIR/codepage/cputils.js" | sed "s#require\('./cptable'\)#cptable#" | sed 's#root\.cptable#root["cptable"]#' | sed 's#cptable = factory(cptable)#root["cptable"] = factory(cptable)#' | sed 's#cpt\.utils#cpt["utils"]#' | sed 's#{ decode: decode, encode: encode, hascp: hascp, magic: magic, cache:cache }#{ "decode": decode, "encode": encode, "hascp": hascp, "magic": magic, "cache":cache }#' >> "$PROJECT_SOURCE_DIR/lib/codepage.js"
printf '%s\n' '}).call(sabre);' >> "$PROJECT_SOURCE_DIR/lib/codepage.js"
rm -f "$TEMP_DIR/codepage.js"
printf '%s\n' "Finished building codepage dependency." | tee -a $LOG_FILE
popd
1,560 changes: 1,560 additions & 0 deletions src/lib/codepage.js

Large diffs are not rendered by default.

79 changes: 39 additions & 40 deletions src/subtitle-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,16 @@ const parser_prototype = global.Object.create(global.Object, {
);
} else if (config["info"]["version"] > 4)
console.warn(
"Warning: Some subtitle features may not be supported"
"Warning: Some subtitle features may not be supported."
);
config["info"]["is_ass"] = !!version[2];
config["info"]["ass_version"] = version[2].length;
console.info(
"Advanced Sub Station Alpha: " +
config["info"]["is_ass"] + "\tVersion:" + config["info"]["ass_version"]
);
if(config["info"]["is_ass"]){
config["info"]["ass_version"] = version[2].length;
console.info(
"Advanced Sub Station Alpha: " +
config["info"]["is_ass"] + "\tVersion:" + config["info"]["ass_version"]
);
}
return;
}
case "Collisions": {
Expand Down Expand Up @@ -926,52 +928,49 @@ const parser_prototype = global.Object.create(global.Object, {
const second = text.indexOf("}");
if(first !== -1 && second !== -1 && first < second){
file.rewind();
text = file.next(encoding,["}"]).replace(/\\h/g, "\u00A0");
override = true;
text = file.next(encoding,["}"]).replace(/\\h/g, "\u00A0")+"}";
}
event.setText(text);
}

match = /^([^{}]*?)\\([nN])(.*)$/.exec(text);
if (match !== null) {
let new_event = sabre.cloneEventWithoutText(event);
text = match[1];
event.setText(match[1]);
new_event.setText(match[3]);
new_event.setNewLine(match[2] === "N");
events.splice(i + 1, 0, new_event);
}
if(override){
match = /^([^{}]*?)\{(.*?)$/.exec(text); //\}(.*?)$
if (match !== null) {
let new_event = sabre.cloneEventWithoutText(event);
event.setText(match[1]);
//new_event.setText(match[3]);
const _this = this;
new_event.setOverrides(
this._parseOverrides(
{
start: event.getStart(),
end: event.getEnd()
},
function (style_name) {
return _this._styles[style_name];
},
function (new_style) {
new_event.setStyle(new_style);
},
event.getOverrides(),
event.getLineOverrides(),
function (lineTransitionTargetOverrides) {
event.addLineTransitionTargetOverrides(
lineTransitionTargetOverrides
);
},
match[2],
this._config["info"]["is_ass"]
)
);
events.splice(i + 1, 0, new_event);
}
match = /^([^{}]*?)\{(.*?)\}$/.exec(text); //\}(.*?)$
if (match !== null) {
let new_event = sabre.cloneEventWithoutText(event);
event.setText(match[1]);
const _this = this;
new_event.setOverrides(
this._parseOverrides(
{
start: event.getStart(),
end: event.getEnd()
},
function (style_name) {
return _this._styles[style_name];
},
function (new_style) {
new_event.setStyle(new_style);
},
event.getOverrides(),
event.getLineOverrides(),
function (lineTransitionTargetOverrides) {
event.addLineTransitionTargetOverrides(
lineTransitionTargetOverrides
);
},
match[2],
this._config["info"]["is_ass"]
)
);
events.splice(i + 1, 0, new_event);
}
}
return events;
Expand Down
7 changes: 4 additions & 3 deletions src/text-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
//@include [global-constants]
//@include [util]
//@include [lib/codepage]
//@include [color]
//@include [style]
//@include [style-override]
Expand Down Expand Up @@ -82,7 +83,7 @@ const text_server_prototype = Object.create(Object, {
passthrough = true;
}
const delimiter_buffers = delimiters.map((delimiter) => {
return global.cptable.utils.encode(codepage,delimiter);
return sabre.cptable.utils.encode(codepage,delimiter);
});

for(let i = this._position; i < this._buffer.length; i++) {
Expand All @@ -99,7 +100,7 @@ const text_server_prototype = Object.create(Object, {
let subarr = this._buffer.subarray(this._position, i);
let result;
if(!passthrough){
result = global.cptable.utils.decode(codepage, subarr);
result = sabre.cptable.utils.decode(codepage, subarr);
} else {
result = global.String.fromCharCode.apply(String, subarr);
}
Expand All @@ -111,7 +112,7 @@ const text_server_prototype = Object.create(Object, {
}
this._lastPosition = this._position;
this._position = this._buffer.length;
return global.cptable.utils.decode(codepage, this._buffer.subarray(this._lastPosition));
return sabre.cptable.utils.decode(codepage, this._buffer.subarray(this._lastPosition));
},
writable: false
},
Expand Down
1 change: 1 addition & 0 deletions tdata/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*
*/
!codepage.csv
!.gitignore
17 changes: 17 additions & 0 deletions tdata/codepage.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
1252
10000
932
949
1361
936
950
1253
1254
1258
1255
1256
1257
1251
874
1250
850

0 comments on commit a820ee4

Please sign in to comment.