1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var path = require('path');
|
22 | var util = require('util');
|
23 | var url = require('url');
|
24 | var async = require('async');
|
25 |
|
26 | var logger;
|
27 | var akasha;
|
28 | var config;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | module.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 |
|
42 | var 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 |
|
65 | module.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 |
|
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 |
|
134 |
|
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 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | },
|
163 | function(err) {
|
164 | if (err) done(err);
|
165 | else done();
|
166 | });
|
167 | });
|
168 | }
|
169 | ]; |
\ | No newline at end of file |