jontynewman/oku-io

根据路径从一组输入生成输出的功能。

v1.0 2018-10-09 22:25 UTC

This package is not auto-updated.

Last update: 2024-09-29 09:56:44 UTC


README

根据路径从一组输入生成输出的功能。

例如,给定的路径可以接收reStructuredText集合作为输入,并生成缓存的HTML作为输出。

安装

composer require 'jontynewman/oku-io ^1.0'

示例用法

以下示例假设rst2html5 可用,并且已安装以下软件包。

编辑器将允许用户输入或上传reStructuredText以创建静态网页。

<?php

use GuzzleHttp\Psr7\Response;
use JontyNewman\HtmlFilter;
use JontyNewman\Oku\ContextInterface;
use JontyNewman\Oku\IO\Cache;
use JontyNewman\Oku\IO\Repository;
use JontyNewman\Oku\Process;
use JontyNewman\Oku\RequestHandler;
use JontyNewman\Oku\ResponseBuilderInterface;

require 'vendor/autoload.php';

$dir = '/path/to/io/directory';

// Convert reStructuredText input files to HTML output files.
$process = new Process('rst2html5');

// Cache HTML output in the specified directory.
$cache = new Cache($process, "{$dir}/html/", 'html');

// Persist reStructuredText input in the specified directory (for future edits).
$repository = new Repository($cache, "{$dir}/rst/", 'rst');

// Allow users to edit and upload reStructuredText.
$editor = function (
	ResponseBuilderInterface $builder,
	ContextInterface $context
) use ($repository): void {

	$path = $context->request()->getUri()->getPath();
	$in = $repository->directory()->offsetGet($path);
	$render = function () use ($in) {

		if (file_exists($in)) {
			HtmlFilter::passthru($in, 'rb');
		}
	};

	$builder->content(function () use ($render, $context) {
		require 'editor.php';
	});
};

// Use a simple 404 page as the default response.
$default = new Response(404, ['Content-Type' => 'text/plain'], 'Not Found');

// Set up the request handler.
$handler = new RequestHandler($repository, $default, $editor);

// Run the application.
$handler->run();

以下为编辑器的示例模板(即 editor.php)。

<?php

use JontyNewman\Oku\IO\Repository;
use JontyNewman\Oku\Helpers\Html;

/* @var $render callable */
/* @var $context \JontyNewman\Oku\ContextInterface */

?>
<!DOCTYPE html>
<html>
  <head>
    <title>JontyNewman\Oku\IO</title>
  </head>
  <body>
    <form action="" method="post" enctype="multipart/form-data">
      <p>
        <label>
          File
          <input type="file" name="<?= Html::escape(Repository::FILE); ?>">
        </label>
      </p>
      <p>
        <label>
          Text
          <textarea name="<?= Html::escape(Repository::TEXT); ?>"><?php ($render)(); ?></textarea>
        </label>
      </p>
      <p>
        <?= $context->token(); ?>
        <?= $context->put(); ?>
        <input type="submit" value="Save">
      </p>
    </form>
    <form action="" method="post">
      <p>
        <?= $context->token(); ?>
        <?= $context->delete(); ?>
        <input type="submit" value="Delete">
      </p>
    </form>
    <?= $context->inset(); ?>
  </body>
</html>