Last active
December 17, 2015 10:39
-
-
Save ezzatron/5596411 to your computer and use it in GitHub Desktop.
Filthy h4x for converting GitHub flavoured Markdown to HTML with identical styling.
This file contains 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
#!/usr/bin/env php | |
<?php | |
$header = <<<'EOD' | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>{title}</title> | |
<link href="http://sindresorhus.com/github-markdown-css/github-markdown.css" media="all" rel="stylesheet" type="text/css"> | |
<style> | |
.markdown-body { | |
min-width: 200px; | |
max-width: 918px; | |
margin: 0 auto; | |
padding: 30px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div id="readme"> | |
<article class="markdown-body entry-content" itemprop="mainContentOfPage"> | |
EOD; | |
$footer = <<<'EOD' | |
</article> | |
</div> | |
</div> | |
</body> | |
</html> | |
EOD; | |
if (!isset($_SERVER['argv'][1])) { | |
die('Path required.'); | |
} | |
$data = file_get_contents($_SERVER['argv'][1]); | |
if (false === $data) { | |
die('Unable to load markdown.'); | |
} | |
$url = 'https://api.github.com/markdown/raw'; | |
$options = array( | |
'http' => array( | |
'user_agent' => 'PHP', | |
'header' => "Content-type: text/plain\r\n", | |
'method' => 'POST', | |
'content' => $data, | |
), | |
); | |
$context = stream_context_create($options); | |
$content = file_get_contents($url, false, $context); | |
$content = str_replace('id="user-content-', 'id="', $content); | |
$content = str_replace('name="user-content-', 'name="', $content); | |
preg_match('/<h1>.*?\s*([^>]*?)\s*<\/h1>/s', $content, $matches); | |
printf('%s%s%s', str_replace('{title}', $matches[1], $header), $content, $footer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment