Last active
December 3, 2020 19:30
-
-
Save cobusc/8707593 to your computer and use it in GitHub Desktop.
Example script that will convert Github Flavoured Markdown text into HTML (using Github's API).
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 | |
$RENDER_URL = "https://api.github.com/markdown"; | |
$INPUT = $argv[1]; | |
$OUTPUT = $argv[2] | |
$payload["text"] = file_get_contents($INPUT); | |
$payload["mode"] = "markdown"; | |
$payload["context"] = ""; | |
$ch = curl_init($RENDER_URL); | |
$fp = fopen($OUTPUT, "w"); | |
fwrite($fp, ' | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=\'utf-8\'> | |
<title>'.basename($INPUT).'</title> | |
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | |
<link href="https://github.global.ssl.fastly.net/assets/github-6f5a51551139a0f4ed187834f9522a150751a30c.css" media="all" rel="stylesheet" type="text/css" /> | |
<link href="https://github.global.ssl.fastly.net/assets/github2-768582e3e2bdce1c2130a3d4d736780c2218f756.css" media="all" rel="stylesheet" type="text/css" /> | |
</head> | |
<body class="logged_in env-production linux vis-private page-blob"> | |
<div class="wrapper"> | |
<div id="readme" class="blob instapaper_body"> | |
<div class="markdown-body entry-content" itemprop="mainContentOfPage"> | |
'); | |
$options = array( | |
CURLOPT_URL => $RENDER_URL, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => json_encode($payload), | |
CURLOPT_FILE => $fp, | |
CURLOPT_USERAGENT => "cobusc" | |
); | |
curl_setopt_array($ch,$options); | |
curl_exec($ch); | |
curl_close($ch); | |
fwrite($fp,' | |
</div> | |
</div> | |
</div> | |
</body> | |
</html>'); | |
fclose($fp); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One can use
html2ps
and thenps2pdf
to convert the generated HTML to a PDF document.Note that
html2ps
can generate a table of contents, if required.