-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
209 additions
and
64 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
#!/usr/bin/env node | ||
|
||
console.log("J2h CLI") | ||
import transpile from './transpile.js' | ||
import validate from './validate.js' | ||
import { Readable } from 'node:stream' | ||
|
||
import { argv, stdin, stdout } from 'node:process' | ||
|
||
const [, , file] = argv | ||
|
||
if (file) { | ||
console.log('TODO HANDLE FILE ARG', `<${file}>`) | ||
process.exit(1) | ||
} | ||
|
||
const stream = stdin.resume() | ||
|
||
if (stream.readable === false) process.exit() | ||
|
||
Readable.from(stream) | ||
.reduce(async (acc, cur) => (acc += cur), '') | ||
.then(validate) | ||
.then(transpile) | ||
.then(console.log) | ||
|
||
|
||
process.on('SIGINT', process.exit) | ||
|
||
// readStream.isTTY | ||
// const raw = await Readable.from(stream).reduce(async (acc, cur) => (acc += cur), '') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
const { log: l } = console | ||
import transpile from './transpile.js' | ||
import validate from './validate.js' | ||
|
||
l("J2H INIT") | ||
|
||
|
||
export const | ||
export default data => validate(data).then(transpile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export default function (json) { | ||
let inner = '' | ||
let indent = '' | ||
function walk(nodes) { | ||
for (const node of nodes) { | ||
const [tag, attributes, content] = node || [] | ||
inner += `${indent}<${tag}` | ||
for (const attr in attributes) { | ||
inner += ` ${attr}="${attributes[attr]}"` | ||
} | ||
if (content) { | ||
inner += '>' | ||
if (Array.isArray(content)) { | ||
const hasChildren = Array.isArray(content.at(0)) | ||
indent += '\t' | ||
inner += '\n' | ||
walk((hasChildren && content) || [content]) | ||
indent = indent.replace(/\t$/, '') | ||
inner += `${indent}</${tag}>\n` | ||
} else { | ||
inner += `${content}</${tag}>\n` | ||
} | ||
} else { | ||
inner += ' />\n' | ||
} | ||
} | ||
} | ||
|
||
walk(json) | ||
|
||
return inner | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import assert from 'node:assert/strict' | ||
import transpile from './transpile.js' | ||
|
||
export default function (raw) { | ||
try { | ||
// TODO VALIDATE SCHEMA | ||
// console.log('VAL', raw, '<<<') | ||
// return Promise.resolve(transpile(JSON.parse(raw))) | ||
return Promise.resolve(JSON.parse(raw)) | ||
} catch (err) { | ||
console.error('ERR', err.message) | ||
process.exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { readFile, writeFile, appendFile } from 'node:fs/promises' | ||
|
||
const { log } = console | ||
|
||
const { content } = JSON.parse(await readFile('./tests/basic.jsonc', { encoding: 'utf8' })) | ||
|
||
const write = data => writeFile('./tests/basic.html', data) | ||
const append = data => appendFile('./tests/basic.html', data) | ||
|
||
await write('<!doctype html>\n\n') | ||
|
||
let inner = '' | ||
let indent = '' | ||
|
||
async function walk(nodes) { | ||
log('\n -- WALK --\n', nodes, '\n -- ==== --\n') | ||
for (const node of nodes) { | ||
log('\t -- NODE --\n', node, '\n\t -- ==== --\n') | ||
const [tag, attributes, content] = node || [] | ||
inner += `${indent}<${tag}` | ||
for (const attr in attributes) { | ||
log({ attr }) | ||
inner += ` ${attr}="${attributes[attr]}"` | ||
} | ||
if (content) { | ||
inner += '>' | ||
if (Array.isArray(content)) { | ||
log(' ++ STEP IN', { content }) | ||
const hasChildren = Array.isArray(content.at(0)) | ||
indent += '\t' | ||
inner += '\n' | ||
walk((hasChildren && content) || [content]) | ||
indent = indent.replace(/\t$/, '') | ||
inner += `${indent}</${tag}>\n` | ||
} else { | ||
log(' == STRING CONTENT') | ||
inner += `${content}</${tag}>\n` | ||
} | ||
} else { | ||
inner += ' />\n' | ||
} | ||
} | ||
} | ||
|
||
walk(content) | ||
|
||
await append(inner) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"version": "0.1", | ||
"content": [ | ||
[ | ||
"div", | ||
{ "align": "center", "class": "wrapper" }, | ||
[ | ||
["img", { "src": "example.com" }], | ||
["img", { "src": "google.com" }] | ||
] | ||
], | ||
[ | ||
"footer", | ||
{ "class": "foot" }, | ||
[ | ||
[ | ||
"a", | ||
{ "href": "foobar.com" }, | ||
["p", { "class": "hoge" }, "some text"] | ||
], | ||
["i", { "class": "ico", "x11": "xorg" }] | ||
] | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
[ | ||
"div", | ||
{ "class": "wrapper" }, | ||
"foo" | ||
] | ||
] |