krz/hn

A brutalist web client for Hacker News.

clone: git clone https://gitbay.org/krz/hn.git

6c65601cc4781a4fac00f4ae2a7a8def24c28e72

unsigned

author: Christian Cleberg <hello@cleberg.net> · 2023-06-01T02:36:23Z

formatting
 index.php                   | 187 ++++++++++++++++++++++----------------------
 src/View/class-template.php |  15 ++--
 templates/template.html     |  50 ++++++------
 3 files changed, 126 insertions(+), 126 deletions(-)

diff --git a/index.php b/index.php
index a61df1e..28a2cfe 100644
--- a/index.php
+++ b/index.php
@@ -1,115 +1,116 @@
 <?php
 
-$full_domain = 'https://hn.cleberg.net';
-$path = ltrim($_SERVER['REQUEST_URI'], '/');
-$elements = explode('/', $path);
+global $full_domain = 'https://hn.cleberg.net',
+	$path = ltrim($_SERVER['REQUEST_URI'], '/'),
+	$elements = explode('/', $path);
 
 if (empty($elements[0])) {
-        $html_output = get_stories(
-                'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
-                'Top'
-        );
-        echo_html(
-       		$GLOBALS['full_domain'] . '/top/',
+	$html_output = get_stories(
+		'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
+		'Top'
+	);
+	echo_html(
+		$GLOBALS['full_domain'] . '/top/',
+		'The top stories from Hacker News, proxied by hn.',
 		'hn',
 		$html_output
-        );
+	);
 } else {
 	switch (array_shift($elements)) {
 		case 'top':
 			$html_output = get_stories(
-                                'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
-			        'Top'
-		        );
-                        echo_html(
-       	                	$GLOBALS['full_domain'] . '/top/',
-                		'The top stories from Hacker News, proxied by hn.',
-		                'hn',
-		                $html_output
-                        );
-			break;
+				'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
+				'Top'
+			);
+			echo_html(
+				$GLOBALS['full_domain'] . '/top/',
+				'The top stories from Hacker News, proxied by hn.',
+				'hn',
+				$html_output
+			);
+		break;
 
 		case 'best':
 			$html_output = get_stories(
-                                'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"',
-			        'Best'
-		        );
-                        echo_html(
-       	                	$GLOBALS['full_domain'] . '/best/',
-		                'The best 30 stories from Hacker News, proxied by hn.',
-		                'hn',
-		                $html_output
-                        );
-			break;
+				'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"',
+				'Best'
+			);
+			echo_html(
+				$GLOBALS['full_domain'] . '/best/',
+				'The best 30 stories from Hacker News, proxied by hn.',
+				'hn',
+				$html_output
+			);
+		break;
 
 		case 'new':
 			$html_output = get_stories(
-                                'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"',
-			        'New'
-		        );
-                        echo_html(
-       	                	$GLOBALS['full_domain'] . '/new/',
-		                'The newest 30 stories from Hacker News, proxied by hn.',
-		                'hn',
-		                $html_output
-                        );
-			break;
+				'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"',
+				'New'
+			);
+			echo_html(
+				$GLOBALS['full_domain'] . '/new/',
+				'The newest 30 stories from Hacker News, proxied by hn.',
+				'hn',
+				$html_output
+			);
+		break;
 
 		case 'ask':
 			$html_output = get_stories(
-                                'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"',
-			        'Ask'
-		        );
-                        echo_html(
-       	                	$GLOBALS['full_domain'] . '/ask/',
-		                'The latest 30 asks from Hacker News, proxied by hn.',
-		                'hn',
-		                $html_output
-                        );
-			break;
+				'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"',
+				'Ask'
+			);
+			echo_html(
+				$GLOBALS['full_domain'] . '/ask/',
+				'The latest 30 asks from Hacker News, proxied by hn.',
+				'hn',
+				$html_output
+			);
+		break;
 
 		case 'show':
 			$html_output = get_stories(
-                                'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"',
-			        'Show'
-		        );
-                        echo_html(
-       	                	$GLOBALS['full_domain'] . '/show/',
-		                'The latest 30 show stories from Hacker News, proxied by hn.',
-		                'hn',
-		                $html_output
-                        );
-			break;
+				'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"',
+				'Show'
+			);
+			echo_html(
+				$GLOBALS['full_domain'] . '/show/',
+				'The latest 30 show stories from Hacker News, proxied by hn.',
+				'hn',
+				$html_output
+			);
+		break;
 
 		case 'job':
 			$html_output = get_stories(
-                                'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"',
-			        'Job'
-		        );
-                        echo_html(
-       	                	$GLOBALS['full_domain'] . '/job/',
-		                'The latest 30 job posts from Hacker News, proxied by hn.',
-		                'hn',
-		                $html_output
-                        );
-			break;
+				'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"',
+				'Job'
+			);
+			echo_html(
+				$GLOBALS['full_domain'] . '/job/',
+				'The latest 30 job posts from Hacker News, proxied by hn.',
+				'hn',
+				$html_output
+			);
+		break;
 
 		default:
 			header('HTTP/1.1 404 Not Found');
