-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-template.js
161 lines (123 loc) · 3.9 KB
/
reverse-template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {parse} from 'parse5'
import {fromParse5} from 'hast-util-from-parse5'
//import glob from 'tiny-glob'
import {visit} from 'unist-util-visit'
import {toHtml} from 'hast-util-to-html'
import diff from 'unist-diff'
import fs from 'fs'
import reverseStringTemplate from './reverse-string-template/index.js'
import Underscore from 'underscore'
// TODO
//const pathGlob = "path/to/input/dir/**/*.html"
const d = "path/to/input/dir"
const leftFile = `${d}/path/to/original-left.html`
const rightFile = `${d}/path/to/original-right.html`
Underscore.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
function getAst(source) {
const p5ast = parse(source, {
sourceCodeLocationInfo: true,
})
const hast = fromParse5(p5ast)
return hast
}
function normalizeHtml(source) {
return toHtml(getAst(source))
}
async function main() {
let leftSource, rightSource, expectedTemplate
var isTest = false
var isTest = true
if (isTest) {
// generate test sources
expectedTemplate = `
<html>
<head>
<title>{{data[0]}}</title>
</head>
<body>
<section>
<h1>{{data[1]}}</h1>
<div>{{data[2]}}</div>
</section>
</body>
</html>
`
const dataLength = 3
function data(side, length) {
length = dataLength
return {
// left data 0, left data 1, ...
data: Array.from({length}).map((_, idx) => `${side} data ${idx}`)
}
}
const render = Underscore.template(expectedTemplate);
leftSource = render(data("left"))
rightSource = render(data("right"))
}
else {
// use real source files
leftSource = fs.readFileSync(leftFile, "utf8")
rightSource = fs.readFileSync(rightFile, "utf8")
}
const left = getAst(leftSource)
const right = getAst(rightSource)
//console.dir({leftSource, rightSource}, {depth: 3})
const changes = diff(left, right)
//delete changes.left; console.dir(changes, {depth: 3})
//console.log("left"); console.dir(left, {depth: 8})
//console.log("replace")
// replace
let nodeIndex = 0
let dataIndex = -1
const leftData = []
function visitor(node, _index, _parent) {
// note: nodeIndex != index
nodeIndex++
//console.dir({nodeIndex, node}, {depth: 5})
if (nodeIndex in changes) {
const change = changes[nodeIndex]
if (change.type == "text") {
//console.log("replace node.children")
//console.dir({nodeIndex, node}, {depth: 5})
dataIndex++
leftData.push(toHtml({type: "root", children: node.children}))
node.children = [
{
type: "text",
value: `{{data[${dataIndex}]}}` // {{data[0]}}
//value: `{{data_${dataIndex}}}` // {{data_0}}
}
]
}
}
}
visit(left, visitor)
const template = toHtml(left)
if (isTest) {
console.log(`expected template:\n${normalizeHtml(expectedTemplate)}\n`)
}
console.log(`template:\n${template}\n`)
console.dir({leftData})
console.dir({leftData_length: leftData.length})
// parse rightData
// https://github.com/mbrevoort/reverse-string-template
const rightData = reverseStringTemplate(normalizeHtml(rightSource), template)
// https://github.com/blerik/razor.js
// TypeError: Cannot read properties of undefined (reading 'data_0')
//const rightData = razor.getModel(template, normalizeHtml(rightSource));
console.dir({rightData})
if (isTest == false) {
console.log(`writing template.html`)
fs.writeFileSync("template.html", template, "utf8")
console.log(`writing left.html`)
fs.writeFileSync("left.html", normalizeHtml(leftSource), "utf8")
console.log(`writing right.html`)
fs.writeFileSync("right.html", normalizeHtml(rightSource), "utf8")
console.log(`compare:`)
console.log(`git diff --color-words=. template.html left.html`)
console.log(`git diff --color-words=. template.html right.html`)
}
}
main()