Skip to content

Commit 89eb93f

Browse files
committed
chore(tools): add syntax highlighting logic for examples
1 parent 4b830d3 commit 89eb93f

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/tmp
66
/deploy
77
/screenshots
8+
/src/examples-highlights
89

910
# dependencies
1011
/node_modules
@@ -33,4 +34,4 @@
3334
/libpeerconnection.log
3435
npm-debug.log
3536
testem.log
36-
/.chrome
37+
/.chrome
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
examplesSourcePath=./src/examples-highlights
3+
baseExamplesFolder=./src/examples
4+
highlightTool=./tools/code-highlight/syntax-highlight.js
5+
6+
if [ ! -d $examplesSourcePath ];
7+
then
8+
mkdir $examplesSourcePath
9+
fi
10+
11+
find $baseExamplesFolder -mindepth 2 -type f \( -name "*.css" -or -iname "*.ts" -or -iname "*.html" \) | xargs -P 30 -I {} $highlightTool {} $examplesSourcePath
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
/**
5+
* Creates a syntax highlighted HTML file from a source file using highlight.js.
6+
*
7+
* Use:
8+
* syntax-highlight path/to/input/file path/to/output/dir/
9+
*/
10+
11+
const fs = require('fs');
12+
const path = require('path');
13+
const hljs = require('highlight.js');
14+
15+
const inputFile = process.argv[2];
16+
const outputPath = process.argv[3];
17+
18+
let extension = path.extname(inputFile).toLowerCase().slice(1);
19+
let language = extension;
20+
21+
// Highlight.js expects 'typescript' written out instead of 'ts'.
22+
if (language == 'ts') {
23+
language = 'typescript';
24+
}
25+
26+
27+
fs.readFile(inputFile, 'utf8', (error, content) => {
28+
if (error) {
29+
console.error(`Could not read file ${inputFile}`);
30+
exit(1);
31+
}
32+
33+
let highlighted = hljs.highlight(language, content);
34+
35+
let filename = path.basename(inputFile);
36+
filename = filename.slice(0, filename.lastIndexOf('.')) + '-' + extension + '.html';
37+
let outputFile = path.join(outputPath, filename);
38+
39+
fs.writeFile(outputFile, highlighted.value, {encoding: 'utf8'});
40+
});

tools/gulp/tasks/docs.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import gulp = require('gulp');
22
const markdown = require('gulp-markdown');
33
const transform = require('gulp-transform');
44
const hljs = require('highlight.js');
5+
const exec = require('child_process').exec;
56
import {task} from 'gulp';
67
import * as path from 'path';
78

@@ -16,7 +17,7 @@ const EXAMPLE_PATTERN = /<!--\W*example\(([^)]+)\)\W*-->/g;
1617
// documentation page. Using a RegExp to rewrite links in HTML files to work in the docs.
1718
const LINK_PATTERN = /(<a[^>]*) href="([^"]*)"/g;
1819

19-
gulp.task('docs', ['markdown-docs', 'api-docs'])
20+
gulp.task('docs', ['markdown-docs', 'highlight-docs', 'api-docs'])
2021

2122
gulp.task('markdown-docs', () => {
2223
return gulp.src(['src/lib/**/*.md', 'guides/*.md'])
@@ -36,6 +37,11 @@ gulp.task('markdown-docs', () => {
3637
.pipe(gulp.dest('dist/docs/markdown'));
3738
});
3839

40+
gulp.task('highlight-docs', () => {
41+
exec('tools/code-highlight/highlight-examples.sh');
42+
console.log('Highlighting example sources. This may take 15 - 20 seconds...');
43+
});
44+
3945
task('api-docs', () => {
4046
const Dgeni = require('dgeni');
4147
const docsPackage = require(path.resolve(__dirname, '../../dgeni'));

0 commit comments

Comments
 (0)