initial impl with wontache
This commit is contained in:
parent
26adbdb220
commit
60e7a2e68c
7 changed files with 367 additions and 327 deletions
|
|
@ -82,14 +82,13 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"diff": "5.1.0",
|
"diff": "5.1.0",
|
||||||
"hogan.js": "3.0.2"
|
"wontache": "0.1.0"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
"highlight.js": "11.6.0"
|
"highlight.js": "11.6.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/diff": "5.0.2",
|
"@types/diff": "5.0.2",
|
||||||
"@types/hogan.js": "3.0.1",
|
|
||||||
"@types/jest": "28.1.6",
|
"@types/jest": "28.1.6",
|
||||||
"@types/mkdirp": "1.0.2",
|
"@types/mkdirp": "1.0.2",
|
||||||
"@types/node": "18.6.0",
|
"@types/node": "18.6.0",
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
||||||
|
/// <reference path="../typings/wontache/wontache.d.ts" />
|
||||||
/*
|
/*
|
||||||
* Copyright 2011 Twitter, Inc.
|
* Copyright 2011 Twitter, Inc.
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
|
@ -16,7 +18,7 @@
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
|
||||||
import * as hogan from 'hogan.js';
|
import mustache from 'wontache';
|
||||||
import nopt from 'nopt';
|
import nopt from 'nopt';
|
||||||
import * as mkderp from 'mkdirp';
|
import * as mkderp from 'mkdirp';
|
||||||
|
|
||||||
|
|
@ -107,25 +109,20 @@ function removeByteOrderMark(text: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap templates
|
// Wrap templates
|
||||||
function wrap(file: string, name: string, openedFile: string): string {
|
function wrap(name: string, openedFile: string): string {
|
||||||
const hoganTemplateString = `new Hogan.Template(${hogan.compile(openedFile, { asString: true })})`;
|
const templateString = mustache(openedFile).source;
|
||||||
|
|
||||||
const objectName = options.variable || 'templates';
|
const objectName = options.variable || 'templates';
|
||||||
const objectAccessor = `${objectName}["${name}"]`;
|
const objectAccessor = `${objectName}["${name}"]`;
|
||||||
const objectStmt = `${objectAccessor} = ${hoganTemplateString};`;
|
const objectStmt = `${objectAccessor} = ${templateString};`;
|
||||||
|
|
||||||
switch (options.wrapper) {
|
switch (options.wrapper) {
|
||||||
case 'amd':
|
|
||||||
return `define(${
|
|
||||||
!options.outputdir ? `"${path.join(path.dirname(file), name)}", ` : ''
|
|
||||||
}["hogan.js"], function(Hogan) { return ${hoganTemplateString}; });`;
|
|
||||||
|
|
||||||
case 'node':
|
case 'node':
|
||||||
// If we have a template per file the export will expose the template directly
|
// If we have a template per file the export will expose the template directly
|
||||||
return options.outputdir ? `global.${objectStmt};\nmodule.exports = ${objectAccessor};` : `global.${objectStmt}`;
|
return options.outputdir ? `global.${objectStmt};\nmodule.exports = ${objectAccessor};` : `global.${objectStmt}`;
|
||||||
|
|
||||||
case 'ts':
|
case 'ts':
|
||||||
return `// @ts-ignore\n${objectStmt}`;
|
return `// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\n${objectStmt}`;
|
||||||
default:
|
default:
|
||||||
return objectStmt;
|
return objectStmt;
|
||||||
}
|
}
|
||||||
|
|
@ -137,16 +134,18 @@ function prepareOutput(content: string): string {
|
||||||
case 'amd':
|
case 'amd':
|
||||||
return content;
|
return content;
|
||||||
case 'node':
|
case 'node':
|
||||||
return `(function() {
|
return `const mustache = require('wontache');
|
||||||
|
(function() {
|
||||||
if (!!!global.${variableName}) global.${variableName} = {};
|
if (!!!global.${variableName}) global.${variableName} = {};
|
||||||
var Hogan = require("hogan.js");
|
|
||||||
${content}
|
${content}
|
||||||
${!options.outputdir ? `module.exports = global.${variableName};\n` : ''})();`;
|
${!options.outputdir ? `module.exports = global.${variableName};\n` : ''})();`;
|
||||||
|
|
||||||
case 'ts':
|
case 'ts':
|
||||||
return `import * as Hogan from "hogan.js";
|
return `/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||||
type CompiledTemplates = { [name: string]: Hogan.Template };
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
export const ${variableName}: CompiledTemplates = {};
|
// @ts-nocheck
|
||||||
|
import mustache, { CompiledTemplate } from 'wontache';
|
||||||
|
export const defaultTemplates: { [_: string]: CompiledTemplate } = {};
|
||||||
${content}`;
|
${content}`;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -172,7 +171,7 @@ const templates = extractFiles(options.argv.remain)
|
||||||
if (!timmedFileContents) return;
|
if (!timmedFileContents) return;
|
||||||
|
|
||||||
const name = namespace(path.basename(file).replace(/\..*$/, ''));
|
const name = namespace(path.basename(file).replace(/\..*$/, ''));
|
||||||
const cleanFileContents = wrap(file, name, removeByteOrderMark(timmedFileContents));
|
const cleanFileContents = wrap(name, removeByteOrderMark(timmedFileContents));
|
||||||
|
|
||||||
if (!options.outputdir) return cleanFileContents;
|
if (!options.outputdir) return cleanFileContents;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import * as Hogan from 'hogan.js';
|
import mustache, { CompiledTemplate, Partials } from 'wontache';
|
||||||
|
|
||||||
import { defaultTemplates } from './diff2html-templates';
|
import { defaultTemplates } from './diff2html-templates';
|
||||||
|
|
||||||
|
|
@ -7,7 +7,7 @@ export interface RawTemplates {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CompiledTemplates {
|
export interface CompiledTemplates {
|
||||||
[name: string]: Hogan.Template;
|
[name: string]: CompiledTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HoganJsUtilsConfig {
|
export interface HoganJsUtilsConfig {
|
||||||
|
|
@ -21,7 +21,7 @@ export default class HoganJsUtils {
|
||||||
constructor({ compiledTemplates = {}, rawTemplates = {} }: HoganJsUtilsConfig) {
|
constructor({ compiledTemplates = {}, rawTemplates = {} }: HoganJsUtilsConfig) {
|
||||||
const compiledRawTemplates = Object.entries(rawTemplates).reduce<CompiledTemplates>(
|
const compiledRawTemplates = Object.entries(rawTemplates).reduce<CompiledTemplates>(
|
||||||
(previousTemplates, [name, templateString]) => {
|
(previousTemplates, [name, templateString]) => {
|
||||||
const compiledTemplate: Hogan.Template = Hogan.compile(templateString, { asString: false });
|
const compiledTemplate: CompiledTemplate = mustache(templateString);
|
||||||
return { ...previousTemplates, [name]: compiledTemplate };
|
return { ...previousTemplates, [name]: compiledTemplate };
|
||||||
},
|
},
|
||||||
{},
|
{},
|
||||||
|
|
@ -30,21 +30,21 @@ export default class HoganJsUtils {
|
||||||
this.preCompiledTemplates = { ...defaultTemplates, ...compiledTemplates, ...compiledRawTemplates };
|
this.preCompiledTemplates = { ...defaultTemplates, ...compiledTemplates, ...compiledRawTemplates };
|
||||||
}
|
}
|
||||||
|
|
||||||
static compile(templateString: string): Hogan.Template {
|
static compile(templateString: string): CompiledTemplate {
|
||||||
return Hogan.compile(templateString, { asString: false });
|
return mustache(templateString);
|
||||||
}
|
}
|
||||||
|
|
||||||
render(namespace: string, view: string, params: Hogan.Context, partials?: Hogan.Partials, indent?: string): string {
|
render(namespace: string, view: string, params: object, partials?: Partials): string {
|
||||||
const templateKey = this.templateKey(namespace, view);
|
const templateKey = this.templateKey(namespace, view);
|
||||||
try {
|
try {
|
||||||
const template = this.preCompiledTemplates[templateKey];
|
const template = this.preCompiledTemplates[templateKey];
|
||||||
return template.render(params, partials, indent);
|
return template(params, { partials });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(`Could not find template to render '${templateKey}'`);
|
throw new Error(`Could not find template to render '${templateKey}'`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template(namespace: string, view: string): Hogan.Template {
|
template(namespace: string, view: string): CompiledTemplate {
|
||||||
return this.preCompiledTemplates[this.templateKey(namespace, view)];
|
return this.preCompiledTemplates[this.templateKey(namespace, view)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,17 +63,19 @@ export default class LineByLineRenderer {
|
||||||
const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file');
|
const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file');
|
||||||
const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file));
|
const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file));
|
||||||
|
|
||||||
return fileDiffTemplate.render({
|
return fileDiffTemplate({
|
||||||
file: file,
|
file: file,
|
||||||
fileHtmlId: renderUtils.getHtmlId(file),
|
fileHtmlId: renderUtils.getHtmlId(file),
|
||||||
diffs: diffs,
|
diffs: diffs,
|
||||||
filePath: filePathTemplate.render(
|
filePath: filePathTemplate(
|
||||||
{
|
{
|
||||||
fileDiffName: renderUtils.filenameDiff(file),
|
fileDiffName: renderUtils.filenameDiff(file),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fileIcon: fileIconTemplate,
|
partials: {
|
||||||
fileTag: fileTagTemplate,
|
fileIcon: fileIconTemplate,
|
||||||
|
fileTag: fileTagTemplate,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -63,17 +63,19 @@ export default class SideBySideRenderer {
|
||||||
const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file');
|
const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, 'file');
|
||||||
const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file));
|
const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file));
|
||||||
|
|
||||||
return fileDiffTemplate.render({
|
return fileDiffTemplate({
|
||||||
file: file,
|
file: file,
|
||||||
fileHtmlId: renderUtils.getHtmlId(file),
|
fileHtmlId: renderUtils.getHtmlId(file),
|
||||||
diffs: diffs,
|
diffs: diffs,
|
||||||
filePath: filePathTemplate.render(
|
filePath: filePathTemplate(
|
||||||
{
|
{
|
||||||
fileDiffName: renderUtils.filenameDiff(file),
|
fileDiffName: renderUtils.filenameDiff(file),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fileIcon: fileIconTemplate,
|
partials: {
|
||||||
fileTag: fileTagTemplate,
|
fileIcon: fileIconTemplate,
|
||||||
|
fileTag: fileTagTemplate,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
13
typings/wontache/wontache.d.ts
vendored
Normal file
13
typings/wontache/wontache.d.ts
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
declare module 'wontache' {
|
||||||
|
export default function compile(template: string | object): CompiledTemplate;
|
||||||
|
type Partials = {
|
||||||
|
[_: string]: string | object;
|
||||||
|
};
|
||||||
|
type Options = {
|
||||||
|
partials?: Partials;
|
||||||
|
};
|
||||||
|
interface CompiledTemplate {
|
||||||
|
(data: object, opt?: Options): string;
|
||||||
|
source: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue