Skip to content

Commit

Permalink
Fmt: Add :humanize_url manipulator.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 20, 2023
1 parent 5467574 commit a3342c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/fmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ private function expand_brace($s, $pos, $fctx) {
} else if ($m[3] === ":numlist") {
assert(is_array($value));
$value = numrangejoin($value);
} else if ($m[3] === ":humanize_url") {
if (preg_match('/\Ahttps?:\/\/([^\[\]:\/?#\s]*)([\/?#]\S*|)\z/i', $value, $mm)) {
$value = $mm[1] . ($mm[2] === "/" ? "" : $mm[2]);
}
} else if ($m[3] === ":ftext") {
if ($vformat === null) {
list($vformat, $value) = Ftext::parse($value);
Expand Down
9 changes: 9 additions & 0 deletions test/t_fmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,13 @@ function test_ftext() {
xassert_eqq($ms->_("<5>{a}", new FmtArg("a", "&", 0)), "<5>&amp;");
xassert_eqq($ms->_("<5>{a:html}", new FmtArg("a", "&", 0)), "<5>&amp;");
}

function test_humanize_url() {
$ms = new Fmt;
xassert_eqq($ms->_("{}", "http://www.hello.com/"), "http://www.hello.com/");
xassert_eqq($ms->_("{:humanize_url}", "http://www.hello.com/"), "www.hello.com");
xassert_eqq($ms->_("{:humanize_url}", "https://www.hello.com/"), "www.hello.com");
xassert_eqq($ms->_("<5><a href=\"{0}\">{0:humanize_url}</a>", new FmtArg(0, "http://www.hello.com/", 0)), "<5><a href=\"http://www.hello.com/\">www.hello.com</a>");
xassert_eqq($ms->_("<5><a href=\"{0}\">{0:humanize_url}</a>", new FmtArg(0, "https://www.hello.com/\"", 0)), "<5><a href=\"https://www.hello.com/&quot;\">www.hello.com/&quot;</a>");
}
}

0 comments on commit a3342c7

Please sign in to comment.