Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ PrintCode converts the code being edited into an HTML file, displays it by brows
| fontSize | 12 | Controls the font size in pixels |
| paperSize | a4 | Paper size and orientation |
| lineNumbers | on | Print line numbers |
| useTrueLineNumbers | true | When printing a region, don't start from 1 |
| skipBeforeTag | PRINTCODE_SKIPBEFORE | Don't print anything before this string |
| skipAfterTag | PRINTCODE_SKIPAFTER | Don't print anything after this string |
| printFilePath | filename | Amount of file's path info in document title |
| browserPath | none | Open with your non-default browser |
| webServerPort | 4649 | Port number for local WebServer. |
Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
],
"default": "on"
},
"printcode.useTrueLineNumbers": {
"type": "boolean",
"description": "If printing partial file, don't start count from 1.",
"default": true
},
"printcode.skipBeforeTag": {
"type": "string",
"description": "Tag to separate non-printing part -- skip lines before this.",
"default": "PRINTCODE_SKIPBEFORE"
},
"printcode.skipAfterTag": {
"type": "string",
"description": "Tag to separate non-printing part -- skip lines after this.",
"default": "PRINTCODE_SKIPAFTER"
},
"printcode.printFilePath": {
"type": "string",
"description": "Amount of file's path info in document title.",
Expand Down
31 changes: 29 additions & 2 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require("codemirror/mode/meta.js");

let server = null;
let portNumberInUse = null;
let firstLineNumber = 1;

function activate(context) {
let disposable = vscode.commands.registerCommand(
Expand Down Expand Up @@ -121,14 +122,39 @@ const requestHandler = (request, response) => {
} else {
response.end("");
}
};
}; // PRINTCODE_SKIPBEFORE // example tag -- anything before that will not be printed

function getHtml(editor) {
let myConfig = vscode.workspace.getConfiguration("printcode", null);
let useTrueLineNumbers = myConfig.get("useTrueLineNumbers");
let skipBeforeTag = myConfig.get("skipBeforeTag");
let skipAfterTag = myConfig.get("skipAfterTag");

let language = editor.document.languageId;
let text = editor.document.getText();

let skipBeforeMatch = new RegExp(skipBeforeTag);
if (text.match(skipBeforeMatch)) {
let actualLines = (text.match(/\r?\n/g) || '').length + 1;
let replaceBeforeMatch = new RegExp(`^[\\s\\S]+${skipBeforeTag}\\S*\\s*`, "m");
text = text.replace(replaceBeforeMatch, "");
let printingLines = (text.match(/\r?\n/g) || '').length + 1;
if (useTrueLineNumbers) {
firstLineNumber = actualLines - printingLines + 1;
} else {
firstLineNumber = 1;
}
}
let skipAfterMatch = new RegExp(skipAfterTag);
if (text.match(skipAfterMatch)) {
// matching several variations of comments from different languages
let replaceAfterMatch = new RegExp(`\\s*\\S*[/*#/'%!-]*\\s*${skipAfterTag}[\\s\\S]+`, "m");
text = text.replace(replaceAfterMatch, "");
}

let html = buildHtml(text, language);
return html;
}
} // PRINTCODE_SKIPAFTER example tag -- anything after that will not be printed

function buildHtml(text, language) {
var body = text.EscapeForJSON();
Expand Down Expand Up @@ -346,6 +372,7 @@ function buildHtml(text, language) {
var cm = CodeMirror(document.getElementById("code"), {
value: "",
lineNumbers: ${lineNumbering},
firstLineNumber: ${firstLineNumber},
lineWrapping: true,
tabSize: ${tabSize},
readOnly: true,
Expand Down