Skip to content

Commit f636ff8

Browse files
authored
Merge pull request finale-lua#665 from asherber/generate-rtf-notes
Generate RTFNotes from Notes during build process
2 parents f92466c + e934506 commit f636ff8

15 files changed

+678
-62
lines changed

.github/actions/bundle/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
enable-pre-post-scripts=true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
{\rtf1\ansi\deff0{\fonttbl{\f0 \fswiss Helvetica;}{\f1 \fmodern Courier New;}}
3+
{\colortbl;\red255\green0\blue0;\red0\green0\blue255;}
4+
\widowctrl\hyphauto
5+
\f0\fs20
6+
\f1\fs20
7+
$body$
8+
}
9+

.github/actions/bundle/dist/index.js

Lines changed: 125 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,46 @@ function isLoopbackAddress(host) {
17811781

17821782
/***/ }),
17831783

1784+
/***/ 3166:
1785+
/***/ ((module) => {
1786+
1787+
module.exports = function dedent(templateStrings) {
1788+
var values = [];
1789+
for (var _i = 1; _i < arguments.length; _i++) {
1790+
values[_i - 1] = arguments[_i];
1791+
}
1792+
var matches = [];
1793+
var strings = typeof templateStrings === 'string' ? [templateStrings] : templateStrings.slice();
1794+
// 1. Remove trailing whitespace.
1795+
strings[strings.length - 1] = strings[strings.length - 1].replace(/\r?\n([\t ]*)$/, '');
1796+
// 2. Find all line breaks to determine the highest common indentation level.
1797+
for (var i = 0; i < strings.length; i++) {
1798+
var match = void 0;
1799+
if (match = strings[i].match(/\n[\t ]+/g)) {
1800+
matches.push.apply(matches, match);
1801+
}
1802+
}
1803+
// 3. Remove the common indentation from all strings.
1804+
if (matches.length) {
1805+
var size = Math.min.apply(Math, matches.map(function (value) { return value.length - 1; }));
1806+
var pattern = new RegExp("\n[\t ]{" + size + "}", 'g');
1807+
for (var i = 0; i < strings.length; i++) {
1808+
strings[i] = strings[i].replace(pattern, '\n');
1809+
}
1810+
}
1811+
// 4. Remove leading whitespace.
1812+
strings[0] = strings[0].replace(/^\r?\n/, '');
1813+
// 5. Perform interpolation.
1814+
var string = strings[0];
1815+
for (var i = 0; i < values.length; i++) {
1816+
string += values[i] + strings[i + 1];
1817+
}
1818+
return string;
1819+
};
1820+
//# sourceMappingURL=index.js.map
1821+
1822+
/***/ }),
1823+
17841824
/***/ 9479:
17851825
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
17861826

@@ -6781,7 +6821,11 @@ const bundleFileBase = (name, importedFiles, mixins, fetcher) => {
67816821
};
67826822
exports.bundleFileBase = bundleFileBase;
67836823
const bundleFile = (name, sourcePath, mixins) => {
6784-
return (0, remove_comments_1.removeComments)((0, exports.bundleFileBase)(name, exports.files, mixins, (fileName) => fs_1.default.readFileSync(path_1.default.join(sourcePath, fileName)).toString()));
6824+
const bundled = (0, exports.bundleFileBase)(name, exports.files, mixins, (fileName) => fs_1.default.readFileSync(path_1.default.join(sourcePath, fileName)).toString());
6825+
const parts = (0, helpers_1.getFileParts)(bundled);
6826+
return (0, remove_comments_1.removeComments)(parts.prolog, true)
6827+
+ (0, remove_comments_1.removeComments)(parts.plugindef, false)
6828+
+ (0, remove_comments_1.removeComments)(parts.epilog, true);
67856829
};
67866830
exports.bundleFile = bundleFile;
67876831

@@ -6794,7 +6838,7 @@ exports.bundleFile = bundleFile;
67946838
"use strict";
67956839

