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 @@
11<?php
22
3$full_domain = 'https://hn.cleberg.net';
4$path = ltrim($_SERVER['REQUEST_URI'], '/');
5$elements = explode('/', $path);
3global $full_domain = 'https://hn.cleberg.net',
4 $path = ltrim($_SERVER['REQUEST_URI'], '/'),
5 $elements = explode('/', $path);
66
77if (empty($elements[0])) {
8 $html_output = get_stories(
9 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
10 'Top'
11 );
12 echo_html(
13 $GLOBALS['full_domain'] . '/top/',
8 $html_output = get_stories(
9 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
10 'Top'
11 );
12 echo_html(
13 $GLOBALS['full_domain'] . '/top/',
14 'The top stories from Hacker News, proxied by hn.',
1415 'hn',
1516 $html_output
16 );
17 );
1718} else {
1819 switch (array_shift($elements)) {
1920 case 'top':
2021 $html_output = get_stories(
21 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
22 'Top'
23 );
24 echo_html(
25 $GLOBALS['full_domain'] . '/top/',
26 'The top stories from Hacker News, proxied by hn.',
27 'hn',
28 $html_output
29 );
30 break;
22 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
23 'Top'
24 );
25 echo_html(
26 $GLOBALS['full_domain'] . '/top/',
27 'The top stories from Hacker News, proxied by hn.',
28 'hn',
29 $html_output
30 );
31 break;
3132
3233 case 'best':
3334 $html_output = get_stories(
34 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"',
35 'Best'
36 );
37 echo_html(
38 $GLOBALS['full_domain'] . '/best/',
39 'The best 30 stories from Hacker News, proxied by hn.',
40 'hn',
41 $html_output
42 );
43 break;
35 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"',
36 'Best'
37 );
38 echo_html(
39 $GLOBALS['full_domain'] . '/best/',
40 'The best 30 stories from Hacker News, proxied by hn.',
41 'hn',
42 $html_output
43 );
44 break;
4445
4546 case 'new':
4647 $html_output = get_stories(
47 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"',
48 'New'
49 );
50 echo_html(
51 $GLOBALS['full_domain'] . '/new/',
52 'The newest 30 stories from Hacker News, proxied by hn.',
53 'hn',
54 $html_output
55 );
56 break;
48 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"',
49 'New'
50 );
51 echo_html(
52 $GLOBALS['full_domain'] . '/new/',
53 'The newest 30 stories from Hacker News, proxied by hn.',
54 'hn',
55 $html_output
56 );
57 break;
5758
5859 case 'ask':
5960 $html_output = get_stories(
60 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"',
61 'Ask'
62 );
63 echo_html(
64 $GLOBALS['full_domain'] . '/ask/',
65 'The latest 30 asks from Hacker News, proxied by hn.',
66 'hn',
67 $html_output
68 );
69 break;
61 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"',
62 'Ask'
63 );
64 echo_html(
65 $GLOBALS['full_domain'] . '/ask/',
66 'The latest 30 asks from Hacker News, proxied by hn.',
67 'hn',
68 $html_output
69 );
70 break;
7071
7172 case 'show':
7273 $html_output = get_stories(
73 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"',
74 'Show'
75 );
76 echo_html(
77 $GLOBALS['full_domain'] . '/show/',
78 'The latest 30 show stories from Hacker News, proxied by hn.',
79 'hn',
80 $html_output
81 );
82 break;
74 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"',
75 'Show'
76 );
77 echo_html(
78 $GLOBALS['full_domain'] . '/show/',
79 'The latest 30 show stories from Hacker News, proxied by hn.',
80 'hn',
81 $html_output
82 );
83 break;
8384
8485 case 'job':
8586 $html_output = get_stories(
86 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"',
87 'Job'
88 );
89 echo_html(
90 $GLOBALS['full_domain'] . '/job/',
91 'The latest 30 job posts from Hacker News, proxied by hn.',
92 'hn',
93 $html_output
94 );
95 break;
87 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"',
88 'Job'
89 );
90 echo_html(
91 $GLOBALS['full_domain'] . '/job/',
92 'The latest 30 job posts from Hacker News, proxied by hn.',
93 'hn',
94 $html_output
95 );
96 break;
9697
9798 default:
9899 header('HTTP/1.1 404 Not Found');
99 }
100 }
100101}
101102
102103/**
103 * Extract a set of stories from Hacker News API and format in HTML
104 *
105 * @access public
106 * @author cmc <hello@cleberg.net>
107 * @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 * @return string $html_output The formatted HTML result of stories from the API
110 */
104* Extract a set of stories from Hacker News API and format in HTML
105*
106* @access public
107* @author cmc <hello@cleberg.net>
108* @param string $api_url The API endpoint to use for extraction
109* @param string $inline_title The <h1> title to use in the HTML
110* @return string $html_output The formatted HTML result of stories from the API
111*/
111112function get_stories($api_url, $inline_title) {
112 $response_raw = file_get_contents($api_url);
113 $response_raw = file_get_contents($api_url);
113114 $response = json_decode($response_raw, true);
114115
115116 $html_output = '<h1>' . $inline_title . '</h1>';
@@ -130,23 +131,23 @@ function get_stories($api_url, $inline_title) {
130131}
131132
132133/**
133 * Send formatted HTML results to the user via a template
134 *
135 * @access public
136 * @author cmc <hello@cleberg.net>
137 * @param string $page_url Canoncial URL for HTML header
138 * @param string $page_description Page description for HTML header
139 * @param string $page_title Page title for HTML header
140 * @param string $page_content Page content to display in <main>
141 */
134* Send formatted HTML results to the user via a template
135*
136* @access public
137* @author cmc <hello@cleberg.net>
138* @param string $page_url Canoncial URL for HTML header
139* @param string $page_description Page description for HTML header
140* @param string $page_title Page title for HTML header
141* @param string $page_content Page content to display in <main>
142*/
142143function echo_html(string $page_url, string $page_description, string $page_title, string $page_content) {
143144 include_once 'src/View/class-template.php';
144145
145146 $template = new HN\View\Template(
146 $page_url,
147 $page_description,
148 $page_title,
149 $page_content
147 $page_url,
148 $page_description,
149 $page_title,
150 $page_content
150151 );
151152
152153 $template->echo_template();
src/View/class-template.php +7 −8
@@ -3,14 +3,13 @@
33namespace HN\View;
44
55/**
6 * Template View
7 *
8 * @author cmc <hello@cleberg.net>
9 */
6* Template View
7*
8* @author cmc <hello@cleberg.net>
9*/
1010class Template
1111{
12 public function __construct(string $canonical_url, string $page_description, string $page_title, string $content_col)
13 {
12 public function __construct(string $canonical_url, string $page_description, string $page_title, string $content_col) {
1413 $this->canonical_url = $canonical_url;
1514 $this->description = $page_description;
1615 $this->title = $page_title;
@@ -18,8 +17,7 @@ class Template
1817 $this->current_year = date("Y");
1918 }
2019
21 public function echo_template()
22 {
20 public function echo_template() {
2321 // Get the template file
2422 $template_file = 'templates/template.html';
2523 $page = file_get_contents($template_file);
@@ -37,3 +35,4 @@ class Template
3735}
3836
3937// EOF
38
templates/template.html +25 −25
@@ -2,35 +2,35 @@
22<html lang="en">
33
44<head>
5 <title>{page_title}</title>
6 <meta charset="UTF-8">
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">
9 <meta name="author" content="My Name">
10 <meta name="description" content="{page_description}">
11 <link rel="canonical" href="{canoncial_url}">
12 <link rel="icon" href="/favicon.ico">
13 <link rel="stylesheet" href="/static/styles.css">
5 <title>{page_title}</title>
6 <meta charset="UTF-8">
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">
9 <meta name="author" content="My Name">
10 <meta name="description" content="{page_description}">
11 <link rel="canonical" href="{canoncial_url}">
12 <link rel="icon" href="/favicon.ico">
13 <link rel="stylesheet" href="/static/styles.css">
1414</head>
1515
1616<body>
17<main id="main">
18 <nav class="links">
19 <span><a href="/">Top</a> &middot; </span>
20 <span><a href="/best/">Best</a> &middot;</span>
21 <span><a href="/new/">New</a> &middot;</span>
22 <span><a href="/ask/">Ask</a> &middot;</span>
23 <span><a href="/show/">Show</a> &middot;</span>
24 <span><a href="/job/">Job</a></span>
25 </nav>
26 {content}
27</main>
17 <main id="main">
18 <nav class="links">
19 <span><a href="/">Top</a> &middot; </span>
20 <span><a href="/best/">Best</a> &middot;</span>
21 <span><a href="/new/">New</a> &middot;</span>
22 <span><a href="/ask/">Ask</a> &middot;</span>
23 <span><a href="/show/">Show</a> &middot;</span>
24 <span><a href="/job/">Job</a></span>
25 </nav>
26 {content}
27 </main>
2828
29<footer>
30 <h2>Contact</h2>
31 <p><a href="https://sr.ht/~cmc/hn/">Source Code</a></p>
32 <p>Copyright &copy; 2023 - {current_year}</p>
33</footer>
29 <footer>
30 <h2>Contact</h2>
31 <p><a href="https://sr.ht/~cmc/hn/">Source Code</a></p>
32 <p>Copyright &copy; 2023 - {current_year}</p>
33 </footer>
3434
3535</body>
3636