├── .gitignore ├── LICENSE ├── README.md ├── index-daniel.php ├── index-theme-simple.php ├── index.php ├── rss.py └── setup.sh /.gitignore: -------------------------------------------------------------------------------- 1 | posts 2 | image 3 | config.php 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020- Daniel C 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TinyBlog 2 | Single File HTML/CSS/PHP blog in less than 100 lines. 3 | See it in action: http://danielc.dev/blog/ 4 | Personally, I'm using https://github.com/petabyt/tinyblog2 now 5 | 6 | ## Features 7 | - Tiny source code. Just pull index.php and customize. 8 | - Minimal markdown parser. Easy to customize to your liking. 9 | 10 | ## Setup 11 | In the `posts` folder, create a file named "1" for the first 12 | post, "2" for the second, and so on. 13 | 14 | ## Theme 15 | 16 | A second drop-in replacement white theme is available (index-theme-simple.php), 17 | made by @xiamuguizhi. 18 | 19 | ## Markdown Syntax 20 | 21 | 22 | TinyBlog has a built-in Markdown parser. It supports most of the typical 23 | Markdown syntax, but has some additional features: 24 | 25 | - Type `---` to insert a "Read More" link, and cut off the rest of the text. 26 | - Use `\*` to prevent the asterisk from being recognized. 27 | -------------------------------------------------------------------------------- /index-daniel.php: -------------------------------------------------------------------------------- 1 | "); 10 | 11 | $f = fopen("blog/posts/" . strval($postCount), "r"); 12 | echo("

" . substr(fgets($f), 2) . "

"); 13 | echo("

" . fgets($f) . "

"); 14 | fclose($f); 15 | 16 | echo(""); 17 | } 18 | 19 | */ 20 | ?> 21 | 22 | 23 | 24 | 25 | " . substr(fgets($f), 2) . ""); 41 | fclose($f); 42 | } else { 43 | echo("Daniel's Blog"); 44 | } 45 | ?> 46 | 47 | 88 | 89 | 90 |
91 |

Daniel's Blog

