Skip to content

Instantly share code, notes, and snippets.

@neckro
Created July 6, 2020 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neckro/f0c073a879b50a48b9e5b9516b0f5876 to your computer and use it in GitHub Desktop.
Save neckro/f0c073a879b50a48b9e5b9516b0f5876 to your computer and use it in GitHub Desktop.
<?php
error_reporting(-1);
$file = realpath(@$_GET['f']);
$p = pathinfo($file);
if (!$file || strtolower(@$p['extension']) !== 'md') {
http_response_code(403);
exit;
}
$cmd = sprintf(
'./peg-multimarkdown/multimarkdown %s --smart',
escapeshellcmd($file)
);
$html = html_entity_decode(
preg_replace(
[
'/&#8211;(\d)/', // wrong dash before number
'/&#8211;/', // wrong em-dash
],
[
'&#8209;\1', // non-breaking hyphen
'&mdash;' // proper em-dash
],
shell_exec($cmd)
),
ENT_HTML5,
'UTF-8'
);
// set the title to the contents of the first header tag
if (preg_match('/<(h[1-4][^>]*>)(.*)<\//U', $html, $matches)) {
// strip extraneous HTML tags
$title = preg_replace('/<.*>/U', '', $matches[2]);
} else {
$title = str_replace('_', ' ', $p['filename']);
}
header("Content-Type: text/html");
?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link href="/cgi/static/markdown.css" rel="stylesheet" />
<title><?= $title ?></title>
</head>
<body>
<?= $html ?>
<?php /* <a href="?orig" style="display: block; position: absolute; right: 0; margin-top: 1em; padding: .2em; line-height: 0.8em; background-color: #ddd; font-size: .8em; font-family: monospace;"><?= "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ?></a> */?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment