-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdebug_html.js
More file actions
62 lines (53 loc) · 2.52 KB
/
Copy pathdebug_html.js
File metadata and controls
62 lines (53 loc) · 2.52 KB
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
const fs = require('fs');
const path = require('path');
const { JSDOM } = require('jsdom');
const HOMEPAGE_DIR = '/workspace/homepage';
const PARTIALS_DIR = path.join(HOMEPAGE_DIR, 'templates/partials');
function preprocessTemplate(html) {
return html
.replace(/\{\{\s*get_url\(path=['"]([^'"]+)['"]\)\s*\}\}/g, (match, p1) => '/' + p1)
.replace(/\{%\s*include\s+["']([^"']+)["']\s*%\}/g, '')
.replace(/\{%\s*block\s+\w+\s*%\}/g, '')
.replace(/\{%\s*endblock\s*%\}/g, '');
}
const basePath = path.join(HOMEPAGE_DIR, 'templates/base.html');
const indexPath = path.join(HOMEPAGE_DIR, 'templates/index.html');
let html = fs.readFileSync(basePath, 'utf-8');
const indexHtml = fs.readFileSync(indexPath, 'utf-8');
const blockRegex = /\{%\s*block\s+(\w+)\s*%\}([\s\S]*?)\{%\s*endblock(?:\s+\w+)?\s*%\}/g;
let blockMatch;
while ((blockMatch = blockRegex.exec(indexHtml)) !== null) {
const blockName = blockMatch[1];
const blockContent = blockMatch[2];
console.log('Replacing block:', blockName, 'with content length:', blockContent.length);
const baseBlockPattern = new RegExp('\\{%\\s*block\\s+' + blockName + '\\s*%\\}([\\s\\S]*?)\\{%\\s*endblock(?:\\s+\\w+)?\\s*%\\}');
html = html.replace(baseBlockPattern, blockContent);
}
console.log('After block replace, includes remaining:');
const includes = [...html.matchAll(/\{%\s*include\s+["']([^"']+)["']\s*%\}/g)];
includes.forEach(m => console.log(' ', m[1]));
let safety = 0;
while (safety++ < 20) {
const matches = [...html.matchAll(/\{%\s*include\s+["']([^"']+)["']\s*%\}/g)];
if (matches.length === 0) break;
for (const m of matches) {
const includePath = m[1];
const partialFile = path.basename(includePath).replace('.html', '') + '.html';
const partialPath = path.join(PARTIALS_DIR, partialFile);
let partialContent = '';
if (fs.existsSync(partialPath)) {
partialContent = fs.readFileSync(partialPath, 'utf-8');
}
html = html.replace(m[0], partialContent);
}
}
html = html.replace(/\{\{\s*get_url\(path=['"]([^'"]+)['"]\)\s*\}\}/g, (m, p1) => '/' + p1);
const dom = new JSDOM(preprocessTemplate(html));
const doc = dom.window.document;
console.log('\nParsed results:');
console.log('Articles:', doc.querySelectorAll('article').length);
console.log('Sections:', doc.querySelectorAll('section').length);
console.log('Headers:', doc.querySelectorAll('header').length);
console.log('Navs:', doc.querySelectorAll('nav').length);
console.log('Footers:', doc.querySelectorAll('footer').length);
console.log('Main:', doc.querySelectorAll('main').length);