@pretextbook/import 0.0.1 → 0.3.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/README.md CHANGED
@@ -1,121 +1,118 @@
1
- # @pretextbook/import
2
-
3
- Shared helpers for importing existing source content into PreTeXt.
4
-
5
- Two use cases:
6
-
7
- - VS Code extension logic: format detection, cleaning, and conversion utilities.
8
- - Web apps: React components for paste-or-upload import UX, plus a non-React
9
- library entry point suitable for server-side or extension-host use.
10
-
11
- The LaTeX cleaning pipeline is a TypeScript port of
12
- [`davidfarmer/PreprocessLaTeX`](https://github.com/davidfarmer/PreprocessLaTeX)
13
- (`src/main.js`'s `describeFiles` flow): drop comments, normalize whitespace,
14
- expand `\input`/`\include`, rewrite plain-TeX font directives, scrub
15
- presentation macros, and convert via `@pretextbook/latex-pretext`.
16
-
17
- ## Install
18
-
19
- ```sh
20
- npm install @pretextbook/import
21
- ```
22
-
23
- ## Utilities
24
-
25
- ```ts
26
- import {
27
- detectSourceFormat,
28
- convertSourceToPretext,
29
- importProjectFromFiles,
30
- } from "@pretextbook/import";
31
-
32
- const format = detectSourceFormat(rawInput);
33
- const result = convertSourceToPretext(rawInput, format);
34
-
35
- if ("pretextError" in result) {
36
- console.error(result.pretextError);
37
- } else {
38
- console.log(result.pretextSource);
39
- console.log(result.warnings); // cleaning diagnostics
40
- }
41
-
42
- // Multi-file project import (file paths -> source text):
43
- const project = importProjectFromFiles({
44
- "main.tex": "\\documentclass{article}\\begin{document}Hi.\\end{document}",
45
- });
46
- if ("outputFiles" in project) {
47
- // Standard PreTeXt project layout, keyed by path:
48
- console.log(project.outputFiles["source/main.ptx"]);
49
- console.log(project.outputFiles["project.ptx"]);
50
- console.log(project.outputFiles["publication/publication.ptx"]);
51
- }
52
- ```
53
-
54
- ### Book splitting
55
-
56
- If the converted source is a `<book>`, the layout splitter writes each
57
- `<chapter>` to its own file under `source/`, and `source/main.ptx` is rebuilt
58
- with `xi:include` references. Chapter filenames use `ch-<xml:id>.ptx` when an
59
- `xml:id` is present, falling back to a zero-padded index. Pass
60
- `splitSections: true` to additionally split each chapter by section.
61
-
62
- ```ts
63
- importProjectFromFiles(files, {
64
- documentKind: "book", // override auto-detect
65
- splitSections: true,
66
- });
67
- ```
68
-
69
- ## Upload pipeline
70
-
71
- ```ts
72
- import { handleImportUploadFile } from "@pretextbook/import";
73
-
74
- const result = await handleImportUploadFile(file);
75
- if ("pretextError" in result) {
76
- console.error(result.pretextError);
77
- } else {
78
- // Text files (the converted PreTeXt project + routed .bib auxiliaries)
79
- for (const [path, content] of Object.entries(result.outputFiles)) {
80
- saveOrUpload(path, content);
81
- }
82
- // Binary assets (images, PDFs, EPS) routed to source/assets/
83
- for (const [path, bytes] of Object.entries(result.outputAssets)) {
84
- saveBinary(path, bytes);
85
- }
86
- // Optional cleaned native source (LaTeX/Markdown), if the input was that format
87
- if (result.nativeOutputFiles) {
88
- for (const [path, content] of Object.entries(result.nativeOutputFiles)) {
89
- saveOrUpload(path, content);
90
- }
91
- }
92
- }
93
- ```
94
-
95
- Supports `.tex`, `.md`, `.ptx`, `.xml`, `.zip`, and `.tar.gz` uploads. For zip
96
- and tar.gz archives, the extractor identifies the main source file, expands
97
- LaTeX `\input`/`\include` or PreTeXt `xi:include` references, and routes
98
- binaries (`.png`, `.jpg`, `.pdf`, `.eps`, ...) into `source/assets/` and `.bib`
99
- files into `source/`.
100
-
101
- ## React Components
102
-
103
- ```tsx
104
- import {
105
- ImportSourceForm,
106
- ImportUploadPanel,
107
- } from "@pretextbook/import/react";
108
-
109
- function ImportPanel() {
110
- return (
111
- <>
112
- <ImportSourceForm onImport={(r) => console.log(r)} />
113
- <ImportUploadPanel onImport={(r) => console.log(r)} />
114
- </>
115
- );
116
- }
117
- ```
118
-
119
- `ImportUploadPanel` exposes UI controls for the `documentKind` override and
120
- `splitSections` toggle. Pass `importOptions={...}` to suppress those controls
121
- and use a fixed configuration.
1
+ # @pretextbook/import
2
+
3
+ Shared helpers for importing existing source content into PreTeXt.
4
+
5
+ Two use cases:
6
+
7
+ - VS Code extension logic: format detection, cleaning, and conversion utilities.
8
+ - Web apps: React components for paste-or-upload import UX, plus a non-React
9
+ library entry point suitable for server-side or extension-host use.
10
+
11
+ The LaTeX cleaning pipeline is a TypeScript port of
12
+ [`davidfarmer/PreprocessLaTeX`](https://github.com/davidfarmer/PreprocessLaTeX)
13
+ (`src/main.js`'s `describeFiles` flow): drop comments, normalize whitespace,
14
+ expand `\input`/`\include`, rewrite plain-TeX font directives, scrub
15
+ presentation macros, and convert via `@pretextbook/latex-pretext`.
16
+
17
+ ## Install
18
+
19
+ ```sh
20
+ npm install @pretextbook/import
21
+ ```
22
+
23
+ ## Utilities
24
+
25
+ ```ts
26
+ import {
27
+ detectSourceFormat,
28
+ convertSourceToPretext,
29
+ importProjectFromFiles,
30
+ } from "@pretextbook/import";
31
+
32
+ const format = detectSourceFormat(rawInput);
33
+ const result = convertSourceToPretext(rawInput, format);
34
+
35
+ if ("pretextError" in result) {
36
+ console.error(result.pretextError);
37
+ } else {
38
+ console.log(result.pretextSource);
39
+ console.log(result.warnings); // cleaning diagnostics
40
+ }
41
+
42
+ // Multi-file project import (file paths -> source text):
43
+ const project = importProjectFromFiles({
44
+ "main.tex": "\\documentclass{article}\\begin{document}Hi.\\end{document}",
45
+ });
46
+ if ("outputFiles" in project) {
47
+ // Standard PreTeXt project layout, keyed by path:
48
+ console.log(project.outputFiles["source/main.ptx"]);
49
+ console.log(project.outputFiles["project.ptx"]);
50
+ console.log(project.outputFiles["publication/publication.ptx"]);
51
+ }
52
+ ```
53
+
54
+ ### Book splitting
55
+
56
+ If the converted source is a `<book>`, the layout splitter writes each
57
+ `<chapter>` to its own file under `source/`, and `source/main.ptx` is rebuilt
58
+ with `xi:include` references. Chapter filenames use `ch-<xml:id>.ptx` when an
59
+ `xml:id` is present, falling back to a zero-padded index. Pass
60
+ `splitSections: true` to additionally split each chapter by section.
61
+
62
+ ```ts
63
+ importProjectFromFiles(files, {
64
+ documentKind: "book", // override auto-detect
65
+ splitSections: true,
66
+ });
67
+ ```
68
+
69
+ ## Upload pipeline
70
+
71
+ ```ts
72
+ import { handleImportUploadFile } from "@pretextbook/import";
73
+
74
+ const result = await handleImportUploadFile(file);
75
+ if ("pretextError" in result) {
76
+ console.error(result.pretextError);
77
+ } else {
78
+ // Text files (the converted PreTeXt project + routed .bib auxiliaries)
79
+ for (const [path, content] of Object.entries(result.outputFiles)) {
80
+ saveOrUpload(path, content);
81
+ }
82
+ // Binary assets (images, PDFs, EPS) routed to source/assets/
83
+ for (const [path, bytes] of Object.entries(result.outputAssets)) {
84
+ saveBinary(path, bytes);
85
+ }
86
+ // Optional cleaned native source (LaTeX/Markdown), if the input was that format
87
+ if (result.nativeOutputFiles) {
88
+ for (const [path, content] of Object.entries(result.nativeOutputFiles)) {
89
+ saveOrUpload(path, content);
90
+ }
91
+ }
92
+ }
93
+ ```
94
+
95
+ Supports `.tex`, `.md`, `.ptx`, `.xml`, `.zip`, and `.tar.gz` uploads. For zip
96
+ and tar.gz archives, the extractor identifies the main source file, expands
97
+ LaTeX `\input`/`\include` or PreTeXt `xi:include` references, and routes
98
+ binaries (`.png`, `.jpg`, `.pdf`, `.eps`, ...) into `source/assets/` and `.bib`
99
+ files into `source/`.
100
+
101
+ ## React Components
102
+
103
+ ```tsx
104
+ import { ImportSourceForm, ImportUploadPanel } from "@pretextbook/import/react";
105
+
106
+ function ImportPanel() {
107
+ return (
108
+ <>
109
+ <ImportSourceForm onImport={(r) => console.log(r)} />
110
+ <ImportUploadPanel onImport={(r) => console.log(r)} />
111
+ </>
112
+ );
113
+ }
114
+ ```
115
+
116
+ `ImportUploadPanel` exposes UI controls for the `documentKind` override and
117
+ `splitSections` toggle. Pass `importOptions={...}` to suppress those controls
118
+ and use a fixed configuration.