Skip to content

Instantly share code, notes, and snippets.

@neckro
Created August 29, 2013 19:25
Show Gist options
  • Save neckro/6382342 to your computer and use it in GitHub Desktop.
Save neckro/6382342 to your computer and use it in GitHub Desktop.
Quick and dirty PHP script to convert Markdown to HTML for web display
<?php
$file = realpath(@$_GET['f']);
$p = pathinfo($file);
if (!$file || strtolower(@$p['extension']) !== 'md') {
http_response_code(403);
exit;
}
$cmd = sprintf(
'./md/peg-multimarkdown/multimarkdown "%s" --smart | sed "s/\&#8211\;/\&mdash\;/g"',
escapeshellcmd($file)
);
$html = shell_exec($cmd);
// 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" />
<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