/* bikeshed claims to support markdown syntax, but not (yet) commonmark. ReSpec supports markdown formatting, but this shows up on the page before being rendered Hence we render the markdown to HTML ourselves, this gives us complete control over formatting and syntax highlighting (where highlight.js does a better job than bikeshed's Pygments) */ 'use strict'; /** @author Mike Ralphson **/ const fs = require('fs'); const path = require('path'); const url = require('url'); const util = require('util'); const hljs = require('highlight.js'); const cheerio = require('cheerio'); let argv = require('yargs') .boolean('bikeshed') .alias('b','bikeshed') .describe('bikeshed','Output in bikeshed format') .boolean('respec') .alias('r','respec') .describe('respec','Output in respec format') .default('respec',true) .string('maintainers') .alias('m','maintainers') .describe('maintainers','path to MAINTAINERS.md') .require(1) .argv; let maintainers = []; let emeritus = []; const md = require('markdown-it')({ html: true, linkify: true, typographer: true, highlight: function (str, lang) { if (lang && hljs.getLanguage(lang)) { // && !argv.respec) { try { return '
' +
                  hljs.highlight(str, { language: lang }).value +
                  '
'; } catch (__) { } } return '
' + md.utils.escapeHtml(str) + '
'; } }); function preface(title,options) { const respec = { specStatus: "base", editors: maintainers, formerEditors: emeritus, publishDate: options.publishDate, subtitle: 'Version '+options.subtitle, processVersion: 2017, edDraftURI: "https://github.com/OAI/OpenAPI-Specification/", github: { repoURL: "https://github.com/OAI/OpenAPI-Specification/", branch: "master" }, shortName: "OAS", noTOC: false, lint: false, additionalCopyrightHolders: "the Linux Foundation", includePermalinks: true }; let preface = `${md.utils.escapeHtml(title)}`; if (options.respec) { preface += ''; preface += ``; preface += fs.readFileSync('./analytics/google.html','utf8'); preface += ''; preface += ''; preface += '
'; preface += 'The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for REST APIs, which allows both humans and computers to discover and understand the capabilities of a service without requiring access to source code, additional documentation, or inspection of network traffic. When properly defined via OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interface descriptions have done for lower-level programming, the OpenAPI Specification removes guesswork in calling a service.'; preface += '
'; preface += '
'; preface += '

Status of This Document

'; preface += 'The source-of-truth for the specification is the GitHub markdown file referenced above.'; preface += '
'; } else { preface += ''; } return preface; } function doMaintainers() { let m = fs.readFileSync(argv.maintainers,'utf8'); let h = md.render(m); let $ = cheerio.load(h); let u = $('ul').first(); $(u).children('li').each(function(e){ let t = $(this).text().split('@')[0]; maintainers.push({name:t}); }); u = $('ul').eq(1); $(u).children('li').each(function(e){ let t = $(this).text().split('@')[0]; emeritus.push({name:t}); }); } function getPublishDate(m) { let h = md.render(m); let $ = cheerio.load(h); let t = $('tbody').last(); let c = $(t).children('tr').children('td'); let v = $(c[0]).text(); let d = $(c[1]).text(); argv.subtitle = v; if (d === 'TBA') return new Date(); return new Date(d); } if (argv.maintainers) { doMaintainers(); } let s = fs.readFileSync(argv._[0],'utf8'); if (argv.respec) { argv.publishDate = getPublishDate(s); } let lines = s.split('\r').join().split('\n'); let prevHeading = 0; let lastIndent = 0; let inTOC = false; let inDefs = false; let inCodeBlock = false; let bsFix = true; let indents = [0]; // process the markdown for (let l in lines) { let line = lines[l]; if (line.startsWith('## Table of Contents')) inTOC = true; if (line.startsWith('