aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 47d4d260d95b38f8e398b2254e2799903447265b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# PHP-Blog

PHP-Blog is a simple, database-free blogging web app written in PHP, with the
support of Markdown and JSON.

## Philosophy

Websites, blogs included, should be able to
[degrade gracefully over time](https://brandur.org/fragments/graceful-degradation-time).
For blogs, this means open-sourcing your blog and publishing articles and
metadata straight to the Git repository.

This project allows you to blog and keep your articles, article metadata, and
user comments within this repo. This is done through the use of JSON files to
store article metadata and user comments (instead of using a database), and the
use of Markdown files to store written articles.

## Installation

Steps:

```bash
cd yourBlog.com
```

```bash
git clone <REPO_URL>
```

Change the global variables at the top of the `index.php` script:

```php
$websiteProtocol = 'https://';
$shortDomain = 'yourBlog.com';
```

Remove my personal data files:

```bash
cd _data/ && rm -rf *
```

Add blank data files:

```bash
touch comments.json && touch metadata.json
```

Remove my personal posts:

```bash
cd ../posts/ && rm -rf *
```

Add your first post:

```bash
touch 001-your-first-post.md
```

For every post you add, also add the applicable JSON object in
`_data/metadata.json`.

Finally, enable `FallbackResource` in your Apache `.conf` file or in your
`.htaccess` file:

```apacheconf
FallbackResource /index.php
```

## Project Structure

This project uses the structure show below. Continue reading for a brief
description of each file/folder.

```text
blog/
│─── .gitignore
│─── favicon.ico
│─── index.php
│─── LICENSE
│─── README.md
│
└─── _classes/
│   │─── Comment.php
│   │─── Parsedown.php
│   └─── Template.php
│
└─── _data/
│   │─── comments.json
│   └─── metadata.json
│
└─── _functions/
│   │─── loadJSON.php
│   │─── loadRSS.php
│   │─── parseMarkdown.php
│   └─── submitComment.php
│
└─── _templates/
│   └───template.html
│
└─── posts/
│   │─── 001-your-first-post.md
│   │─── ...
│   └─── 999-many-posts-later.md
│
└─── static/
    │─── app.css
    │─── prism.css
    └─── prism.js
```

### File: `index.php`

This file is the FallbackResource for the entire directory, so non-existent
folders/files requested by users will be sent to `index.php` for processing.
This file uses a switch statement to read the request URL and direct the user to
various functions.

Each function in this file will call various other functions/classes and then
send the output to the template file.

---

### Folder: `_classes`

#### File: `Comment.php`

This class allows for the creation of user comments and provides the
`saveComment()` function to do so.

#### File: `Parsedown.php`

This class was not created by me, it comes from
[erusev/parsedown](https://github.com/erusev/parsedown). See his
[MIT License](https://raw.githubusercontent.com/erusev/parsedown/master/LICENSE.txt)
for licensing questions on this code.

This class provides numerous capabilities to parse Markdown files and variables
to HTML.

#### File: `Template.php`

This class allows for the creation of a final HTML page to show the user. The
`echoTemplate()` function will replace the `{}` templates in the
`_templates/template.html` file.

---

### Folder: `_data`

#### File: `metadata.json`

Your `metadata.json` file will need to contain an object for each post in the
`post` folder. If you don't add an object for your new blog post, it won't be
displayed in the browser. The structure will follow this exactly:

```
{
  "id": "001",
  "title": "Title of Post",
  "author": "Your Name",
  "description": "A quick little description for SEO and RSS.",
  "tag": "my-category",
  "created": "2018-12-08 00:00:00",
  "modified": "2018-12-08 00:00:00",
  "link": "https:\/\/www.example.com\/post\/blog-post-name.html",
  "published": "Yes"
}
```

#### File: `comments.json`

This file is updated server-side as users submit comments on posts. You can
commit these changes back to your repository by making sure the server-side git
repo can read & write. Then just create git commits back to your repo from there
(you can even create periodic cron jobs to do this for you, if you trust it).

You will need to perform the following commands to ensure whatever 'user' is
writing to the `comments.json` file has the proper permissions. For Apache, the
user is probably `www-data`.

```bash
chgrp -R www-data /path/to/website/
chmod -R g+w _data/comments.json
```

---

### Folder: `_functions`

#### File: `loadJSON.php`

This file contains four (4) functions that can be called to load JSON data from
a local file in various ways.

#### File: `loadRSS.php`

This file contains a single function that will grab the `_data/metadata.json`
file and generate a complete RSS file.

#### File: `parseMarkdown.php`

This file uses the `Parsedown` class to parse a posts or comments from Markdown
to HTML. It also adds additional attributes to any link found in the Markdown.

#### File: `submitComment.php`

This file uses the `Comment` class to create a comment and then save it to
`_data/comments.json`.

---

### Folder: `_templates`

#### File: `template.html`

This file is the end result template that will be shown to users, regardless of
the URL they enter. The main content in the `<body>` and various elements in the
`<head>` will change depending which function calls this template.

---

### Folder: `posts`

Your `posts` folder will contain the `.md` files that will comprise the body of
your blog post. Formatting follows standard Markdown guidelines.

---

### Folder: `static`

This folder contains the main CSS file (`app.css`), as well as a pair of
optional static files from [PrismJS](https://prismjs.com) to enable syntax
highlighting in any code blocks found in posts or comments.

To remove the optional files, simply delete the unwanted files and remove the
following from the `pageExtras` parameter string in the `ShowComments()` and
`ShowPost()` functions in `index.php`:

```html
<link rel="stylesheet" href="/static/prism.css" />
<script src="/static/prism.js"></script>
```