|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const Handlebars = require('handlebars'); |
| 5 | + |
| 6 | +require('./helpers'); |
| 7 | + |
| 8 | +const admzip = require('adm-zip'); |
| 9 | +const temp = require('temp').track(); |
| 10 | +const sqlite3 = require('sqlite3').verbose(); |
| 11 | + |
| 12 | +/** |
| 13 | + * Dash export plugin for doxdox. |
| 14 | + * |
| 15 | + * @example parseInputs(inputs, {'parser': 'dox', 'layout': 'dash'}).then(content => console.log(content)); |
| 16 | + * @param {Array} data Methods parsed using a doxdox parser. |
| 17 | + * @return {Promise} Promise with generated content. |
| 18 | + * @public |
| 19 | + */ |
| 20 | + |
| 21 | +const plugin = data => new Promise((resolve, reject) => { |
| 22 | + |
| 23 | + const zip = new admzip(); |
| 24 | + const tempdb = temp.openSync('temp.sqlite'); |
| 25 | + const db = new sqlite3.Database(tempdb.path); |
| 26 | + |
| 27 | + fs.readFile(path.join(__dirname, 'templates/method.hbs'), 'utf8', (err, contents) => { |
| 28 | + |
| 29 | + if (err) { |
| 30 | + |
| 31 | + return reject(err); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + const methodTemplate = Handlebars.compile(contents); |
| 36 | + |
| 37 | + fs.readFile(path.join(__dirname, 'templates/Info.plist.hbs'), 'utf8', (err, contents) => { |
| 38 | + |
| 39 | + if (err) { |
| 40 | + |
| 41 | + return reject(err); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + const plistTemplate = Handlebars.compile(contents); |
| 46 | + |
| 47 | + zip.addFile( |
| 48 | + `${data.title}.docset/Contents/Info.plist`, |
| 49 | + plistTemplate(data) |
| 50 | + ); |
| 51 | + |
| 52 | + zip.addLocalFile( |
| 53 | + path.join(__dirname, 'templates/resources/bootstrap.min.css'), |
| 54 | + `${data.title}.docset/Contents/Resources/Documents/resources/` |
| 55 | + ); |
| 56 | + |
| 57 | + zip.addLocalFile( |
| 58 | + path.join(__dirname, 'templates/resources/github.min.css'), |
| 59 | + `${data.title}.docset/Contents/Resources/Documents/resources/` |
| 60 | + ); |
| 61 | + |
| 62 | + db.serialize(() => { |
| 63 | + |
| 64 | + db.run('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);'); |
| 65 | + db.run('CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);'); |
| 66 | + |
| 67 | + data.files.forEach(file => { |
| 68 | + |
| 69 | + file.methods.forEach(method => { |
| 70 | + |
| 71 | + zip.addFile( |
| 72 | + `${data.title}.docset/Contents/Resources/Documents/${method.uid}.html`, |
| 73 | + methodTemplate(method) |
| 74 | + ); |
| 75 | + |
| 76 | + db.run('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ($name, $type, $path);', { |
| 77 | + '$name': method.name, |
| 78 | + '$path': `${method.uid}.html`, |
| 79 | + '$type': method.type.replace(/^[a-z]/, match => match.toUpperCase()) |
| 80 | + }); |
| 81 | + |
| 82 | + if (method.tags.property) { |
| 83 | + |
| 84 | + method.tags.property.forEach(property => { |
| 85 | + |
| 86 | + db.run('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ($name, $type, $path);', { |
| 87 | + '$name': `${method.name}.${property.name}`, |
| 88 | + '$path': `${method.uid}.html#//apple_ref/cpp/Property/${property.name}`, |
| 89 | + '$type': 'Property' |
| 90 | + }); |
| 91 | + |
| 92 | + }); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + }); |
| 97 | + |
| 98 | + }); |
| 99 | + |
| 100 | + }); |
| 101 | + |
| 102 | + db.close(err => { |
| 103 | + |
| 104 | + if (err) { |
| 105 | + |
| 106 | + return reject(err); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + fs.readFile(tempdb.path, (err, contents) => { |
| 111 | + |
| 112 | + if (err) { |
| 113 | + |
| 114 | + return reject(err); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + zip.addFile( |
| 119 | + `${data.title}.docset/Contents/Resources/docSet.dsidx`, |
| 120 | + contents |
| 121 | + ); |
| 122 | + |
| 123 | + return resolve(zip.toBuffer()); |
| 124 | + |
| 125 | + }); |
| 126 | + |
| 127 | + return false; |
| 128 | + |
| 129 | + }); |
| 130 | + |
| 131 | + return false; |
| 132 | + |
| 133 | + }); |
| 134 | + |
| 135 | + return false; |
| 136 | + |
| 137 | + }); |
| 138 | + |
| 139 | +}); |
| 140 | + |
| 141 | +module.exports = plugin; |
0 commit comments