Skip to content

Commit

Permalink
fix: 343-handlebars
Browse files Browse the repository at this point in the history
Feature/343-handlebars
  • Loading branch information
PeterWone authored Sep 17, 2024
2 parents 6606045 + 1676992 commit 1901818
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 83 deletions.
103 changes: 97 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@
"description": "%print.folder.include.description%"
},
"print.includePaths": {
"default": ["."],
"default": [
"."
],
"type": "array",
"items": {
"type": "string"
Expand Down Expand Up @@ -637,6 +639,7 @@
"axios": "^1.7.2",
"braces": "^3.0.3",
"cors": "^2.8.5",
"handlebars": "^4.7.8",
"highlight.js": "^11.9.0",
"katex": "^0.16.10",
"lodash": "^4.17.21",
Expand Down Expand Up @@ -674,6 +677,7 @@
"css-loader": "^6.10.0",
"eslint": "^8.56.0",
"file-loader": "^6.2.0",
"handlebars-loader": "^1.7.3",
"markdown-it": "^14.0.0",
"marked": "^12.0.1",
"mermaid": "^10.9.1",
Expand All @@ -691,4 +695,4 @@
"publisherId": "a803a703-65fb-4fa9-955a-c9259bf2560d",
"isPreReleaseVersion": false
}
}
}
131 changes: 72 additions & 59 deletions src/renderers/html-document-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import micromatch from 'micromatch';
import tildify from '../tildify';
import { PrintSession } from '../print-session';
import { ResourceProxy } from './resource-proxy';
import Handlebars from "handlebars";

const templateFolderItem = require("../templates/folder-item.html").default.toString();
const templateDocument: string = require("../templates/document.html").default.toString();

