Skip to content

Commit

Permalink
Merge pull request #7 from djmattyg007/increase_flexibility_of_librar…
Browse files Browse the repository at this point in the history
…y_code

Add templates for use by some library methods
  • Loading branch information
tburry committed Apr 26, 2016
2 parents 731ba5a + c2e991e commit 34f13a0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
53 changes: 53 additions & 0 deletions src/BBCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ class BBCode {
protected $local_img_url; // The URL path to local images (possibly a relative path).
protected $url_targetable; // If true, [url] tags can accept a target="..." parameter.
protected $url_target; // If non-false, [url] tags will use this target and no other.
protected $url_template; // The default template used with the default [url] tag.
protected $quote_template; // The default template used with the default [quote] tag.
protected $wiki_url_template; // The default template used when rendering wiki links.
protected $email_template; // The default template used with the default [email] tag.
protected $rule_html; // The default HTML to output for a [rule] tag.
protected $pre_trim; // How to trim the whitespace at the start of the input.
protected $post_trim; // How to trim the whitespace at the end of the input.
Expand Down Expand Up @@ -252,6 +256,11 @@ public function __construct() {
$this->url_pattern = '<a href="{$url/h}">{$text/h}</a>';
$this->url_targetable = false;
$this->url_target = false;
$this->url_template = '<a href="{$url/h}" class="bbcode_url"{$target/v}>{$content/v}</a>';
$this->quote_template = "\n" . '<div class="bbcode_quote">' . "\n" . '<div class="bbcode_quote_head">{$title/v}</div>' . "\n";
$this->quote_template .= '<div class="bbcode_quote_body">{$content/v}</div>' . "\n</div>\n";
$this->wiki_url_template = '<a href="{$wikiURL/v}{$name/v}" class="bbcode_wiki">{$title/h}</a>';
$this->email_template = '<a href="mailto:{$email/h}" class="bbcode_email">{$content/v}</a>';
$this->max_smileys = -1;
$this->escape_content = true;
}
Expand Down Expand Up @@ -440,6 +449,50 @@ public function getURLTarget() {
return $this->url_target;
}


public function setURLTemplate($template) {
$this->url_template = $template;
return $this;
}


public function getURLTemplate() {
return $this->url_template;
}


public function setQuoteTemplate($template) {
$this->quote_template = $template;
return $this;
}


public function getQuoteTemplate() {
return $this->quote_template;
}


public function setWikiURLTemplate($template) {
$this->wiki_url_template = $template;
return $this;
}


public function getWikiURLTemplate() {
return $this->wiki_url_template;
}


public function setEmailTemplate($template) {
$this->email_template = $template;
return $this;
}


public function getEmailTemplate() {
return $this->email_template;
}

/**
* Set the value for escape_content.
*
Expand Down
10 changes: 4 additions & 6 deletions src/BBCodeLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public function doURL(BBCode $bbcode, $action, $name, $default, $params, $conten
// enclosed in an <a href> tag. Remove that if that is the case.
$content = preg_replace('/^\\<a [^\\>]*\\>(.*?)<\\/a>$/', "\\1", $content);

return '<a href="'.htmlspecialchars($url).'" class="bbcode_url"'.$target.'>'.$content.'</a>';
return $bbcode->fillTemplate($bbcode->getURLTemplate(), array("url" => $url, "target" => $target, "content" => $content));
} else {
return htmlspecialchars($params['_tag']).$content.htmlspecialchars($params['_endtag']);
}
Expand Down Expand Up @@ -496,7 +496,7 @@ public function doEmail(BBCode $bbcode, $action, $name, $default, $params, $cont
: $bbcode->unHTMLEncode(strip_tags($content));

if ($bbcode->isValidEmail($email)) {
return '<a href="mailto:'.htmlspecialchars($email).'" class="bbcode_email">'.$content.'</a>';
return $bbcode->fillTemplate($bbcode->getEmailTemplate(), array("email" => $email, "content" => $content));
} else {
return htmlspecialchars($params['_tag']).$content.htmlspecialchars($params['_endtag']);
}
Expand Down Expand Up @@ -623,7 +623,7 @@ public function doWiki(BBCode $bbcode, $action, $name, $default, $params, $conte
}

$wikiURL = $bbcode->getWikiURL();
return "<a href=\"{$wikiURL}$name\" class=\"bbcode_wiki\">".htmlspecialchars($title)."</a>";
return $bbcode->fillTemplate($bbcode->getWikiURLTemplate(), array("wikiURL" => $wikiURL, "name" => $name, "title" => $title));
}


Expand Down Expand Up @@ -747,9 +747,7 @@ public function doQuote(BBCode $bbcode, $action, $name, $default, $params, $cont
$title = htmlspecialchars(trim($default))." wrote:";
}

return "\n<div class=\"bbcode_quote\">\n<div class=\"bbcode_quote_head\">"
.$title."</div>\n<div class=\"bbcode_quote_body\">"
.$content."</div>\n</div>\n";
return $bbcode->fillTemplate($bbcode->getQuoteTemplate(), array("title" => $title, "content" => $content));
}

/**
Expand Down

0 comments on commit 34f13a0

Please sign in to comment.