-    }
+	}
 }
 
 /**
- * Extract a set of stories from Hacker News API and format in HTML
- *
- * @access public
- * @author cmc <hello@cleberg.net>
- * @param string $api_url The API endpoint to use for extraction
- * @param string $inline_title The <h1> title to use in the HTML
- * @return string $html_output The formatted HTML result of stories from the API
- */
+* Extract a set of stories from Hacker News API and format in HTML
+*
+* @access public
+* @author cmc <hello@cleberg.net>
+* @param string $api_url The API endpoint to use for extraction
+* @param string $inline_title The <h1> title to use in the HTML
+* @return string $html_output The formatted HTML result of stories from the API
+*/
 function get_stories($api_url, $inline_title) {
-        $response_raw = file_get_contents($api_url);
+	$response_raw = file_get_contents($api_url);
 	$response = json_decode($response_raw, true);
 
 	$html_output = '<h1>' . $inline_title . '</h1>';
@@ -130,23 +131,23 @@ function get_stories($api_url, $inline_title) {
 }
 
 /**
- * Send formatted HTML results to the user via a template
- *
- * @access public
- * @author cmc <hello@cleberg.net>
- * @param string $page_url Canoncial URL for HTML header
- * @param string $page_description Page description for HTML header
- * @param string $page_title Page title for HTML header
- * @param string $page_content Page content to display in <main>
- */
+* Send formatted HTML results to the user via a template
+*
+* @access public
+* @author cmc <hello@cleberg.net>
+* @param string $page_url Canoncial URL for HTML header
+* @param string $page_description Page description for HTML header
+* @param string $page_title Page title for HTML header
+* @param string $page_content Page content to display in <main>
+*/
 function echo_html(string $page_url, string $page_description, string $page_title, string $page_content) {
 	include_once 'src/View/class-template.php';
 
 	$template = new HN\View\Template(
-                $page_url,
-	        $page_description,
-	        $page_title,
-	        $page_content
+		$page_url,
+		$page_description,
+		$page_title,
+		$page_content
 	);
 
 	$template->echo_template();
diff --git a/src/View/class-template.php b/src/View/class-template.php
index ea75648..ddcb1fa 100644
--- a/src/View/class-template.php
+++ b/src/View/class-template.php
@@ -3,14 +3,13 @@
 namespace HN\View;
 
 /**
- * Template View
- *
- * @author cmc <hello@cleberg.net>
- */
+* Template View
+*
+* @author cmc <hello@cleberg.net>
+*/
 class Template
 {
-	public function __construct(string $canonical_url, string $page_description, string $page_title, string $content_col)
-	{
+	public function __construct(string $canonical_url, string $page_description, string $page_title, string $content_col) {
 		$this->canonical_url = $canonical_url;
 		$this->description = $page_description;
 		$this->title = $page_title;
@@ -18,8 +17,7 @@ class Template
 		$this->current_year = date("Y");
 	}
 
-	public function echo_template()
-	{
+	public function echo_template() {
 		// Get the template file
 		$template_file = 'templates/template.html';
 		$page = file_get_contents($template_file);
@@ -37,3 +35,4 @@ class Template
 }
 
 // EOF
+	
diff --git a/templates/template.html b/templates/template.html
index 9cf7c2a..bceb26f 100644
--- a/templates/template.html
+++ b/templates/template.html
@@ -2,35 +2,35 @@
 <html lang="en">
 
 <head>
-    <title>{page_title}</title>
-    <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
-    <meta http-equiv="x-ua-compatible" content="ie=edge">
-    <meta name="author" content="My Name">
-    <meta name="description" content="{page_description}">
-    <link rel="canonical" href="{canoncial_url}">
-    <link rel="icon" href="/favicon.ico">
-    <link rel="stylesheet" href="/static/styles.css">
+	<title>{page_title}</title>
+	<meta charset="UTF-8">
+	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
+	<meta http-equiv="x-ua-compatible" content="ie=edge">
+	<meta name="author" content="My Name">
+	<meta name="description" content="{page_description}">
+	<link rel="canonical" href="{canoncial_url}">
+	<link rel="icon" href="/favicon.ico">
+	<link rel="stylesheet" href="/static/styles.css">
 </head>
 
 <body>
-<main id="main">
-    <nav class="links">
-        <span><a href="/">Top</a> &middot; </span>
-        <span><a href="/best/">Best</a> &middot;</span>
-        <span><a href="/new/">New</a> &middot;</span>
-        <span><a href="/ask/">Ask</a> &middot;</span>
-        <span><a href="/show/">Show</a> &middot;</span>
-        <span><a href="/job/">Job</a></span>
-    </nav>
-    {content}
-</main>
+	<main id="main">
+		<nav class="links">
+			<span><a href="/">Top</a> &middot; </span>
+			<span><a href="/best/">Best</a> &middot;</span>
+			<span><a href="/new/">New</a> &middot;</span>
+			<span><a href="/ask/">Ask</a> &middot;</span>
+			<span><a href="/show/">Show</a> &middot;</span>
+			<span><a href="/job/">Job</a></span>
+		</nav>
+		{content}
+	</main>
 
-<footer>
-    <h2>Contact</h2>
-    <p><a href="https://sr.ht/~cmc/hn/">Source Code</a></p>
-    <p>Copyright &copy; 2023 - {current_year}</p>
-</footer>
+	<footer>
+		<h2>Contact</h2>
+		<p><a href="https://sr.ht/~cmc/hn/">Source Code</a></p>
+		<p>Copyright &copy; 2023 - {current_year}</p>
+	</footer>
 
 </body>