67966840
Object.defineProperty(exports, "__esModule", ({ value: true }));
6797-
exports.getAllImports = exports.getImport = exports.requireRegex = void 0;
6841+
exports.getFileParts = exports.getAllImports = exports.getImport = exports.requireRegex = void 0;
67986842
exports.requireRegex = new RegExp([
67996843
/(?:^|[\W])/,
68006844
/(?:__original_)?require/,
@@ -6833,6 +6877,19 @@ const getAllImports = (file) => {
68336877
return [...imports];
68346878
};
68356879
exports.getAllImports = getAllImports;
6880+
const plugindefRegex = /(?<plugindef>^function\s+plugindef.*?^end)/ms;
6881+
const getFileParts = (contents) => {
6882+
const plugindefMatch = contents.match(plugindefRegex);
6883+
if ((plugindefMatch === null || plugindefMatch === void 0 ? void 0 : plugindefMatch.index) !== undefined) {
6884+
const prolog = contents.slice(0, plugindefMatch.index);
6885+
const epilog = contents.slice(plugindefMatch.index + plugindefMatch[0].length);
6886+
return { prolog, plugindef: plugindefMatch[0], epilog };
6887+
}
6888+
else {
6889+
return { prolog: '', plugindef: '', epilog: contents };
6890+
}
6891+
};
6892+
exports.getFileParts = getFileParts;
68366893

68376894

68386895
/***/ }),
@@ -6885,30 +6942,64 @@ sourceFiles.forEach(file => {
68856942
/***/ }),
68866943

