1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-24 22:40:55 +01:00

Support relative links in Phriction

Summary:
Resolves T7691.  This turned out more complex than I really wanted, mainly because I needed to feed the slug information through to both the document renderer and the preview window that appears in the edit controller.

After this change, you can now create relative links in Phriction by doing `[[ ./../some/relative/path ]]`.  Relative paths aren't handled anywhere else (they'll still render, but the dots are turned into a literal 'dot' as per existing behaviour).

Test Plan: Created some Phriction documents with relative links, saw them all link correctly.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

Maniphest Tasks: T7691

Differential Revision: https://secure.phabricator.com/D15732
This commit is contained in:
June Rhodes 2016-04-16 02:07:03 +00:00
parent d96b6506d7
commit dd1023e5a8
6 changed files with 67 additions and 3 deletions

View file

@ -3905,6 +3905,7 @@ phutil_register_library_map(array(
'PhrictionHistoryController' => 'applications/phriction/controller/PhrictionHistoryController.php', 'PhrictionHistoryController' => 'applications/phriction/controller/PhrictionHistoryController.php',
'PhrictionInfoConduitAPIMethod' => 'applications/phriction/conduit/PhrictionInfoConduitAPIMethod.php', 'PhrictionInfoConduitAPIMethod' => 'applications/phriction/conduit/PhrictionInfoConduitAPIMethod.php',
'PhrictionListController' => 'applications/phriction/controller/PhrictionListController.php', 'PhrictionListController' => 'applications/phriction/controller/PhrictionListController.php',
'PhrictionMarkupPreviewController' => 'applications/phriction/controller/PhrictionMarkupPreviewController.php',
'PhrictionMoveController' => 'applications/phriction/controller/PhrictionMoveController.php', 'PhrictionMoveController' => 'applications/phriction/controller/PhrictionMoveController.php',
'PhrictionNewController' => 'applications/phriction/controller/PhrictionNewController.php', 'PhrictionNewController' => 'applications/phriction/controller/PhrictionNewController.php',
'PhrictionRemarkupRule' => 'applications/phriction/markup/PhrictionRemarkupRule.php', 'PhrictionRemarkupRule' => 'applications/phriction/markup/PhrictionRemarkupRule.php',
@ -8702,6 +8703,7 @@ phutil_register_library_map(array(
'PhrictionHistoryController' => 'PhrictionController', 'PhrictionHistoryController' => 'PhrictionController',
'PhrictionInfoConduitAPIMethod' => 'PhrictionConduitAPIMethod', 'PhrictionInfoConduitAPIMethod' => 'PhrictionConduitAPIMethod',
'PhrictionListController' => 'PhrictionController', 'PhrictionListController' => 'PhrictionController',
'PhrictionMarkupPreviewController' => 'PhabricatorController',
'PhrictionMoveController' => 'PhrictionController', 'PhrictionMoveController' => 'PhrictionController',
'PhrictionNewController' => 'PhrictionController', 'PhrictionNewController' => 'PhrictionController',
'PhrictionRemarkupRule' => 'PhutilRemarkupRule', 'PhrictionRemarkupRule' => 'PhutilRemarkupRule',

View file

@ -59,7 +59,7 @@ final class PhabricatorPhrictionApplication extends PhabricatorApplication {
'new/' => 'PhrictionNewController', 'new/' => 'PhrictionNewController',
'move/(?P<id>[1-9]\d*)/' => 'PhrictionMoveController', 'move/(?P<id>[1-9]\d*)/' => 'PhrictionMoveController',
'preview/' => 'PhabricatorMarkupPreviewController', 'preview/(?P<slug>.+/)' => 'PhrictionMarkupPreviewController',
'diff/(?P<id>[1-9]\d*)/' => 'PhrictionDiffController', 'diff/(?P<id>[1-9]\d*)/' => 'PhrictionDiffController',
), ),
); );

View file

@ -272,7 +272,7 @@ final class PhrictionEditController
$preview = id(new PHUIRemarkupPreviewPanel()) $preview = id(new PHUIRemarkupPreviewPanel())
->setHeader($content->getTitle()) ->setHeader($content->getTitle())
->setPreviewURI('/phriction/preview/') ->setPreviewURI('/phriction/preview/'.$document->getSlug())
->setControlID('document-textarea') ->setControlID('document-textarea')
->setPreviewType(PHUIRemarkupPreviewPanel::DOCUMENT); ->setPreviewType(PHUIRemarkupPreviewPanel::DOCUMENT);

View file

@ -0,0 +1,28 @@
<?php
final class PhrictionMarkupPreviewController
extends PhabricatorController {
public function processRequest() {
$request = $this->getRequest();
$viewer = $request->getUser();
$text = $request->getStr('text');
$slug = $request->getURIData('slug');
$output = PhabricatorMarkupEngine::renderOneObject(
id(new PhabricatorMarkupOneOff())
->setPreserveLinebreaks(true)
->setDisableCache(true)
->setContent($text),
'default',
$viewer,
array(
'phriction.isPreview' => true,
'phriction.slug' => $slug,
));
return id(new AphrontAjaxResponse())
->setContent($output);
}
}

View file

@ -15,6 +15,39 @@ final class PhrictionRemarkupRule extends PhutilRemarkupRule {
public function markupDocumentLink(array $matches) { public function markupDocumentLink(array $matches) {
$link = trim($matches[1]); $link = trim($matches[1]);
// Handle relative links.
if (substr($link, 0, 2) === './') {
$base = null;
$context = $this->getEngine()->getConfig('contextObject');
if ($context !== null && $context instanceof PhrictionContent) {
// Handle content when it's being rendered in document view.
$base = $context->getSlug();
}
if ($context !== null && is_array($context) &&
idx($context, 'phriction.isPreview')) {
// Handle content when it's a preview for the Phriction editor.
$base = idx($context, 'phriction.slug');
}
if ($base !== null) {
$base_parts = explode('/', rtrim($base, '/'));
$rel_parts = explode('/', substr(rtrim($link, '/'), 2));
foreach ($rel_parts as $part) {
if ($part === '.') {
// Consume standalone dots in a relative path, and do
// nothing with them.
} else if ($part === '..') {
if (count($base_parts) > 0) {
array_pop($base_parts);
}
} else {
array_push($base_parts, $part);
}
}
$link = implode('/', $base_parts).'/';
}
}
$name = trim(idx($matches, 2, $link)); $name = trim(idx($matches, 2, $link));
if (empty($matches[2])) { if (empty($matches[2])) {
$name = explode('/', trim($name, '/')); $name = explode('/', trim($name, '/'));

View file

@ -27,7 +27,8 @@ final class PhrictionContent extends PhrictionDAO
return PhabricatorMarkupEngine::renderOneObject( return PhabricatorMarkupEngine::renderOneObject(
$this, $this,
self::MARKUP_FIELD_BODY, self::MARKUP_FIELD_BODY,
$viewer); $viewer,
$this);
} }
protected function getConfiguration() { protected function getConfiguration() {