Skip to content

Commit

Permalink
parser: Allow importing URLs in Duplicate URL format
Browse files Browse the repository at this point in the history
  • Loading branch information
x-sheep authored Sep 8, 2024
1 parent 8dfaafe commit c3523d0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/puzzle/FileData.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@

bd.initBoardSize(pzl.cols, pzl.rows);

if (pzl.variant !== null) {
puzzle.setConfig("variant", true);
puzzle.setConfig("variantid", pzl.variant);
}

this.filever = pzl.filever;
if (filetype !== pzl.FILE_PBOX_XML) {
this.lineseek = 0;
Expand Down
44 changes: 33 additions & 11 deletions src/pzpr/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,40 @@
}
// ぱずぷれv3の場合
else {
if (url.startsWith("v:/")) {
url = url.substring(3);
this.variant = true;
}
var qs = url.indexOf("/", url.indexOf("?"));
var start = url.indexOf("?");
var qs = url.indexOf("/", start);
var first = "";
if (qs > -1) {
first = url.substring(url.indexOf("?") + 1, qs);
first = url.substring(start + 1, qs);

while (first.match(/^(\w+)\=(\w+)\&/)) {
if (RegExp.$1 === "type") {
this.mode = RegExp.$2;
}

start = url.indexOf("&", start + 1);
first = url.substring(start + 1, qs);
}
} else {
first = url.substr(url.indexOf("?") + 1);
first = url.substring(start + 1);
}

if (first.startsWith("v:")) {
var newstart = start + first.length + 2;
var next = url.indexOf("/", newstart);
first = url.substring(newstart, next);
}

if (first.match(/^pzprv[0-9.]*$/)) {
if (start > -1 && start < qs) {
// Remove other querystring parameters
url = url.substring(start + 1);
}

// encoded file; extract pid
url = decodeURIComponent(url);
var parts = url.split("/");
if (parts.length >= 2) {
this.pid = parts[1];
}
var parts = url.split("/", 4);
this.pid = parts[0].startsWith("v:") ? parts[2] : parts[1];
this.qdata = url;
this.type = URL_PZPRFILE;
} else {
Expand Down Expand Up @@ -437,6 +453,7 @@
pzpr.parser.FileData.prototype = {
pid: "",
type: FILE_AUTO /* == 0 */,
variant: null,
filever: 0,
fstr: "",
qdata: "",
Expand Down Expand Up @@ -477,6 +494,11 @@
delete this.fstr;

/* ヘッダからパズルの種類・ファイルの種類を判定する */
if (firstline.match(/^v:(.*)/)) {
this.variant = RegExp.$1;
firstline = lines.shift();
}

if (firstline.match(/^pzprv3/)) {
this.type = FILE_PZPR;
if (firstline.match(/pzprv3\.(\d+)/)) {
Expand Down
1 change: 1 addition & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"ecmaVersion": 6
},
"globals": {
"beforeEach": "readonly",
"describe": "readonly",
"it": "readonly"
},
Expand Down
51 changes: 50 additions & 1 deletion test/pzpr/parser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,56 @@ var assert = require("assert");

var pzpr = require("../../");

var puzzle = new pzpr.Puzzle();
var puzzle;
beforeEach(function() {
puzzle = new pzpr.Puzzle();
});

describe("URLData", function() {
it("parses files", function() {
var url = "pzprv3/nurikabe/3/3/# # . /1 # 3 /# # . /";
var pzl = pzpr.parser.parseURL(url);

assert.equal("nurikabe", pzl.pid);

puzzle.open(pzl);
assert.equal("nurikabe", puzzle.pid);

assert.equal(puzzle.board.getc(3, 3).qans, 1);
});

it("parses variant files", function() {
var url = "v:/pzprv3/nurikabe/3/3/# # . /1 # 3 /# # . /";
var pzl = pzpr.parser(url);
assert.equal("nurikabe", pzl.pid);
puzzle.open(pzl);
assert.equal("nurikabe", puzzle.pid);
assert.equal(true, puzzle.getConfig("variant"));
});

it("parses variant urls", function() {
var url =
"https://puzz.link/p?type=editor&v:/pzprv3/nurikabe/3/3/%23%20%23%20.%20/1%20%23%203%20/%23%20%23%20.%20//";
var pzl = pzpr.parser(url);
assert.equal("nurikabe", pzl.pid);
puzzle.open(pzl);
assert.equal("nurikabe", puzzle.pid);
assert.equal(true, puzzle.getConfig("variant"));
});

it("parses duplicated urls", function() {
var url =
"https://puzz.link/p?type=editor&pzprv3/nurikabe/3/3/%23%20%23%20.%20/1%20%23%203%20/%23%20%23%20.%20//";
var pzl = pzpr.parser(url);

assert.equal("nurikabe", pzl.pid);

puzzle.open(pzl);
assert.equal("nurikabe", puzzle.pid);

assert.equal(puzzle.board.getc(3, 3).qans, 1);
});
});

describe("changeProperPid:URL", function() {
it("Check bonsan", function() {
Expand Down

0 comments on commit c3523d0

Please sign in to comment.