Skip to content

Instantly share code, notes, and snippets.

@xuwangyin
Created April 25, 2016 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwangyin/8259c4e2ba3338ab2b1c399583bf6ec8 to your computer and use it in GitHub Desktop.
Save xuwangyin/8259c4e2ba3338ab2b1c399583bf6ec8 to your computer and use it in GitHub Desktop.
find my_guardian_json_file_dir -name "*.json" > guardian_json_files; php render_guardian_json_files.php guardian_json_files > guardian_articles.html; open guardian_articles.html;
<?php
error_reporting(E_ERROR | E_PARSE);
function get_content($body) {
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$content = "";
if ($doc->loadXML($body)) {
foreach ($doc->getElementsByTagName("body") as $t) {
foreach ($t->getElementsByTagName("paragraph") as $p) {
$content = $content . '<p>' . $p->textContent . '</p>';
}
}
} else {
$doc->loadHTML($body);
$bodytext = $doc->textContent;
foreach (explode("\n", $bodytext) as $line) {
$content = $content . '<p>' . trim($line) . '</p>';
}
}
return $content;
}
function output_before_body() {
echo '<!DOCTYPE html>';
echo '<html lang="en">';
echo '<head>';
echo '<title>News Article Listing</title>';
echo '<meta charset="utf-8">';
echo '<meta name="viewport" content="width=device-width, initial-scale=1">';
echo '<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">';
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>';
echo '<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>';
echo '</head>';
echo '<body>';
echo '<div class="container">';
echo '<div class="col-sm-12">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<p id="article_number">Loading...</p>';
echo '</div>';
echo '<div class="panel-body">';
echo '<table class="table table-striped story-table">';
echo '<tbody>';
}
function output_after_body($article_counter) {
echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<script>';
echo "document.getElementById(\"article_number\").innerHTML = \"Rendered $article_counter Articles\"";
echo '</script>';
echo '</body>';
echo '</html>';
}
function output_body($article_counter, $title, $content, $summary, $update_time, $url, $filepath) {
$filename = basename($filepath);
echo '<tr>';
echo "<td class=\"table-text\"><div><p><strong><a href=\"_\"data-toggle=\"modal\" data-target=\"#$article_counter\">$title</a></strong></p> <p><a target=\"_blank\" href=\"$filepath\">$filename</a></p> </div></td>";
/* echo "<td> <a class=\"btn\" data-toggle=\"modal\" data-target=\"#$article_counter\"> Details » </a> </td>"; */
echo "<div class=\"modal fade\" id=\"$article_counter\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\">";
echo '<div class="modal-dialog modal-lg" role="document">';
echo '<div class="modal-content">';
echo '<div class="modal-header">';
echo '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
echo "<h4 class=\"modal-title\" id=\"myModalLabel\">$title</h4>";
echo "<a target=\"_blank\" href=\"$url\">$url</a>";
echo "<p>$update_time</p>";
echo '</div>';
echo '<div class="modal-body">';
echo '<div class="row">';
echo '<div class="col-sm-6">';
echo "<p><strong>$summary</strong></p>";
echo "<p>$content</p>";
echo '</div>';
echo '<div class="col-sm-6">';
}
function output_img($caption, $href, $alt, $width, $height, $extra) {
echo "<img width=\"$width\" height=\"$height\" src=\"$href\">";
if (!empty(trim($caption)))
echo "<p>caption: \"$caption\"</p>";
if (!empty(trim($alt)))
echo "<p>alt: \"$alt\"</p>";
echo $extra;
echo "<div> &nbsp;&nbsp; </div>";
}
function output_after_img() {
echo '</div>'; /* col-md-4 */
echo '</div>'; /* row */
echo '</div>';
echo '<div class="modal-footer">';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</tr>';
}
output_before_body();
$handle = fopen($argv[1], "r");
if ($handle === FALSE)
die("Cannot open file: " . $argv[1]);
$article_counter = 0;
while (($line = fgets($handle)) !== false) {
$filepath = trim($line);
$string = file_get_contents(trim($line));
if ($string === FALSE)
continue;
$article_counter += 1;
$json_a = json_decode($string, true);
if ($json_a == NULL)
continue;
$title = $json_a['title'];
$content = get_content($json_a['body']);
$summary = $json_a['standFirst'];
$update_time = $json_a['lastModified'];
$url = $json_a['links']['webUri'];
if (empty($title) or empty($content) or empty($url))
continue;
output_body($article_counter, $title, $content, $summary, $update_time, $url, $filepath);
$images = array_merge($json_a['displayImages'], $json_a['bodyImages']);
if (!empty($json_a['headerImage'])) {
array_push($images, $json_a['headerImage']);
}
foreach ($images as $image) {
$relation_content = $relation['content'];
$caption = $image['caption'];
$cleancaption = $image['cleanCaption'];
$href = $image['urlTemplate'];
$alt = $image['altText'];
$width = $image['width'];
$height = $image['height'];
$ratio = 300. / $width;
if ($ratio < 1) {
$width = $width * $ratio;
$height = $height * $ratio;
}
output_img($caption, $href, $alt, $width, $height, "<p>cleanCaption: \"$cleancaption\"</p>");
}
output_after_img();
}
fclose($handle);
output_after_body($article_counter);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment