From d04e10f9f7d9d4b65b23fae00a315a64ad86e5d2 Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Thu, 6 Jun 2024 13:27:59 -0300 Subject: [PATCH] docs: install page releases extension --- docs/antora-playbook.yml | 1 + docs/extensions/mrdocs-releases.js | 70 ++++++++++++++++++++++++++++ docs/local-antora-playbook.yml | 1 + docs/modules/ROOT/nav.adoc | 6 +-- docs/modules/ROOT/pages/install.adoc | 6 ++- 5 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 docs/extensions/mrdocs-releases.js diff --git a/docs/antora-playbook.yml b/docs/antora-playbook.yml index 83afbdd73..ededc9832 100644 --- a/docs/antora-playbook.yml +++ b/docs/antora-playbook.yml @@ -83,4 +83,5 @@ asciidoc: extensions: - '@asciidoctor/tabs' - ./extensions/mrdocs-demos.js + - ./extensions/mrdocs-releases.js diff --git a/docs/extensions/mrdocs-releases.js b/docs/extensions/mrdocs-releases.js new file mode 100644 index 000000000..aec964db4 --- /dev/null +++ b/docs/extensions/mrdocs-releases.js @@ -0,0 +1,70 @@ +/* + Copyright (c) 2024 Alan de Freitas (alandefreitas@gmail.com) + + 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 humanizeBytes(bytes) { + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; + if (bytes == 0) return '0 Byte'; + const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); + return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; +} + +function humanizeDate(date) { + const options = { year: 'numeric', month: 'long', day: 'numeric' }; + return new Date(date).toLocaleDateString('en-US', options); +} + +module.exports = function (registry) { + registry.blockMacro('mrdocs-releases', function () { + const self = this + self.process(function (parent, target, attrs) { + // Collect all release URLs + let cacheFilenamePath = 'releasesResponse.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 releasesResponse = + readFromCacheFile ? + fs.readFileSync(cachePath, 'utf-8') : + request('GET', 'https://api.github.com/repos/cppalliance/mrdocs/releases', { + headers: { + 'User-Agent': 'request' + } + }).getBody('utf-8') + if (!readFromCacheFile) { + fs.writeFileSync(cachePath, releasesResponse); + } + const releases = JSON.parse(releasesResponse) + + // Create table + let text = '|===\n' + text += '| 3+| 🪟 Windows 2+| 🐧 Linux \n' + text += '| 📃 Release | 📦 7z | 📦 msi | 📦 zip | 📦 tar.xz | 📦 tar.gz \n' + for (const release of releases) { + if (release.name === 'llvm-package') continue + text += `| ${release.html_url}[${release.name},window=_blank]\n\n${humanizeDate(release.published_at)} ` + const assetSuffixes = ['win64.7z', 'win64.msi', 'win64.zip', 'Linux.tar.xz', 'Linux.tar.gz'] + for (const suffix of assetSuffixes) { + const asset = release.assets.find(asset => asset.name.endsWith(suffix)) + if (asset) { + text += `| ${asset.browser_download_url}[🔗 ${asset.name}]\n\n(${humanizeBytes(asset.size)}) ` + } else { + text += '| - ' + } + } + text += '\n' + } + text += '|===\n' + return self.parseContent(parent, text) + }) + }) +} diff --git a/docs/local-antora-playbook.yml b/docs/local-antora-playbook.yml index 42d91385c..2fa45efcb 100644 --- a/docs/local-antora-playbook.yml +++ b/docs/local-antora-playbook.yml @@ -80,4 +80,5 @@ asciidoc: extensions: - '@asciidoctor/tabs' - ./extensions/mrdocs-demos.js + - ./extensions/mrdocs-releases.js diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index f7e64301a..dfe6f20d1 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -1,9 +1,9 @@ * xref:index.adoc[] * xref:demos.adoc[] -* xref:install.adoc[] -* xref:usage.adoc[] +* xref:install.adoc[Installation] +* xref:usage.adoc[Getting Started] +* xref:commands.adoc[Documenting the Code] * xref:config-file.adoc[] -* xref:commands.adoc[] * xref:design-notes.adoc[] * xref:contribute.adoc[] * xref:license.adoc[] diff --git a/docs/modules/ROOT/pages/install.adoc b/docs/modules/ROOT/pages/install.adoc index 2c0825fb0..1a9e8703a 100644 --- a/docs/modules/ROOT/pages/install.adoc +++ b/docs/modules/ROOT/pages/install.adoc @@ -1,13 +1,15 @@ = Install [#mrdocs-binaries] -== Binaries +== Binary Packages Binary packages are available from our https://github.com/cppalliance/mrdocs/releases[Release Page,window="_blank"]. Most users should use these packages. +mrdocs-releases::[] + [#mrdocs-source] -== Source +== Install from Source The following instructions assume we are at a parent directory that's going to contain both the MrDocs and the third-party dependencies directories.