Skip to content

Commit

Permalink
docs: demos page has direct links
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Jun 11, 2024
1 parent 83ff42c commit 0d52577
Show file tree
Hide file tree
Showing 6 changed files with 577 additions and 70 deletions.
4 changes: 4 additions & 0 deletions docs/antora-playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ antora:
extensions:
- require: '@antora/lunr-extension' # https://gitlab.com/antora/antora-lunr-extension
index_latest_only: true
asciidoc:
extensions:
- '@asciidoctor/tabs'
- ./extensions/mrdocs-demos.js

173 changes: 173 additions & 0 deletions docs/extensions/mrdocs-demos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
Copyright (c) 2024 Alan de Freitas ([email protected])
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Official repository: https://github.com/cppalliance/mrdocs
*/

const https = require('https');
const request = require('sync-request');
const fs = require("fs");

function getSubdirectoriesSync(url) {
try {
let urlPath = new URL(url).pathname;
let cacheFilenamePath = urlPath.replace(/[^a-zA-Z0-9]/g, '') + '.json';
let cachePath = `${__dirname}/../build/requests/${cacheFilenamePath}`;
fs.mkdirSync(`${__dirname}/../build/requests/`, { recursive: true });
const readFromCacheFile = fs.existsSync(cachePath) && fs.statSync(cachePath).mtime > new Date(Date.now() - 1000 * 60 * 60 * 24);
const data =
readFromCacheFile ?
fs.readFileSync(cachePath, 'utf-8') :
request('GET', url).getBody('utf-8')
if (!readFromCacheFile) {
fs.writeFileSync(cachePath, data);
}
const regex = /<a href="([^"]+)\/">/g;
const directories = [];
let match;
while ((match = regex.exec(data)) !== null) {
if (match[1] !== '..' && match[1] !== '.') {
directories.push(match[1]);
}
}
return directories;
} catch (error) {
console.error('Error:', error);
return [];
}
}

function humanizePageType(pageType) {
if (pageType === 'single') {
return 'Single Page';
} else if (pageType === 'multi') {
return 'Multi Page';
} else {
return pageType;
}
}

function humanizeFormat(format) {
if (format === 'html') {
return 'HTML';
} else if (format === 'adoc') {
return 'Asciidoc';
} else if (format === 'adoc-asciidoc') {
return 'Rendered AsciiDoc';
} else if (format === 'xml') {
return 'XML';
}
return format;
}

function humanizeLibrary(library) {
if (library === 'boost-url') {
return 'Boost.URL';
}
const boostLibrary = library.match(/boost-([\w]+)/);
if (boostLibrary) {
const capitalized = boostLibrary[1].charAt(0).toUpperCase() + boostLibrary[1].slice(1);
return `Boost.${capitalized}`;
}
return library;
}

function libraryLink(library) {
if (library === 'boost-url') {
return 'https://github.com/boostorg/url[Boost.URL,window=_blank]';
} else if (library === 'boost-scope') {
return 'https://github.com/boostorg/scope[Boost.Scope,window=_blank]';
}
return humanizeLibrary(library);
}

