UNPKG

5.25 kBJavaScriptView Raw
1/**
2 *
3 * Copyright 2015 David Herron
4 *
5 * This file is part of AkashaCMS-embeddables (http://akashacms.com/).
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20
21var path = require('path');
22var util = require('util');
23var url = require('url');
24var async = require('async');
25
26var logger;
27var akasha;
28var config;
29
30/**
31 * Add ourselves to the config data.
32 **/
33module.exports.config = function(_akasha, _config) {
34 akasha = _akasha;
35 config = _config;
36 logger = akasha.getLogger("blog-podcast");
37 config.root_partials.push(path.join(__dirname, 'partials'));
38
39 return module.exports;
40};
41
42var findBlogDocs = function(config, metadata, blogcfg) {
43 var documents = akasha.findMatchingDocuments(blogcfg.matchers);
44
45 documents.sort(function(a, b) {
46 var aPublicationDate = Date.parse(
47 a.frontmatter.yaml.publicationDate
48 ? a.frontmatter.yaml.publicationDate
49 : a.stat.mtime
50 );
51 var bPublicationDate = Date.parse(
52 b.frontmatter.yaml.publicationDate
53 ? b.frontmatter.yaml.publicationDate
54 : b.stat.mtime
55 );
56 if (aPublicationDate < bPublicationDate) return -1;
57 else if (aPublicationDate === bPublicationDate) return 0;
58 else return 1;
59 });
60 documents.reverse();
61
62 return documents;
63};
64
65module.exports.mahabhuta = [
66 function($, metadata, dirty, done) {
67 var elements = [];
68 var documents, blogcfg;
69 $('blog-news-river').each(function(i, elem) { elements.push(elem); });
70 if (elements.length > 0) {
71 blogcfg = config.blogPodcast[metadata.blogtag];
72 documents = findBlogDocs(config, metadata, blogcfg);
73 }
74 async.eachSeries(elements, function(element, next) {
75 if (! metadata.blogtag) {
76 next(new Error("no blogtag"));
77 } else if (! config.blogPodcast.hasOwnProperty(metadata.blogtag)) {
78 next(new Error("no blogPodcast item for "+ metadata.blogtag));
79 }
80
81 // console.log(element.name +' '+ metadata.blogtag);
82
83 var rssitems = [];
84 for (var q = 0; q < documents.length; q++) {
85 var doc = documents[q];
86 rssitems.push({
87 title: doc.frontmatter.yaml.title,
88 description: doc.frontmatter.yaml.teaser ? doc.frontmatter.yaml.teaser : "",
89 url: config.root_url +'/'+ doc.renderedFileName,
90 date: doc.frontmatter.yaml.publicationDate
91 ? doc.frontmatter.yaml.publicationDate
92 : doc.stat.mtime
93 });
94 }
95
96 var feedRenderTo = blogcfg.rssurl;
97 akasha.generateRSS(blogcfg.rss, {
98 feed_url: config.root_url + feedRenderTo,
99 pubDate: new Date()
100 },
101 rssitems, feedRenderTo, function(err) {
102 if (err) logger.error(err);
103 });
104
105 akasha.partial("blog-news-river.html.ejs", {
106 documents: documents,
107 feedUrl: feedRenderTo
108 },
109 function(err, htmlRiver) {
110 if (err) next(err);
111 else {
112 $(element).replaceWith(htmlRiver);
113 next();
114 }
115 });
116 },
117 function(err) {
118 if (err) done(err);
119 else done();
120 });
121 },
122
123 function($, metadata, dirty, done) {
124 var elements = [];
125 var documents;
126 akasha.readDocumentEntry(metadata.documentPath, function(err, docEntry) {
127 $('blog-next-prev').each(function(i, elem) { elements.push(elem); });
128 if (elements.length > 0) {
129 blogcfg = config.blogPodcast[metadata.blogtag];
130 documents = findBlogDocs(config, metadata, blogcfg);
131 }
132 async.eachSeries(elements, function(element, next) {
133 // what's the current document
134 // find it within documents
135 var docIndex = -1;
136 for (var j = 0; j < documents.length; j++) {
137 if (documents[j].path === docEntry.path) {
138 docIndex = j;
139 }
140 }
141 if (docIndex >= 0) {
142 var prevDoc = docIndex === 0 ? documents[documents.length - 1] : documents[docIndex - 1];
143 var nextDoc = docIndex === documents.length - 1 ? documents[0] : documents[docIndex + 1];
144 akasha.partial('blog-next-prev.html.ejs', {
145 prevDoc: prevDoc, nextDoc: nextDoc, thisDoc: docEntry, documents: documents
146 }, function(err, html) {
147 if (err) next(err);
148 else {
149 $(element).replaceWith(html);
150 next();
151 }
152 });
153 } else {
154 next(new Error('did not find document in blog'));
155 }
156 // prevDoc =
157 // nextDoc =
158 // akasha.partial('blog-next-prev.html.ejs', {
159 // prevDoc: prevDoc, nextDoc: nextDoc
160 // })
161 // next();
162 },
163 function(err) {
164 if (err) done(err);
165 else done();
166 });
167 });
168 }
169];
\No newline at end of file