-class Action extends Observer
-{
- private function serveReference(\http\Url $url, \stdClass $payload) {
- $finder = new Finder($this->baseUrl, REFS);
- $path = $finder->find($url);
- $payload->listing = new RefListing($path,
- $finder->glob($path, "/[:_a-zA-Z]*.md"));
- $payload->title = $payload->listing->getSelf()->formatLink();
- $payload->refs = $finder;
- if ($path->isFile()) {
- $payload->html = new Markdown($path);
- $payload->sublisting = new RefListing($path,
- $finder->glob($path, "/[_a-z]*.md"));
- return true;
+class Action {
+ /**
+ * The reference
+ * @var \mdref\Reference
+ */
+ private $reference;
+
+ /**
+ * @var \http\Env\Request
+ */
+ private $request;
+
+ /**
+ * @var \http\Env\Response
+ */
+ private $response;
+
+ /**
+ * @var resource
+ */
+ private $output;
+
+ /**
+ * @var \http\Url
+ */
+ private $baseUrl;
+
+ /**
+ * Initialize the reference
+ */
+ public function __construct(Reference $ref, Request $req, Response $res, BaseUrl $baseUrl, $output = null) {
+ $this->reference = $ref;
+ $this->request = $req;
+ $this->response = $res;
+ $this->baseUrl = $baseUrl;
+ $this->output = $output;
+ ob_start($res);
+ }
+
+ /**
+ * Shorthand for \htmlspecialchars()
+ * @param $txt string
+ * @return string
+ */
+ function esc(string $txt) : string {
+ return htmlspecialchars($txt);
+ }
+
+ /**
+ * Create the view payload
+ * @return \stdClass
+ */
+ private function createPayload() : object {
+ $pld = new stdClass;
+
+ $pld->esc = "htmlspecialchars";
+ $pld->anchor = [$this->reference, "formatAnchor"];
+ $pld->quick = [$this->reference, "formatString"];
+ $pld->file = [$this->reference, "formatFile"];
+
+ $pld->ref = $this->baseUrl->pathinfo(
+ $this->baseUrl->mod($this->request->getRequestUrl()));
+
+ $pld->markup = function($page) use($pld) {
+ return $this->reference->getFormatter()->markup($page, $pld);
+ };
+
+ $pld->refs = $this->reference;
+ $pld->baseUrl = $this->baseUrl;
+
+ return $pld;
+ }
+
+ /**
+ * Redirect to canonical url
+ * @param string $cnn
+ */
+ private function serveCanonical(string $cnn) : void {
+ $this->response->setHeader("Location", $this->baseUrl->mod(["path" => $cnn]));
+ $this->response->setResponseCode(301);
+ if (is_resource($this->output)) {
+ $this->response->send($this->output);
+ } else {
+ $this->response->send();