1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | 'use strict';
|
20 |
|
21 | const path = require('path');
|
22 | const util = require('util');
|
23 | const url = require('url');
|
24 | const async = require('async');
|
25 | const akasha = require('akasharender');
|
26 | const mahabhuta = require('mahabhuta');
|
27 | const co = require('co');
|
28 |
|
29 | const log = require('debug')('akasha:blog-podcast-plugin');
|
30 | const error = require('debug')('akasha:error-blog-podcast-plugin');
|
31 |
|
32 | const pluginName = "akashacms-blog-podcast";
|
33 |
|
34 | module.exports = class BlogPodcastPlugin extends akasha.Plugin {
|
35 | constructor() { super(pluginName); }
|
36 |
|
37 | configure(config) {
|
38 | this._config = config;
|
39 | config.addPartialsDir(path.join(__dirname, 'partials'));
|
40 | config.addMahabhuta(module.exports.mahabhuta);
|
41 | config.pluginData(pluginName).bloglist = [];
|
42 | }
|
43 |
|
44 | addBlogPodcast(config, name, blogPodcast) {
|
45 | config.pluginData(pluginName).bloglist[name] = blogPodcast;
|
46 | return config;
|
47 | }
|
48 |
|
49 | isLegitLocalHref(config, href) {
|
50 |
|
51 | for (var blogkey in config.pluginData(pluginName).bloglist) {
|
52 | var blogcfg = config.pluginData(pluginName).bloglist[blogkey];
|
53 |
|
54 | if (blogcfg.rssurl === href) {
|
55 | return true;
|
56 | }
|
57 | }
|
58 | return false;
|
59 | }
|
60 |
|
61 | onSiteRendered(config) {
|
62 |
|
63 | return co(function* () {
|
64 | for (var blogkey in config.pluginData(pluginName).bloglist) {
|
65 | var blogcfg = config.pluginData(pluginName).bloglist[blogkey];
|
66 |
|
67 | var documents = yield findBlogDocs(config, undefined, blogcfg);
|
68 | var count = 0;
|
69 | var documents2 = documents.filter(doc => {
|
70 | if (typeof maxEntries === "undefined"
|
71 | || (typeof maxEntries !== "undefined" && count++ < maxEntries)) {
|
72 | return true;
|
73 | } else return false;
|
74 | });
|
75 | // log('blog-news-river documents2 '+ util.inspect(documents2));
|
76 |
|
77 | var rssitems = documents2.map(doc => {
|
78 | return {
|
79 | title: doc.metadata.title,
|
80 | description: doc.metadata.teaser ? doc.metadata.teaser : "",
|
81 | url: config.root_url +'/'+ doc.renderpath,
|
82 | date: doc.metadata.publicationDate ? doc.metadata.publicationDate : doc.stat.mtime
|
83 | };
|
84 | });
|
85 |
|
86 | var maxItems;
|
87 | if (typeof blogcfg.maxItems === 'undefined') {
|
88 | maxItems = 60;
|
89 | } else if (blogcfg.maxItems <= 0) {
|
90 | maxItems = undefined;
|
91 | } else {
|
92 | maxItems = blogcfg.maxItems;
|
93 | }
|
94 |
|
95 | if (maxItems) {
|
96 | let rssitems2 = [];
|
97 | let count = 0;
|
98 | for (let item of rssitems) {
|
99 | if (count < maxItems) {
|
100 | rssitems2.push(item);
|
101 | // console.log(`${blogkey} PUSH ITEM ${count} ${util.inspect(item)}`);
|
102 | }
|
103 | count++;
|
104 | }
|
105 | rssitems = rssitems2;
|
106 | }
|
107 |
|
108 | // console.log(`GENERATE RSS rssitems # ${rssitems.length} maxItems ${maxItems} ${util.inspect(blogcfg)} `);
|
109 |
|
110 | // console.log(`GENERATE RSS ${config.renderDestination + blogcfg.rssurl} ${util.inspect(rssitems)}`);
|
111 |
|
112 | yield akasha.generateRSS(config, blogcfg, {
|
113 | feed_url: config.renderDestination + blogcfg.rssurl,
|
114 | pubDate: new Date()
|
115 | },
|
116 | rssitems, blogcfg.rssurl);
|
117 | }
|
118 | });
|
119 | }
|
120 | }
|
121 |
|
122 | /**
|
123 | *
|
124 | blogPodcast: {
|
125 | "news": {
|
126 | rss: {
|
127 | title: "AkashaCMS News",
|
128 | description: "Announcements and news about the AkashaCMS content management system",
|
129 | site_url: "http://akashacms.com/news/index.html",
|
130 | image_url: "http://akashacms.com/logo.gif",
|
131 | managingEditor: 'David Herron',
|
132 | webMaster: 'David Herron',
|
133 | copyright: '2015 David Herron',
|
134 | language: 'en',
|
135 | categories: [ "Node.js", "Content Management System", "HTML5", "Static website generator" ]
|
136 | },
|
137 | rssurl: "/news/rss.xml",
|
138 | matchers: {
|
139 | layouts: [ "blog.html.ejs" ],
|
140 | path: /^news\//
|
141 | }
|
142 | },
|
143 |
|
144 | "howto": {
|
145 | rss: {
|
146 | title: "AkashaCMS Tutorials",
|
147 | description: "Tutorials about using the AkashaCMS content management system",
|
148 | site_url: "http://akashacms.com/howto/index.html",
|
149 | image_url: "http://akashacms.com/logo.gif",
|
150 | managingEditor: 'David Herron',
|
151 | webMaster: 'David Herron',
|
152 | copyright: '2015 David Herron',
|
153 | language: 'en',
|
154 | categories: [ "Node.js", "Content Management System", "HTML5", "HTML5", "Static website generator" ]
|
155 | },
|
156 | rssurl: "/howto/rss.xml",
|
157 | matchers: {
|
158 | layouts: [ "blog.html.ejs" ],
|
159 | path: /^howto\//
|
160 | }
|
161 | }
|
162 | },
|
163 | *
|
164 | */
|
165 | var findBlogDocs = co.wrap(function* (config, metadata, blogcfg) {
|
166 |
|
167 | var documents = yield akasha.documentSearch(config, {
|
168 | // rootPath: docDirPath,
|
169 | pathmatch: blogcfg.matchers.path ? blogcfg.matchers.path : undefined,
|
170 | renderers: [ akasha.HTMLRenderer ],
|
171 | layouts: blogcfg.matchers.layouts ? blogcfg.matchers.layouts : undefined,
|
172 | rootPath: blogcfg.rootPath ? blogcfg.rootPath : undefined
|
173 | });
|
174 |
|
175 | // console.log('findBlogDocs '+ util.inspect(documents));
|
176 | documents.sort((a, b) => {
|
177 | var aPublicationDate = Date.parse(
|
178 | a.metadata.publicationDate ? a.metadata.publicationDate : a.stat.mtime
|
179 | );
|
180 | var bPublicationDate = Date.parse(
|
181 | b.metadata.publicationDate ? b.metadata.publicationDate : b.stat.mtime
|
182 | );
|
183 | if (aPublicationDate < bPublicationDate) return -1;
|
184 | else if (aPublicationDate === bPublicationDate) return 0;
|
185 | else return 1;
|
186 | });
|
187 | documents.reverse();
|
188 | return documents;
|
189 | });
|
190 |
|
191 | function findBlogIndexes(config, metadata, blogcfg) {
|
192 | if (!blogcfg.indexmatchers) return Promise.resolve([]);
|
193 |
|
194 | return akasha.documentSearch(config, {
|
195 | pathmatch: blogcfg.indexmatchers.path ? blogcfg.indexmatchers.path : undefined,
|
196 | renderers: [ akasha.HTMLRenderer ],
|
197 | layouts: blogcfg.indexmatchers.layouts ? blogcfg.indexmatchers.layouts : undefined,
|
198 | rootPath: blogcfg.rootPath ? blogcfg.rootPath : undefined
|
199 | });
|
200 | }
|
201 |
|
202 | module.exports.mahabhuta = new mahabhuta.MahafuncArray("akashacms-blog-podcast", {});
|
203 |
|
204 | class BlogNewsRiverElement extends mahabhuta.CustomElement {
|
205 | get elementName() { return "blog-news-river"; }
|
206 | process($element, metadata, dirty) {
|
207 | var blogtag = $element.attr("blogtag");
|
208 | if (!blogtag) {
|
209 | blogtag = metadata.blogtag;
|
210 | }
|
211 | if (!blogtag) {// no blog tag, skip? error?
|
212 | error("NO BLOG TAG in blog-news-river"+ metadata.document.path);
|
213 | throw new Error("NO BLOG TAG in blog-news-river"+ metadata.document.path);
|
214 | }
|
215 |
|
216 | // log('blog-news-river '+ blogtag +' '+ metadata.document.path);
|
217 |
|
218 | var blogcfg = metadata.config.pluginData(pluginName).bloglist[blogtag];
|
219 | if (!blogcfg) throw new Error('No blog configuration found for blogtag '+ blogtag);
|
220 |
|
221 | var _blogcfg = {};
|
222 | for (var key in blogcfg) {
|
223 | _blogcfg[key] = blogcfg[key];
|
224 | }
|
225 |
|
226 | var maxEntries = $element.attr('maxentries');
|
227 |
|
228 | var template = $element.attr("template");
|
229 | if (!template) template = "blog-news-river.html.ejs";
|
230 |
|
231 | var rootPath = $element.attr('root-path');
|
232 | if (rootPath) {
|
233 | _blogcfg.rootPath = rootPath;
|
234 | }
|
235 |
|
236 | var docRootPath = $element.attr('doc-root-path');
|
237 | if (docRootPath) {
|
238 | _blogcfg.rootPath = path.dirname(docRootPath);
|
239 | }
|
240 |
|
241 | return findBlogDocs(metadata.config, metadata, _blogcfg)
|
242 | .then(documents => {
|
243 |
|
244 | // log('blog-news-river documents '+ util.inspect(documents));
|
245 |
|
246 | var count = 0;
|
247 | var documents2 = documents.filter(doc => {
|
248 | if (typeof maxEntries === "undefined"
|
249 | || (typeof maxEntries !== "undefined" && count++ < maxEntries)) {
|
250 | return true;
|
251 | } else return false;
|
252 | });
|
253 | // log('blog-news-river documents2 '+ util.inspect(documents2));
|
254 |
|
255 | return akasha.partial(metadata.config, template, {
|
256 | documents: documents2,
|
257 | feedUrl: _blogcfg.rssurl
|
258 | });
|
259 | });
|
260 | }
|
261 | }
|
262 | module.exports.mahabhuta.addMahafunc(new BlogNewsRiverElement());
|
263 |
|
264 | class BlogNewsIndexElement extends mahabhuta.CustomElement {
|
265 | get elementName() { return "blog-news-index"; }
|
266 | process($element, metadata, dirty) {
|
267 | var blogtag = $element.attr("blogtag");
|
268 | if (!blogtag) {
|
269 | blogtag = metadata.blogtag;
|
270 | }
|
271 | if (!blogtag) {// no blog tag, skip? error?
|
272 | error("NO BLOG TAG in blog-news-index"+ metadata.document.path);
|
273 | throw new Error("NO BLOG TAG in blog-news-index"+ metadata.document.path);
|
274 | }
|
275 |
|
276 | var blogcfg = metadata.config.pluginData(pluginName).bloglist[blogtag];
|
277 | if (!blogcfg) return done(new Error('No blog configuration found for blogtag '+ blogtag));
|
278 |
|
279 | var template = $element.attr("template");
|
280 | if (!template) template = "blog-news-indexes.html.ejs";
|
281 |
|
282 | return findBlogIndexes(metadata.config, metadata, blogcfg)
|
283 | .then(indexDocuments => {
|
284 | return akasha.partial(metadata.config, template, { indexDocuments });
|
285 | });
|
286 | }
|
287 | }
|
288 | module.exports.mahabhuta.addMahafunc(new BlogNewsIndexElement());
|
289 |
|
290 | class BlogRSSIconElement extends mahabhuta.CustomElement {
|
291 | get elementName() { return "blog-rss-icon"; }
|
292 | process($element, metadata, dirty) {
|
293 | var blogtag = $element.attr("blogtag");
|
294 | if (!blogtag) {
|
295 | blogtag = metadata.blogtag;
|
296 | }
|
297 | if (!blogtag) {// no blog tag, skip? error?
|
298 | error("NO BLOG TAG in blog-rss-icon"+ metadata.document.path);
|
299 | throw new Error("NO BLOG TAG in blog-rss-icon"+ metadata.document.path);
|
300 | }
|
301 |
|
302 | var blogcfg = metadata.config.pluginData(pluginName).bloglist[blogtag];
|
303 | if (!blogcfg) throw new Error('No blog configuration found for blogtag '+ blogtag);
|
304 |
|
305 | var template = $element.attr("template");
|
306 | if (!template) template = "blog-rss-icon.html.ejs";
|
307 |
|
308 | return akasha.partial(metadata.config, template, {
|
309 | feedUrl: blogcfg.rssurl
|
310 | });
|
311 | }
|
312 | }
|
313 | module.exports.mahabhuta.addMahafunc(new BlogRSSIconElement());
|
314 |
|
315 | module.exports.mahabhuta.addMahafunc([
|
316 | function($, metadata, dirty, done) {
|
317 | if (! metadata.blogtag) {return done(); }
|
318 | var blogcfg = metadata.config.pluginData(pluginName).bloglist[metadata.blogtag];
|
319 | if (!blogcfg) return done(new Error('No blog configuration found for blogtag '+ metadata.blogtag +' in '+ metadata.document.path));
|
320 | var elements = [];
|
321 | $('blog-next-prev').each(function(i, elem) { elements.push(elem); });
|
322 | if (elements.length > 0) {
|
323 | // log('blog-next-prev');
|
324 | findBlogDocs(metadata.config, metadata, blogcfg)
|
325 | .then(documents => {
|
326 | async.eachSeries(elements,
|
327 | (element, next) => {
|
328 | var docIndex = -1;
|
329 | for (var j = 0; docIndex === -1 && j < documents.length; j++) {
|
330 | // log(`blog-next-prev ${documents[j].docpath} === ${metadata.document.path}`);
|
331 | if (documents[j].docpath === metadata.document.path) {
|
332 | docIndex = j;
|
333 | }
|
334 | }
|
335 | if (docIndex >= 0) {
|
336 | var prevDoc = docIndex === 0 ? documents[documents.length - 1] : documents[docIndex - 1];
|
337 | var nextDoc = docIndex === documents.length - 1 ? documents[0] : documents[docIndex + 1];
|
338 | akasha.partial(metadata.config, 'blog-next-prev.html.ejs', {
|
339 | prevDoc, nextDoc
|
340 | })
|
341 | .then(html => {
|
342 | $(element).replaceWith(html);
|
343 | next();
|
344 | })
|
345 | .catch(err => { next(err); });
|
346 | } else {
|
347 | next(new Error('did not find document '+ metadata.document.path +' in blog'));
|
348 | }
|
349 | },
|
350 | err => {
|
351 | if (err) done(err);
|
352 | else done();
|
353 | });
|
354 | })
|
355 | .catch(err => { done(err); });
|
356 | } else done();
|
357 | }
|
358 | ]);
|
359 |
|
\ | No newline at end of file |