From 05263b487c9b8d17ec949e84b4b221f4779f55fa Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 26 Jan 2016 09:12:07 -0700 Subject: [PATCH] [generate/docs/examples] allow overriding individual example files based on version --- docs/_examples/index.js | 6 ++++++ scripts/generate/js_api.js | 16 +++++++++++++++- scripts/generate/templates/api_methods.tmpl | 2 +- scripts/generate/templates/index.js | 12 ------------ 4 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 docs/_examples/index.js diff --git a/docs/_examples/index.js b/docs/_examples/index.js new file mode 100644 index 000000000..e35fa92e9 --- /dev/null +++ b/docs/_examples/index.js @@ -0,0 +1,6 @@ +module.exports = [ + { + version: '*', + examples: {} + } +] diff --git a/scripts/generate/js_api.js b/scripts/generate/js_api.js index 4cc607dc1..f53750b71 100644 --- a/scripts/generate/js_api.js +++ b/scripts/generate/js_api.js @@ -28,6 +28,10 @@ module.exports = function (branch, done) { clientActionModifier: false }); + var examples = version.mergeOpts(require('../../docs/_examples/index.js'), { + examples: {} + }).examples; + var steps = [ readSpecFiles, parseSpecFiles, @@ -100,6 +104,17 @@ module.exports = function (branch, done) { apiSpec.proxies.push(create); + [].concat(apiSpec.actions, apiSpec.proxies) + .forEach(function (action) { + var examplePath = examples[action.name] || action.name + '.asciidoc'; + + try { + action.examples = fs.readFileSync(fromRoot('docs/_examples', examplePath), 'utf8'); + } catch (e) { + action.examples = '// no examples'; + } + }) + done(); } @@ -336,4 +351,3 @@ module.exports = function (branch, done) { return actions; } }; - diff --git a/scripts/generate/templates/api_methods.tmpl b/scripts/generate/templates/api_methods.tmpl index 3358a4c20..01574797d 100644 --- a/scripts/generate/templates/api_methods.tmpl +++ b/scripts/generate/templates/api_methods.tmpl @@ -25,7 +25,7 @@ client.<%= action.name %>([params, [callback]]) The default method is `<%= action.spec.method || 'GET' %>` and the usual <> apply. See <%= action.docUrl %>[the elasticsearch docs] for more about this method. -<%= examples(action.name) %> +<%= action.examples %> <% if (_.size(action.allParams)) { %> *Params* diff --git a/scripts/generate/templates/index.js b/scripts/generate/templates/index.js index 29d50fa14..0c3ef4cad 100644 --- a/scripts/generate/templates/index.js +++ b/scripts/generate/templates/index.js @@ -66,18 +66,6 @@ var templateGlobals = { } }, - examples: function (action) { - try { - return fs.readFileSync(path.join(__dirname, '../../../docs/_examples/' + action + '.asciidoc')); - } catch (e) { - if (~e.message.indexOf('ENOENT')) { - return '// no examples'; - } else { - throw e; - } - } - }, - paramType: function (type) { switch (type && type.toLowerCase ? type.toLowerCase() : 'any') { case 'time':