Last active
August 23, 2017 00:54
-
-
Save mitry/45dda329dfd1090101f2119389cfdf59 to your computer and use it in GitHub Desktop.
markdown apache handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @usage: | |
| * .htaccess or Apache httpd.conf file: | |
| * ``` | |
| * AddType text/markdown .md .markdown .mrd | |
| * Action markdown /cgi-bin/md.php | |
| * AddHandler markdown .md .markdown .md.txt | |
| * ``` | |
| */ | |
| $disqus_id = 'you_disqus_name'; | |
| $path = realpath(@$_SERVER['PATH_TRANSLATED']); | |
| $file = pathinfo($path); | |
| // In .htaccess `SetEnv MARKDOWN_NO_COMMENTS 1` | |
| $need_comments = ! (@$_SERVER['MARKDOWN_NO_COMMENTS'] || @$_SERVER['REDIRECT_MARKDOWN_NO_COMMENTS']); | |
| if (!$file || !in_array(strtolower(@$file['extension']), array('md', 'markdown', 'text', 'txt'))) { | |
| header("Status: 404 Not Found", true, 404); | |
| exit; | |
| } | |
| // Показать исходный код страницы | |
| if(@$_SERVER['REDIRECT_QUERY_STRING'] === 'source') { | |
| // сбрасываем буфер вывода PHP, чтобы избежать переполнения памяти выделенной под скрипт | |
| // если этого не сделать файл будет читаться в память полностью! | |
| if (ob_get_level()) ob_end_clean(); | |
| header('Content-Type: text/plain; charset=UTF-8'); | |
| header('Content-Disposition: inline; filename=' . basename($path)); | |
| header('Expires: 0'); | |
| header('Cache-Control: must-revalidate'); | |
| header('Pragma: public'); | |
| header('Content-Length: ' . filesize($path)); | |
| readfile($path); // читаем файл и отправляем его пользователю | |
| exit; | |
| } | |
| require_once 'Michelf/MarkdownExtra.inc.php'; | |
| use Michelf\MarkdownExtra; | |
| function Markdown($s) {return MarkdownExtra::defaultTransform($s);} | |
| include_once 'smartypants-typographer.php'; | |
| ?> | |
| <!DOCTYPE html> | |
| <html lang="ru"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title><?= $file['filename']?></title> | |
| <?php if(1): // Syntax highlight using cdnjs ?> | |
| <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/solarized-light.min.css" rel="stylesheet" > | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script> | |
| <?php else: // or using Yandex CDN ?> | |
| <link href="//yandex.st/highlightjs/7.3/styles/solarized_light.min.css" rel="stylesheet" > | |
| <script src="//yandex.st/highlightjs/7.3/highlight.min.js"></script> | |
| <?php endIf ?> | |
| <script>hljs.initHighlightingOnLoad();</script> | |
| <link href="/static/markdown.css<?php echo '?' . filemtime(__DIR__ . '\\..\\static\\markdown.css') ?>" rel="stylesheet" > | |
| </head> | |
| <body id="page-<?= preg_replace('/[^[:alnum:]]+/','-',$file['filename']) ?>" class="standart"> | |
| <article><?= SmartyPants(Markdown(file_get_contents($path))) ?></article> | |
| <?php if ($need_comments) : ?> | |
| <footer class="comments well"> | |
| <div id="disqus_thread"></div> | |
| <script src="//<?= $disqus_id ?>.disqus.com/embed.js" async="true"></script> | |
| </footer> | |
| <?php endIf ?> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment