Skip to content

Commit

Permalink
Fix: fisharebest#479 fisharebest#712 - replace markdown-extra with co…
Browse files Browse the repository at this point in the history
…mmonmark, add autolinking for XREFs
  • Loading branch information
fisharebest committed Oct 17, 2017
1 parent 0782630 commit 22acdb0
Show file tree
Hide file tree
Showing 593 changed files with 14,075 additions and 46,311 deletions.
35 changes: 14 additions & 21 deletions app/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
namespace Fisharebest\Webtrees;

use HTMLPurifier;
use HTMLPurifier_Config;
use Michelf\MarkdownExtra;
use League\CommonMark\Converter;
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;
use Webuni\CommonMark\TableExtension\TableExtension;

/**
* Filter input and escape output.
Expand All @@ -41,7 +43,7 @@ class Filter {
public static function formatText($text, Tree $WT_TREE) {
switch ($WT_TREE->getPreference('FORMAT_TEXT')) {
case 'markdown':
return '<div class="markdown" dir="auto">' . self::markdown($text) . '</div>';
return '<div class="markdown" dir="auto">' . self::markdown($text, $WT_TREE) . '</div>';
default:
return '<div style="white-space: pre-wrap;" dir="auto">' . self::expandUrls($text) . '</div>';
}
Expand All @@ -68,28 +70,19 @@ function ($m) {
* Format a block of text, using "Markdown".
*
* @param string $text
* @param Tree tree
*
* @return string
*/
public static function markdown($text) {
$parser = new MarkdownExtra;
$parser->empty_element_suffix = '>';
$parser->no_markup = true;
$text = $parser->transform($text);
public static function markdown($text, Tree $tree) {
$environment = Environment::createCommonMarkEnvironment();
$environment->mergeConfig(['html_input' => 'escape']);
$environment->addExtension(new TableExtension());
$environment->addInlineParser(new MarkdownXrefParser($tree));

// HTMLPurifier needs somewhere to write temporary files
$HTML_PURIFIER_CACHE_DIR = WT_DATA_DIR . 'html_purifier_cache';
$converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));

if (!is_dir($HTML_PURIFIER_CACHE_DIR)) {
mkdir($HTML_PURIFIER_CACHE_DIR);
}

$config = HTMLPurifier_Config::createDefault();
$config->set('Cache.SerializerPath', $HTML_PURIFIER_CACHE_DIR);
$purifier = new HTMLPurifier($config);
$text = $purifier->purify($text);

return $text;
return $converter->convertToHtml($text);
}

/**
Expand Down
82 changes: 82 additions & 0 deletions app/MarkdownXrefParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2017 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Fisharebest\Webtrees;

use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Inline\Parser\AbstractInlineParser;
use League\CommonMark\InlineParserContext;

/**
* Convert XREFs within markdown text to links
*/
class MarkdownXrefParser extends AbstractInlineParser {
/** @var Tree - match XREFs in this tree */
private $tree;

/**
* MarkdownXrefParser constructor.
*
* @param Tree $tree
*/
public function __construct(Tree $tree) {
$this->tree = $tree;
}

/**
* We are only interested in text that begins with '@'.
*
* @return array
*/
public function getCharacters() {
return ['@'];
}

/**
* @param InlineParserContext $context
*
* @return bool
*/
public function parse(InlineParserContext $context) {
// The cursor should be positioned on the opening '@'.
$cursor = $context->getcursor();

// If this isn't the start of an XREF, we'll need to rewind.
$previous_state = $cursor->saveState();

$handle = $cursor->match('/@' . WT_REGEX_XREF . '@/');
if (empty($handle)) {
// Not an XREF?
$cursor->restoreState($previous_state);

return false;
}

$record = GedcomRecord::getInstance(trim($handle, '@'), $this->tree);

if ($record === null) {
// Linked record does not exist?
$cursor->restoreState($previous_state);

return false;
}

$url = $record->getRawUrl();
$text = strip_tags($record->getFullName());
$context->getContainer()->appendChild(new Link($url, $text));

return true;
}
}
2 changes: 1 addition & 1 deletion app/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function extractNames() {
if ($text) {
switch ($this->getTree()->getPreference('FORMAT_TEXT')) {
case 'markdown':
$text = Filter::markdown($text);
$text = Filter::markdown($text, $this->getTree());
$text = strip_tags($text);
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
break;
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"config": {
"platform": {
"php" : "5.6"
}
},
"sort-packages": true
},
"require": {
"ext-gd": "*",
Expand All @@ -22,23 +23,23 @@
"ext-session": "*",
"ext-simplexml": "*",
"ext-xml": "*",
"ezyang/htmlpurifier": "~4.9",
"fisharebest/algorithm": "~1.3",
"fisharebest/ext-calendar": "~2.3",
"fisharebest/localization": "~1.10",
"guzzlehttp/guzzle": "~6.0",
"league/commonmark": "0.15.3",
"league/flysystem": "~1.0",
"league/flysystem-ziparchive": "~1.0",
"league/glide": "~1.2",
"michelf/php-markdown": "1.*",
"ramsey/uuid": "~3.6",
"swiftmailer/swiftmailer": "~5.0",
"symfony/http-foundation": "~3.2",
"symfony/polyfill-mbstring": "~1.4",
"symfony/polyfill-php70": "~1.4",
"symfony/polyfill-php71": "~1.4",
"symfony/polyfill-php72": "~1.4",
"tecnickcom/tcpdf": "~6.2"
"tecnickcom/tcpdf": "~6.2",
"webuni/commonmark-table-extension": "~0.6"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "*",
Expand Down
Loading

0 comments on commit 22acdb0

Please sign in to comment.