|
| 1 | +/** |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict' |
| 18 | + |
| 19 | +const Hogan = require('hogan.js'); |
| 20 | +const fs = require('fs'); |
| 21 | +const glob = require('glob'); |
| 22 | +const slug = require('slug'); |
| 23 | +const pygmentize = require('pygmentize-bundled'); |
| 24 | +const minify = require('html-minifier').minify; |
| 25 | +const path = require('path'); |
| 26 | +const mkdirp = require('mkdirp').sync; |
| 27 | +const phantom = require('phantom'); |
| 28 | + |
| 29 | +const DocumentParser = require('./DocumentParser'); |
| 30 | + |
| 31 | +const templatesDir = path.join(__dirname, '../templates/embed'); |
| 32 | + |
| 33 | +const templates = { |
| 34 | + embed: compileTemplate('embed.html'), |
| 35 | + template: compileTemplate('template.html'), |
| 36 | + preview: compileTemplate('preview.html'), |
| 37 | + styles: compileTemplate('styles.css'), |
| 38 | + embedJs: compileTemplate('embed.js'), |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * Reads all html files in a folder and generates the embeds. |
| 43 | + */ |
| 44 | +function generateEmbeds(config) { |
| 45 | + glob(config.src + '/**/*.html', {}, (err, files) => { |
| 46 | + files.forEach(file => generateEmbed(config, file)); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Generates embeds for a given file. |
| 52 | + */ |
| 53 | +function generateEmbed(config, file) { |
| 54 | + const targetPath = path.join(config.destDir, path.relative(config.src, file)); |
| 55 | + const document = parseDocument(file); |
| 56 | + const sampleSections = document.sections.filter( |
| 57 | + s => s.inBody && !s.isEmptyCodeSection() |
| 58 | + ); |
| 59 | + sampleSections.forEach((section, index) => { |
| 60 | + highlight(section.code).then(code => { |
| 61 | + const tag = section.doc ? slug(section.doc.toLowerCase()) : index; |
| 62 | + const context = { |
| 63 | + sample: { |
| 64 | + file: path.basename(targetPath), |
| 65 | + preview: addFlag(targetPath, tag, 'preview'), |
| 66 | + embed: addFlag(targetPath, tag, 'embed'), |
| 67 | + template: addFlag(targetPath, tag, 'template'), |
| 68 | + body: section.code, |
| 69 | + code: code, |
| 70 | + }, |
| 71 | + config: config, |
| 72 | + document: document, |
| 73 | + }; |
| 74 | + generate(context.sample.preview, templates.preview, context, /* minify */ true); |
| 75 | + generate(context.sample.embed, templates.embed, context, /* minify */ true); |
| 76 | + generateTemplate(context); |
| 77 | + }); |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Syntax highlights a string. |
| 83 | + */ |
| 84 | +function highlight(code) { |
| 85 | + return new Promise((resolve, reject) => { |
| 86 | + pygmentize({ lang: 'html', format: 'html' }, code, function (err, result) { |
| 87 | + if (err) { |
| 88 | + console.log(err); |
| 89 | + reject(err); |
| 90 | + } else { |
| 91 | + resolve(result.toString()); |
| 92 | + } |
| 93 | + }); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Renders the given template into a file. |
| 99 | + */ |
| 100 | +function generate(file, template, context, minifyResult) { |
| 101 | + let string = template.render(context, { |
| 102 | + 'styles.css': templates.styles, |
| 103 | + 'embed.js': templates.embedJs |
| 104 | + }); |
| 105 | + if (minifyResult) { |
| 106 | + string = minify(string, { |
| 107 | + caseSensitive: true, |
| 108 | + collapseWhitespace: true, |
| 109 | + html5: true, |
| 110 | + minifyCSS: true, |
| 111 | + minifyJS: true, |
| 112 | + removeComments: true, |
| 113 | + removeAttributeQuotes: true |
| 114 | + }); |
| 115 | + } |
| 116 | + writeFile(path.join(context.config.destRoot, file), string); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Appends a list of flags separated by a '.' to a filename. |
| 121 | + */ |
| 122 | +function addFlag() { |
| 123 | + const filename = arguments[0]; |
| 124 | + const postfix = [].slice.call(arguments, 1).join('.'); |
| 125 | + return filename.replace('.html', '.' + postfix + '.html'); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Parses an ABE document from a file. |
| 130 | + */ |
| 131 | +function parseDocument(file) { |
| 132 | + const inputString = fs.readFileSync(file, 'utf-8'); |
| 133 | + return DocumentParser.parse(inputString); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Opens the embed html file in a browser to determine the initial height. |
| 138 | + */ |
| 139 | +function generateTemplate(context) { |
| 140 | + let _page; |
| 141 | + let _phantom; |
| 142 | + phantom.create([], { logLevel: 'error' }).then(function (ph) { |
| 143 | + _phantom = ph; |
| 144 | + ph.createPage() |
| 145 | + .then(page => { |
| 146 | + _page = page; |
| 147 | + const url = path.join(context.config.destRoot, context.sample.embed); |
| 148 | + return _page.property('viewportSize', {width: 1024, height: 768}) |
| 149 | + .then(() => _page.open(url) ); |
| 150 | + }) |
| 151 | + .then(status => { |
| 152 | + return _page.evaluate(function() { |
| 153 | + const element = document.querySelector('#source-panel'); |
| 154 | + const height = element.getBoundingClientRect().top + element.offsetHeight; |
| 155 | + return height; |
| 156 | + }); |
| 157 | + }) |
| 158 | + .then(height => { |
| 159 | + context.sample.height = height; |
| 160 | + generate(context.sample.template, templates.template, context); |
| 161 | + }) |
| 162 | + .then(() => { |
| 163 | + _page.close(); |
| 164 | + _phantom.exit(); |
| 165 | + }) |
| 166 | + .catch(err => console.log(err)); |
| 167 | + }); |
| 168 | +} |
| 169 | + |
| 170 | +function compileTemplate(filePath) { |
| 171 | + let string = fs.readFileSync(path.join(templatesDir, filePath), 'utf8'); |
| 172 | + return Hogan.compile(string); |
| 173 | +} |
| 174 | + |
| 175 | +function writeFile(filePath, content) { |
| 176 | + mkdirp(path.dirname(filePath)); |
| 177 | + fs.writeFileSync(filePath, content); |
| 178 | +} |
| 179 | + |
| 180 | +module.exports = {}; |
| 181 | +module.exports.generatePreview = generateEmbeds; |
0 commit comments