92 |
93 | = 1; $post--) { 97 | makePost($post, FALSE); 98 | } 99 | } else { 100 | if (file_exists("posts/" . strval($_GET["post"]))) { 101 | makePost($_GET["post"], TRUE); 102 | } else { 103 | echo("Bad param"); 104 | } 105 | } 106 | 107 | function makePost($post, $showRest) { 108 | $text = file_get_contents("posts/" . strval($post)); 109 | $text = parse($text, $showRest, $post); 110 | 111 | echo("
" . $text . "
"); 112 | } 113 | 114 | function parse($string, $showRest, $post) { 115 | $asHtml = $string; 116 | 117 | $asHtml = htmlspecialchars($asHtml); 118 | 119 | # Replace "# " with h1 120 | $asHtml = preg_replace("/## (.+)/i", "

$1

", $asHtml); 121 | 122 | # Replace "# " with h1 123 | $asHtml = preg_replace("/# (.+)/i", "

$1

", $asHtml); 124 | 125 | # Replace [text](link) with HTML links. Prevent ][ and )( in them 126 | $findBetween = "([^\n|\[\]\(\)]+)"; 127 | 128 | # Parse images first, since they require a ! in front of them 129 | $imageRegex = "/\!\[" . $findBetween . "\]\(" . $findBetween . "\)/i"; 130 | $asHtml = preg_replace($imageRegex, "$1", $asHtml); 131 | 132 | # Parse links, Prevent ! at beginning 133 | $linkRegex = "/(?!\!)\[" . $findBetween . "\]\(" . $findBetween . "\)/i"; 134 | $asHtml = preg_replace($linkRegex, "$1", $asHtml); 135 | 136 | # Different regex for comments as one needs newline. 137 | $asHtml = preg_replace("/```([^```]+)```/s", "$1", $asHtml); 138 | $asHtml = preg_replace("/\`([^\n]+)\`/i", "$1", $asHtml); 139 | 140 | # Replace bold, then italics 141 | $asHtml = preg_replace("/\*\*([^\n]+)\*\*/i", "$1", $asHtml); 142 | $asHtml = preg_replace("/\*([^\n]+)\*/i", "$1", $asHtml); 143 | 144 | # Replace --- with mothing and add "back" link or add "read more" if chosen. 145 | if ($showRest == TRUE) { 146 | $asHtml = preg_replace("/---/s", "", $asHtml); 147 | $asHtml .= "Back"; 148 | } else { 149 | $asHtml = preg_replace("/---(.+)/s", "

Read more

", $asHtml); 150 | } 151 | 152 | return $asHtml; 153 | } 154 | ?> 155 | 156 | 157 | -------------------------------------------------------------------------------- /index-theme-simple.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | My Blog 8 | 9 | 189 | 190 |
191 |

My Blog

192 |

This is where my stuff goes.

193 | = 1; $post--) { 208 | makePost($post, FALSE); 209 | } 210 | } else { 211 | makePost($_GET["post"], TRUE); 212 | } 213 | 214 | function makePost($post, $showRest) { 215 | $text = file_get_contents("posts/" . strval($post)); 216 | $text = parse($text, $showRest, $post); 217 | 218 | echo("
" . $text . "
"); 219 | } 220 | 221 | function parse($string, $showRest, $post) { 222 | $asHtml = $string; 223 | $asHtml = htmlspecialchars($asHtml); 224 | 225 | # Replace "# " with h1 226 | $asHtml = preg_replace("/# (.+)/i", "

$1

", $asHtml); 227 | # Replace [text](link) with HTML links. Prevent ][ and )( in them 228 | $findBetween = "([^\n|\[\]\(\)]+)"; 229 | 230 | # Parse images first, since they require a ! in front of them 231 | $imageRegex = "/\!\[" . $findBetween . "\]\(" . $findBetween . "\)/i"; 232 | $asHtml = preg_replace($imageRegex, "$1", $asHtml); 233 | 234 | # Parse links, Prevent ! at beginning 235 | $linkRegex = "/(?!\!)\[" . $findBetween . "\]\(" . $findBetween . "\)/i"; 236 | $asHtml = preg_replace($linkRegex, "$1", $asHtml); 237 | 238 | # Different regex for comments as one needs newline. 239 | $asHtml = preg_replace("/```([^```]+)```/s", "$1", $asHtml); 240 | $asHtml = preg_replace("/\`([^\n]+)\`/i", "$1", $asHtml); 241 | 242 | # Replace bold, then italics (don't replace \*) 243 | $asHtml = preg_replace("/\*\*([^\n]+)\*\*/i", "$1", $asHtml); 244 | $asHtml = preg_replace("/\*[^\\\\]([^\n]+)[^\\\\]\*/i", "$1", $asHtml); 245 | 246 | # Replace \* with * 247 | # Optional, may break C/C++ code. 248 | #$asHtml = preg_replace("/(\\\\\*)/i", "*", $asHtml); 249 | 250 | # Replace --- with mothing and add "back" link or add "read more" if chosen. 251 | if ($showRest == TRUE) { 252 | $asHtml = preg_replace("/---/s", "", $asHtml); 253 | $asHtml .= "Back"; 254 | } else { 255 | $asHtml = preg_replace("/---(.+)/s", "

Read more

", $asHtml); 256 | } 257 | 258 | return $asHtml; 259 | } 260 | ?> 261 |
262 | 263 | 264 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | My Blog 5 | 28 | 29 | 30 |
31 |

My Blog

32 |

This is where my stuff goes.

33 |
34 | = 1; $post--) { 50 | makePost($post, FALSE); 51 | } 52 | } else { 53 | if (file_exists("posts/" . strval($_GET["post"]))) { 54 | makePost($_GET["post"], TRUE); 55 | } else { 56 | echo("Bad param"); 57 | } 58 | } 59 | 60 | function makePost($post, $showRest) { 61 | $text = file_get_contents("posts/" . strval($post)); 62 | $text = parse($text, $showRest, $post); 63 | 64 | echo("
" . $text . "
"); 65 | } 66 | 67 | function parse($string, $showRest, $post) { 68 | $asHtml = $string; 69 | 70 | $asHtml = htmlspecialchars($asHtml); 71 | 72 | $asHtml = str_replace("\\`", "`", $asHtml); 73 | $asHtml = str_replace("\\*", "*", $asHtml); 74 | 75 | # Replace "# " with h1 76 | $asHtml = preg_replace("/## (.+)/i", "

$1

", $asHtml); 77 | 78 | # Replace "# " with h1 79 | $asHtml = preg_replace("/# (.+)/i", "

$1

", $asHtml); 80 | 81 | # Replace [text](link) with HTML links. Prevent ][ and )( in them 82 | $findBetween = "([^\n|\[\]\(\)]+)"; 83 | 84 | # Parse images first, since they require a ! in front of them 85 | $imageRegex = "/\!\[" . $findBetween . "\]\(" . $findBetween . "\)/i"; 86 | $asHtml = preg_replace($imageRegex, "$1", $asHtml); 87 | 88 | # Parse links, Prevent ! at beginning 89 | $linkRegex = "/(?!\!)\[" . $findBetween . "\]\(" . $findBetween . "\)/i"; 90 | $asHtml = preg_replace($linkRegex, "$1", $asHtml); 91 | 92 | # Different regex for comments as one needs newline. 93 | $asHtml = preg_replace("/```([^```]+)```/s", "$1", $asHtml); 94 | $asHtml = preg_replace("/\`([^\n`]+)\`/i", "$1", $asHtml); 95 | 96 | # Replace bold, then italics 97 | $asHtml = preg_replace("/\*\*([^\n\*]+)\*\*/i", "$1", $asHtml); 98 | $asHtml = preg_replace("/\*([^\n\*]+)\*/i", "$1", $asHtml); 99 | 100 | # Replace --- with mothing and add "back" link or add "read more" if chosen. 101 | if ($showRest == TRUE) { 102 | $asHtml = preg_replace("/---/s", "", $asHtml); 103 | $asHtml .= "Back"; 104 | } else { 105 | $asHtml = preg_replace("/---(.+)/s", "

Read more

", $asHtml); 106 | } 107 | 108 | return $asHtml; 109 | } 110 | ?> 111 | 112 | 113 | -------------------------------------------------------------------------------- /rss.py: -------------------------------------------------------------------------------- 1 | import os, re 2 | 3 | # Post RSS.py 4 | # For a basic PHP version with '@' seperated post information, 5 | # see https://github.com/petabyt/tinyblog/issues/6 6 | 7 | url = "https://petabyt.dev/blog/" 8 | title = "Daniel's Blog" 9 | desc = "This is the place where I put stuff" 10 | 11 | print(""" 12 | 13 | 14 | """ + title + """ 15 | """ + url + """ 16 | """ + desc + """ 17 | Tinyblog 18 | en 19 | Mon, 08 Feb 2021 00:00:00 +0000 20 | """) 21 | 22 | def getMatch(a): 23 | return a.groups(0)[0] 24 | 25 | 26 | files = os.listdir("posts/") 27 | 28 | for i in range(1, len(files)): 29 | fp = open("posts/" + str(i)) 30 | text = fp.read() 31 | text = re.sub(r"# (.+)", getMatch, text) 32 | text = text.split("\n") 33 | print(""" 34 | 35 | """ + text[0] + """ 36 | """ + text[1] + """ 37 | """ + url + str(i) + """ 38 | """ + url + str(i) + """ 39 | """ + text[2] + """ 40 | """) 41 | 42 | print(""" 43 | 44 | 45 | """) 46 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # This is an easy setup file. 3 | 4 | mkdir posts 5 | mkdir media 6 | 7 | cd posts 8 | echo "Welcome to post 1" > 1 9 | echo "Welcome to post 2" > 2 10 | cd .. 11 | --------------------------------------------------------------------------------