0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Local Wikiという名のMarkdown管理

Last updated at Posted at 2023-05-12
  • Markdownが増えてきて探すのが面倒になってきた
  • いちおうgitで管理したい
  • ローカルがいい(PHPだけども)
  • PHPでMarkdownをHTML化できるライブラリ様

とりあえず以下のindex.phpを作って「doc」フォルダにMarkdownを突っ込む。
ツリー表示で更新日も表示してくれる、いえい。


<?php

require_once("libs/PHPMarkdown/Michelf/MarkdownExtra.inc.php");

$script = 'index.php';
$docdir = 'doc';

$docdir = rtrim($docdir, '/');

// ファイル検索
function getFileList($dir, $ext = null) {
    $files = scandir($dir);
    $files = array_filter($files, function ($file) {
        return !in_array($file, array('.', '..'));
    });
    $list = array();
    $t_dir = rtrim($dir, '/');
    foreach ($files as $file) {
        $fullpath = $t_dir . '/' . $file;
        if (is_file($fullpath)) {
            if (! $ext || $ext == strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
                $list[] = $fullpath;
            }
        }
        if (is_dir($fullpath)) {
            $list = array_merge($list, getFileList($fullpath, $ext));
        }
    }
    return $list;
}
$mdFiles = getFileList($docdir, 'md');

$mdtext = '';

if (isset($_GET['target'])) {  // ページ指定がある場合
    if (in_array($_GET['target'], $mdFiles)) {
        $mdtext = file_get_contents($_GET['target']);
    }
    if ($mdtext) {
        $pattern = "/\]\(($docdir\/[^\)]+\.md)/";
        $mdtext = preg_replace($pattern, "]($script?target=$1", $mdtext);
        $mdtext .= "\n\n<br><br><br>[トップに戻る]($script)\n";
    } else {
        //header("Location: $script");
        header("HTTP/1.1 404 Not Found");
        //echo $_GET['target'] . "<br>\n";
        echo '404 Not Found';
        return;
    }
} else {  // トップページ
    natsort($mdFiles);
    $mdtext = "# Local Wiki\n";
    $pattern = "/^(.+\/)?([^\/]+)$/";
    $olddir = false;
    $olddirs = [];
    $oldcount = 0;
    $indent = '';
    // フォルダツリー表示
    foreach ($mdFiles as $file) {
        $m = [];
        preg_match($pattern, $file, $m);
        $dir = rtrim($m[1], "/");
        $name = $m[2];
        $name = substr($name, 0, strlen($name) - strlen('.md'));
        if ($dir) {
            if ($dir !== $olddir) {
                $dirs = explode("/", $dir);
                $indent = "";
                $count = 0;
                foreach ($dirs as $d) {
                    if ($count >= $oldcount || $olddirs[$count] != $d) {
                        $mdtext .= "$indent- $d\n";
                    }
                    $indent .= "    ";
                    $count++;
                }
                $olddir = $dir;
                $olddirs = $dirs;
                $oldcount = $count;
            }
            $mdtext .= $indent;
        }
        $d = date("Y/m/d", filemtime($file));
        $enc = urlencode($file);
        $mdtext .= "- [$name]($script?target=$enc) -- $d\n";
    }
}

// Markdown HTMLの生成
$mdhtml = Michelf\MarkdownExtra::defaultTransform($mdtext);
?>
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta content="width=device-width,initial-scale=1,shrink-to-fit=no,viewport-fit=cover" name="viewport">
    <title>Local Wiki</title>
  </head>
  <body>
    <div class="markdown-content">
        <style>
            table { border-spacing: 0; }
            th, td { border: 1px solid #CCC; padding: 4px 8px; }
            th { background-color: #39F; color: #FFF;}
            pre { padding: 20px 24px; background-color: #444; color: #FFF; width: fit-content; max-width: 90%; overflow-x: auto;}
        </style>
        <?php print $mdhtml; ?>
    </div>
  </body>
</html>

memo.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?