module.exports = function (registry) {
registry.blockMacro('mrdocs-demos', function () {
const self = this
self.process(function (parent, target, attrs) {
// Collect all demo URLs
let finalDemoDirs = [];
const versions = getSubdirectoriesSync('https://mrdocs.com/demos/');
for (const version of versions) {
const demoLibraries = getSubdirectoriesSync(`https://mrdocs.com/demos/${version}/`);
for (const demoLibrary of demoLibraries) {
const pageTypes = getSubdirectoriesSync(`https://mrdocs.com/demos/${version}/${demoLibrary}/`);
for (const pageType of pageTypes) {
const demoFormats = getSubdirectoriesSync(`https://mrdocs.com/demos/${version}/${demoLibrary}/${pageType}/`);
for (const demoFormat of demoFormats) {
finalDemoDirs.push({
url: `https://mrdocs.com/demos/${version}/${demoLibrary}/${pageType}/${demoFormat}`,
version: version,
library: demoLibrary,
pageType: pageType,
format: demoFormat
});
}
}
}
}

// Create arrays for all unique versions, libraries, page types and formats
let allVersions = [];
for (const demoDir of finalDemoDirs) {
if (!allVersions.includes(demoDir.version)) {
allVersions.push(demoDir.version);
}
}

// Create tables
let text = ''
for (const version of allVersions) {
text += '\n'
text += `**${version}**\n\n`;
text += `|===\n`;

let versionPageTypes = [];
let versionFormats = [];
let versionLibraries = [];
for (const demoDir of finalDemoDirs) {
if (demoDir.version !== version) {
continue
}
if (!versionPageTypes.includes(demoDir.pageType)) {
versionPageTypes.push(demoDir.pageType);
}
if (!versionFormats.includes(demoDir.format)) {
versionFormats.push(demoDir.format);
}
if (!versionLibraries.includes(demoDir.library)) {
versionLibraries.push(demoDir.library);
}
}

text += `| ${versionPageTypes.map(pageType => `${versionFormats.length}+| *${humanizePageType(pageType)}*`).join(' ')}\n`;
let formatColumns = versionFormats.map(format => `*${humanizeFormat(format)}*`).join(' | ');
text += `| *Library* | ${formatColumns} | ${formatColumns}\n\n`;
for (const library of versionLibraries) {
text += `| ${libraryLink(library)}`
for (const pageType of versionPageTypes) {
for (const format of versionFormats) {
const demoDir = finalDemoDirs.find(
demoDir => demoDir.version === version &&
demoDir.library === library &&
demoDir.pageType === pageType &&
demoDir.format === format);
if (demoDir) {
text += `| ${demoDir.url}[🔗,window=_blank]`
} else {
text += `| `
}
}
}
text += `\n`
}
text += `|===\n\n`
}

return self.parseContent(parent, text)
})
})
}
4 changes: 4 additions & 0 deletions docs/local-antora-playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ antora:
extensions:
- require: '@antora/lunr-extension' # https://gitlab.com/antora/antora-lunr-extension
index_latest_only: true
asciidoc:
extensions:
- '@asciidoctor/tabs'
- ./extensions/mrdocs-demos.js

24 changes: 1 addition & 23 deletions docs/modules/ROOT/pages/demos.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,5 @@

A few examples of reference documentation generated with MrDocs are available in https://mrdocs.com/demos/:

|===
| 4+| *Multi-Page* 4+| *Single-Page*
| *Library* | *Asciidoc* | *Rendered Asciidoc* | *HTML* | *XML* | *Asciidoc* | *Rendered Asciidoc* | *HTML* | *XML*
mrdocs-demos::[]

| https://github.com/boostorg/url[Boost.URL]
| https://mrdocs.com/demos/develop/boost-url/multi/adoc/[🔗]
| https://mrdocs.com/demos/develop/boost-url/multi/adoc-asciidoc/[🔗]
| https://mrdocs.com/demos/develop/boost-url/multi/html/[🔗]
| https://mrdocs.com/demos/develop/boost-url/multi/xml/[🔗]
| https://mrdocs.com/demos/develop/boost-url/single/adoc/[🔗]
| https://mrdocs.com/demos/develop/boost-url/single/adoc-asciidoc/[🔗]
| https://mrdocs.com/demos/develop/boost-url/single/html/[🔗]
| https://mrdocs.com/demos/develop/boost-url/single/xml/[🔗]

| https://github.com/boostorg/scope[Boost.Scope]
| https://mrdocs.com/demos/develop/boost-scope/multi/adoc/[🔗]
| https://mrdocs.com/demos/develop/boost-scope/multi/adoc-asciidoc/[🔗]
|
|
| https://mrdocs.com/demos/develop/boost-scope/single/adoc/[🔗]
| https://mrdocs.com/demos/develop/boost-scope/single/adoc-asciidoc/[🔗]
|
|
|===
Loading

0 comments on commit 0d52577

Please sign in to comment.