@thi.ng/transclude 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Change Log
2
+
3
+ - **Last updated**: 2022-12-16T12:52:26Z
4
+ - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
+
6
+ All notable changes to this project will be documented in this file.
7
+ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelines.
8
+
9
+ **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
+ and/or version bumps of transitive dependencies.
11
+
12
+ ## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/transclude@0.1.0) (2022-12-16)
13
+
14
+ #### 🚀 Features
15
+
16
+ - import as new pkg ([dc05320](https://github.com/thi-ng/umbrella/commit/dc05320))
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,100 @@
1
+ <!-- This file is generated - DO NOT EDIT! -->
2
+
3
+ # ![@thi.ng/transclude](https://media.thi.ng/umbrella/banners-20220914/thing-transclude.svg?f63d93c4)
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@thi.ng/transclude.svg)](https://www.npmjs.com/package/@thi.ng/transclude)
6
+ ![npm downloads](https://img.shields.io/npm/dm/@thi.ng/transclude.svg)
7
+ [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
+
9
+ This project is part of the
10
+ [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo.
11
+
12
+ - [About](#about)
13
+ - [Status](#status)
14
+ - [Related packages](#related-packages)
15
+ - [Installation](#installation)
16
+ - [Dependencies](#dependencies)
17
+ - [API](#api)
18
+ - [Authors](#authors)
19
+ - [License](#license)
20
+
21
+ ## About
22
+
23
+ Extensible functional template engine for text document generation, incl. various high-level Markdown features
24
+
25
+ This package evolved from this monorepo's readme generator toolchain and
26
+ currently is mainly used to generate all ~350 readme & changelog files in this
27
+ repo, but otherwise is **not** bound to this project in any way.
28
+
29
+ Please see
30
+ [`/tools/src/readme.ts`](https://github.com/thi-ng/umbrella/blob/develop/tools/src/readme.ts)
31
+ for a concrete usage example...
32
+
33
+ ## Status
34
+
35
+ **ALPHA** - bleeding edge / work-in-progress
36
+
37
+ [Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Btransclude%5D+in%3Atitle)
38
+
39
+ ## Related packages
40
+
41
+ - [@thi.ng/hiccup-markdown](https://github.com/thi-ng/umbrella/tree/develop/packages/hiccup-markdown) - Markdown parser & serializer from/to Hiccup format
42
+ - [@thi.ng/markdown-table](https://github.com/thi-ng/umbrella/tree/develop/packages/markdown-table) - Markdown table formatter/generator with support for column alignments
43
+ - [@thi.ng/tangle](https://github.com/thi-ng/umbrella/tree/develop/packages/tangle) - Literate programming code block tangling / codegen utility, inspired by org-mode & noweb
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ yarn add @thi.ng/transclude
49
+ ```
50
+
51
+ ES module import:
52
+
53
+ ```html
54
+ <script type="module" src="https://cdn.skypack.dev/@thi.ng/transclude"></script>
55
+ ```
56
+
57
+ [Skypack documentation](https://docs.skypack.dev/)
58
+
59
+ For Node.js REPL:
60
+
61
+ ```js
62
+ const transclude = await import("@thi.ng/transclude");
63
+ ```
64
+
65
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.86 KB
66
+
67
+ ## Dependencies
68
+
69
+ - [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
70
+ - [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
71
+ - [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
72
+ - [@thi.ng/file-io](https://github.com/thi-ng/umbrella/tree/develop/packages/file-io)
73
+ - [@thi.ng/logger](https://github.com/thi-ng/umbrella/tree/develop/packages/logger)
74
+ - [@thi.ng/strings](https://github.com/thi-ng/umbrella/tree/develop/packages/strings)
75
+ - [@thi.ng/transducers](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers)
76
+
77
+ ## API
78
+
79
+ [Generated API docs](https://docs.thi.ng/umbrella/transclude/)
80
+
81
+ TODO
82
+
83
+ ## Authors
84
+
85
+ - [Karsten Schmidt](https://thi.ng)
86
+
87
+ If this project contributes to an academic publication, please cite it as:
88
+
89
+ ```bibtex
90
+ @misc{thing-transclude,
91
+ title = "@thi.ng/transclude",
92
+ author = "Karsten Schmidt",
93
+ note = "https://thi.ng/transclude",
94
+ year = 2022
95
+ }
96
+ ```
97
+
98
+ ## License
99
+
100
+ &copy; 2022 Karsten Schmidt // Apache License 2.0
package/api.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type { Fn3, IObjectOf } from "@thi.ng/api";
2
+ import type { ILogger } from "@thi.ng/logger";
3
+ /**
4
+ * Template functions receive the current {@link TranscludeCtx} and array of
5
+ * (raw) template arguments and the path of the current file.
6
+ */
7
+ export type TemplateFn<T = any> = Fn3<TranscludeCtx<T>, string[], string, string>;
8
+ export interface TranscludeCtx<T = any> {
9
+ /**
10
+ * User provided data of type `T`
11
+ */
12
+ user: T;
13
+ logger: ILogger;
14
+ /**
15
+ * Object of all known templates in this context.
16
+ */
17
+ templates: IObjectOf<TemplateFn<T> | string>;
18
+ /**
19
+ * Global pre-processing functions, in order of application. Receives entire
20
+ * file contents as first and only template arg, MUST return entire updated
21
+ * file contents.
22
+ */
23
+ pre: TemplateFn<T>[];
24
+ /**
25
+ * Global post-processing functions, in order of application. Receives
26
+ * entire file contents as first and only template arg, MUST return entire
27
+ * updated file contents.
28
+ */
29
+ post: TemplateFn<T>[];
30
+ /**
31
+ * Current contents to be processed.
32
+ */
33
+ src: string;
34
+ /**
35
+ * Template tag match regexp.
36
+ */
37
+ match: RegExp;
38
+ /**
39
+ * Line ending to be used for templates generating lists or other multi-line
40
+ * content.
41
+ *
42
+ * @defaultValue "\n"
43
+ */
44
+ eol: string;
45
+ }
46
+ //# sourceMappingURL=api.d.ts.map
package/api.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./api.js";
2
+ export * from "./transclude.js";
3
+ export * from "./tpl/file.js";
4
+ export * from "./tpl/markdown.js";
5
+ export * from "./tpl/pkg.js";
6
+ export * from "./tpl/whitespace.js";
7
+ //# sourceMappingURL=index.d.ts.map
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./api.js";
2
+ export * from "./transclude.js";
3
+ export * from "./tpl/file.js";
4
+ export * from "./tpl/markdown.js";
5
+ export * from "./tpl/pkg.js";
6
+ export * from "./tpl/whitespace.js";
package/package.json ADDED
@@ -0,0 +1,112 @@
1
+ {
2
+ "name": "@thi.ng/transclude",
3
+ "version": "0.1.0",
4
+ "description": "Extensible functional template engine for text document generation, incl. various high-level Markdown features",
5
+ "type": "module",
6
+ "module": "./index.js",
7
+ "typings": "./index.d.ts",
8
+ "sideEffects": false,
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/thi-ng/umbrella.git"
12
+ },
13
+ "homepage": "https://github.com/thi-ng/umbrella/tree/develop/packages/transclude#readme",
14
+ "funding": [
15
+ {
16
+ "type": "github",
17
+ "url": "https://github.com/sponsors/postspectacular"
18
+ },
19
+ {
20
+ "type": "patreon",
21
+ "url": "https://patreon.com/thing_umbrella"
22
+ }
23
+ ],
24
+ "author": "Karsten Schmidt (https://thi.ng)",
25
+ "license": "Apache-2.0",
26
+ "scripts": {
27
+ "build": "yarn clean && tsc --declaration",
28
+ "clean": "rimraf '*.js' '*.d.ts' '*.map' doc tpl",
29
+ "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
+ "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
31
+ "doc:readme": "yarn doc:stats && tools:readme",
32
+ "doc:stats": "tools:module-stats",
33
+ "pub": "yarn npm publish --access public",
34
+ "test": "testament test"
35
+ },
36
+ "dependencies": {
37
+ "@thi.ng/api": "^8.6.0",
38
+ "@thi.ng/checks": "^3.3.5",
39
+ "@thi.ng/errors": "^2.2.6",
40
+ "@thi.ng/file-io": "^0.3.22",
41
+ "@thi.ng/logger": "^1.4.5",
42
+ "@thi.ng/strings": "^3.3.20",
43
+ "@thi.ng/transducers": "^8.3.26"
44
+ },
45
+ "devDependencies": {
46
+ "@microsoft/api-extractor": "^7.33.7",
47
+ "@thi.ng/testament": "^0.3.7",
48
+ "rimraf": "^3.0.2",
49
+ "tools": "^0.0.1",
50
+ "typedoc": "^0.23.22",
51
+ "typescript": "^4.9.4"
52
+ },
53
+ "keywords": [
54
+ "engine",
55
+ "functional",
56
+ "generator",
57
+ "json",
58
+ "markdown",
59
+ "npm",
60
+ "template",
61
+ "text",
62
+ "typescript"
63
+ ],
64
+ "publishConfig": {
65
+ "access": "public"
66
+ },
67
+ "browser": {
68
+ "process": false,
69
+ "setTimeout": false
70
+ },
71
+ "engines": {
72
+ "node": ">=14"
73
+ },
74
+ "files": [
75
+ "./*.js",
76
+ "./*.d.ts",
77
+ "tpl"
78
+ ],
79
+ "exports": {
80
+ ".": {
81
+ "default": "./index.js"
82
+ },
83
+ "./api": {
84
+ "default": "./api.js"
85
+ },
86
+ "./tpl/file": {
87
+ "default": "./tpl/file.js"
88
+ },
89
+ "./tpl/markdown": {
90
+ "default": "./tpl/markdown.js"
91
+ },
92
+ "./tpl/pkg": {
93
+ "default": "./tpl/pkg.js"
94
+ },
95
+ "./tpl/whitespace": {
96
+ "default": "./tpl/whitespace.js"
97
+ },
98
+ "./transclude": {
99
+ "default": "./transclude.js"
100
+ }
101
+ },
102
+ "thi.ng": {
103
+ "related": [
104
+ "hiccup-markdown",
105
+ "markdown-table",
106
+ "tangle"
107
+ ],
108
+ "status": "alpha",
109
+ "year": 2022
110
+ },
111
+ "gitHead": "f445a9cc8022bcdebbf6ff91fd66ced016d72f01\n"
112
+ }
package/tpl/file.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { TemplateFn } from "../api.js";
2
+ /**
3
+ * Parametric template function. Includes file given as template argument:
4
+ * `{{include header.md}}`
5
+ *
6
+ * @param ctx
7
+ * @param args
8
+ * @param currPath
9
+ */
10
+ export declare const includeFile: TemplateFn;
11
+ /**
12
+ * Pre-processing stage parametric template function. Includes file given as
13
+ * template argument: `<!-- include header.md -->`.
14
+ *
15
+ * @param ctx
16
+ * @param args
17
+ * @param currPath
18
+ */
19
+ export declare const preincludeFile: TemplateFn;
20
+ //# sourceMappingURL=file.d.ts.map
package/tpl/file.js ADDED
@@ -0,0 +1,20 @@
1
+ import { readText } from "@thi.ng/file-io";
2
+ import { resolve } from "path";
3
+ /**
4
+ * Parametric template function. Includes file given as template argument:
5
+ * `{{include header.md}}`
6
+ *
7
+ * @param ctx
8
+ * @param args
9
+ * @param currPath
10
+ */
11
+ export const includeFile = ({ logger }, args, currPath) => readText(resolve(resolve(currPath, ".."), args[2].trim()), logger);
12
+ /**
13
+ * Pre-processing stage parametric template function. Includes file given as
14
+ * template argument: `<!-- include header.md -->`.
15
+ *
16
+ * @param ctx
17
+ * @param args
18
+ * @param currPath
19
+ */
20
+ export const preincludeFile = ({ logger }, [src], currPath) => src.replace(new RegExp(`<!-- include (.+) -->`, "g"), (_, path) => readText(resolve(resolve(currPath, ".."), path.trim()), logger));
@@ -0,0 +1,31 @@
1
+ import type { TemplateFn } from "../api.js";
2
+ export declare const image: (url: string, alt?: string) => string;
3
+ export declare const link: (title: string, href: string) => string;
4
+ export declare const list: (items: string[], eol?: string) => string;
5
+ export interface TOCOpts {
6
+ /**
7
+ * Min headline level
8
+ *
9
+ * @defaultValue 2
10
+ */
11
+ min: number;
12
+ /**
13
+ * Max headline level
14
+ *
15
+ * @defaultValue 4
16
+ */
17
+ max: number;
18
+ /**
19
+ *
20
+ */
21
+ title: string;
22
+ }
23
+ /**
24
+ * Higher order, configurable post-processing template function. Injects a table
25
+ * of contents for the first `<!-- toc -->` occurrence. See {@link TOCOpts} for
26
+ * config options.
27
+ *
28
+ * @param opts
29
+ */
30
+ export declare const toc: (opts?: Partial<TOCOpts>) => TemplateFn;
31
+ //# sourceMappingURL=markdown.d.ts.map
@@ -0,0 +1,35 @@
1
+ import { repeat } from "@thi.ng/strings/repeat";
2
+ import { slugifyGH } from "@thi.ng/strings/slugify";
3
+ import { split } from "@thi.ng/strings/split";
4
+ import { comp } from "@thi.ng/transducers/comp";
5
+ import { filter } from "@thi.ng/transducers/filter";
6
+ import { map } from "@thi.ng/transducers/map";
7
+ import { str } from "@thi.ng/transducers/str";
8
+ import { transduce } from "@thi.ng/transducers/transduce";
9
+ export const image = (url, alt = "") => `![${alt}](${url})`;
10
+ export const link = (title, href) => `[${title}](${href})`;
11
+ export const list = (items, eol = "\n") => transduce(map((x) => `- ${x}`), str(eol), items);
12
+ /**
13
+ * Higher order, configurable post-processing template function. Injects a table
14
+ * of contents for the first `<!-- toc -->` occurrence. See {@link TOCOpts} for
15
+ * config options.
16
+ *
17
+ * @param opts
18
+ */
19
+ export const toc = (opts) => ({ src, eol }) => {
20
+ const { min, max, match, disable, title } = {
21
+ min: 2,
22
+ max: 4,
23
+ match: "<!-- toc -->",
24
+ disable: /<\!-- notoc -->/i,
25
+ ...opts,
26
+ };
27
+ const isHeading = new RegExp(`^#{${min},${max}}\\s`);
28
+ const reHeading = new RegExp(`^(#{${min},${max}})\\s(.+)`);
29
+ const toc = transduce(comp(filter((line) => isHeading.test(line) && !disable.test(line)), map((hd) => {
30
+ const [_, level, title] = reHeading.exec(hd);
31
+ const indent = repeat(" ", Math.max(0, level.length - min));
32
+ return `${indent}- ${link(title, `#${slugifyGH(title)}`)}`;
33
+ })), str(eol), split(src));
34
+ return src.replace(match, title ? title + toc : toc);
35
+ };
package/tpl/pkg.d.ts ADDED
@@ -0,0 +1,87 @@
1
+ import type { Fn, IObjectOf } from "@thi.ng/api";
2
+ import type { TemplateFn } from "../api.js";
3
+ export interface Package {
4
+ name: string;
5
+ description: string;
6
+ version: string;
7
+ author: string | User;
8
+ license: string;
9
+ homepage: string;
10
+ contributors?: (string | User)[];
11
+ dependencies?: IObjectOf<string>;
12
+ devDependencies?: IObjectOf<string>;
13
+ keywords?: string[];
14
+ }
15
+ export interface User {
16
+ name: string;
17
+ email?: string;
18
+ url?: string;
19
+ }
20
+ export declare const LICENSES: IObjectOf<{
21
+ name: string;
22
+ url: string;
23
+ }>;
24
+ /**
25
+ * Returns the unscoped name of a package, i.e. the name without the package's
26
+ * group ID (if any).
27
+ *
28
+ * @param name
29
+ */
30
+ export declare const shortName: (name: string) => string;
31
+ /**
32
+ * Returns name of the author, given in any format supported by NPM.
33
+ *
34
+ * @param author
35
+ */
36
+ export declare const author: (author: string | User) => string;
37
+ /**
38
+ * Similar to {@link author}, but if an URL is available, returns a Markdown
39
+ * link to the author's URL, else just the name.
40
+ *
41
+ * @param author
42
+ */
43
+ export declare const authorLink: (author: string | User) => string;
44
+ /**
45
+ * Formats the {@link Package.contributors} as Markdown list.
46
+ *
47
+ * @param people
48
+ * @param eol
49
+ */
50
+ export declare const contributors: (people: (string | User)[], eol?: string) => string;
51
+ /**
52
+ * Similar to {@link contributors}, but formatted as list of links.
53
+ *
54
+ * @param people
55
+ * @param eol
56
+ */
57
+ export declare const contributorLinks: (people: (string | User)[], eol?: string) => string;
58
+ /**
59
+ * Returns the name for given SPDX license ID (only those in
60
+ * {@link LICENSES} are supported).
61
+ *
62
+ * @param spdxID
63
+ */
64
+ export declare const license: (spdxID: string) => string;
65
+ /**
66
+ * Similar to {@link license}, but returns Markdown link to license.
67
+ *
68
+ * @param spdxID
69
+ */
70
+ export declare const licenseLink: (spdxID: string) => string;
71
+ export interface PackageTemplateOpts {
72
+ /**
73
+ * Headline for list of contributors (only used if any present).
74
+ */
75
+ hdContributors: string;
76
+ }
77
+ export type PackageTemplates<T> = Record<"pkg.name" | "pkg.shortName" | "pkg.version" | "pkg.description" | "pkg.link" | "pkg.author" | "pkg.authorLink" | "pkg.contributors" | "pkg.contributorLinks" | "pkg.allAuthors" | "pkg.allAuthorLinks" | "pkg.license" | "pkg.licenseLink", TemplateFn<T>>;
78
+ /**
79
+ * Higher order function returning various useful NPM package.json related
80
+ * templates. The user provided `pkg` function is used to look up the loaded
81
+ * {@link Package} data within the {@link TranscludeCtx.user} object.
82
+ *
83
+ * @param pkg
84
+ * @param opts
85
+ */
86
+ export declare const packageTemplates: <T = any>(pkg: Fn<T, Package>, opts?: Partial<PackageTemplateOpts>) => PackageTemplates<T>;
87
+ //# sourceMappingURL=pkg.d.ts.map
package/tpl/pkg.js ADDED
@@ -0,0 +1,219 @@
1
+ import { isString } from "@thi.ng/checks/is-string";
2
+ import { link, list } from "./markdown.js";
3
+ export const LICENSES = {
4
+ "Apache-2.0": {
5
+ name: "Apache License 2.0",
6
+ url: "https://spdx.org/licenses/Apache-2.0.html",
7
+ },
8
+ "BSD-2-Clause": {
9
+ name: 'BSD 2-Clause "Simplified" License',
10
+ url: "https://spdx.org/licenses/BSD-2-Clause.html",
11
+ },
12
+ "BSD-3-Clause": {
13
+ name: 'BSD 3-Clause "New" or "Revised" License',
14
+ url: "https://spdx.org/licenses/BSD-3-Clause.html",
15
+ },
16
+ "CC-BY-4.0": {
17
+ name: "Creative Commons Attribution 4.0 International",
18
+ url: "https://spdx.org/licenses/CC-BY-4.0.html",
19
+ },
20
+ "CC-BY-NC-4.0": {
21
+ name: "Creative Commons Attribution Non Commercial 4.0 International",
22
+ url: "https://spdx.org/licenses/CC-BY-NC-4.0.html",
23
+ },
24
+ "CC-BY-NC-ND-4.0": {
25
+ name: "Creative Commons Attribution Non Commercial No Derivatives 4.0 International",
26
+ url: "https://spdx.org/licenses/CC-BY-NC-ND-4.0.html",
27
+ },
28
+ "CC-BY-NC-SA-4.0": {
29
+ name: "Creative Commons Attribution Non Commercial Share Alike 4.0 International",
30
+ url: "https://spdx.org/licenses/CC-BY-NC-SA-4.0.html",
31
+ },
32
+ "CC-BY-ND-4.0": {
33
+ name: "Creative Commons Attribution No Derivatives 4.0 International",
34
+ url: "https://spdx.org/licenses/CC-BY-ND-4.0.html",
35
+ },
36
+ "CC-BY-SA-4.0": {
37
+ name: "Creative Commons Attribution Share Alike 4.0 International",
38
+ url: "https://spdx.org/licenses/CC-BY-SA-4.0.html",
39
+ },
40
+ "CPAL-1.0": {
41
+ name: "Common Public Attribution License 1.0",
42
+ url: "https://spdx.org/licenses/CPAL-1.0.html",
43
+ },
44
+ "CPL-1.0": {
45
+ name: "Common Public License 1.0",
46
+ url: "https://spdx.org/licenses/CPL-1.0.html",
47
+ },
48
+ "ECL-2.0": {
49
+ name: "Educational Community License v2.0",
50
+ url: "https://spdx.org/licenses/ECL-2.0.html",
51
+ },
52
+ "EPL-2.0": {
53
+ name: "Eclipse Public License 2.0",
54
+ url: "https://spdx.org/licenses/EPL-2.0.html",
55
+ },
56
+ EUDatagrid: {
57
+ name: "EU DataGrid Software License",
58
+ url: "https://spdx.org/licenses/EUDatagrid.html",
59
+ },
60
+ "EUPL-1.2": {
61
+ name: "European Union Public License 1.2",
62
+ url: "https://spdx.org/licenses/EUPL-1.2.html",
63
+ },
64
+ "GFDL-1.3-or-later": {
65
+ name: "GNU Free Documentation License v1.3 or later",
66
+ url: "https://spdx.org/licenses/GFDL-1.3-or-later.html",
67
+ },
68
+ "GPL-3.0-or-later": {
69
+ name: "GNU General Public License v3.0 or later",
70
+ url: "https://spdx.org/licenses/GPL-3.0-or-later.html",
71
+ },
72
+ ISC: { name: "ISC License", url: "https://spdx.org/licenses/ISC.html" },
73
+ "LGPL-3.0-or-later": {
74
+ name: "GNU Lesser General Public License v3.0 or later",
75
+ url: "https://spdx.org/licenses/LGPL-3.0-or-later.html",
76
+ },
77
+ MIT: { name: "MIT License", url: "https://spdx.org/licenses/MIT.html" },
78
+ "MPL-2.0": {
79
+ name: "Mozilla Public License 2.0",
80
+ url: "https://spdx.org/licenses/MPL-2.0.html",
81
+ },
82
+ "OFL-1.1": {
83
+ name: "SIL Open Font License 1.1",
84
+ url: "https://spdx.org/licenses/OFL-1.1.html",
85
+ },
86
+ Unlicense: {
87
+ name: "The Unlicense",
88
+ url: "https://spdx.org/licenses/Unlicense.html",
89
+ },
90
+ "UPL-1.0": {
91
+ name: "Universal Permissive License v1.0",
92
+ url: "https://spdx.org/licenses/UPL-1.0.html",
93
+ },
94
+ Zlib: { name: "zlib License", url: "https://spdx.org/licenses/Zlib.html" },
95
+ };
96
+ /**
97
+ * Returns the unscoped name of a package, i.e. the name without the package's
98
+ * group ID (if any).
99
+ *
100
+ * @param name
101
+ */
102
+ export const shortName = (name) => {
103
+ const idx = name.indexOf("/");
104
+ return idx > 0 ? name.substring(idx + 1) : name;
105
+ };
106
+ /**
107
+ * Returns name of the author, given in any format supported by NPM.
108
+ *
109
+ * @param author
110
+ */
111
+ export const author = (author) => isString(author) ? author.split(/\s*[<\(]/)[0] : author.name;
112
+ /**
113
+ * Similar to {@link author}, but if an URL is available, returns a Markdown
114
+ * link to the author's URL, else just the name.
115
+ *
116
+ * @param author
117
+ */
118
+ export const authorLink = (author) => {
119
+ if (isString(author)) {
120
+ const [name, a, b] = author.split(/\s*[<\(]/);
121
+ const href = b ? (b[b.length - 1] == ")" ? b : a) : a ? a : "";
122
+ return href.length && href[href.length - 1] === ")"
123
+ ? link(name, href.substring(0, href.length - 1))
124
+ : name;
125
+ }
126
+ return author.url ? link(author.name, author.url) : author.name;
127
+ };
128
+ /**
129
+ * Formats the {@link Package.contributors} as Markdown list.
130
+ *
131
+ * @param people
132
+ * @param eol
133
+ */
134
+ export const contributors = (people, eol = "\n") => list(people.map(author), eol);
135
+ /**
136
+ * Similar to {@link contributors}, but formatted as list of links.
137
+ *
138
+ * @param people
139
+ * @param eol
140
+ */
141
+ export const contributorLinks = (people, eol = "\n") => list(people.map(authorLink), eol);
142
+ /**
143
+ * Returns the name for given SPDX license ID (only those in
144
+ * {@link LICENSES} are supported).
145
+ *
146
+ * @param spdxID
147
+ */
148
+ export const license = (spdxID) => LICENSES[spdxID].name || `${spdxID} license`;
149
+ /**
150
+ * Similar to {@link license}, but returns Markdown link to license.
151
+ *
152
+ * @param spdxID
153
+ */
154
+ export const licenseLink = (spdxID) => {
155
+ const license = LICENSES[spdxID];
156
+ return license ? link(license.name, license.url) : `${spdxID} license`;
157
+ };
158
+ /**
159
+ * Higher order function returning various useful NPM package.json related
160
+ * templates. The user provided `pkg` function is used to look up the loaded
161
+ * {@link Package} data within the {@link TranscludeCtx.user} object.
162
+ *
163
+ * @param pkg
164
+ * @param opts
165
+ */
166
+ export const packageTemplates = (pkg, opts) => {
167
+ const $opts = {
168
+ hdContributors: "### Contributors\n\n",
169
+ ...opts,
170
+ };
171
+ const tpls = {
172
+ "pkg.name": ({ user }) => pkg(user).name,
173
+ "pkg.shortName": ({ user }) => shortName(pkg(user).name),
174
+ "pkg.version": ({ user }) => "v" + pkg(user).version,
175
+ "pkg.description": ({ user }) => pkg(user).description,
176
+ "pkg.link": ({ user }) => link(pkg(user).name, pkg(user).homepage),
177
+ "pkg.author": ({ user }) => author(pkg(user).author),
178
+ "pkg.authorLink": ({ user }) => authorLink(pkg(user).author),
179
+ "pkg.contributors": ({ user, eol }) => {
180
+ const people = pkg(user).contributors;
181
+ return people
182
+ ? $opts.hdContributors + contributors(people, eol)
183
+ : "";
184
+ },
185
+ "pkg.contributorLinks": ({ user, eol }) => {
186
+ const people = pkg(user).contributors;
187
+ return people
188
+ ? $opts.hdContributors + contributorLinks(people, eol)
189
+ : "";
190
+ },
191
+ "pkg.allAuthors": ({ user, eol }) => {
192
+ const $pkg = pkg(user);
193
+ const $author = author($pkg.author);
194
+ const res = [];
195
+ if ($pkg.contributors) {
196
+ res.push($author + " (Main author)", ...$pkg.contributors.map(author));
197
+ }
198
+ else {
199
+ res.push($author);
200
+ }
201
+ return list(res, eol);
202
+ },
203
+ "pkg.allAuthorLinks": ({ user, eol }) => {
204
+ const $pkg = pkg(user);
205
+ const $author = authorLink($pkg.author);
206
+ const res = [];
207
+ if ($pkg.contributors) {
208
+ res.push($author + " (Main author)", ...$pkg.contributors.map(authorLink));
209
+ }
210
+ else {
211
+ res.push($author);
212
+ }
213
+ return list(res, eol);
214
+ },
215
+ "pkg.license": ({ user }) => license(pkg(user).license),
216
+ "pkg.licenseLink": ({ user }) => licenseLink(pkg(user).license),
217
+ };
218
+ return tpls;
219
+ };
@@ -0,0 +1,16 @@
1
+ import type { TemplateFn } from "../api";
2
+ /**
3
+ * Post-processing stage template function. Replaces all leading tabs (per line
4
+ * indentation) with `size` spaces.
5
+ *
6
+ * @param size
7
+ */
8
+ export declare const tabsToSpaces: (size?: number) => TemplateFn;
9
+ /**
10
+ * Post-processing stage template function. Compacts successive empty lines to a
11
+ * max. of 1 empty line.
12
+ *
13
+ * @param ctx
14
+ */
15
+ export declare const compactEmptyLines: TemplateFn;
16
+ //# sourceMappingURL=whitespace.d.ts.map
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Post-processing stage template function. Replaces all leading tabs (per line
3
+ * indentation) with `size` spaces.
4
+ *
5
+ * @param size
6
+ */
7
+ export const tabsToSpaces = (size = 4) => {
8
+ const indent = " ".repeat(size);
9
+ return ({ src }) => src.replace(/^\t+/gm, (x) => indent.repeat(x.length));
10
+ };
11
+ /**
12
+ * Post-processing stage template function. Compacts successive empty lines to a
13
+ * max. of 1 empty line.
14
+ *
15
+ * @param ctx
16
+ */
17
+ export const compactEmptyLines = ({ src, eol }) => src.replace(/(\r?\n){2,}/g, eol.repeat(2)).trim() + eol;
@@ -0,0 +1,25 @@
1
+ import type { SomeRequired } from "@thi.ng/api";
2
+ import type { TranscludeCtx } from "./api.js";
3
+ type Mandatory = "src" | "templates" | "user";
4
+ export declare const transclude: <T>(ctx: SomeRequired<TranscludeCtx<T>, Mandatory>, path: string) => {
5
+ match: RegExp;
6
+ pre: import("./api.js").TemplateFn<T>[];
7
+ logger: import("@thi.ng/logger").ILogger;
8
+ post: import("./api.js").TemplateFn<T>[];
9
+ eol: string;
10
+ user: T;
11
+ src: string;
12
+ templates: import("@thi.ng/api").IObjectOf<string | import("./api.js").TemplateFn<T>>;
13
+ };
14
+ export declare const transcludeFile: <T>(path: string, ctx: SomeRequired<TranscludeCtx<T>, "user" | "templates">) => {
15
+ match: RegExp;
16
+ pre: import("./api.js").TemplateFn<T>[];
17
+ logger: import("@thi.ng/logger").ILogger;
18
+ post: import("./api.js").TemplateFn<T>[];
19
+ eol: string;
20
+ user: T;
21
+ src: string;
22
+ templates: import("@thi.ng/api").IObjectOf<string | import("./api.js").TemplateFn<T>>;
23
+ };
24
+ export {};
25
+ //# sourceMappingURL=transclude.d.ts.map
package/transclude.js ADDED
@@ -0,0 +1,38 @@
1
+ import { readText } from "@thi.ng/file-io/text";
2
+ import { ConsoleLogger } from "@thi.ng/logger";
3
+ import { resolve } from "path";
4
+ const DEFAULT_LOGGER = new ConsoleLogger("transclude");
5
+ export const transclude = (ctx, path) => {
6
+ const $ctx = {
7
+ logger: DEFAULT_LOGGER,
8
+ match: /\{\{([a-z0-9.]+)([^}]*)\}\}/gi,
9
+ pre: [],
10
+ post: [],
11
+ eol: "\n",
12
+ ...ctx,
13
+ };
14
+ $ctx.pre.reduce((acc, fn) => ($ctx.src = fn($ctx, [acc], path)), $ctx.src);
15
+ $ctx.src = $ctx.src.replace($ctx.match, (...xs) => {
16
+ const [orig, id] = xs;
17
+ const tpl = $ctx.templates[id];
18
+ if (tpl !== undefined) {
19
+ return typeof tpl === "function" ? tpl($ctx, xs, path) : tpl;
20
+ }
21
+ else {
22
+ $ctx.logger.warn(`skipping unknown tpl ID: "${id}"`);
23
+ return orig;
24
+ }
25
+ });
26
+ $ctx.post.reduce((acc, fn) => ($ctx.src = fn($ctx, [acc], path)), $ctx.src);
27
+ return $ctx;
28
+ };
29
+ export const transcludeFile = (path, ctx) => {
30
+ const $ctx = {
31
+ logger: DEFAULT_LOGGER,
32
+ src: "",
33
+ ...ctx,
34
+ };
35
+ path = resolve(path);
36
+ $ctx.src = readText(path, $ctx.logger);
37
+ return transclude($ctx, path);
38
+ };