@tobi2409/layout 0.9.0 → 0.9.2

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
@@ -37,6 +37,12 @@ Process the HTML file:
37
37
  npx layout path/to/application.html
38
38
  ```
39
39
 
40
+ You can also pass a directory. It is searched recursively for `.html` and `.htm` files; output directories are skipped:
41
+
42
+ ```sh
43
+ npx layout path/to/pages
44
+ ```
45
+
40
46
  The output directory defaults to `generated`. Use `--output-dir` (or `-o`) to choose another name:
41
47
 
42
48
  ```sh
package/bin/layout.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { mkdir, readFile, writeFile } from 'node:fs/promises'
3
+ import { mkdir, readFile, readdir, realpath, stat, writeFile } from 'node:fs/promises'
4
4
  import { basename, dirname, extname, join } from 'node:path'
5
- import { pathToFileURL } from 'node:url'
5
+ import { fileURLToPath } from 'node:url'
6
6
  import { parse, serialize } from 'parse5'
7
7
 
8
8
  const ALIGNMENTS = new Set(['top', 'right', 'bottom', 'left', 'client'])
@@ -36,6 +36,11 @@ function walk(node, visitor) {
36
36
  for (const child of node.childNodes ?? []) {
37
37
  walk(child, visitor)
38
38
  }
39
+
40
+ // parse5 speichert den Inhalt von <template> in einem separaten DocumentFragment.
41
+ if (node.content) {
42
+ walk(node.content, visitor)
43
+ }
39
44
  }
40
45
 
41
46
  function parseAlignment(value, source) {
@@ -313,13 +318,40 @@ export async function compileFile(fileName, outputDirectoryName = 'generated') {
313
318
  return { ...result, htmlFile, cssFile, runtimeFile }
314
319
  }
315
320
 
321
+ async function findHtmlFiles(directory, outputDirectoryName) {
322
+ const files = []
323
+
324
+ for (const entry of await readdir(directory, { withFileTypes: true })) {
325
+ const entryPath = join(directory, entry.name)
326
+
327
+ if (entry.isDirectory()) {
328
+ if (entry.name !== outputDirectoryName) {
329
+ files.push(...await findHtmlFiles(entryPath, outputDirectoryName))
330
+ }
331
+ } else if (entry.isFile() && /\.html?$/i.test(entry.name)) {
332
+ files.push(entryPath)
333
+ }
334
+ }
335
+
336
+ return files
337
+ }
338
+
339
+ export async function compilePath(inputPath, outputDirectoryName = 'generated') {
340
+ const input = await stat(inputPath)
341
+ const files = input.isDirectory()
342
+ ? await findHtmlFiles(inputPath, outputDirectoryName)
343
+ : [inputPath]
344
+
345
+ return Promise.all(files.map((fileName) => compileFile(fileName, outputDirectoryName)))
346
+ }
347
+
316
348
  async function main() {
317
349
  const args = process.argv.slice(2)
318
350
  const fileName = args[0]
319
351
  let outputDirectoryName = 'generated'
320
352
 
321
353
  if (!fileName) {
322
- console.error('Usage: layout <document.html> [--output-dir <name>]')
354
+ console.error('Usage: layout <document.html|directory> [--output-dir <name>]')
323
355
  process.exitCode = 1
324
356
  return
325
357
  }
@@ -339,14 +371,19 @@ async function main() {
339
371
  }
340
372
  }
341
373
 
342
- const result = await compileFile(fileName, outputDirectoryName)
374
+ const results = await compilePath(fileName, outputDirectoryName)
343
375
 
344
- console.log(`Generated ${result.htmlFile}`)
345
- console.log(`Generated ${result.cssFile}`)
346
- console.log(`Generated ${result.runtimeFile}`)
376
+ for (const result of results) {
377
+ console.log(`Generated ${result.htmlFile}`)
378
+ console.log(`Generated ${result.cssFile}`)
379
+ console.log(`Generated ${result.runtimeFile}`)
380
+ }
347
381
  }
348
382
 
349
- if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
383
+ const isMainModule = process.argv[1]
384
+ && await realpath(process.argv[1]) === await realpath(fileURLToPath(import.meta.url))
385
+
386
+ if (isMainModule) {
350
387
  main().catch((error) => {
351
388
  console.error(error.message)
352
389
  process.exitCode = 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tobi2409/layout",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Build-time HTML frame layout generator using CSS Grid",
5
5
  "license": "MIT",
6
6
  "type": "module",