Skip to content

Instantly share code, notes, and snippets.

@xuwangyin
Created April 20, 2016 14:49
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/2f03f9c2d774e704f9743f2ed068c0cb to your computer and use it in GitHub Desktop.
Save xuwangyin/2f03f9c2d774e704f9743f2ed068c0cb to your computer and use it in GitHub Desktop.
find my_json_file_dir -name "*.json" > json_files; php render_json_files.php json_files > articles.html; open articles.html;
<?php
error_reporting(E_ERROR | E_PARSE);
function get_content($body) {
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
if (!$doc->loadXML($body))
return $body;
$content = "";
foreach ($doc->getElementsByTagName("body") as $t) {
foreach ($t->getElementsByTagName("paragraph") as $p) {
$content = $content . '<p>' . $p->textContent . '</p>';
}
}
return $content;
}
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-offset-2 col-sm-8">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<p id="article_number">Articles</p>';
echo '</div>';
echo '<div class="panel-body">';
echo '<table class="table table-striped story-table">';
echo '<tbody>';
$num_articles = 0;
$handle = fopen($argv[1], "r");
if ($handle) {
$article_counter = 0;
while (($line = fgets($handle)) !== false) {
$filepath = trim($line);
$filename = basename(trim($line));
$string = file_get_contents(trim($line));
if ($string) {
$article_counter += 1;
$json_a = json_decode($string, true);
if ($json_a == NULL)
continue;
$title = $json_a['name'];
$content = get_content($json_a['body']);
$summary = $json_a['summary'];
$update_time = date(DATE_ATOM, $json_a['lastUpdated']/1000);
$url = $json_a['shareUrl'];
if (empty($title) or empty($content) or empty($url))
continue;
$num_articles += 1;
echo '<tr>';
echo "<td class=\"table-text\"><div><p>$title</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 style="width:80%" 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-md-6">';
echo "<p><strong>$summary</strong></p>";
echo "<p>$content</p>";
echo '</div>';
/* echo '<div class="col-md-2 col-md-offset-1">'; */
echo '<div class="col-md-6">';
foreach ($json_a["relations"] as $relation) {
if ($relation['primaryType'] == "bbc.mobile.news.image") {
$relation_content = $relation['content'];
$caption = $relation_content['caption'];
$href = $relation_content['href'];
$alt = $relation_content['altText'];
$width = $relation_content['width'];
$height = $relation_content['height'];
$ratio = 300. / $width;
if ($ratio < 1) {
$width = $width * $ratio;
$height = $height * $ratio;
}
/* echo '<hr>'; */
echo "<img src=\"$href\" alt=\"$alt\" width=\"$width\" height=\"$height\">";
if (!empty(trim($caption)))
echo "<p>caption: $caption</p>";
if (!empty(trim($alt)))
echo "<p>alt: $alt</p>";
echo "<div> &nbsp;&nbsp; </div>";
}
}
echo '</div>'; /* col-md-4 */
echo '</div>'; /* row */
echo '</div>';
echo '<div class="modal-footer">';
/* echo '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>'; */
/* echo '<button type="button" class="btn btn-primary">Save changes</button>'; */
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</tr>';
}
}
fclose($handle);
} else {
die("Cannot open file: " . $argv[1]);
}
echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<script>';
echo "document.getElementById(\"article_number\").innerHTML = \"Rendered $num_articles Articles\"";
echo '</script>';
echo '</body>';
echo '</html>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment