Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Akeda Bagus committed Oct 20, 2012
0 parents commit 8b92699
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
@@ -0,0 +1,32 @@
Preview Github README.md
========================

## Requirements ##
* PHP 5.3+
* curl enabled
* Internet connection to send POST request to Github API

## How to use ##
You have project that need to be published onto Github and you're not
sure about how the README.md will render in your repository's homepage.
Assuming your local development is accessible via `http://localhost/`
and you're cloning this preview scripts into the docroot of your localhost.
Now you can preview your local README.md:

```
http://localhost/preview_github_readme.php
```

or you can use PHP built-in server:

```
php -S localhost:8080
```

and open above url with specified port.

`preview_github_readme.php` looks for passed arg `?file=/path/to/readme.md` and
if the file exists, send it to Github Markdown's API, otherwise lookup README.md,
Readme.md, or readme.md file in root directory.

The markup and stylesheet for previewing are copied from Github.
7 changes: 7 additions & 0 deletions preview_github_readme.css

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions preview_github_readme.html
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="imagetoolbar" content="false" />
<meta name="MSSmartTagsPreventParsing" content="true" />
<title>Preview GitHub Readme.md</title>

<link href="/preview_github_readme.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<div id="readme" class="clearfix announce instapaper_body md">
<article class="markdown-body entry-content">
%markdown%
</article>
</div>
</div>
</body>
</html>
64 changes: 64 additions & 0 deletions preview_github_readme.php
@@ -0,0 +1,64 @@
<?php
/**
* Preview your local github Readme.md.
* @author Akeda Bagus <admin@gedex.web.id>
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*/

$url = 'https://api.github.com/markdown/raw';
$readme_file = null;
$template = file_get_contents("preview_github_readme.html");

/**
* Lookup readme file by checking query args and common filename
* on current directory
*/
function lookup_readme_file() {
global $readme_file;

// First check is via GET param
if ( isset( $_GET['file'] ) && !empty( $_GET['file'] ) ) {
$readme_file = $_GET['file'];
if ( file_exists($readme_file) ) {
return;
}
}

$readme_candidates = array( 'README.md', 'Readme.md', 'readme.md' );
foreach ( $readme_candidates as $readme ) {
if ( file_exists($readme) ) {
$readme_file = $readme;
return;
}
}

$readme_file = null;
}
lookup_readme_file();

if ( !$readme_file ) {
trigger_error("No README.md file found!", E_USER_ERROR);
}

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($readme_file));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/plain'));

$response = curl_exec($ch);
curl_close($ch);

ob_start(function($buffer) {
global $template, $response;

$tpl = file_get_contents($readme_file);
return (str_replace("%markdown%", $response, $buffer));
});

echo $template;

ob_end_flush();

0 comments on commit 8b92699

Please sign in to comment.