Skip to content

Commit 7d84175

Browse files
authored
Merge pull request #38 from tylerjpeterson/master
Custom HTML templates
2 parents 9582c19 + 59470c9 commit 7d84175

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Diff to Html generates pretty HTML diffs from unified and git diff output in you
6363
--lm, --matching Diff line matching type [choices: "lines", "words", "none"] [default: "none"]
6464
--lmt, --matchWordsThreshold Diff line matching word threshold [default: "0.25"]
6565
--lmm, --matchingMaxComparisons Diff line matching maximum line comparisons of a block of changes [default: 2500]
66+
--hwt, --htmlWrapperTemplate Path to custom template to be rendered when using the "html" output format [string]
6667
-f, --format Output format [choices: "html", "json"] [default: "html"]
6768
-d, --diff Diff style [choices: "word", "char"] [default: "word"]
6869
-i, --input Diff input source [choices: "file", "command", "stdin"] [default: "command"]
@@ -84,6 +85,14 @@ Diff to Html generates pretty HTML diffs from unified and git diff output in you
8485
-> print json format to stdout
8586
diff2html -F my-pretty-diff.html -- -M HEAD~1
8687
-> print to file
88+
diff2html -F my-pretty-diff.html --hwt my-custom-template.html -- -M HEAD~1
89+
-> print to file using custom markup
90+
templates can include the following variables:
91+
`<!--diff2html-css-->` - writes default CSS to page
92+
`<!--diff2html-js-ui-->` - writes default JavaScript UI scripts to page
93+
`//diff2html-fileListCloseable` - writes code to support selected list interaction, must be within a <script> block
94+
`//diff2html-synchronisedScroll` - writes code to support selected scroll interaction, must be within a <script> block
95+
`<!--diff2html-diff-->` - writes diff content to page
8796

8897
© 2014-2016 rtfpessoa
8998
For support, check out https://github.com/rtfpessoa/diff2html-cli

src/cli.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
(function() {
9+
var fs = require('fs');
910
var os = require('os');
1011
var path = require('path');
1112

@@ -66,8 +67,14 @@
6667
Diff2HtmlInterface.prototype.getOutput = function(baseConfig, input, callback) {
6768
var that = this;
6869
var config = baseConfig;
70+
var defaultTemplate = path.resolve(__dirname, '..', 'dist', 'template.html');
6971
config.wordByWord = (baseConfig.diff === 'word');
7072
config.charByChar = (baseConfig.diff === 'char');
73+
config.template = baseConfig.htmlWrapperTemplate || defaultTemplate;
74+
75+
if (!fs.existsSync(config.template)) {
76+
return callback(new Error('Template (`' + baseConfig.template + '`) not found!'));
77+
}
7178

7279
var jsonContent = diff2Html.getJsonFromDiff(input, config);
7380

@@ -99,7 +106,7 @@
99106
};
100107

101108
Diff2HtmlInterface.prototype._prepareHTML = function(content, config) {
102-
var templatePath = path.resolve(__dirname, '..', 'dist', 'template.html');
109+
var templatePath = config.template;
103110
var template = utils.readFileSync(templatePath);
104111

105112
var diff2htmlPath = path.join(path.dirname(require.resolve('diff2html')), '..');

src/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ var argv = yargs.usage('Usage: diff2html [options] -- [diff args]')
129129
type: 'string'
130130
}
131131
})
132+
.options({
133+
'hwt': {
134+
alias: 'htmlWrapperTemplate',
135+
describe: 'Use a custom template when generating markup',
136+
nargs: 1,
137+
type: 'string'
138+
}
139+
})
132140
.example('diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1',
133141
'diff last commit, line by line, word comparison between lines,' +
134142
'previewed in the browser and input from git diff command')

0 commit comments

Comments
 (0)