-
Notifications
You must be signed in to change notification settings - Fork 1
/
md.php
30 lines (23 loc) · 1.05 KB
/
md.php
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
<?php
$templateFileName = './template.html';
require_once('./lib/markdown/markdown.php');
$contents = file($_SERVER['DOCUMENT_ROOT']. preg_replace('/\.html$/', '.md', $_SERVER['REQUEST_URI']));
//メタデータの抜き出し
$re = '/^\s*\$(?<key>[a-zA-Z][a-zA-Z0-9_]*((\.|:)[a-zA-Z][a-zA-Z0-9_]*)*)\s*=\s*(?<val>.*)\s*$/';
$metas = array(); $match = array();
while (($row = array_shift($contents)) && preg_match($re, $row, $match))
$metas[$match['key']] = trim($match['val']);
while (trim($row) == '') $row = array_shift($contents);
$title = trim($row, '#\n\r\t ');
array_unshift($contents, $row);//タイトル行を戻す
$body = Markdown(implode('', $contents));
$meta = '';
foreach ($metas as $key=>$val)
$meta .= "<meta name=\"$key\" content=\"$val\" />";
//テンプレート読み込み
$html = file_get_contents($templateFileName);
//テンプレート変数の置換え
$html = str_replace('<!-- TITLE -->', $title, $html);
$html = str_replace('<!-- BODY -->', $body, $html);
$html = str_replace('<!-- META -->', $meta, $html);
echo $html;