class Formatter

Properties

protected array $configurationCallbacks
protected array $parsingCallbacks
protected array $unparsingCallbacks
protected array $renderingCallbacks
protected array{host: string, path: string}|null $forumOrigin The forum's own host and base path, worked out once per request.
protected string|false|null $faviconUrl The forum's favicon URL, or null if there isn't one.

Methods

__construct( Repository $cache, string $cacheDir, Config|null $config = null)

`$config` is optional because extensions subclass this and call `parent::__construct()` with the two arguments that existed before it was added. When it is absent the forum's address is resolved from the container instead, so those subclasses keep working untouched.

void
addConfigurationCallback(callable $callback)

No description

void
addParsingCallback(callable $callback)

No description

void
addUnparsingCallback(callable $callback)

No description

void
addRenderingCallback(callable $callback)

No description

string
parse(string $text, mixed $context = null, User|null $user = null)

No description

string
render(string $xml, mixed $context = null, ServerRequestInterface|null $request = null)

No description

string|null
unparse(string|null $xml, mixed $context = null)

No description

void
flush()

Flush the cache so that the formatter components are regenerated.

void
warm()

Build and cache the formatter now, rather than leaving it to the first post that needs rendering.

Configurator
getConfigurator()

No description

void
configureExternalLinks( Configurator $configurator)

Let the link template carry attributes decided when a post is rendered.

void
configureDiscussionLinks( Configurator $configurator)

Show a link to a discussion as the discussion it points at.

string
getDiscussionLinkTemplate()

The markup for a discussion link that is showing its label.

mixed
getComponent(string $name)

Get a TextFormatter component ("renderer" or "parser" or "js").

Parser
getParser(mixed $context = null)

No description

Renderer
getRenderer()

No description

string
getJs()

Get the formatter JavaScript.

string
configureDefaultsOnLinks(string $xml)

Decide how each link in a post should be treated, based on where it goes.

array|null
parseDiscussionUrl(string $url)

Pick out the discussion a URL points at, if it points at one.

string|null
getFaviconUrl()

The forum's favicon, if one has been uploaded.

array
getForumOrigin()

The forum's host and base path.

bool
isInternalUrl(string $url)

Does this URL point back at the forum itself?

string
convert(string|null $content)

Converts a plain text string (with or without Markdown) to it's HTML equivalent.

Details

__construct( Repository $cache, string $cacheDir, Config|null $config = null)

`$config` is optional because extensions subclass this and call `parent::__construct()` with the two arguments that existed before it was added. When it is absent the forum's address is resolved from the container instead, so those subclasses keep working untouched.

Parameters

Repository $cache
string $cacheDir
Config|null $config

void addConfigurationCallback(callable $callback)

Parameters

callable $callback

Return Value

void

void addParsingCallback(callable $callback)

Parameters

callable $callback

Return Value

void

void addUnparsingCallback(callable $callback)

Parameters

callable $callback

Return Value

void

void addRenderingCallback(callable $callback)

Parameters

callable $callback

Return Value

void

string parse(string $text, mixed $context = null, User|null $user = null)

Parameters

string $text
mixed $context
User|null $user

Return Value

string

string render(string $xml, mixed $context = null, ServerRequestInterface|null $request = null)

Parameters

string $xml
mixed $context
ServerRequestInterface|null $request

Return Value

string

string|null unparse(string|null $xml, mixed $context = null)

Parameters

string|null $xml
mixed $context

Return Value

string|null

void flush()

Flush the cache so that the formatter components are regenerated.

Return Value

void

void warm()

Build and cache the formatter now, rather than leaving it to the first post that needs rendering.

Compiling the renderer can be expensive on a forum with many extensions, and it happens lazily inside whichever request first renders a post after the cache was cleared. On a tight web memory limit that request can hit a fatal and fail silently. Warming here — from `cache:clear`, where the limit is usually higher — keeps that cost out of the render path.

Return Value

void

protected Configurator getConfigurator()

Return Value

Configurator

Let the link template carry attributes decided when a post is rendered.

Whether a link points back at this forum is not known when the post is written — the forum may since have moved — so `rel`, `target` and `class` are worked out at render time and copied through here. A theme that replaces this template needs to copy them too, or links lose the treatment.

Parameters

Configurator $configurator

Return Value

void

Show a link to a discussion as the discussion it points at.

A pasted address says nothing useful in the middle of a sentence, so where the writer pasted one bare — leaving the address as the link's own text — it is shown as `#123` instead, with the post number alongside it when the link points at a particular reply. When the writer chose their own words for the link, those words are what appears; the label replaces an address, never a sentence.

Parameters

Configurator $configurator

Return Value

void

protected string getDiscussionLinkTemplate()

The markup for a discussion link that is showing its label.

Return Value

string

protected mixed getComponent(string $name)

Get a TextFormatter component ("renderer" or "parser" or "js").

Parameters

string $name

Return Value

mixed

protected Parser getParser(mixed $context = null)

Parameters

mixed $context

Return Value

Parser

protected Renderer getRenderer()

Return Value

Renderer

string getJs()

Get the formatter JavaScript.

Return Value

string

Decide how each link in a post should be treated, based on where it goes.

Only fills in what has not already been set, so an extension rendering callback — which runs before this — keeps the last word on any link.

Parameters

string $xml

Return Value

string

protected array|null parseDiscussionUrl(string $url)

Pick out the discussion a URL points at, if it points at one.

Matches the shape the discussion route declares: an id, optionally followed by a slug that carries no meaning here, and optionally a post number after it.

Parameters

string $url

Return Value

array|null

protected string|null getFaviconUrl()

The forum's favicon, if one has been uploaded.

Looked up once per request for the same reason as the forum's address: this is consulted for every discussion link on the page.

Return Value

string|null

protected array getForumOrigin()

The forum's host and base path.

`Config::url()` builds a fresh Uri on every call, and this is consulted once per link in every post on the page, so the answer is kept rather than rebuilt. It cannot change within a request.

Return Value

array

protected bool isInternalUrl(string $url)

Does this URL point back at the forum itself?

Host and path both have to match: a forum installed at example.com/forum does not own example.com/shop. The scheme is ignored, since a forum reachable over both http and https is still one forum, and a link is not a different destination for having been copied before the certificate was installed.

Parameters

string $url

Return Value

bool

string convert(string|null $content)

Converts a plain text string (with or without Markdown) to it's HTML equivalent.

Parameters

string|null $content

Return Value

string