Skip to content

Instantly share code, notes, and snippets.

@cobusc
Last active December 3, 2020 19:30
Show Gist options
  • Save cobusc/8707593 to your computer and use it in GitHub Desktop.
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).
#!/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);
?>
@cobusc
Copy link
Author

cobusc commented Jan 30, 2014

One can use html2ps and then ps2pdf to convert the generated HTML to a PDF document.
Note that html2ps can generate a table of contents, if required.

@ErwanLeroux
Copy link

A semi colon is missing line 7

Thanks for the script !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment