commit 4bfe2a6bc18c4cc368a0fce6bc0ebd979e649971 Author: MuitaPata Date: Wed Jan 7 01:25:40 2026 +0000 first post diff --git a/bin/gemgen b/bin/gemgen new file mode 100755 index 0000000..36fea1b Binary files /dev/null and b/bin/gemgen differ diff --git a/config.php b/config.php new file mode 100644 index 0000000..0892387 --- /dev/null +++ b/config.php @@ -0,0 +1,9 @@ + diff --git a/update_gmi_content.php b/update_gmi_content.php new file mode 100644 index 0000000..bc3e0be --- /dev/null +++ b/update_gmi_content.php @@ -0,0 +1,230 @@ + PDO::ERRMODE_EXCEPTION, +]); + + +$sql = <<query($sql); + +$prevusername =''; +$username =''; +$index =[]; +$users =[]; +$descs =[]; + +foreach ($stmt as $row) { + $username = clean($row['username'] ?? 'nouser'); + $slug = clean($row['slug'] ?? 'no-slug'); + $description = $row['description'] ?? '--'; + + echo "\n-----------------------------------"; + echo "\n# Getting posts for user: $username"; + echo "\n-----------------------------------"; + + $userDir = $baseDir . DIRECTORY_SEPARATOR . $username; + + if ($username!=$prevusername) + { + $users[]=$username; + $descs[$username]=$description; + if (count($index)>0) + saveOneBlogIndex($prevusername, $index); + $index = []; + $prevusername=$username; + } + + $index[] = $slug; + $txt = (string)$row['content']; + saveBothFormats($userDir . DIRECTORY_SEPARATOR . $slug, $txt ); + +} + +#save last index after loop +saveOneBlogIndex($username, $index); +saveUserList($users,$descs); + +saveRecentPosts(); +saveRootIndex(); + + +echo "\n### Done.\n"; + +function saveBothFormats($path, $txt) +{ + $mdPath = $path.'.md'; + $gmiPath = $path.'.gmi'; + + echo "\n...Saving: $gmiPath / .md\n"; + + createMissingDirsForFilePath($mdPath); + + $txtSpaceHack = str_replace("\n\n", '|||', $txt); + $txtSpaceHack = str_replace("\n", '$$$', $txtSpaceHack); + file_put_contents($mdPath,$txtSpaceHack); + + #Convert from .md to.gmi + shell_exec("./bin/gemgen -a below -p below $mdPath"); + + $cleanMe = file_get_contents($gmiPath); + + $cleanMe = str_replace('|||',"\n\n", $cleanMe); + $cleanMe = str_replace('$$$',"\n", $cleanMe); + + //Cleaned up gmi with propre new lines + file_put_contents($gmiPath,$cleanMe); + + //Overwrite again with clean original .md file + file_put_contents($mdPath,$txt); + +} + + +function saveUserList($users,$descs) +{ + echo "# Generating blog list..\n"; + + global $baseDir; + + #save user list in /blogs/index.gmi + $userFile = $baseDir . DIRECTORY_SEPARATOR . 'blogs'.DIRECTORY_SEPARATOR.'index.gmi'; + $body = "# Lista dos blogs...\n\n"; + + sort($users, SORT_STRING | SORT_FLAG_CASE); + + foreach ($users as $user) + $body .= "=> /$user"." $user :: ".$descs[$user]."\n"; + + createMissingDirsForFilePath($userFile); + file_put_contents($userFile,$body); + +} + +function saveRootIndex() +{ + global $baseDir; + global $pdo; + + $outFile = $baseDir . DIRECTORY_SEPARATOR . 'index'; + + createMissingDirsForFilePath($outFile); + echo "\n## Saved Root index: $outFile"; + + $sql = "select * from appcontent where id='about'"; + $stmt = $pdo->query($sql); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + + #Create .md as we convert it + $body = "# ".$row['title']."\n\n"; + $body .= $row['content']; + + global $titleRecentPosts; + global $titleBlogList ; + + $body .= "[$titleRecentPosts](/read)\n"; + $body .= "[$titleBlogList](/blogs)\n\n"; + + + saveBothFormats($outFile,$body); + +} + +function saveRecentPosts() +{ + + global $baseDir; + global $pdo; + + $outFile = $baseDir . DIRECTORY_SEPARATOR . 'read'.DIRECTORY_SEPARATOR.'index'; + +$sql = <<query($sql); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + + $body = "# Posts recentes \n"; + + foreach ($stmt as $row) { + $username = clean($row['username'] ?? 'nouser'); + $slug = clean($row['slug'] ?? 'no-slug'); + + $body .= "=> /$username/$slug".".gmi $slug \n"; + } + + saveBothFormats($outFile,$body); + echo "\n## Saved Recent index: $outFile"; + +} + + + +function saveOneBlogIndex($username,$index) +{ + global $baseDir; + + $prevuserDir = $baseDir . DIRECTORY_SEPARATOR . $username; + $body = "# $username\n"; + + foreach ($index as $link) + $body .= "=> $link".".gmi $link\n"; + + $outIndexFile = $prevuserDir . DIRECTORY_SEPARATOR . 'index.gmi'; + + createMissingDirsForFilePath($outIndexFile); + echo "\nSaved index: $outIndexFile"; + file_put_contents($outIndexFile, $body); + +} + +function clean($s) { + $s = (string)$s; + $s = trim($s); + #cleanup + $s = preg_replace('~[^A-Za-z0-9._-]+~', '_', $s); + return $s === '' ? 'unknown' : $s; +}; + +function createMissingDirsForFilePath($filePath) +{ + $dir = dirname($filePath); + + if (!is_dir($dir)) { + if (!mkdir($dir, 0775, true)) { + throw new RuntimeException("Failed to create dir: $dir"); + } + } + +} + + +?>