Commit 6c65601cc4

6c65601cc4781a4fac00f4ae2a7a8def24c28e72

parent: cac7a38b16

Unsigned

cmc <hello@cleberg.net> · 2023-06-01 02:36 UTC

formatting

Layout: unified · split

index.php +94 −93
@@ -1,115 +1,116 @@
1<?php 1<?php
2 2
3$full_domain = 'https://hn.cleberg.net'; 3global $full_domain = 'https://hn.cleberg.net',
4$path = ltrim($_SERVER['REQUEST_URI'], '/'); 4 $path = ltrim($_SERVER['REQUEST_URI'], '/'),
5$elements = explode('/', $path); 5 $elements = explode('/', $path);
6 6
7if (empty($elements[0])) { 7if (empty($elements[0])) {
8 $html_output = get_stories( 8 $html_output = get_stories(
9 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"', 9 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
10 'Top' 10 'Top'
11 ); 11 );
12 echo_html( 12 echo_html(
13 $GLOBALS['full_domain'] . '/top/', 13 $GLOBALS['full_domain'] . '/top/',
14 'The top stories from Hacker News, proxied by hn.',
14 'hn', 15 'hn',
15 $html_output 16 $html_output
16 ); 17 );
17} else { 18} else {
18 switch (array_shift($elements)) { 19 switch (array_shift($elements)) {
19 case 'top': 20 case 'top':
20 $html_output = get_stories( 21 $html_output = get_stories(
21 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"', 22 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
22 'Top' 23 'Top'
23 ); 24 );
24 echo_html( 25 echo_html(
25 $GLOBALS['full_domain'] . '/top/', 26 $GLOBALS['full_domain'] . '/top/',
26 'The top stories from Hacker News, proxied by hn.', 27 'The top stories from Hacker News, proxied by hn.',
27 'hn', 28 'hn',
28 $html_output 29 $html_output
29 ); 30 );
30 break; 31 break;
31 32
32 case 'best': 33 case 'best':
33 $html_output = get_stories( 34 $html_output = get_stories(
34 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"', 35 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"',
35 'Best' 36 'Best'
36 ); 37 );
37 echo_html( 38 echo_html(
38 $GLOBALS['full_domain'] . '/best/', 39 $GLOBALS['full_domain'] . '/best/',
39 'The best 30 stories from Hacker News, proxied by hn.', 40 'The best 30 stories from Hacker News, proxied by hn.',
40 'hn', 41 'hn',
41 $html_output 42 $html_output
42 ); 43 );
43 break; 44 break;
44 45
45 case 'new': 46 case 'new':
46 $html_output = get_stories( 47 $html_output = get_stories(
47 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"', 48 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"',
48 'New' 49 'New'
49 ); 50 );
50 echo_html( 51 echo_html(
51 $GLOBALS['full_domain'] . '/new/', 52 $GLOBALS['full_domain'] . '/new/',
52 'The newest 30 stories from Hacker News, proxied by hn.', 53 'The newest 30 stories from Hacker News, proxied by hn.',
53 'hn', 54 'hn',
54 $html_output 55 $html_output
55 ); 56 );
56 break; 57 break;
57 58
58 case 'ask': 59 case 'ask':
59 $html_output = get_stories( 60 $html_output = get_stories(
60 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"', 61 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"',
61 'Ask' 62 'Ask'
62 ); 63 );
63 echo_html( 64 echo_html(
64 $GLOBALS['full_domain'] . '/ask/', 65 $GLOBALS['full_domain'] . '/ask/',
65 'The latest 30 asks from Hacker News, proxied by hn.', 66 'The latest 30 asks from Hacker News, proxied by hn.',
66 'hn', 67 'hn',
67 $html_output 68 $html_output
68 ); 69 );
69 break; 70 break;
70 71
71 case 'show': 72 case 'show':
72 $html_output = get_stories( 73 $html_output = get_stories(
73 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"', 74 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"',
74 'Show' 75 'Show'
75 ); 76 );
76 echo_html( 77 echo_html(
77 $GLOBALS['full_domain'] . '/show/', 78 $GLOBALS['full_domain'] . '/show/',
78 'The latest 30 show stories from Hacker News, proxied by hn.', 79 'The latest 30 show stories from Hacker News, proxied by hn.',
79 'hn', 80 'hn',
80 $html_output 81 $html_output
81 ); 82 );
82 break; 83 break;
83 84
84 case 'job': 85 case 'job':
85 $html_output = get_stories( 86 $html_output = get_stories(
86 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"', 87 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"',
87 'Job' 88 'Job'
88 ); 89 );
89 echo_html( 90 echo_html(
90 $GLOBALS['full_domain'] . '/job/', 91 $GLOBALS['full_domain'] . '/job/',
91 'The latest 30 job posts from Hacker News, proxied by hn.', 92 'The latest 30 job posts from Hacker News, proxied by hn.',
92 'hn', 93 'hn',
93 $html_output 94 $html_output
94 ); 95 );
95 break; 96 break;
96 97
97 default: 98 default:
98 header('HTTP/1.1 404 Not Found'); 99 header('HTTP/1.1 404 Not Found');
99 } 100 }
100} 101}
101 102
102/** 103/**
103 * Extract a set of stories from Hacker News API and format in HTML 104* Extract a set of stories from Hacker News API and format in HTML
104 * 105*
105 * @access public 106* @access public
106 * @author cmc <hello@cleberg.net> 107* @author cmc <hello@cleberg.net>
107 * @param string $api_url The API endpoint to use for extraction 108* @param string $api_url The API endpoint to use for extraction
108 * @param string $inline_title The <h1> title to use in the HTML 109* @param string $inline_title The <h1> title to use in the HTML
109 * @return string $html_output The formatted HTML result of stories from the API 110* @return string $html_output The formatted HTML result of stories from the API
110 */ 111*/
111function get_stories($api_url, $inline_title) { 112function get_stories($api_url, $inline_title) {
112 $response_raw = file_get_contents($api_url); 113 $response_raw = file_get_contents($api_url);
113 $response = json_decode($response_raw, true); 114 $response = json_decode($response_raw, true);
114 115
115 $html_output = '<h1>' . $inline_title . '</h1>'; 116 $html_output = '<h1>' . $inline_title . '</h1>';
@@ -130,23 +131,23 @@ function get_stories($api_url, $inline_title) {
130} 131}
131 132
132/** 133/**
133 * Send formatted HTML results to the user via a template 134* Send formatted HTML results to the user via a template
134 * 135*
135 * @access public 136* @access public
136 * @author cmc <hello@cleberg.net> 137* @author cmc <hello@cleberg.net>
137 * @param string $page_url Canoncial URL for HTML header 138* @param string $page_url Canoncial URL for HTML header
138 * @param string $page_description Page description for HTML header 139* @param string $page_description Page description for HTML header
139 * @param string $page_title Page title for HTML header 140* @param string $page_title Page title for HTML header
140 * @param string $page_content Page content to display in <main> 141* @param string $page_content Page content to display in <main>
141 */ 142*/
142function echo_html(string $page_url, string $page_description, string $page_title, string $page_content) { 143function echo_html(string $page_url, string $page_description, string $page_title, string $page_content) {
143 include_once 'src/View/class-template.php'; 144 include_once 'src/View/class-template.php';
144 145
145 $template = new HN\View\Template( 146 $template = new HN\View\Template(
146 $page_url, 147 $page_url,
147 $page_description, 148 $page_description,
148 $page_title, 149 $page_title,
149 $page_content 150 $page_content
150 ); 151 );
151 152
152 $template->echo_template(); 153 $template->echo_template();
src/View/class-template.php +7 −8
@@ -3,14 +3,13 @@
3namespace HN\View; 3namespace HN\View;
4 4
5/** 5/**
6 * Template View 6* Template View
7 * 7*
8 * @author cmc <hello@cleberg.net> 8* @author cmc <hello@cleberg.net>
9 */ 9*/
10class Template 10class Template
11{ 11{
12 public function __construct(string $canonical_url, string $page_description, string $page_title, string $content_col) 12 public function __construct(string $canonical_url, string $page_description, string $page_title, string $content_col) {
13 {
14 $this->canonical_url = $canonical_url; 13 $this->canonical_url = $canonical_url;
15 $this->description = $page_description; 14 $this->description = $page_description;
16 $this->title = $page_title; 15 $this->title = $page_title;
@@ -18,8 +17,7 @@ class Template
18 $this->current_year = date("Y"); 17 $this->current_year = date("Y");
19 } 18 }
20 19
21 public function echo_template() 20 public function echo_template() {
22 {
23 // Get the template file 21 // Get the template file
24 $template_file = 'templates/template.html'; 22 $template_file = 'templates/template.html';
25 $page = file_get_contents($template_file); 23 $page = file_get_contents($template_file);
@@ -37,3 +35,4 @@ class Template
37} 35}
38 36
39// EOF 37// EOF
38
templates/template.html +25 −25
@@ -2,35 +2,35 @@
2<html lang="en"> 2<html lang="en">
3 3
4<head> 4<head>
5 <title>{page_title}</title> 5 <title>{page_title}</title>
6 <meta charset="UTF-8"> 6 <meta charset="UTF-8">
7 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 7 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
8 <meta http-equiv="x-ua-compatible" content="ie=edge"> 8 <meta http-equiv="x-ua-compatible" content="ie=edge">
9 <meta name="author" content="My Name"> 9 <meta name="author" content="My Name">
10 <meta name="description" content="{page_description}"> 10 <meta name="description" content="{page_description}">
11 <link rel="canonical" href="{canoncial_url}"> 11 <link rel="canonical" href="{canoncial_url}">
12 <link rel="icon" href="/favicon.ico"> 12 <link rel="icon" href="/favicon.ico">
13 <link rel="stylesheet" href="/static/styles.css"> 13 <link rel="stylesheet" href="/static/styles.css">
14</head> 14</head>
15 15
16<body> 16<body>
17<main id="main"> 17 <main id="main">
18 <nav class="links"> 18 <nav class="links">
19 <span><a href="/">Top</a> &middot; </span> 19 <span><a href="/">Top</a> &middot; </span>
20 <span><a href="/best/">Best</a> &middot;</span> 20 <span><a href="/best/">Best</a> &middot;</span>
21 <span><a href="/new/">New</a> &middot;</span> 21 <span><a href="/new/">New</a> &middot;</span>
22 <span><a href="/ask/">Ask</a> &middot;</span> 22 <span><a href="/ask/">Ask</a> &middot;</span>
23 <span><a href="/show/">Show</a> &middot;</span> 23 <span><a href="/show/">Show</a> &middot;</span>
24 <span><a href="/job/">Job</a></span> 24 <span><a href="/job/">Job</a></span>
25 </nav> 25 </nav>
26 {content} 26 {content}
27</main> 27 </main>
28 28
29<footer> 29 <footer>
30 <h2>Contact</h2> 30 <h2>Contact</h2>
31 <p><a href="https://sr.ht/~cmc/hn/">Source Code</a></p> 31 <p><a href="https://sr.ht/~cmc/hn/">Source Code</a></p>
32 <p>Copyright &copy; 2023 - {current_year}</p> 32 <p>Copyright &copy; 2023 - {current_year}</p>
33</footer> 33 </footer>
34 34
35</body> 35</body>
36 36