/
markdown.module
460 lines (392 loc) · 13.3 KB
/
markdown.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
/**
* @file
* Provides a Markdown input filter.
*/
/**
* Implements hook_help().
*/
function markdown_help($path, $arg) {
switch ($path) {
case 'admin/help#markdown':
return t('<p>The Markdown filter allows you to enter content using <a href="http://daringfireball.net/projects/markdown">Markdown</a>, a simple plain-text syntax that is transformed into valid XHTML.</p>');
}
}
/**
* Implements hook_filter_info().
*/
function markdown_filter_info() {
$filters['filter_markdown'] = array(
'title' => t('Markdown'),
'description' => t('Allows content to be submitted using Markdown, a simple plain-text syntax that is filtered into valid XHTML.'),
'process callback' => '_filter_markdown',
'settings callback' => '_filter_markdown_settings',
'tips callback' => '_filter_markdown_tips',
);
return $filters;
}
/**
* Returns the markdown input filter tips.
* @TODO: make it easier for translators.
*/
function _filter_markdown_tips($format, $long = FALSE) {
if ($long) {
return t('Quick Tips:<ul>
<li>Two or more spaces at a line\'s end = Line break</li>
<li>Double returns = Paragraph</li>
<li>*Single asterisks* or _single underscores_ = <em>Emphasis</em></li>
<li>**Double** or __double__ = <strong>Strong</strong></li>
<li>This is [a link](http://the.link.example.com "The optional title text")</li>
</ul>For complete details on the Markdown syntax, see the <a href="http://daringfireball.net/projects/markdown/syntax">Markdown documentation</a> and <a href="http://michelf.com/projects/php-markdown/extra/">Markdown Extra documentation</a> for tables, footnotes, and more.');
}
else {
return t('You can use <a href="@filter_tips">Markdown syntax</a> to format and style the text. Also see <a href="@markdown_extra">Markdown Extra</a> for tables, footnotes, and more.', array('@filter_tips' => url('filter/tips'), '@markdown_extra' => 'http://michelf.com/projects/php-markdown/extra/'));
}
}
/**
* Provides content for the markdown help block.
*/
function _markdown_help_block() {
return '<pre>' . t("
## Header 2 ##
### Header 3 ###
#### Header 4 ####
##### Header 5 #####
(Hashes on right are optional)
Link [Drupal](http://drupal.org)
Inline markup like _italics_,
**bold**, and `code()`.
> Blockquote. Like email replies
>> And, they can be nested
* Bullet lists are easy too
- Another one
+ Another one
1. A numbered list
2. Which is numbered
3. With periods and a space
And now some code:
// Code is indented text
is_easy() to_remember();") . '</pre>';
}
/**
* Filter process callback.
*/
function _filter_markdown($text, $format) {
if (!empty($text)) {
module_load_include('php', 'markdown', 'lib/Markdown');
module_load_include('php', 'markdown', 'lib/Smartypants');
// yaml header
if (preg_match("/^---(?P<yaml>(.|\n)*)---/", $text, $matches) == 1) {
$yaml = $matches['yaml'];
$text = substr($text, strlen($matches[0]));
}
// replace flickr image url
//$text = preg_replace('/ /', '', $text);
//$flickr = new Flickr();
preg_match_all('/\{\{flickr:(?P<photo_id>\d+)(\|(?P<size>\w+))?\}\}/', $text, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$photo_url = get_flickr_photo_url($match['photo_id'], $match['size']);
$text = str_replace($match[0], $photo_url, $text);
}
// markdown
$text = MarkdownExtra::defaultTransform($text);
$text = SmartyPants($text);
}
return $text;
}
/**
* Filter settings callback. Just provides a version overview.
*/
function _filter_markdown_settings($form, &$form_state, $filter, $format, $defaults) {
module_load_include('php', 'markdown', 'markdown');
$settings['markdown_wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Markdown'),
);
$links = array(
'Markdown PHP Version: <a href="http://michelf.com/projects/php-markdown/">' . MARKDOWN_VERSION . '</a>',
'Markdown Extra Version: <a href="http://michelf.com/projects/php-markdown/">' . MARKDOWNEXTRA_VERSION . '</a>',
);
$settings['markdown_wrapper']['markdown_status'] = array(
'#title' => t('Versions'),
'#type' => 'item',
'#markup' => theme('item_list', array('items' => $links)),
);
return $settings;
}
/*
function markdown_node_load($nodes, $types) {
include('Spyc.php');
foreach ($nodes as $node) {
if (preg_match("/^---(?P<yaml>(.|\n)*)---/", $node->body[$node->language][0]['value'], $matches) == 1) {
//print_r($matches);
$node->yaml = Spyc::YAMLLoad($matches['yaml']);
//print_r($yaml);
//print_r($yaml['private']);
//print_r(Spyc::YAMLDump($yaml));
} else {
$node->yaml = array();
}
// set private value
if (array_key_exists('private', $node->yaml) && $node->yaml['private']) {
$node->private = TRUE;
} else {
$node->private = FALSE;
}
//print_r($node);
//drupal_exit();
//$node->private = TRUE;
//$node->mathjax = FALSE;
}
}
function markdown_node_access($node, $op, $account) {
$type = is_string($node) ? $node : $node->type;
if (in_array($type, array('blog', 'quote', 'favorite')) && !$node->private) {
return NODE_ACCESS_ALLOW;
}
return NODE_ACCESS_DENY;
}
*/
/**
*
*/
function markdown_menu() {
$items['status'] = array(
'title' => 'Status',
'page callback' => 'status_page',
'access arguments' => array('administer content'),
);
$items['cron/static'] = array(
'page callback' => 'generate_static_pages',
'access arguments' => array('administer content'),
);
$item['cron/pull-status-mailbox'] = array(
'page callback' => 'pull_status_mailbox',
'access arguments' => array('administer content'),
);
$items['node/%/edit/ajax'] = array(
'page callback' => '_node_form_ajax',
'page arguments' => array(1),
'access arguments' => array('administer content'),
'type' => MENU_CALLBACK
);
$items['node/add/%/ajax'] = array(
'page callback' => '_node_add_ajax',
'page arguments' => array(2),
'access arguments' => array('administer content'),
'type' => MENU_CALLBACK
);
return $items;
}
function status_page() {
$query = new EntityFieldQuery;
$entities = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'status')
->propertyOrderBy('created', 'DESC')
->pager(10);
$result = $query->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
$content = drupal_render(node_view_multiple($nodes, 'full'));
return $content;
$page = array('content' => $content,
'#type' => 'page'
);
return drupal_render($page);
} else {
return "No status found";
}
}
function _node_add_ajax($node_type) {
if (!isset($_POST['submit']) || !isset($_POST['body']) || $_POST['body'] == '') {
echo "no data";
print_r($_POST);
drupal_exit();
}
// have not consider other node type
if ($node_type != 'status') {
echo "not status!";
drupal_exit();
}
$title = 'status';
$body = $_POST['body'];
if ($node = _node_save($node_type, $title, $body)) {
//echo drupal_render(node_view($node));
drupal_goto('status');
} else {
echo "error when saving";
}
drupal_exit();
}
/**
* just return the node edit form
*/
function _node_form_ajax($nid_or_type) {
module_load_include('inc', 'node', 'node.pages');
if (is_numeric($nid_or_type)) {
$node = node_load($nid_or_type);
} else {
global $user;
$node = new stdClass();
$node->uid = $user->uid;
$node->name = $user->name;
$node->type = $nid_or_type;
node_object_prepare($node);
}
echo drupal_render(drupal_get_form($node->type."_node_form", $node));
drupal_exit();
}
function generate_static_pages() {
$static_root = '/var/www/chunliang.me/static';
// in the form of `node/1`
$site_frontpage = variable_get('site_frontpage', 'node');
$query = new EntityFieldQuery;
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('status', 1);
$result = $query->execute();
$base_url = 'http://chunliang.me';
$options = array('absolute' => TRUE,
'https' => FALSE,
'base_url' => $base_url);
$folder = $static_root;
$files = glob($folder . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
$url = 'node/'.$node->nid;
if ($url == $site_frontpage) {
$full_url = $base_url . '/index';
} else {
$full_url = url($url, $options);
}
$content = drupal_render(node_view($node));
$page = array(
'content' => $content,
'#type' => 'page',
'#show_messages' => FALSE
);
variable_set('static_page', true);
$html = drupal_render($page);
variable_del('static_page');
// somthing like blog.html
$path = $folder . substr($full_url, strlen($base_url)) . '.html';
$dir = dirname($path);
if (!is_dir($dir) && !mkdir($dir, 0777, true)) {
die('Failed to create folders ' . $path);
}
file_put_contents($path, $html);
}
}
$output = '';
print '<pre>';
passthru('tree ' . $static_root);
print '</pre>';
drupal_exit();
}
function pull_status_mailbox() {
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'status@chunliang.me';
$password = '%E9V7md3';
$statuses = fetch_status_mailbox($hostname, $username, $password);
$count = 0;
foreach ($statuses as $status) {
if (_node_save('status', $status['title'], $status['body'], $status)) {
$count += 1;
}
}
if ($count == count($statuses)) {
echo "successfully imported statuses: " . $count;
drupal_exit();
} else {
echo "error happens";
drupal_exit();
}
}
function _node_save($node_type, $title, $body, $optional = array()) {
$node = new stdClass();
$node->type = $node_type;
node_object_prepare($node);
$node->status = 0;
$node->promote = 0;
$node->sticky = 0;
$node->title = $title;
$node->language = LANGUAGE_NONE;
$node->uid = 1;
$node->body[$node->language][0]['value'] = $body;
//$node->body[$node->language][0]['summary'] = text_summary($body);
$node->body[$node->language][0]['format'] = 'markdown';
// since node_submit will set $node->created by $node->date
if (isset($optional['created'])) {
$node->date = $optional['created'];
}
if ($node = node_submit($node)) {
node_save($node);
return $node;
} else {
return false;
}
}
function fetch_status_mailbox($hostname, $username, $password) {
$statuses = array();
$inbox = imap_open($hostname, $username, $password) or die("Cannot connect to Gmail: " . imap_last_error());
$emails = imap_search($inbox, 'ALL');
if ($emails) {
$output = '';
rsort($emails);
foreach ($emails as $email_number) {
$uid = imap_uid($inbox, $email_number);
$header = imap_fetchheader($inbox, $email_number);
$overview = imap_fetch_overview($inbox, $email_number, 0);
$message = imap_fetchbody($inbox, $email_number, 2);
$output .= $email_number;
$output .= '<li>' . $overview[0]->subject . '</li>';
$output .= '<li>' . $overview[0]->date . '</li>';
$output .= '<li>' . $overview[0]->from . '</li>';
print_r($overview);
print_r($header);
print_r($uid);
$title = 'email:' . $uid;
echo "\n\n";
$status = array(
'title' => 'email:' . $uid,
'body' => message,
'created' => $overview->date
);
$statuses[] = $status;
}
}
imap_close($inbox);
return $statuses;
}
function get_flickr_token() {
module_load_include('php', 'markdown', 'lib/Flickr');
$f = new phpFlickr("4231c9fc6d682d17c618091def128f87", "f15dfdb9e761282c");
//change this to the permissions you will need
if (empty($_GET['frob'])) {
$f->auth("write", false);
} else {
$token = $f->auth_getToken($_GET['frob']);
echo "use this token to authenticate: ". $token['token'];
}
}
function get_flickr_photo_url($photo_id, $size='m') {
module_load_include('php', 'markdown', 'lib/Flickr');
$api_key = '4231c9fc6d682d17c618091def128f87';
$api_secret = 'f15dfdb9e761282c';
$api_token = '72157633245931161-c47f26f9fc81772a';
$flickr = null;
$flickr = new phpFlickr($api_key, $api_secret);
$flickr->setToken($api_token);
if ($photo = $flickr->photos_getInfo($photo_id)) {
return $flickr->buildPhotoURL($photo, $size);
} else {
return False;
}
}