Skip to content

Instantly share code, notes, and snippets.

@ErwanLeroux
Forked from cobusc/gfm2html.php
Last active December 3, 2020 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErwanLeroux/ae7a7083e16f31995b69 to your computer and use it in GitHub Desktop.
Save ErwanLeroux/ae7a7083e16f31995b69 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
/*
Permalink : https://gist.github.com/ErwanLeroux/ae7a7083e16f31995b69
Forked from https://gist.github.com/cobusc/8707593/1cf6eb7b932890a8bcddf927c95091154bede377
*/
<?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");
//TODO supprimer l'extension du fichier dans la balise titre
fwrite($fp, '
<!DOCTYPE html>
<html>
<head>
<meta charset=\'utf-8\'>
<title>Shaarli - '.basename($INPUT).'</title>
<style type="text/css">code{white-space: pre;}</style>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="github-markdown.css">
</head>
<body>
<h1 id="'.basename($INPUT).'">'.basename($INPUT).'</h1>
');
$options = array(
CURLOPT_URL => $RENDER_URL,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_FILE => $fp,
CURLOPT_USERAGENT => "ErwanLeroux"
);
curl_setopt_array($ch,$options);
curl_exec($ch);
curl_close($ch);
fwrite($fp,'
</body>
</html>');
fclose($fp);
?>
@ErwanLeroux
Copy link
Author

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