const hbMultiDocument = Handlebars.compile(require("../templates/multi-document.html").default.toString());
const hbFolderItem = Handlebars.compile(require("../templates/multi-document-item.html").default.toString());
const hbDocument = Handlebars.compile(require("../templates/document.html").default.toString());
const multifileCssRefs =
`
<link href="bundled/default.css" rel="stylesheet" />
<link href="bundled/line-numbers.css" rel="stylesheet" />
<link href="bundled/colour-scheme.css" rel="stylesheet" />
<link href="bundled/settings.css" rel="stylesheet" />
`;
export class HtmlDocumentBuilder {
private filepath: string;
constructor(
Expand All @@ -35,29 +43,28 @@ export class HtmlDocumentBuilder {
const docs = await this.docsInMultiselection();
const summary =
`<h3>${docs.length} printable files</h3><pre>${docs.map(d => printConfig.filepathAsDocumentHeading === "Relative" ? this.workspacePath(d.uri) : tildify(d.fileName)).join("\n")}</pre>\r`;
let composite = "";
for (let doc of docs) {
const folderItems = await Promise.all(docs.map(async (doc) => {
const renderer = DocumentRenderer.get(doc.languageId);
const bodyText = doc.getText();
const langId = doc.languageId;
const options = { startLine: 1, lineNumbers: this.printLineNumbers, uri: this.uri };
const bodyHtml = await renderer.getBodyHtml(this.generatedResources, bodyText, langId, options);
composite += templateFolderItem
.replace("VSCODE_PRINT_FOLDER_ITEM_TITLE", printConfig.filepathAsDocumentHeading === "Relative" ? this.workspacePath(doc.uri) : tildify(doc.fileName))
.replace("VSCODE_PRINT_FOLDER_ITEM_CONTENT", () => `<table class="hljs">\n${bodyHtml}\n</table>\n`)
}
return templateDocument
.replace("VSCODE_PRINT_BASE_URL", this.baseUrl)
.replace(/VSCODE_PRINT_DOCUMENT_(?:TITLE|HEADING)/g, "<h2>Selected files</h2>")
.replace("VSCODE_PRINT_PRINT_AND_CLOSE", printAndClose)
.replace("VSCODE_PRINT_CONTENT", () => `${summary}\n${composite}`) // replacer fn suppresses interpretation of $
.replace("VSCODE_PRINT_SCRIPT_TAGS", "")
.replace("VSCODE_PRINT_STYLESHEET_LINKS",
'<link href="bundled/default.css" rel="stylesheet" />\n' +
'\t<link href="bundled/line-numbers.css" rel="stylesheet" />\n' +
'\t<link href="bundled/colour-scheme.css" rel="stylesheet" />\n' +
'\t<link href="bundled/settings.css" rel = "stylesheet" /> ')
;
const docHtml = hbFolderItem({
multiDocumentItemTitle: printConfig.filepathAsDocumentHeading === "Relative" ? this.workspacePath(doc.uri) : tildify(doc.fileName),
multiDocumentItemContent: `<table class="hljs">\n${bodyHtml}\n</table>\n`
});
return docHtml;
}));
return hbMultiDocument({
baseUrl: this.baseUrl,
documentTitle: "Selected files",
documentHeading: "Selected files",
printAndClose: !this.isPreview,
summary: summary,
items: folderItems,
stylesheetLinks: multifileCssRefs,
scriptTags: ""
});
} else if (this.language === "folder") {
logger.debug(`Folder ${this.workspacePath(this.uri)}`);
this.filepath = this.uri.fsPath;
Expand All @@ -66,39 +73,43 @@ export class HtmlDocumentBuilder {
`<h3>${docs.length} printable files</h3><pre>${docs.map(d => printConfig.filepathAsDocumentHeading === "Relative" ? this.workspacePath(d.uri) : tildify(d.fileName)).join("\n")}</pre>` :
`<h3>${docs.length} printable files</h3><p>(file list disabled)</p>`;

let composite: string;
if (docs.length > printConfig.folder.maxFiles) {
const msgTooManyFiles = composite =
const msgTooManyFiles =
vscode.l10n.t("The selected directory contains too many files to print them all. Only the summary will be printed.");
vscode.window.showWarningMessage(msgTooManyFiles);
return hbMultiDocument({
baseUrl: this.baseUrl,
documentTitle: this.workspacePath(this.uri),
documentHeading: `Folder ${this.workspacePath(this.uri)}`,
printAndClose: !this.isPreview,
summary: summary,
items: [],
stylesheetLinks: multifileCssRefs,
scriptTags: ""
});
}
else {
composite = "";
for (let doc of docs) {
const renderer = DocumentRenderer.get(doc.languageId);
const bodyText = doc.getText();
const langId = doc.languageId;
const options = { startLine: 1, lineNumbers: this.printLineNumbers, uri: this.uri };
const bodyHtml = await renderer.getBodyHtml(this.generatedResources, bodyText, langId, options);
composite += templateFolderItem
.replace("VSCODE_PRINT_FOLDER_ITEM_TITLE", printConfig.filepathAsDocumentHeading === "Relative" ? this.workspacePath(doc.uri) : tildify(doc.fileName))
.replace("VSCODE_PRINT_FOLDER_ITEM_CONTENT", `<table class="hljs">\n${bodyHtml}\n</table>\n`);
}
}
return templateDocument
.replace("VSCODE_PRINT_BASE_URL", this.baseUrl)
.replace(/VSCODE_PRINT_DOCUMENT_TITLE/g, this.workspacePath(this.uri))
.replace(/VSCODE_PRINT_DOCUMENT_HEADING/g, `<h2>Folder ${this.workspacePath(this.uri)}</h2>`)
.replace("VSCODE_PRINT_PRINT_AND_CLOSE", printAndClose)
.replace("VSCODE_PRINT_CONTENT", () => `${summary}\n${composite}`) // replacer fn suppresses interpretation of $
.replace("VSCODE_PRINT_SCRIPT_TAGS", "")
.replace("VSCODE_PRINT_STYLESHEET_LINKS",
'<link href="bundled/default.css" rel="stylesheet" />\n' +
'\t<link href="bundled/line-numbers.css" rel="stylesheet" />\n' +
'\t<link href="bundled/colour-scheme.css" rel="stylesheet" />\n' +
'\t<link href="bundled/settings.css" rel = "stylesheet" /> ')
;
} else {
const multiDocumentItems = await Promise.all(docs.map(async (doc) => {
const renderer = DocumentRenderer.get(doc.languageId);
const bodyText = doc.getText();
const langId = doc.languageId;
const options = { startLine: 1, lineNumbers: this.printLineNumbers, uri: this.uri };
const bodyHtml = await renderer.getBodyHtml(this.generatedResources, bodyText, langId, options);
return hbFolderItem({
multiDocumentItemTitle: printConfig.filepathAsDocumentHeading === "Relative" ? this.workspacePath(doc.uri) : tildify(doc.fileName),
multiDocumentItemContent: `<table class="hljs">\n${bodyHtml}\n</table>\n`
});
}));
return hbMultiDocument({
baseUrl: this.baseUrl,
documentTitle: this.workspacePath(this.uri),
documentHeading: `Folder ${this.workspacePath(this.uri)}`,
printAndClose: printConfig.printAndClose,
summary: summary,
items: multiDocumentItems,
stylesheetLinks: multifileCssRefs,
scriptTags: ""
});
} else { // one file
logger.debug(`Printing ${this.filepath}`);
let docHeading = "";
if (printConfig.filepathHeadingForIndividuallyPrintedDocuments) {
Expand Down Expand Up @@ -133,15 +144,17 @@ export class HtmlDocumentBuilder {
const bodyHtml = await documentRenderer.getBodyHtml(this.generatedResources, this.code, this.language, options);
const cssLinks = documentRenderer.getCssLinks(this.uri);
const scriptTags = documentRenderer.getScriptTags(this.uri);
return templateDocument
.replace("VSCODE_PRINT_BASE_URL", this.baseUrl)
.replace(/VSCODE_PRINT_DOCUMENT_TITLE/g, documentRenderer.getTitle(this.uri))
.replace(/VSCODE_PRINT_DOCUMENT_HEADING/g, thePath)
.replace("VSCODE_PRINT_PRINT_AND_CLOSE", printAndClose)
.replace("VSCODE_PRINT_CONTENT", bodyHtml)
.replace("VSCODE_PRINT_STYLESHEET_LINKS", cssLinks)
.replace("VSCODE_PRINT_SCRIPT_TAGS", scriptTags)
;
const documentTitle = documentRenderer.getTitle(this.uri);
let doc = hbDocument({
baseUrl: this.baseUrl,
documentTitle: documentTitle,
documentHeading: thePath,
printAndClose: printAndClose,
content: bodyHtml,
stylesheetLinks: cssLinks,
scriptTags: scriptTags
});
return doc;
}
}
async docsInMultiselection() {
Expand Down
Loading

0 comments on commit 1901818

Please sign in to comment.