X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/cf6302904d61ead65e6294e7f1be406eb68ef5f9..3c39d1553de92cd6cac891f2c3b186701ec60332:/lib/blog.js?ds=inline diff --git a/lib/blog.js b/lib/blog.js index 483577f..e1a76a8 100644 --- a/lib/blog.js +++ b/lib/blog.js @@ -1,39 +1,43 @@ -'use strict'; +import { access, cp, readdir, readFile, writeFile } from 'fs/promises'; +import { exec } from 'child_process'; +import { basename, join } from 'path'; +import ParseGemini from 'gemini-to-html/parse.js'; +import RenderGemini from 'gemini-to-html/render.js'; +import { debuglog, promisify } from 'util'; +import { ensureDirectoryExists, rmIfExists } from './utils.js'; +import { kFileNotFoundError } from './constants.js'; -const Fs = require('fs'); -const Markdown = require('markdown'); -const Mustache = require('mustache'); -const Ncp = require('ncp'); -const Path = require('path'); -const Rimraf = require('rimraf'); -const Util = require('util'); +// Generators for the Blog + +import StaticGenerator from './generators/static.js'; +import HTMLGenerator from './generators/html.js'; +import RSSGenerator from './generators/rss.js'; +import TXTGenerator from './generators/txt.js'; + +// Archiving Methods + +import GemlogArchiver from './archivers/gemlog.js'; + +// Remote Handler + +import Remote from './remote.js'; const internals = { // Promisified functions + exec: promisify(exec), - fs: { - access: Util.promisify(Fs.access), - mkdir: Util.promisify(Fs.mkdir), - readdir: Util.promisify(Fs.readdir), - readFile: Util.promisify(Fs.readFile), - writeFile: Util.promisify(Fs.writeFile) - }, - ncp: Util.promisify(Ncp.ncp), - rimraf: Util.promisify(Rimraf), - debuglog: Util.debuglog('blog'), + debuglog: debuglog('blog'), // constants - kAssetsDirectoryName: 'assets', - kIndexName: 'index.html', - kFileNotFoundError: 'ENOENT', - kMarkdownRe: /\.md$/i, + kGeminiRe: /\.gmi$/i, + kMetadataFilename: 'metadata.json', // Strings strings: { - markdownNotFound: 'Markdown file was not found in blog directory. Please update.' + geminiNotFound: 'Gemini file was not found in blog directory. Please update.' } }; @@ -42,10 +46,10 @@ const internals = { * updating posts, and handling the publishing. * * @class Blog - * @param {Potluck.tConfiguration} config the initialization options to + * @param {Blog.tConfiguration} config the initialization options to * extend the instance */ -module.exports = class Blog { +export default class Blog { constructor(config) { @@ -53,36 +57,47 @@ module.exports = class Blog { } /** - * Shifts the blog posts, adds the passed path to slot 0, and + * Shifts the blog posts, adds the passed file to slot 0, and * generates files. * * @function add * @memberof Blog - * @param {string} postLocation the path to the directory containing - * the post structure + * @param {string} postLocation the path to the blog post file * @return {Promise} empty promise, returns no value * @instance */ async add(postLocation) { + await ensureDirectoryExists(this.postsDirectory); + try { + await this.syncDown(); + } + catch {} + await this._shift(); - await this.update(postLocation); + const firstDirectory = join(this.postsDirectory, '0'); + await rmIfExists(firstDirectory); + await ensureDirectoryExists(firstDirectory); + await this._update(postLocation); } /** - * Adds the passed path to slot 0, and generates files. + * Update slot 0 with the passed gmi file, and generates files. * * @function update * @memberof Blog - * @param {string} postLocation the path to the directory containing - * the post structure + * @param {string} postLocation the path to the blog post file * @return {Promise} empty promise, returns no value * @instance */ async update(postLocation) { - await this._copyPost(postLocation); - await this._generate(); + try { + await this.syncDown(); + } + catch {} + + await this._update(postLocation); } /** @@ -93,70 +108,235 @@ module.exports = class Blog { * @return {Promise} empty promise, returns no value * @instance */ - async publish() { + async publish(host) { - console.error('Publishing not yet implemented'); + internals.debuglog(`Publishing to ${host}`); + try { + await internals.exec('which rsync'); + } + catch (err) { + console.error('Please install and configure rsync to publish.'); + } + + try { + internals.debuglog(`Copying ephemeral blog from ${this.blogOutputDirectory}`); + await internals.exec(`rsync -r ${this.blogOutputDirectory}/ ${host}`); + } + catch (err) { + console.error('Failed to publish'); + console.error(err.stderr); + } + + internals.debuglog('Finished publishing'); } - // Parses markdown for each page, copies assets and generates index. + /** + * Publishes the archive to a host using rsync. Currently assumes + * gemlog archive. + * + * @function publishArchive + * @memberof Blog + * @return {Promise} empty promise, returns no value + * @instance + */ + async publishArchive(host) { - async _generate() { + internals.debuglog(`Publishing archive to ${host}`); + try { + await internals.exec('which rsync'); + } + catch (err) { + console.error('Please install rsync to publish the archive.'); + } - const assetsTarget = Path.join(this.staticDirectory, internals.kAssetsDirectoryName); - const indexTarget = Path.join(this.staticDirectory, internals.kIndexName); - const indexLocation = Path.join(this.templatesDirectory, internals.kIndexName); - const posts = []; + try { + internals.debuglog(`Copying archive from ${this.archiveOutputDirectory}`); + await internals.exec(`rsync -r ${this.archiveOutputDirectory}/ ${host}`); + } + catch (err) { + console.error('Failed to publish archive'); + console.error(err.stderr); + } - internals.debuglog(`Removing ${assetsTarget}`); - await internals.rimraf(assetsTarget); + internals.debuglog('Finished publishing'); + } - for (let i = 0; i < this.maxPosts; ++i) { - const sourcePath = Path.join(this.postsDirectory, `${i}`); - const assetsSource = Path.join(sourcePath, internals.kAssetsDirectoryName); - const postContentPath = await this._findBlogContent(sourcePath); + /** + * Adds a remote + * + * @function addRemote + * @memberof Blog + * @return {Promise} empty promise, returns no value + * @instance + */ + async addRemote(remote) { + + await ensureDirectoryExists(this.configDirectory); + await Remote.add(this.remoteConfig, remote); + } + + /** + * Removes a remote + * + * @function removeRemote + * @memberof Blog + * @return {Promise} empty promise, returns no value + * @instance + */ + async removeRemote() { + + await Remote.remove(this.remoteConfig); + } + + + /** + * Pulls the posts and archive from the remote + * + * @function syncDown + * @memberof Blog + * @return {Promise} empty promise, returns no value + * @instance + */ + async syncDown() { + + internals.debuglog('Pulling remote state'); + await ensureDirectoryExists(this.dataDirectory); + await Remote.syncDown(this.remoteConfig, this.dataDirectory); + internals.debuglog('Pulled remote state'); + } + + /** + * Pushes the posts and archive to the remote + * + * @function syncUp + * @memberof Blog + * @return {Promise} empty promise, returns no value + * @instance + */ + async syncUp() { - internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`); - await internals.ncp(assetsSource, assetsTarget); + internals.debuglog('Pushing remote state'); + await ensureDirectoryExists(this.dataDirectory); + await Remote.syncUp(this.remoteConfig, this.dataDirectory); + internals.debuglog('Pushed remote state'); + } + + // Adds the passed path to slot 0, and generates files. + + async _update(postLocation) { + + const metadata = await this._getMetadata(); + await ensureDirectoryExists(this.postsDirectory); + await this._copyPost(postLocation); + await this._writeMetadata(metadata); - internals.debuglog(`Reading ${postContentPath}`); - const postContent = await internals.fs.readFile(postContentPath, { encoding: 'utf8' }); + await this._archive(postLocation); - internals.debuglog('Parsing markdown'); - posts.push({ - html: Markdown.markdown.toHTML(postContent), - id: i + 1 - }); + await this.generate(); + try { + await this.syncUp(); } + catch {} + } + + + // Parses Gemini for each page, copies assets and generates index. - internals.debuglog(`Reading ${indexLocation}`); - const indexTemplate = await internals.fs.readFile(indexLocation, { encoding: 'utf8' }); + async generate() { - internals.debuglog('Generating HTML'); - const indexHtml = Mustache.render(indexTemplate, { posts }); - await internals.fs.writeFile(indexTarget, indexHtml); + internals.debuglog('Generating output'); + + const posts = await this._readPosts(); + + // Start from a clean slate. + await rmIfExists(this.blogOutputDirectory); + await ensureDirectoryExists(this.blogOutputDirectory); + + // Run each generator + await StaticGenerator(this.staticDirectory, this.blogOutputDirectory, posts); + await HTMLGenerator(await this._templateDirectoryFor('index.html'), this.blogOutputDirectory, posts); + await RSSGenerator(await this._templateDirectoryFor('feed.xml'), this.blogOutputDirectory, posts); + await TXTGenerator(await this._templateDirectoryFor('index.txt'), this.blogOutputDirectory, posts); + + // Start from a clean slate. + await rmIfExists(this.archiveOutputDirectory); + await ensureDirectoryExists(this.archiveOutputDirectory); + await ensureDirectoryExists(this.archiveDirectory); + + // Run each archiver + await GemlogArchiver(await this._templateDirectoryFor('index.gmi'), this.archiveDirectory, this.archiveOutputDirectory); + // TODO: GopherArchiver + } + + // Reads the posts into an array + + async _readPosts() { + + internals.debuglog('Reading posts'); + const posts = []; + + for (let i = 0; i < this.maxPosts; ++i) { + try { + posts.push(await this._readPost(i)); + } + catch (error) { + if (error.code === kFileNotFoundError) { + internals.debuglog(`Skipping ${i}`); + continue; + } + + throw error; + } + } + + return posts; + } + + // Reads an individual post + + async _readPost(index = 0) { + + const postSourcePath = join(this.postsDirectory, `${index}`); + + internals.debuglog(`Reading ${postSourcePath}`); + + await access(postSourcePath); + + const metadata = await this._getMetadata(index); + + const postContentPath = await this._findBlogContent(postSourcePath); + internals.debuglog(`Reading ${postContentPath}`); + const postContent = await readFile(postContentPath, { encoding: 'utf8' }); + + internals.debuglog('Parsing Gemini'); + return { + ...metadata, + location: postSourcePath, + index, + html: RenderGemini(ParseGemini(postContent)), + raw: postContent + }; } // Shift the posts, delete any remainder. async _shift() { - await this._ensurePostsDirectoryExists(); - for (let i = this.maxPosts - 1; i > 0; --i) { - const targetPath = Path.join(this.postsDirectory, `${i}`); - const sourcePath = Path.join(this.postsDirectory, `${i - 1}`); + for (let i = this.maxPosts - 1; i >= 1; --i) { + const targetPath = join(this.postsDirectory, `${i}`); + const sourcePath = join(this.postsDirectory, `${i - 1}`); try { - await internals.fs.access(sourcePath); - - internals.debuglog(`Removing ${targetPath}`); - await internals.rimraf(targetPath); + internals.debuglog(`Archiving ${targetPath}`); + await rmIfExists(targetPath); + await access(sourcePath); // check the source path internals.debuglog(`Shifting blog post ${sourcePath} to ${targetPath}`); - await internals.ncp(sourcePath, targetPath); + await cp(sourcePath, targetPath, { recursive: true }); } catch (error) { - if (error.code === internals.kFileNotFoundError) { + if (error.code === kFileNotFoundError) { internals.debuglog(`Skipping ${sourcePath}: Does not exist.`); continue; } @@ -166,55 +346,103 @@ module.exports = class Blog { } } - // Copies a post directory to the latest slot. + // Moves older posts to the archive - async _copyPost(postLocation) { + async _archive() { - await this._ensurePostsDirectoryExists(); + internals.debuglog('Archiving post'); + const post = await this._readPost(0); + await ensureDirectoryExists(this.archiveDirectory); - const targetPath = Path.join(this.postsDirectory, '0'); + const targetPath = join(this.archiveDirectory, post.id); internals.debuglog(`Removing ${targetPath}`); - await internals.rimraf(targetPath); - - internals.debuglog(`Adding ${postLocation} to ${targetPath}`); - await internals.ncp(postLocation, targetPath); + await rmIfExists(targetPath); + internals.debuglog(`Adding ${post.location} to ${targetPath}`); + await ensureDirectoryExists(targetPath); + await cp(post.location, targetPath, { recursive: true }); + internals.debuglog(`Added ${post.location} to ${targetPath}`); } - // Ensures the posts directory exists. + // Attempts to read existing metadata. Otherwise generates new set. + + async _getMetadata(index = 0) { - async _ensurePostsDirectoryExists() { + const metadataTarget = join(this.postsDirectory, String(index), internals.kMetadataFilename); - internals.debuglog(`Checking if ${this.postsDirectory} exists.`); try { - await internals.fs.access(this.postsDirectory); + internals.debuglog(`Looking for metadata at ${metadataTarget}`); + return JSON.parse(await readFile(metadataTarget, { encoding: 'utf8' })); } - catch (error) { - if (error.code === internals.kFileNotFoundError) { - internals.debuglog('Creating posts directory'); - await internals.fs.mkdir(this.postsDirectory); - return; - } - - throw error; + catch (e) { + internals.debuglog(`Metadata not found or unreadable. Generating new set.`); + const createdOn = Date.now(); + const metadata = { + id: String(createdOn), + createdOn + }; + + return metadata; } } - // Looks for a `.md` file in the blog directory, and returns the path + // Writes metadata. Assumes post 0 since it only gets written + // on create + + async _writeMetadata(metadata) { + + const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename); + internals.debuglog(`Writing ${metadataTarget}`); + await writeFile(metadataTarget, JSON.stringify(metadata, null, 2)); + } + + // Copies a post file to the latest slot. + + async _copyPost(postLocation) { + + internals.debuglog(`Copying ${postLocation}`); + const targetPath = join(this.postsDirectory, '0'); + const postName = basename(postLocation); + const targetPost = join(targetPath, postName); + + await rmIfExists(targetPath); + await ensureDirectoryExists(targetPath); + await cp(postLocation, targetPost, { recursive: true }); + internals.debuglog(`Added ${postLocation} to ${targetPath}`); + } + + // Looks for a `.gmi` file in the blog directory, and returns the path async _findBlogContent(directory) { - const entries = await internals.fs.readdir(directory); + const entries = await readdir(directory); - const markdownEntries = entries - .filter((entry) => internals.kMarkdownRe.test(entry)) - .map((entry) => Path.join(directory, entry)); + const geminiEntries = entries + .filter((entry) => internals.kGeminiRe.test(entry)) + .map((entry) => join(directory, entry)); - if (markdownEntries.length > 0) { - internals.debuglog(`Found markdown file: ${markdownEntries[0]}`); - return markdownEntries[0]; + if (geminiEntries.length > 0) { + internals.debuglog(`Found gemini file: ${geminiEntries[0]}`); + return geminiEntries[0]; } - throw new Error(internals.strings.markdownNotFound); + throw new Error(internals.strings.geminiNotFound); } -}; + + // Gets the template directory for a given template. + async _templateDirectoryFor(template) { + + try { + await access(join(this.templatesDirectory, template)); + return this.templatesDirectory; + } + catch (error) { + if (error.code === kFileNotFoundError) { + internals.debuglog(`No custom template for ${template}`); + return this.defaultTemplatesDirectory; + } + + throw error; + } + } +}