book-kit 0.0.0-stage → 0.6.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/LICENSE +21 -0
- package/NOTICE.md +72 -0
- package/README.md +251 -2
- package/bin/book-kit.js +115 -0
- package/commands/_emoji_io.py +32 -0
- package/commands/audit-chapter-lengths.js +244 -0
- package/commands/check-epub-size.js +68 -0
- package/commands/check-pdf-page-count.js +162 -0
- package/commands/composite-kindle-emoji.py +85 -0
- package/commands/composite-print-emoji.py +98 -0
- package/commands/conventions.js +76 -0
- package/commands/convert-pdfx.sh +194 -0
- package/commands/download-icc-profile.sh +105 -0
- package/commands/fonts.py +212 -0
- package/commands/generate-reference-docx.py +344 -0
- package/commands/kindle-fonts.js +173 -0
- package/commands/links-to-footnotes.js +366 -0
- package/commands/markdownlint.js +25 -0
- package/commands/minify-css.js +43 -0
- package/commands/optimize-images.js +73 -0
- package/commands/parity.sh +150 -0
- package/commands/postprocess-epub.js +541 -0
- package/commands/prep-print-src.js +77 -0
- package/commands/prose.js +38 -0
- package/commands/release-files.js +89 -0
- package/commands/reorder-print-frontmatter.js +79 -0
- package/commands/strip-page-rules.js +1168 -0
- package/commands/strip-property-rules.js +93 -0
- package/commands/update-key-takeaways.js +314 -0
- package/commands/validate.js +283 -0
- package/configs/markdownlint-cli2.jsonc +98 -0
- package/configs/textlintrc.json +22 -0
- package/css/theme/01-tokens.css +83 -0
- package/css/theme/02-fonts.css +79 -0
- package/css/theme/03-prose.css +53 -0
- package/css/theme/04-base.css +226 -0
- package/css/theme/05-components.css +546 -0
- package/css/theme/06-utilities.css +40 -0
- package/css/theme/07-ereader.css +245 -0
- package/css/theme/08-epub.css +865 -0
- package/css/theme/09-back-matter.css +47 -0
- package/css/theme/10-dark.css +302 -0
- package/css/theme/11-print.css +1229 -0
- package/css/theme.css +38 -0
- package/defaults/book.config.yml +318 -0
- package/defaults/pandoc-base.yml +19 -0
- package/defaults/pandoc-docx.yml +6 -0
- package/defaults/pandoc-epub-fonts.yml +6 -0
- package/defaults/pandoc-epub.yml +15 -0
- package/defaults/pandoc-kindle.yml +6 -0
- package/emoji/1f382.png +0 -0
- package/emoji/1f389.png +0 -0
- package/emoji/1f440.png +0 -0
- package/emoji/1f44d.png +0 -0
- package/emoji/1f44f.png +0 -0
- package/emoji/1f454.png +0 -0
- package/emoji/1f4a1.png +0 -0
- package/emoji/1f4bb.png +0 -0
- package/emoji/1f602.png +0 -0
- package/emoji/1f603.png +0 -0
- package/emoji/1f604.png +0 -0
- package/emoji/1f60a.png +0 -0
- package/emoji/1f60f.png +0 -0
- package/emoji/1f64c.png +0 -0
- package/emoji/1f64f.png +0 -0
- package/emoji/1f914.png +0 -0
- package/emoji/1f923.png +0 -0
- package/emoji/2714.png +0 -0
- package/emoji/274c.png +0 -0
- package/filters/add-div-titles.lua +196 -0
- package/filters/arc-notice.lua +36 -0
- package/filters/body-emoji-images.lua +48 -0
- package/filters/callout-classes.lua +40 -0
- package/filters/lib/book_kit.lua +110 -0
- package/filters/revision.lua +17 -0
- package/filters/strip-comments.lua +40 -0
- package/filters/strip-emoji-kindle.lua +134 -0
- package/fonts/LICENSE-source-code-pro.md +93 -0
- package/fonts/LICENSE-source-sans.md +93 -0
- package/fonts/LICENSE-source-serif.md +93 -0
- package/fonts/recipe.json +41 -0
- package/just/book-kit.just +229 -0
- package/lib/acronyms.js +47 -0
- package/lib/callouts.js +17 -0
- package/lib/config.js +211 -0
- package/lib/is-main.js +21 -0
- package/lib/lint.js +148 -0
- package/lib/pandoc.js +148 -0
- package/lib/paragraphs.js +173 -0
- package/lib/revision.js +35 -0
- package/lib/structure.js +39 -0
- package/lib/textlint-github-formatter.js +58 -0
- package/lib/validate.js +202 -0
- package/package.json +98 -4
- package/presets/business-nonfiction/CONVENTIONS.md +35 -0
- package/presets/business-nonfiction/preset.yml +128 -0
- package/profiles/pdfx-def.ps +42 -0
- package/requirements.txt +14 -0
- package/rules/no-ai-like-patterns.js +209 -0
- package/rules/no-assumptive.js +76 -0
- package/rules/no-bare-acronyms.js +129 -0
- package/rules/no-cliches.js +544 -0
- package/rules/no-cultural-idioms.js +422 -0
- package/rules/no-vague-intensifiers.js +94 -0
- package/templates/template-epub.html +79 -0
- package/templates/template.html +73 -0
- package/toolchain.json +42 -0
- package/validators/acronym-expansion.js +94 -0
- package/validators/bold-keywords.js +56 -0
- package/validators/callout-balance.js +74 -0
- package/validators/callout-divs.js +66 -0
- package/validators/callout-labels.js +102 -0
- package/validators/chapter-lengths.js +14 -0
- package/validators/chapter.js +57 -0
- package/validators/cross-references.js +45 -0
- package/validators/doubled-words.js +76 -0
- package/validators/duplicate-headings.js +52 -0
- package/validators/em-dashes.js +73 -0
- package/validators/escaped-smallcaps.js +46 -0
- package/validators/footnotes.js +78 -0
- package/validators/heading-hierarchy.js +58 -0
- package/validators/hypothetical-hooks.js +103 -0
- package/validators/index-integrity.js +28 -0
- package/validators/index.js +73 -0
- package/validators/list-punctuation.js +123 -0
- package/validators/min-opener-length.js +87 -0
- package/validators/no-bare-url-link-text.js +38 -0
- package/validators/no-todo-markers.js +49 -0
- package/validators/no-transition-todos.js +34 -0
- package/validators/number-range-dashes.js +72 -0
- package/validators/paragraph-length.js +62 -0
- package/validators/quote-punctuation.js +85 -0
- package/validators/section-intro-final-lead.js +90 -0
- package/validators/section-intro-has-lead.js +41 -0
- package/validators/section-intro-no-tldr.js +37 -0
- package/validators/section-intros.js +33 -0
- package/validators/sentence-starters.js +132 -0
- package/validators/smallcaps.js +67 -0
- package/validators/tldr-length.js +58 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Copyright 2014 - 2023 Adobe (http://www.adobe.com/), with Reserved Font Name ‘Source’. All Rights Reserved. Source is a trademark of Adobe in the United States and/or other countries.
|
|
2
|
+
|
|
3
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
4
|
+
|
|
5
|
+
This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
-----------------------------------------------------------
|
|
9
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
10
|
+
-----------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
PREAMBLE
|
|
13
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
14
|
+
development of collaborative font projects, to support the font creation
|
|
15
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
16
|
+
open framework in which fonts may be shared and improved in partnership
|
|
17
|
+
with others.
|
|
18
|
+
|
|
19
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
20
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
21
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
22
|
+
redistributed and/or sold with any software provided that any reserved
|
|
23
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
24
|
+
however, cannot be released under any other type of license. The
|
|
25
|
+
requirement for fonts to remain under this license does not apply
|
|
26
|
+
to any document created using the fonts or their derivatives.
|
|
27
|
+
|
|
28
|
+
DEFINITIONS
|
|
29
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
30
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
31
|
+
include source files, build scripts and documentation.
|
|
32
|
+
|
|
33
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
34
|
+
copyright statement(s).
|
|
35
|
+
|
|
36
|
+
"Original Version" refers to the collection of Font Software components as
|
|
37
|
+
distributed by the Copyright Holder(s).
|
|
38
|
+
|
|
39
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
40
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
41
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
42
|
+
new environment.
|
|
43
|
+
|
|
44
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
45
|
+
writer or other person who contributed to the Font Software.
|
|
46
|
+
|
|
47
|
+
PERMISSION & CONDITIONS
|
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
49
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
50
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
51
|
+
Software, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
1) Neither the Font Software nor any of its individual components,
|
|
54
|
+
in Original or Modified Versions, may be sold by itself.
|
|
55
|
+
|
|
56
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
57
|
+
redistributed and/or sold with any software, provided that each copy
|
|
58
|
+
contains the above copyright notice and this license. These can be
|
|
59
|
+
included either as stand-alone text files, human-readable headers or
|
|
60
|
+
in the appropriate machine-readable metadata fields within text or
|
|
61
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
62
|
+
|
|
63
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
64
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
65
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
66
|
+
presented to the users.
|
|
67
|
+
|
|
68
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
69
|
+
Software shall not be used to promote, endorse or advertise any
|
|
70
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
71
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
72
|
+
permission.
|
|
73
|
+
|
|
74
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
75
|
+
must be distributed entirely under this license, and must not be
|
|
76
|
+
distributed under any other license. The requirement for fonts to
|
|
77
|
+
remain under this license does not apply to any document created
|
|
78
|
+
using the Font Software.
|
|
79
|
+
|
|
80
|
+
TERMINATION
|
|
81
|
+
This license becomes null and void if any of the above conditions are
|
|
82
|
+
not met.
|
|
83
|
+
|
|
84
|
+
DISCLAIMER
|
|
85
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
88
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
89
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
90
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
91
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
92
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
93
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$comment": "How `book-kit fonts` builds the theme's fonts from the unmodified upstream files pinned in toolchain.json `fonts`: Adobe's Source Serif 4 release, and Google Fonts' builds of Source Sans 3 and Source Code Pro, whose vertical metrics and hinting set the theme's line heights. The kit ships only this recipe, never the derived files: the fonts are OFL-licensed with the Reserved Font Name 'Source', which bars redistributing modified versions under that name, while subsetting them for your own build and embedding them in a book is fine. Output names are what css/theme.css, commands/kindle-fonts.js, and the pandoc defaults load. Changing anything here or in toolchain.json `fonts` can reflow a locked print page count.",
|
|
3
|
+
"unicodes": {
|
|
4
|
+
"serif": "U+0020-007E,U+00A0-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2002-2007,U+2009-200B,U+2010,U+2012-2015,U+2018-201A,U+201C-201E,U+2020-2022,U+2025-2026,U+202F-2030,U+2032-2033,U+2039-203A,U+203C,U+2044,U+2047-2049,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215",
|
|
5
|
+
"sans": "U+0020-007E,U+00A0-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0300-0301,U+0303-0304,U+0308-0309,U+0323,U+0329,U+2009,U+200B,U+2013-2014,U+2018-201A,U+201C-201E,U+2022,U+2026,U+2032-2033,U+2039-203A,U+2044,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF",
|
|
6
|
+
"code": "U+0020-007E,U+00A0-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0300-0301,U+0303-0304,U+0308-0309,U+0323,U+0329,U+2013-2014,U+2018-201A,U+201C-201E,U+2022,U+2026,U+2032-2033,U+2039-203A,U+2044,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF",
|
|
7
|
+
"not-equal": "U+2260"
|
|
8
|
+
},
|
|
9
|
+
"features": {
|
|
10
|
+
"serif": ["aalt", "c2sc", "case", "ccmp", "dnom", "frac", "kern", "liga", "lnum", "locl", "mark", "mkmk", "numr", "onum", "ordn", "pnum", "sinf", "smcp", "ss03", "subs", "sups", "tnum", "zero"],
|
|
11
|
+
"sans": ["ccmp", "dnom", "frac", "kern", "liga", "locl", "mark", "mkmk", "numr", "pnum"],
|
|
12
|
+
"code": ["ccmp", "dnom", "frac", "locl", "mark", "mkmk", "numr"],
|
|
13
|
+
"not-equal": ["kern"]
|
|
14
|
+
},
|
|
15
|
+
"$drop_tables": "Tables to remove after subsetting (Source Serif's BASE never shipped in the theme).",
|
|
16
|
+
"$names": "Name-table records to overwrite (nameID: string). They keep the ≠ faces' embedded font names distinct: WeasyPrint draws the wrong glyph when the ≠ face shares the main face's name.",
|
|
17
|
+
"subsets": [
|
|
18
|
+
{ "out": "SourceSerif4-Variable-Latin.woff2", "source": "source-serif", "member": "source-serif-4.005_Desktop/VAR/SourceSerif4Variable-Roman.ttf", "unicodes": "serif", "features": "serif", "drop_tables": ["BASE"] },
|
|
19
|
+
{ "out": "SourceSerif4-Italic-Variable-Latin.woff2", "source": "source-serif", "member": "source-serif-4.005_Desktop/VAR/SourceSerif4Variable-Italic.ttf", "unicodes": "serif", "features": "serif", "drop_tables": ["BASE"] },
|
|
20
|
+
{ "out": "SourceSerif4-NotEqual.woff2", "source": "source-serif", "member": "source-serif-4.005_Desktop/VAR/SourceSerif4Variable-Roman.ttf", "unicodes": "not-equal", "features": "not-equal", "names": {"1": "Source Serif 4 NotEqual", "3": "SourceSerif4NotEqual;4.005", "4": "Source Serif 4 NotEqual", "6": "SourceSerif4NotEqual"} },
|
|
21
|
+
{ "out": "SourceSerif4-Italic-NotEqual.woff2", "source": "source-serif", "member": "source-serif-4.005_Desktop/VAR/SourceSerif4Variable-Italic.ttf", "unicodes": "not-equal", "features": "not-equal", "names": {"1": "Source Serif 4 NotEqual Italic", "3": "SourceSerif4NotEqual-Italic;4.005", "4": "Source Serif 4 NotEqual Italic", "6": "SourceSerif4NotEqual-Italic"} },
|
|
22
|
+
{ "out": "SourceSans3-Variable-Latin.woff2", "source": "source-sans-3", "unicodes": "sans", "features": "sans" },
|
|
23
|
+
{ "out": "SourceSans3-Italic-Variable-Latin.woff2", "source": "source-sans-3-italic", "unicodes": "sans", "features": "sans" },
|
|
24
|
+
{ "out": "SourceCodePro-Variable-Latin.woff2", "source": "source-code-pro", "unicodes": "code", "features": "code" },
|
|
25
|
+
{ "out": "SourceCodePro-Italic-Variable-Latin.woff2", "source": "source-code-pro-italic", "unicodes": "code", "features": "code" }
|
|
26
|
+
],
|
|
27
|
+
"$instances": "Static instances for Kindle, which ignores variable fonts. Each pins every axis of a subset above; axes left out are pinned at their default. Source Serif uses optical size 16, tuned for body text at e-reader sizes.",
|
|
28
|
+
"instances": [
|
|
29
|
+
{ "out": "kindle/SourceSerif4-Regular.woff2", "from": "SourceSerif4-Variable-Latin.woff2", "axes": { "wght": 400, "opsz": 16 } },
|
|
30
|
+
{ "out": "kindle/SourceSerif4-Bold.woff2", "from": "SourceSerif4-Variable-Latin.woff2", "axes": { "wght": 700, "opsz": 16 } },
|
|
31
|
+
{ "out": "kindle/SourceSerif4-Italic.woff2", "from": "SourceSerif4-Italic-Variable-Latin.woff2", "axes": { "wght": 400, "opsz": 16 } },
|
|
32
|
+
{ "out": "kindle/SourceSerif4-BoldItalic.woff2", "from": "SourceSerif4-Italic-Variable-Latin.woff2", "axes": { "wght": 700, "opsz": 16 } },
|
|
33
|
+
{ "out": "kindle/SourceSans3-Regular.woff2", "from": "SourceSans3-Variable-Latin.woff2", "axes": { "wght": 400 } },
|
|
34
|
+
{ "out": "kindle/SourceSans3-SemiBold.woff2", "from": "SourceSans3-Variable-Latin.woff2", "axes": { "wght": 600 } },
|
|
35
|
+
{ "out": "kindle/SourceSans3-Bold.woff2", "from": "SourceSans3-Variable-Latin.woff2", "axes": { "wght": 700 } },
|
|
36
|
+
{ "out": "kindle/SourceSans3-Italic.woff2", "from": "SourceSans3-Italic-Variable-Latin.woff2", "axes": { "wght": 400 } },
|
|
37
|
+
{ "out": "kindle/SourceCodePro-Regular.woff2", "from": "SourceCodePro-Variable-Latin.woff2", "axes": { "wght": 400 } },
|
|
38
|
+
{ "out": "kindle/SourceCodePro-Bold.woff2", "from": "SourceCodePro-Variable-Latin.woff2", "axes": { "wght": 700 } },
|
|
39
|
+
{ "out": "kindle/SourceCodePro-Italic.woff2", "from": "SourceCodePro-Italic-Variable-Latin.woff2", "axes": { "wght": 400 } }
|
|
40
|
+
]
|
|
41
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# book-kit recipes. A book's justfile imports this file:
|
|
2
|
+
#
|
|
3
|
+
# set allow-duplicate-recipes
|
|
4
|
+
# import 'node_modules/book-kit/just/book-kit.just'
|
|
5
|
+
#
|
|
6
|
+
# Recipes run from the book's directory. Any recipe the book's justfile defines
|
|
7
|
+
# with the same name replaces the one here (that's what allow-duplicate-recipes
|
|
8
|
+
# is for), so a book can override a single step without forking the kit.
|
|
9
|
+
#
|
|
10
|
+
# Settings come from book.config.yml (defaults: the kit's defaults/book.config.yml).
|
|
11
|
+
|
|
12
|
+
set shell := ["bash", "-eu", "-o", "pipefail", "-c"]
|
|
13
|
+
|
|
14
|
+
kit := source_directory() / ".."
|
|
15
|
+
bk := "node " + kit / "bin/book-kit.js"
|
|
16
|
+
kit_defs := kit / "defaults"
|
|
17
|
+
dist := shell(bk + ' config paths.dist')
|
|
18
|
+
name := shell(bk + ' config output.basename')
|
|
19
|
+
work := dist / ".book-kit"
|
|
20
|
+
pdfx_file := dist / (name + shell(bk + ' config output.pdfx_suffix') + ".pdf")
|
|
21
|
+
# Extra pandoc options for the epub, kindle, and html builds, e.g.
|
|
22
|
+
# BOOK_KIT_PANDOC_ARGS='--toc-depth=3' just epub
|
|
23
|
+
# An environment variable rather than recipe arguments, so `just epub kindle
|
|
24
|
+
# html` runs three recipes instead of passing "kindle html" to pandoc.
|
|
25
|
+
pandoc_args := env('BOOK_KIT_PANDOC_ARGS', '')
|
|
26
|
+
# Build time: the book's last commit, so every build of a commit is
|
|
27
|
+
# byte-identical. pandoc stamps EPUB dcterms:modified, DOCX properties, and zip
|
|
28
|
+
# entries with it; postprocess-epub's repack and the dc:date of a book with no
|
|
29
|
+
# metadata.pub_date use it too. A SOURCE_DATE_EPOCH already in the environment
|
|
30
|
+
# wins. Outside a git checkout it's empty, and everything uses the current time.
|
|
31
|
+
export SOURCE_DATE_EPOCH := env('SOURCE_DATE_EPOCH', `git log -1 --format=%ct 2>/dev/null || true`)
|
|
32
|
+
|
|
33
|
+
# ─── Build ──────────────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
# Full build: every format, then the format checks.
|
|
36
|
+
[group('build')]
|
|
37
|
+
all: css epub kindle html pdf pdfx check
|
|
38
|
+
|
|
39
|
+
# Compile the Tailwind stylesheet (book's paths.css, which imports the kit theme).
|
|
40
|
+
# Skips when nothing Tailwind scans is newer than the output: a chapter-only edit
|
|
41
|
+
# still rebuilds, because Tailwind v4 generates utilities from the content.
|
|
42
|
+
[group('build')]
|
|
43
|
+
css: _setup
|
|
44
|
+
@if [ -f {{dist}}/style.css ] && [ -z "$(find "$({{bk}} config paths.css)" "$({{bk}} config paths.src)" "$({{bk}} config paths.index)" {{kit}}/css {{kit}}/templates {{kit}}/filters -newer {{dist}}/style.css 2>/dev/null | head -1)" ]; then \
|
|
45
|
+
echo "{{dist}}/style.css is up to date"; \
|
|
46
|
+
else \
|
|
47
|
+
npx @tailwindcss/cli -i "$({{bk}} config paths.css)" -o {{dist}}/style.css --minify; \
|
|
48
|
+
fi
|
|
49
|
+
|
|
50
|
+
# EPUB 3 for Apple Books, Kobo, Google Play, D2D, IngramSpark (and KDP).
|
|
51
|
+
[group('build')]
|
|
52
|
+
epub: css _strip-css (_prepare "epub")
|
|
53
|
+
{{bk}} minify-css {{dist}}/style-epub.css
|
|
54
|
+
pandoc --defaults {{kit_defs}}/pandoc-epub.yml --defaults {{kit_defs}}/pandoc-epub-fonts.yml \
|
|
55
|
+
--defaults "$({{bk}} config paths.index)" --defaults {{work}}/pandoc-epub.yml \
|
|
56
|
+
{{pandoc_args}} --output {{dist}}/{{name}}.epub
|
|
57
|
+
{{bk}} postprocess-epub {{dist}}/{{name}}.epub
|
|
58
|
+
|
|
59
|
+
# Kindle-optimized EPUB: static fonts, Twemoji images for e-ink, links as
|
|
60
|
+
# footnotes whose URLs stay tappable.
|
|
61
|
+
[group('build')]
|
|
62
|
+
kindle: css _strip-css (_prepare "kindle") _emoji
|
|
63
|
+
{{bk}} kindle-fonts
|
|
64
|
+
{{bk}} minify-css {{dist}}/style-epub.css {{dist}}/style-kindle.css
|
|
65
|
+
{{bk}} prep-print-src --preserve-internal-links --link-footnote-urls >/dev/null
|
|
66
|
+
pandoc --defaults {{kit_defs}}/pandoc-epub.yml --defaults {{kit_defs}}/pandoc-kindle.yml \
|
|
67
|
+
--defaults {{work}}/print-index.yml --defaults {{work}}/pandoc-kindle.yml \
|
|
68
|
+
--metadata kindle=true {{pandoc_args}} --output {{dist}}/{{name}}-kindle.epub
|
|
69
|
+
{{bk}} postprocess-epub {{dist}}/{{name}}-kindle.epub
|
|
70
|
+
|
|
71
|
+
# Web edition (also what the link checker and accessibility tests run against).
|
|
72
|
+
[group('build')]
|
|
73
|
+
html: css (_prepare "html")
|
|
74
|
+
pandoc --defaults {{kit_defs}}/pandoc-base.yml --defaults "$({{bk}} config paths.index)" \
|
|
75
|
+
--defaults {{work}}/pandoc-html.yml {{pandoc_args}} --output {{dist}}/{{name}}.html
|
|
76
|
+
|
|
77
|
+
# DOCX for editorial workflows (track changes with an editor).
|
|
78
|
+
[group('build')]
|
|
79
|
+
docx: (_prepare "docx")
|
|
80
|
+
pandoc --defaults {{kit_defs}}/pandoc-docx.yml --defaults "$({{bk}} config paths.index)" \
|
|
81
|
+
--defaults {{work}}/pandoc-docx.yml --output {{dist}}/{{name}}.docx
|
|
82
|
+
|
|
83
|
+
# Generate a reference.docx (heading and body styles for the DOCX build).
|
|
84
|
+
[group('build')]
|
|
85
|
+
reference-docx:
|
|
86
|
+
{{bk}} generate-reference-docx
|
|
87
|
+
|
|
88
|
+
# Print-ready PDF (6x9 trade paperback by default; links become footnotes).
|
|
89
|
+
[group('build')]
|
|
90
|
+
pdf: css (_prepare "pdf") _emoji
|
|
91
|
+
{{bk}} prep-print-src --inject >/dev/null
|
|
92
|
+
pandoc --defaults {{kit_defs}}/pandoc-base.yml --defaults {{work}}/print-index.yml \
|
|
93
|
+
--defaults {{work}}/pandoc-pdf.yml --metadata pdf=true \
|
|
94
|
+
--to=html5 --standalone --output {{dist}}/{{name}}.print.html
|
|
95
|
+
{{bk}} reorder-print-frontmatter {{dist}}/{{name}}.print.html
|
|
96
|
+
{{bk}} strip-property-rules {{dist}}/{{name}}.print.html
|
|
97
|
+
weasyprint --base-url {{dist}}/ --pdf-version 1.4 --dpi 300 --optimize-images \
|
|
98
|
+
{{dist}}/{{name}}.print.html {{dist}}/{{name}}.pdf
|
|
99
|
+
@rm -f {{dist}}/{{name}}.print.html
|
|
100
|
+
|
|
101
|
+
# PDF/X-1a:2001 (CMYK, no transparency) for IngramSpark and other print services.
|
|
102
|
+
[group('build')]
|
|
103
|
+
pdfx: pdf
|
|
104
|
+
{{bk}} convert-pdfx {{dist}}/{{name}}.pdf {{pdfx_file}} "$({{bk}} config print.pdfx_title)"
|
|
105
|
+
|
|
106
|
+
# Advance reader copies: <name>-arc.epub and <name>-kindle-arc.epub, the retail
|
|
107
|
+
# EPUBs plus a "not for redistribution" notice on the copyright page
|
|
108
|
+
# (filters/arc-notice.lua). Not part of `all`.
|
|
109
|
+
[group('build')]
|
|
110
|
+
arc: arc-epub arc-kindle
|
|
111
|
+
|
|
112
|
+
# ARC EPUB. Builds the retail EPUB first for its prepared CSS and pandoc
|
|
113
|
+
# defaults, then reruns pandoc with arc=true into its own file.
|
|
114
|
+
[group('build')]
|
|
115
|
+
arc-epub: epub
|
|
116
|
+
pandoc --defaults {{kit_defs}}/pandoc-epub.yml --defaults {{kit_defs}}/pandoc-epub-fonts.yml \
|
|
117
|
+
--defaults "$({{bk}} config paths.index)" --defaults {{work}}/pandoc-epub.yml \
|
|
118
|
+
--metadata arc=true {{pandoc_args}} --output {{dist}}/{{name}}-arc.epub
|
|
119
|
+
{{bk}} postprocess-epub {{dist}}/{{name}}-arc.epub
|
|
120
|
+
|
|
121
|
+
# ARC Kindle EPUB (see arc-epub).
|
|
122
|
+
[group('build')]
|
|
123
|
+
arc-kindle: kindle
|
|
124
|
+
pandoc --defaults {{kit_defs}}/pandoc-epub.yml --defaults {{kit_defs}}/pandoc-kindle.yml \
|
|
125
|
+
--defaults {{work}}/print-index.yml --defaults {{work}}/pandoc-kindle.yml \
|
|
126
|
+
--metadata kindle=true --metadata arc=true {{pandoc_args}} --output {{dist}}/{{name}}-kindle-arc.epub
|
|
127
|
+
{{bk}} postprocess-epub {{dist}}/{{name}}-kindle-arc.epub
|
|
128
|
+
|
|
129
|
+
# Release files: copies of the built formats named for the version in
|
|
130
|
+
# <dist>/release/ (book-v1.2.0.epub, book-kindle-v1.2.0.epub, ...) with a
|
|
131
|
+
# checksums.txt. Pass the release tag to fail on a mismatch with VERSION.
|
|
132
|
+
[group('build')]
|
|
133
|
+
release-files tag="":
|
|
134
|
+
{{bk}} release-files {{ if tag != "" { "--tag " + tag } else { "" } }}
|
|
135
|
+
|
|
136
|
+
# ─── Validate ───────────────────────────────────────────────────────────────
|
|
137
|
+
|
|
138
|
+
# Everything a pull request should pass: chapter validators, Markdown lint, prose lint.
|
|
139
|
+
[group('validate')]
|
|
140
|
+
check-all: validate lint prose
|
|
141
|
+
|
|
142
|
+
# Chapter structure and house-style validators (book.config.yml `validators`).
|
|
143
|
+
[group('validate')]
|
|
144
|
+
validate:
|
|
145
|
+
{{bk}} validate
|
|
146
|
+
|
|
147
|
+
# Markdown lint: the kit's config with lint.markdownlint merged in (or the
|
|
148
|
+
# book's own .markdownlint-cli2.jsonc).
|
|
149
|
+
[group('validate')]
|
|
150
|
+
lint *args:
|
|
151
|
+
@{{bk}} markdownlint {{args}}
|
|
152
|
+
|
|
153
|
+
# Prose lint with textlint: the kit's base rules, lint.textlint, and the kit
|
|
154
|
+
# and preset rules lint.rules turns on (or the book's own .textlintrc). On
|
|
155
|
+
# GitHub Actions, findings are reported as annotations on each file and line.
|
|
156
|
+
[group('validate')]
|
|
157
|
+
prose *args:
|
|
158
|
+
@{{bk}} prose {{args}}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# Format checks on built output: page-count lock and EPUB size budgets.
|
|
162
|
+
[group('validate')]
|
|
163
|
+
check:
|
|
164
|
+
@if [ -f {{dist}}/{{name}}.pdf ]; then {{bk}} check-pdf-page-count {{dist}}/{{name}}.pdf; fi
|
|
165
|
+
@if [ -f {{pdfx_file}} ]; then {{bk}} check-pdf-page-count {{pdfx_file}}; fi
|
|
166
|
+
{{bk}} check-epub-size --ci
|
|
167
|
+
|
|
168
|
+
# EPUBCheck on the built EPUBs (needs Java and an epubcheck on PATH or $EPUBCHECK_JAR).
|
|
169
|
+
[group('validate')]
|
|
170
|
+
epubcheck:
|
|
171
|
+
@for f in {{dist}}/{{name}}.epub {{dist}}/{{name}}-kindle.epub {{dist}}/{{name}}-arc.epub {{dist}}/{{name}}-kindle-arc.epub; do \
|
|
172
|
+
[ -f "$f" ] || continue; \
|
|
173
|
+
if [ -n "${EPUBCHECK_JAR:-}" ]; then java -jar "$EPUBCHECK_JAR" "$f"; else epubcheck "$f"; fi; \
|
|
174
|
+
done
|
|
175
|
+
|
|
176
|
+
# ─── Maintenance ────────────────────────────────────────────────────────────
|
|
177
|
+
|
|
178
|
+
# Regenerate each section's key takeaways chapter from its chapters' TL;DRs.
|
|
179
|
+
[group('maintenance')]
|
|
180
|
+
key-takeaways:
|
|
181
|
+
{{bk}} update-key-takeaways
|
|
182
|
+
|
|
183
|
+
# Print the build stamp ("<version> (<sha>)").
|
|
184
|
+
[group('maintenance')]
|
|
185
|
+
revision:
|
|
186
|
+
@{{bk}} revision
|
|
187
|
+
|
|
188
|
+
# Losslessly recompress PNGs under paths.img.
|
|
189
|
+
[group('maintenance')]
|
|
190
|
+
optimize-images:
|
|
191
|
+
{{bk}} optimize-images
|
|
192
|
+
|
|
193
|
+
# Remove build output.
|
|
194
|
+
[group('maintenance')]
|
|
195
|
+
clean:
|
|
196
|
+
rm -rf {{dist}}
|
|
197
|
+
|
|
198
|
+
# ─── Internals ──────────────────────────────────────────────────────────────
|
|
199
|
+
|
|
200
|
+
# Create dist/ and, unless the book brings its own fonts directory, build the
|
|
201
|
+
# theme's fonts (`book-kit fonts`, cached after the first run) and link them
|
|
202
|
+
# there: the theme's @font-face rules load ../fonts/ relative to dist/. A link
|
|
203
|
+
# book-kit made (into an older cache entry, or to the kit's fonts/ from before
|
|
204
|
+
# the kit stopped bundling them) is re-pointed; anything else is the book's own.
|
|
205
|
+
[private]
|
|
206
|
+
_setup:
|
|
207
|
+
@mkdir -p {{work}}
|
|
208
|
+
@{{bk}} fonts --link "$({{bk}} config paths.fonts)"
|
|
209
|
+
|
|
210
|
+
# Write the resolved config (for Lua filters) and per-format pandoc defaults.
|
|
211
|
+
[private]
|
|
212
|
+
_prepare format: _setup
|
|
213
|
+
@{{bk}} prepare {{format}} >/dev/null
|
|
214
|
+
|
|
215
|
+
# EPUB stylesheet: strip print-only and e-reader-unsupported CSS. Minifying is
|
|
216
|
+
# left to each recipe: kindle-fonts must read the unminified output.
|
|
217
|
+
[private]
|
|
218
|
+
_strip-css:
|
|
219
|
+
{{bk}} strip-page-rules {{dist}}/style.css {{dist}}/style-epub.css
|
|
220
|
+
|
|
221
|
+
# Twemoji PNG variants for Kindle (white under alpha) and print (flattened opaque).
|
|
222
|
+
[private]
|
|
223
|
+
_emoji:
|
|
224
|
+
@src="$({{bk}} config emoji.source_dir)"; src="${src:-{{kit}}/emoji}"; \
|
|
225
|
+
kdir="$({{bk}} config emoji.kindle_dir)"; \
|
|
226
|
+
cdir="{{dist}}/$({{bk}} config emoji.pdf_callout_dir)"; \
|
|
227
|
+
bdir="{{dist}}/$({{bk}} config emoji.pdf_body_dir)"; \
|
|
228
|
+
python3 {{kit}}/commands/composite-kindle-emoji.py "$src" "$kdir" >/dev/null; \
|
|
229
|
+
python3 {{kit}}/commands/composite-print-emoji.py "$src" "$cdir" "$bdir" >/dev/null
|
package/lib/acronyms.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// The kit's built-in acronym lists, shared by `book-kit validate` and the
|
|
2
|
+
// no-bare-acronyms textlint rule so the two can't drift apart. Books extend
|
|
3
|
+
// them with lint.acronyms and lint.universal_acronyms in book.config.yml.
|
|
4
|
+
|
|
5
|
+
// Acronyms that should always be written [ACRONYM]{.smallcaps}.
|
|
6
|
+
export const SMALLCAPS_ACRONYMS = [
|
|
7
|
+
'AI', 'API', 'URL', 'PR', 'CI', 'CD', 'HR', 'IC', 'DM', 'PM', 'VP',
|
|
8
|
+
'UI', 'UX', 'RFC', 'ADR', 'OKR', 'REST', 'SSH', 'VPN', 'DNS',
|
|
9
|
+
'HTTP', 'HTTPS', 'CSS', 'HTML', 'PDF', 'EPUB', 'YAML', 'JSON', 'XML',
|
|
10
|
+
'SQL', 'GIF', 'Q&A',
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
// Acronyms readers know without a parenthetical expansion on first use.
|
|
14
|
+
export const UNIVERSAL_ACRONYMS = [
|
|
15
|
+
// Titles and roles
|
|
16
|
+
'CEO', 'CTO', 'COO', 'CFO', 'VP', 'IC', 'PM', 'HR', 'EM',
|
|
17
|
+
// Core tech concepts
|
|
18
|
+
'AI', 'API', 'CLI', 'URL', 'PR', 'CI', 'CD', 'UI', 'UX',
|
|
19
|
+
'HTTP', 'HTTPS', 'CSS', 'HTML', 'PDF', 'DNS', 'SSH', 'VPN',
|
|
20
|
+
'JSON', 'YAML', 'XML', 'SQL', 'REST', 'GIF', 'LLM', 'IDE',
|
|
21
|
+
'CPU', 'PC', 'IT', 'README', 'I/O',
|
|
22
|
+
// Common business/communication
|
|
23
|
+
'FYI', 'CC', 'CSV', 'DM', 'ROI', 'FAQ', 'RSVP', 'HQ', 'OKR', 'GDP',
|
|
24
|
+
// Book format
|
|
25
|
+
'EPUB',
|
|
26
|
+
// Globally recognized references
|
|
27
|
+
'US', 'UK', 'EU', 'DC', 'COVID', 'DNA', 'ISO',
|
|
28
|
+
// Software licenses
|
|
29
|
+
'MIT', 'BSD', 'GPL', 'LGPL', 'SaaS', 'GNU',
|
|
30
|
+
// Publishing
|
|
31
|
+
'ISBN',
|
|
32
|
+
// Open source nomenclature
|
|
33
|
+
'OSS', 'FOSS',
|
|
34
|
+
// Expanded in non-parenthetical context
|
|
35
|
+
'DoS', // "denial-of-service" spelled out on same line
|
|
36
|
+
// Time
|
|
37
|
+
'AM', 'PM', 'UTC',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
// The lists above plus the book's own, from a resolved config.
|
|
41
|
+
export function smallcapsAcronyms(config) {
|
|
42
|
+
return [...SMALLCAPS_ACRONYMS, ...(config.lint.acronyms ?? [])];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function universalAcronyms(config) {
|
|
46
|
+
return [...UNIVERSAL_ACRONYMS, ...(config.lint.universal_acronyms ?? [])];
|
|
47
|
+
}
|
package/lib/callouts.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The callout registry (book.config.yml `callouts`), with helpers for the CSS
|
|
2
|
+
// post-processors. Outside a book (e.g. unit tests) it falls back to the kit
|
|
3
|
+
// defaults, so importing a command never requires a book.config.yml.
|
|
4
|
+
|
|
5
|
+
import { loadConfigOrDefaults } from './config.js';
|
|
6
|
+
|
|
7
|
+
export const callouts = loadConfigOrDefaults().callouts;
|
|
8
|
+
|
|
9
|
+
// All callout classes, as selectors: ['.tldr', '.pro-tip', ...]
|
|
10
|
+
export const calloutSelectors = callouts.map((c) => `.${c.class}`);
|
|
11
|
+
|
|
12
|
+
// Callouts whose label CSS draws with ::before (the build injects the label
|
|
13
|
+
// as text for EPUB, so those pseudo-elements are stripped there).
|
|
14
|
+
export const labelledSelectors = callouts.filter((c) => c.label).map((c) => `.${c.class}`);
|
|
15
|
+
|
|
16
|
+
// Callouts whose label carries an emoji.
|
|
17
|
+
export const emojiSelectors = callouts.filter((c) => c.emoji).map((c) => `.${c.class}`);
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Config loader shared by every book-kit command.
|
|
2
|
+
//
|
|
3
|
+
// Two roots matter, and conflating them is the classic bug when scripts move
|
|
4
|
+
// into a package:
|
|
5
|
+
// KIT_ROOT - this package (filters, templates, theme, fonts, defaults)
|
|
6
|
+
// BOOK_ROOT - the book being built (src/, dist/, index.yml, book.config.yml)
|
|
7
|
+
//
|
|
8
|
+
// BOOK_ROOT is $BOOK_ROOT when set, otherwise the nearest directory at or
|
|
9
|
+
// above the working directory that contains book.config.yml.
|
|
10
|
+
|
|
11
|
+
import { existsSync, readFileSync, readdirSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import * as yaml from 'js-yaml';
|
|
15
|
+
|
|
16
|
+
export const CONFIG_FILENAME = 'book.config.yml';
|
|
17
|
+
export const PRESET_FILENAME = 'preset.yml';
|
|
18
|
+
export const KIT_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
19
|
+
|
|
20
|
+
export function kitPath(...parts) {
|
|
21
|
+
return join(KIT_ROOT, ...parts);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function findBookRoot(start = process.cwd()) {
|
|
25
|
+
if (process.env.BOOK_ROOT) return resolve(process.env.BOOK_ROOT);
|
|
26
|
+
let dir = resolve(start);
|
|
27
|
+
for (;;) {
|
|
28
|
+
if (existsSync(join(dir, CONFIG_FILENAME))) return dir;
|
|
29
|
+
const parent = dirname(dir);
|
|
30
|
+
if (parent === dir) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`No ${CONFIG_FILENAME} found in ${start} or any parent directory. ` +
|
|
33
|
+
`Run book-kit from inside a book, or set BOOK_ROOT.`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
dir = parent;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isPlainObject(value) {
|
|
41
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Maps merge recursively; lists and scalars from `override` replace `base`.
|
|
45
|
+
export function deepMerge(base, override) {
|
|
46
|
+
if (!isPlainObject(base) || !isPlainObject(override)) {
|
|
47
|
+
return override === undefined ? base : override;
|
|
48
|
+
}
|
|
49
|
+
const out = { ...base };
|
|
50
|
+
for (const [key, value] of Object.entries(override)) {
|
|
51
|
+
out[key] = key in base ? deepMerge(base[key], value) : value;
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readYaml(path) {
|
|
57
|
+
return yaml.load(readFileSync(path, 'utf8')) ?? {};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Derived defaults that point at other keys.
|
|
61
|
+
function applyDerivedDefaults(config) {
|
|
62
|
+
config.llm.title ||= config.metadata.title;
|
|
63
|
+
if (config.accessibility.conforms_to) config.accessibility.certified_by ||= config.metadata.publisher;
|
|
64
|
+
config.print.pdfx_title ||= config.metadata.title;
|
|
65
|
+
return config;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A preset is a directory holding a preset.yml: config that sits between the
|
|
69
|
+
// kit defaults and the book, so one house style can be shared by many books.
|
|
70
|
+
// A book names presets in `extends`; presets can extend other presets.
|
|
71
|
+
//
|
|
72
|
+
// An `extends` entry is a path (relative to the file that names it), a kit
|
|
73
|
+
// preset (presets/<name>/), or an npm package with a preset.yml at its root,
|
|
74
|
+
// found in node_modules at or above the book.
|
|
75
|
+
export function kitPresets() {
|
|
76
|
+
const dir = kitPath('presets');
|
|
77
|
+
if (!existsSync(dir)) return [];
|
|
78
|
+
return readdirSync(dir).filter((name) => existsSync(join(dir, name, PRESET_FILENAME))).sort();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function resolvePreset(spec, fromDir, bookRoot) {
|
|
82
|
+
if (typeof spec !== 'string' || spec === '') {
|
|
83
|
+
throw new Error(`extends entries must be preset names or paths, got ${JSON.stringify(spec)}`);
|
|
84
|
+
}
|
|
85
|
+
if (spec.startsWith('.') || isAbsolute(spec)) {
|
|
86
|
+
const dir = resolve(fromDir, spec);
|
|
87
|
+
if (existsSync(join(dir, PRESET_FILENAME))) return dir;
|
|
88
|
+
throw new Error(`Preset "${spec}" in extends has no ${PRESET_FILENAME} (looked in ${dir})`);
|
|
89
|
+
}
|
|
90
|
+
const kit = kitPath('presets', spec);
|
|
91
|
+
if (existsSync(join(kit, PRESET_FILENAME))) return kit;
|
|
92
|
+
for (let dir = bookRoot; ; dir = dirname(dir)) {
|
|
93
|
+
const pkg = join(dir, 'node_modules', spec);
|
|
94
|
+
if (existsSync(join(pkg, PRESET_FILENAME))) return pkg;
|
|
95
|
+
if (dirname(dir) === dir) break;
|
|
96
|
+
}
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Unknown preset "${spec}" in extends. Kit presets: ${kitPresets().join(', ') || 'none'}. ` +
|
|
99
|
+
`Otherwise use a path (./my-preset) or install an npm package with a ${PRESET_FILENAME}.`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The presets `specs` names, parents first. Each is applied once, the first
|
|
104
|
+
// time it's reached, so a preset shared by two others can't re-override them.
|
|
105
|
+
function presetLayers(specs, fromDir, bookRoot, chain = [], applied = new Set()) {
|
|
106
|
+
const layers = [];
|
|
107
|
+
for (const spec of [].concat(specs ?? [])) {
|
|
108
|
+
const dir = resolvePreset(spec, fromDir, bookRoot);
|
|
109
|
+
if (chain.includes(dir)) {
|
|
110
|
+
throw new Error(`Preset cycle in extends: ${[...chain, dir].join(' -> ')}`);
|
|
111
|
+
}
|
|
112
|
+
if (applied.has(dir)) continue;
|
|
113
|
+
const { extends: parents, ...data } = readYaml(join(dir, PRESET_FILENAME));
|
|
114
|
+
layers.push(...presetLayers(parents, dir, bookRoot, [...chain, dir], applied));
|
|
115
|
+
applied.add(dir);
|
|
116
|
+
layers.push({ source: `preset ${spec}`, name: spec, dir, data });
|
|
117
|
+
}
|
|
118
|
+
return layers;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Every layer of a book's config, lowest first: kit defaults, presets, book.
|
|
122
|
+
function configLayers(bookRoot) {
|
|
123
|
+
const { extends: specs, ...own } = readYaml(join(bookRoot, CONFIG_FILENAME));
|
|
124
|
+
return [
|
|
125
|
+
{ source: 'kit defaults', data: readYaml(kitPath('defaults', CONFIG_FILENAME)) },
|
|
126
|
+
...presetLayers(specs, bookRoot, bookRoot),
|
|
127
|
+
{ source: CONFIG_FILENAME, data: own },
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const cache = new Map();
|
|
132
|
+
|
|
133
|
+
export function loadConfig({ root, fresh = false } = {}) {
|
|
134
|
+
const bookRoot = root ? resolve(root) : findBookRoot();
|
|
135
|
+
if (!fresh && cache.has(bookRoot)) return cache.get(bookRoot);
|
|
136
|
+
|
|
137
|
+
const layers = configLayers(bookRoot);
|
|
138
|
+
const config = layers.reduce((merged, layer) => deepMerge(merged, layer.data), {});
|
|
139
|
+
|
|
140
|
+
applyDerivedDefaults(config);
|
|
141
|
+
|
|
142
|
+
Object.defineProperty(config, 'root', { value: bookRoot, enumerable: false });
|
|
143
|
+
// The presets in effect, in merge order, so commands can find their files.
|
|
144
|
+
const presets = layers.filter((l) => l.dir).map(({ name, dir }) => ({ name, dir }));
|
|
145
|
+
Object.defineProperty(config, 'presets', { value: presets, enumerable: false });
|
|
146
|
+
cache.set(bookRoot, config);
|
|
147
|
+
return config;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Which layer set each key: { 'metadata.title': 'book.config.yml', ... }.
|
|
151
|
+
// A list is one value, since a later layer replaces it wholesale. Keys filled
|
|
152
|
+
// in by derived defaults (e.g. print.pdfx_title) report the layer that set
|
|
153
|
+
// them explicitly.
|
|
154
|
+
export function configSources({ root } = {}) {
|
|
155
|
+
const bookRoot = root ? resolve(root) : findBookRoot();
|
|
156
|
+
const sources = {};
|
|
157
|
+
const walk = (node, prefix, source) => {
|
|
158
|
+
for (const [key, value] of Object.entries(node)) {
|
|
159
|
+
const dotted = prefix ? `${prefix}.${key}` : key;
|
|
160
|
+
if (isPlainObject(value) && Object.keys(value).length > 0) walk(value, dotted, source);
|
|
161
|
+
else sources[dotted] = source;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
for (const layer of configLayers(bookRoot)) walk(layer.data, '', layer.source);
|
|
165
|
+
return sources;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// loadConfig(), or the kit defaults when there's no book (e.g. unit tests that
|
|
169
|
+
// import a command module). Never use this for a build: it hides a missing
|
|
170
|
+
// book.config.yml.
|
|
171
|
+
export function loadConfigOrDefaults() {
|
|
172
|
+
try {
|
|
173
|
+
return loadConfig();
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (!String(error.message).includes(CONFIG_FILENAME)) throw error;
|
|
176
|
+
const config = readYaml(kitPath('defaults', CONFIG_FILENAME));
|
|
177
|
+
return applyDerivedDefaults(config);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Absolute path inside the book. bookPath('src', 'intro.md'), or a configured
|
|
182
|
+
// path key: bookPath(config.paths.dist).
|
|
183
|
+
export function bookPath(...parts) {
|
|
184
|
+
return join(loadConfig().root, ...parts);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Resolve a configured path key (e.g. 'dist', 'src', 'index') to an absolute path.
|
|
188
|
+
export function pathFor(key, config = loadConfig()) {
|
|
189
|
+
const rel = config.paths[key];
|
|
190
|
+
if (rel === undefined) throw new Error(`Unknown paths.${key} in ${CONFIG_FILENAME}`);
|
|
191
|
+
return join(config.root, rel);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// dist/<basename><suffix>, e.g. outputFile('.epub') or outputFile('-kindle.epub').
|
|
195
|
+
export function outputFile(suffix, config = loadConfig()) {
|
|
196
|
+
return join(pathFor('dist', config), `${config.output.basename}${suffix}`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Dotted lookup for the CLI: get('output.basename').
|
|
200
|
+
export function get(config, dotted) {
|
|
201
|
+
return dotted.split('.').reduce((node, key) => (node == null ? undefined : node[key]), config);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Lua filters can't parse YAML, so the build writes the resolved config as JSON
|
|
205
|
+
// and points filters at it with $BOOK_KIT_CONFIG.
|
|
206
|
+
export function writeConfigJson(config = loadConfig()) {
|
|
207
|
+
const out = join(pathFor('dist', config), '.book-kit', 'config.json');
|
|
208
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
209
|
+
writeFileSync(out, `${JSON.stringify(config, null, 2)}\n`);
|
|
210
|
+
return out;
|
|
211
|
+
}
|