1 |
|
2 | var Robotskirt, async, fs, hljs,
|
3 | __hasProp = {}.hasOwnProperty,
|
4 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
5 |
|
6 | async = require('async');
|
7 |
|
8 | Robotskirt = require('robotskirt');
|
9 |
|
10 | fs = require('fs');
|
11 |
|
12 | hljs = require('highlight.js');
|
13 |
|
14 | module.exports = function(env, callback) {
|
15 | var RobotskirtPage, _ref;
|
16 | RobotskirtPage = (function(_super) {
|
17 | __extends(RobotskirtPage, _super);
|
18 |
|
19 | function RobotskirtPage() {
|
20 | _ref = RobotskirtPage.__super__.constructor.apply(this, arguments);
|
21 | return _ref;
|
22 | }
|
23 |
|
24 | RobotskirtPage.prototype.getHtml = function(base) {
|
25 | var fullName, loc, name;
|
26 | if (base == null) {
|
27 | base = env.config.baseUrl;
|
28 | }
|
29 | name = this.getFilename();
|
30 | name = name.slice(name.lastIndexOf('/') + 1);
|
31 | loc = this.getLocation(base);
|
32 | fullName = name === 'index.html' ? loc : loc + name;
|
33 | this._html = this._htmlraw.replace(/(<(a|img)[^>]+(href|src)=")(#[^"]+)/g, '$1' + fullName + '$4');
|
34 | this._html = this._html.replace(/(<(a|img)[^>]+(href|src)=")(?!http|\/)([^"]+)/g, '$1' + loc + '$4');
|
35 | if (base) {
|
36 | this._html = this._html.replace(/(<(a|img)[^>]+(href|src)=")\/([^"]+)/g, '$1' + base + '$4');
|
37 | }
|
38 | return this._html;
|
39 | };
|
40 |
|
41 | RobotskirtPage.prototype.getIntro = function(base) {
|
42 | var cutoff, cutoffs, html, i, idx, _i, _len;
|
43 | html = this.getHtml(base);
|
44 | cutoffs = ['<!--more-->', '<span class="more', '<h2', '<hr'];
|
45 | idx = Infinity;
|
46 | for (_i = 0, _len = cutoffs.length; _i < _len; _i++) {
|
47 | cutoff = cutoffs[_i];
|
48 | i = html.indexOf(cutoff);
|
49 | if (i !== -1 && i < idx) {
|
50 | idx = i;
|
51 | }
|
52 | }
|
53 | if (idx !== Infinity) {
|
54 | return html.substr(0, idx);
|
55 | } else {
|
56 | return html;
|
57 | }
|
58 | };
|
59 |
|
60 | RobotskirtPage.property('hasMore', function() {
|
61 | if (this._html == null) {
|
62 | this._html = this.getHtml();
|
63 | }
|
64 | if (this._intro == null) {
|
65 | this._intro = this.getIntro();
|
66 | }
|
67 | if (this._hasMore == null) {
|
68 | this._hasMore = this._html.length > this._intro.length;
|
69 | }
|
70 | return this._hasMore;
|
71 | });
|
72 |
|
73 | RobotskirtPage.renderMarkdownIntoHtml = function(config, page) {
|
74 | var extensions, htmlFlags, isSmartypantsEnabled, markdown, renderedHtml, renderer, robotskirtExtensions, robotskirtHtmlFlags;
|
75 | extensions = config.robotskirt.extensions || [];
|
76 | htmlFlags = config.robotskirt.htmlFlags || [];
|
77 | isSmartypantsEnabled = config.robotskirt.smart || false;
|
78 | robotskirtExtensions = RobotskirtPage.convertConfigurationStringsIntoRobotskirtIDs(extensions);
|
79 | robotskirtHtmlFlags = RobotskirtPage.convertConfigurationStringsIntoRobotskirtIDs(htmlFlags);
|
80 | renderer = new Robotskirt.HtmlRenderer(robotskirtHtmlFlags);
|
81 | renderer = RobotskirtPage.defineSyntaxHighlightingForCodeBlocks(renderer);
|
82 | markdown = new Robotskirt.Markdown(renderer, robotskirtExtensions);
|
83 | renderedHtml = markdown.render(page.markdown);
|
84 | if (isSmartypantsEnabled) {
|
85 | renderedHtml = Robotskirt.smartypantsHtml(renderedHtml);
|
86 | }
|
87 | return renderedHtml;
|
88 | };
|
89 |
|
90 | RobotskirtPage.convertConfigurationStringsIntoRobotskirtIDs = function(configurationStringObject) {
|
91 | var k, robotskirtIDs, uppercaseValue, v, _i, _len;
|
92 | robotskirtIDs = [];
|
93 | for (k = _i = 0, _len = configurationStringObject.length; _i < _len; k = ++_i) {
|
94 | v = configurationStringObject[k];
|
95 | uppercaseValue = v.toUpperCase();
|
96 | robotskirtIDs[k] = Robotskirt[uppercaseValue];
|
97 | }
|
98 | return robotskirtIDs;
|
99 | };
|
100 |
|
101 | RobotskirtPage.defineSyntaxHighlightingForCodeBlocks = function(renderer) {
|
102 | renderer.blockcode = function(code, lang) {
|
103 | var error;
|
104 | if (lang != null) {
|
105 | try {
|
106 | if (lang === 'c') {
|
107 | lang = 'cpp';
|
108 | }
|
109 | return ("<div><pre><code class=\"lang-" + lang + "\">") + hljs.highlight(lang, code).value + "</code></pre></div>";
|
110 | } catch (_error) {
|
111 | error = _error;
|
112 | return code;
|
113 | }
|
114 | } else {
|
115 | lang = 'text';
|
116 | return ("<div><pre><code class=\"lang-" + lang + "\">") + code + "</code></pre></div>";
|
117 | }
|
118 | };
|
119 | return renderer;
|
120 | };
|
121 |
|
122 | return RobotskirtPage;
|
123 |
|
124 | })(env.plugins.MarkdownPage);
|
125 | RobotskirtPage.fromFile = function(filepath, callback) {
|
126 | var _this = this;
|
127 | return async.waterfall([
|
128 | function(callback) {
|
129 | return fs.readFile(filepath.full, callback);
|
130 | }, function(buffer, callback) {
|
131 | return RobotskirtPage.extractMetadata(buffer.toString(), callback);
|
132 | }, function(result, callback) {
|
133 | var markdown, metadata, page;
|
134 | markdown = result.markdown, metadata = result.metadata;
|
135 | page = new _this(filepath, metadata, markdown);
|
136 | return callback(null, page);
|
137 | }, function(page, callback) {
|
138 | var renderedHtml;
|
139 | renderedHtml = RobotskirtPage.renderMarkdownIntoHtml(env.config, page);
|
140 | page._htmlraw = renderedHtml;
|
141 | return callback(null, page);
|
142 | }, function(page, callback) {
|
143 | return callback(null, page);
|
144 | }
|
145 | ], callback);
|
146 | };
|
147 | env.registerContentPlugin('pages', '**/*.*(markdown|mkd|md)', RobotskirtPage);
|
148 | env.helpers.RobotskirtPage = RobotskirtPage;
|
149 | return callback();
|
150 | };
|