|
| 1 | +/* |
| 2 | + Copyright (c) 2024 Alan de Freitas (alandefreitas@gmail.com) |
| 3 | +
|
| 4 | + Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +
|
| 7 | + Official repository: https://github.com/cppalliance/mrdocs |
| 8 | +*/ |
| 9 | + |
| 10 | +const https = require('https'); |
| 11 | +const request = require('sync-request'); |
| 12 | +const fs = require("fs"); |
| 13 | + |
| 14 | +function getSubdirectoriesSync(url) { |
| 15 | + try { |
| 16 | + let urlPath = new URL(url).pathname; |
| 17 | + let cacheFilenamePath = urlPath.replace(/[^a-zA-Z0-9]/g, '') + '.json'; |
| 18 | + let cachePath = `${__dirname}/../build/requests/${cacheFilenamePath}`; |
| 19 | + fs.mkdirSync(`${__dirname}/../build/requests/`, { recursive: true }); |
| 20 | + const readFromCacheFile = fs.existsSync(cachePath) && fs.statSync(cachePath).mtime > new Date(Date.now() - 1000 * 60 * 60 * 24); |
| 21 | + const data = |
| 22 | + readFromCacheFile ? |
| 23 | + fs.readFileSync(cachePath, 'utf-8') : |
| 24 | + request('GET', url).getBody('utf-8') |
| 25 | + if (!readFromCacheFile) { |
| 26 | + fs.writeFileSync(cachePath, data); |
| 27 | + } |
| 28 | + const regex = /<a href="([^"]+)\/">/g; |
| 29 | + const directories = []; |
| 30 | + let match; |
| 31 | + while ((match = regex.exec(data)) !== null) { |
| 32 | + if (match[1] !== '..' && match[1] !== '.') { |
| 33 | + directories.push(match[1]); |
| 34 | + } |
| 35 | + } |
| 36 | + return directories; |
| 37 | + } catch (error) { |
| 38 | + console.error('Error:', error); |
| 39 | + return []; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function humanizePageType(pageType) { |
| 44 | + if (pageType === 'single') { |
| 45 | + return 'Single Page'; |
| 46 | + } else if (pageType === 'multi') { |
| 47 | + return 'Multi Page'; |
| 48 | + } else { |
| 49 | + return pageType; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function humanizeFormat(format) { |
| 54 | + if (format === 'html') { |
| 55 | + return 'HTML'; |
| 56 | + } else if (format === 'adoc') { |
| 57 | + return 'Asciidoc'; |
| 58 | + } else if (format === 'adoc-asciidoc') { |
| 59 | + return 'Rendered AsciiDoc'; |
| 60 | + } else if (format === 'xml') { |
| 61 | + return 'XML'; |
| 62 | + } |
| 63 | + return format; |
| 64 | +} |
| 65 | + |
| 66 | +function humanizeLibrary(library) { |
| 67 | + if (library === 'boost-url') { |
| 68 | + return 'Boost.URL'; |
| 69 | + } |
| 70 | + const boostLibrary = library.match(/boost-([\w]+)/); |
| 71 | + if (boostLibrary) { |
| 72 | + const capitalized = boostLibrary[1].charAt(0).toUpperCase() + boostLibrary[1].slice(1); |
| 73 | + return `Boost.${capitalized}`; |
| 74 | + } |
| 75 | + return library; |
| 76 | +} |
| 77 | + |
| 78 | +function libraryLink(library) { |
| 79 | + if (library === 'boost-url') { |
| 80 | + return 'https://github.com/boostorg/url[Boost.URL,window=_blank]'; |
| 81 | + } else if (library === 'boost-scope') { |
| 82 | + return 'https://github.com/boostorg/scope[Boost.Scope,window=_blank]'; |
| 83 | + } |
| 84 | + return humanizeLibrary(library); |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = function (registry) { |
| 88 | + registry.blockMacro('mrdocs-demos', function () { |
| 89 | + const self = this |
| 90 | + self.process(function (parent, target, attrs) { |
| 91 | + // Collect all demo URLs |
| 92 | + let finalDemoDirs = []; |
| 93 | + const versions = getSubdirectoriesSync('https://mrdocs.com/demos/'); |
| 94 | + for (const version of versions) { |
| 95 | + const demoLibraries = getSubdirectoriesSync(`https://mrdocs.com/demos/${version}/`); |
| 96 | + for (const demoLibrary of demoLibraries) { |
| 97 | + const pageTypes = getSubdirectoriesSync(`https://mrdocs.com/demos/${version}/${demoLibrary}/`); |
| 98 | + for (const pageType of pageTypes) { |
| 99 | + const demoFormats = getSubdirectoriesSync(`https://mrdocs.com/demos/${version}/${demoLibrary}/${pageType}/`); |
| 100 | + for (const demoFormat of demoFormats) { |
| 101 | + finalDemoDirs.push({ |
| 102 | + url: `https://mrdocs.com/demos/${version}/${demoLibrary}/${pageType}/${demoFormat}`, |
| 103 | + version: version, |
| 104 | + library: demoLibrary, |
| 105 | + pageType: pageType, |
| 106 | + format: demoFormat |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Create arrays for all unique versions, libraries, page types and formats |
| 114 | + let allVersions = []; |
| 115 | + for (const demoDir of finalDemoDirs) { |
| 116 | + if (!allVersions.includes(demoDir.version)) { |
| 117 | + allVersions.push(demoDir.version); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Create tables |
| 122 | + let text = '' |
| 123 | + for (const version of allVersions) { |
| 124 | + text += '\n' |
| 125 | + text += `**${version}**\n\n`; |
| 126 | + text += `|===\n`; |
| 127 | + |
| 128 | + let versionPageTypes = []; |
| 129 | + let versionFormats = []; |
| 130 | + let versionLibraries = []; |
| 131 | + for (const demoDir of finalDemoDirs) { |
| 132 | + if (demoDir.version !== version) { |
| 133 | + continue |
| 134 | + } |
| 135 | + if (!versionPageTypes.includes(demoDir.pageType)) { |
| 136 | + versionPageTypes.push(demoDir.pageType); |
| 137 | + } |
| 138 | + if (!versionFormats.includes(demoDir.format)) { |
| 139 | + versionFormats.push(demoDir.format); |
| 140 | + } |
| 141 | + if (!versionLibraries.includes(demoDir.library)) { |
| 142 | + versionLibraries.push(demoDir.library); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + text += `| ${versionPageTypes.map(pageType => `${versionFormats.length}+| *${humanizePageType(pageType)}*`).join(' ')}\n`; |
| 147 | + let formatColumns = versionFormats.map(format => `*${humanizeFormat(format)}*`).join(' | '); |
| 148 | + text += `| *Library* | ${formatColumns} | ${formatColumns}\n\n`; |
| 149 | + for (const library of versionLibraries) { |
| 150 | + text += `| ${libraryLink(library)}` |
| 151 | + for (const pageType of versionPageTypes) { |
| 152 | + for (const format of versionFormats) { |
| 153 | + const demoDir = finalDemoDirs.find( |
| 154 | + demoDir => demoDir.version === version && |
| 155 | + demoDir.library === library && |
| 156 | + demoDir.pageType === pageType && |
| 157 | + demoDir.format === format); |
| 158 | + if (demoDir) { |
| 159 | + text += `| ${demoDir.url}[🔗,window=_blank]` |
| 160 | + } else { |
| 161 | + text += `| ` |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + text += `\n` |
| 166 | + } |
| 167 | + text += `|===\n\n` |
| 168 | + } |
| 169 | + |
| 170 | + return self.parseContent(parent, text) |
| 171 | + }) |
| 172 | + }) |
| 173 | +} |
0 commit comments