68876944
/***/ 9879:
6888-
/***/ ((__unused_webpack_module, exports) => {
6945+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
68896946

68906947
"use strict";
68916948

6949+
var __importDefault = (this && this.__importDefault) || function (mod) {
6950+
return (mod && mod.__esModule) ? mod : { "default": mod };
6951+
};
68926952
Object.defineProperty(exports, "__esModule", ({ value: true }));
68936953
exports.injectExtras = void 0;
6954+
const helpers_1 = __nccwpck_require__(8092);
6955+
const child_process_1 = __nccwpck_require__(2081);
6956+
const dedent_js_1 = __importDefault(__nccwpck_require__(3166));
6957+
const path_1 = __importDefault(__nccwpck_require__(1017));
68946958
const injectExtras = (name, contents) => {
6895-
const functionRegex = /^(\s*)function\s+plugindef/gm;
6896-
const returnRegex = /^(\s*)return/gm;
6897-
const endRegex = /^(\s*)*end/gm;
6898-
const functionMatch = functionRegex.exec(contents);
6899-
if (functionMatch) {
6900-
const functionIndex = functionMatch.index;
6901-
const returnMatch = returnRegex.exec(contents.substring(functionIndex));
6902-
const endMatch = endRegex.exec(contents.substring(functionIndex));
6903-
const index = Math.min(returnMatch ? returnMatch.index : Infinity, endMatch ? endMatch.index : Infinity);
6904-
const strippedName = name.split('.').slice(0, -1).join('');
6905-
const injection = ` finaleplugin.HashURL = \"https://raw.githubusercontent.com/finale-lua/lua-scripts/master/hash/${strippedName}.hash\"`;
6906-
const injectedContents = contents.slice(0, functionIndex + index) + injection + '\n' + contents.slice(functionIndex + index);
6907-
return injectedContents;
6959+
const parts = (0, helpers_1.getFileParts)(contents);
6960+
if (parts.plugindef) {
6961+
if (!parts.plugindef.includes('finaleplugin.RTFNotes'))
6962+
parts.plugindef = inject(parts.plugindef, getRTFNotes(parts.plugindef));
6963+
parts.plugindef = inject(parts.plugindef, getHashURL(name));
69086964
}
6909-
return contents;
6965+
return parts.prolog + parts.plugindef + parts.epilog;
69106966
};
69116967
exports.injectExtras = injectExtras;
6968+
const inject = (contents, injection) => {
6969+
if (injection) {
6970+
const returnRegex = /^(\s*)return/gm;
6971+
const endRegex = /^(\s*)*end/gm;
6972+
const returnMatch = returnRegex.exec(contents);
6973+
const endMatch = endRegex.exec(contents);
6974+
const index = Math.min(returnMatch ? returnMatch.index : Infinity, endMatch ? endMatch.index : Infinity);
6975+
return contents.slice(0, index) + injection + '\n' + contents.slice(index);
6976+
}
6977+
else {
6978+
return contents;
6979+
}
6980+
};
6981+
const getHashURL = (name) => {
6982+
const strippedName = name.split('.').slice(0, -1).join('');
6983+
return ` finaleplugin.HashURL = "https://raw.githubusercontent.com/finale-lua/lua-scripts/master/hash/${strippedName}.hash"`;
6984+
};
6985+
const getRTFNotes = (input) => {
6986+
let result = '';
6987+
const notesRegex = /(?<=finaleplugin.Notes = \[\[).*(?=\]\])/ius;
6988+
const match = input.match(notesRegex);
6989+
if (match) {
6990+
const notes = (0, dedent_js_1.default)(match[0]);
6991+
const templateFile = path_1.default.join(__dirname, "custom_template.rtf");
6992+
const args = ['-f', 'markdown', '-t', 'rtf', '-s', `--template=${templateFile}`];
6993+
const pandocResult = (0, child_process_1.spawnSync)('pandoc', args, { input: notes, encoding: 'utf-8' });
6994+
if (!pandocResult.error) {
6995+
result = pandocResult.stdout.replace(/fs28/g, 'fs24')
6996+
.replace(/fs32/g, 'fs28')
6997+
.replace(/fs36/g, 'fs32');
6998+
result = ` finaleplugin.RTFNotes = [[${result}]]`;
6999+
}
7000+
}
7001+
return result;
7002+
};
69127003

69137004

69147005
/***/ }),
@@ -6942,12 +7033,16 @@ exports.resolveRequiredFile = resolveRequiredFile;
69427033

69437034
Object.defineProperty(exports, "__esModule", ({ value: true }));
69447035
exports.removeComments = void 0;
6945-
const removeComments = (contents) => {
6946-
return contents
7036+
const removeComments = (contents, trimWhitespace) => {
7037+
let result = contents
69477038
.replace(/--\[\[[\s\S]*?\]\]/giu, '')
6948-
.replace(/(?<!")--.*$/gimu, '')
6949-
.replace(/\n\n+/gimu, '\n')
6950-
.replace(/ *$/gimu, '');
7039+
.replace(/(?<!")--.*$/gimu, '');
7040+
if (trimWhitespace) {
7041+
result = result
7042+
.replace(/\n\n+/gimu, '\n')
7043+
.replace(/ *$/gimu, '');
7044+
}
7045+
return result;
69517046
};
69527047
exports.removeComments = removeComments;
69537048

@@ -6985,6 +7080,14 @@ module.exports = require("assert");
69857080

69867081
/***/ }),
69877082

7083+
/***/ 2081:
7084+
/***/ ((module) => {
7085+
7086+
"use strict";
7087+
module.exports = require("child_process");
7088+
7089+
/***/ }),
7090+
69887091
/***/ 2057:
69897092
/***/ ((module) => {
69907093

.github/actions/bundle/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"test": "jest",
1111
"test:watch": "npm run test -- --watch",
1212
"build": "ncc build src/index.ts -o dist --target=es2015",
13-
"dev": "npm run build && NODE_ENV=development node dist/index.js"
13+
"dev": "npm run build && NODE_ENV=development node dist/index.js",
14+
"postbuild": "copyfiles -u 1 src/custom_template.rtf dist"
1415
},
1516
"jest": {
1617
"collectCoverage": true,
@@ -33,6 +34,7 @@
3334
"@types/n-readlines": "^1.0.2",
3435
"@types/node": "^16.4.3",
3536
"@vercel/ncc": "^0.34.0",
37+
"copyfiles": "^2.4.1",
3638
"esbuild": "^0.14.49",
3739
"esbuild-jest": "^0.5.0",
3840
"eslint": "^7.31.0",
@@ -42,6 +44,7 @@
4244
},
4345
"dependencies": {
4446
"@actions/core": "^1.4.0",
47+
"dedent-js": "^1.0.1",
4548
"fs-extra": "^10.0.0",
4649
"luabundle": "^1.6.0",
4750
"luamin": "^1.0.4",

0 commit comments

Comments
 (0)