aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2020-06-03 07:40:42 +0200
committerBen Beltran <ben@nsovocal.com>2020-06-03 07:40:42 +0200
commit5f31ea34aea76b8357913abd003bddb0f47f4dab (patch)
tree37efcdbc86caca0fbd4864926e7a6deb16952ed5
parentdb7b464d701c7d48777ddd38a10f69093cd5442c (diff)
Add TXT support
-rw-r--r--CHANGELOG.md3
-rw-r--r--lib/blog.js5
-rw-r--r--lib/generators/txt.js38
-rw-r--r--templates/index.txt18
4 files changed, 62 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1e3b1f8..a38a69d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
### Added
- Post Metadata support
-- RSS Support
+- RSS support
+- TXT support
### Changed
- Updated dependencies
diff --git a/lib/blog.js b/lib/blog.js
index 98efabd..541d6b3 100644
--- a/lib/blog.js
+++ b/lib/blog.js
@@ -9,6 +9,7 @@ const { debuglog, promisify } = require('util');
const StaticGenerator = require('./generators/static');
const HTMLGenerator = require('./generators/html');
const RSSGenerator = require('./generators/rss');
+const TXTGenerator = require('./generators/txt');
const internals = {
@@ -108,6 +109,7 @@ module.exports = class Blog {
await StaticGenerator(this.postsDirectory, this.staticDirectory, posts);
await HTMLGenerator(this.templatesDirectory, this.staticDirectory, posts);
await RSSGenerator(this.templatesDirectory, this.staticDirectory, posts);
+ await TXTGenerator(this.templatesDirectory, this.staticDirectory, posts);
}
// Reads the posts into an array
@@ -134,7 +136,8 @@ module.exports = class Blog {
internals.debuglog('Parsing markdown');
posts.push({
...metadata,
- html: Marked(postContent)
+ html: Marked(postContent),
+ raw: postContent
});
}
catch (error) {
diff --git a/lib/generators/txt.js b/lib/generators/txt.js
new file mode 100644
index 0000000..af5ec9c
--- /dev/null
+++ b/lib/generators/txt.js
@@ -0,0 +1,38 @@
+'use strict';
+
+const { template, templateSettings } = require('dot');
+const { readFile, writeFile } = require('fs/promises');
+const { join } = require('path');
+const { debuglog } = require('util');
+
+const internals = {
+ debuglog: debuglog('blog'),
+
+ kTextName: 'index.txt'
+};
+
+/**
+ * Generates a TXT version of the blog
+ *
+ * @name TXTGenerator
+ * @param {string} source the source directory
+ * @param {string} target the target directory
+ * @param {Array.<Blog.tPost>} posts the list of posts
+ */
+module.exports = async function TXTGenerator(source, target, posts) {
+
+ internals.debuglog('Generating TXT');
+ const textTarget = join(target, internals.kTextName);
+ const textLocation = join(source, internals.kTextName);
+
+ internals.debuglog(`Reading ${textLocation}`);
+ const textTemplate = await readFile(textLocation, { encoding: 'utf8' });
+
+ internals.debuglog('Writing TXT');
+ const text = template(textTemplate, {
+ ...templateSettings,
+ strip: false
+ })({ posts });
+ await writeFile(textTarget, text);
+};
+
diff --git a/templates/index.txt b/templates/index.txt
new file mode 100644
index 0000000..b3b27a9
--- /dev/null
+++ b/templates/index.txt
@@ -0,0 +1,18 @@
+╔══════════════════════════════════════════════════════════════════════════════╗
+║ ║
+║ The Blog at Unlimited Pizza ║
+║ ║
+╚══════════════════════════════════════════════════════════════════════════════╝
+┌──────────────────────────────────────────────────────────────────────────────┐
+│ Written by Rubén Beltrán del Río │
+└──────────────────────────────────────────────────────────────────────────────┘
+
+{{~ it.posts: post}}
+{{= post.raw}}
+■──────────────────────────────────────────────────────────────────────────────■
+{{~}}
+{{? it.posts.length === 0}}
+# This is a fresh blog!
+
+There are no posts yet.
+{{?}}