Parse markdown on classic votes

This commit is contained in:
Olivier PEREZ 2015-01-01 23:24:17 +01:00
parent 313c1225d0
commit a8345cf7f8
3 changed files with 49 additions and 23 deletions

View File

@ -18,20 +18,18 @@
*/
namespace Framadate;
class Utils
{
public static function get_server_name()
{
class Utils {
public static function get_server_name() {
$scheme = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') ? 'https' : 'http';
$port = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT'];
$dirname = dirname($_SERVER['SCRIPT_NAME']);
$dirname = $dirname === '\\' ? '/' : $dirname . '/';
$server_name = $_SERVER['SERVER_NAME'] . $port . $dirname;
return $scheme . '://' . str_replace('/admin', '', str_replace('//', '/', str_replace('///', '/', $server_name)));
}
public static function is_error($cerr)
{
public static function is_error($cerr) {
global $err;
if ($err == 0) {
return false;
@ -40,8 +38,7 @@ class Utils
return ($err & $cerr) != 0;
}
public static function is_user()
{
public static function is_user() {
return (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) || isset($_SESSION['nom']);
}
@ -49,8 +46,7 @@ class Utils
* @param string $title
* @deprecated
*/
public static function print_header($title = '')
{
public static function print_header($title = '') {
global $lang;
echo '<!DOCTYPE html>
@ -93,19 +89,18 @@ class Utils
* @return bool True if valid. False if not valid.
* @deprecated
*/
public static function isValidEmail($email)
{
public static function isValidEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
/**
* Fonction permettant de générer les URL pour les sondage
*
* @param string $id L'identifiant du sondage
* @param bool $admin True pour générer une URL pour l'administration d'un sondage, False pour un URL publique
* @return string L'url pour le sondage
*/
public static function getUrlSondage($id, $admin = false)
{
public static function getUrlSondage($id, $admin = false) {
if (URL_PROPRE) {
if ($admin === true) {
$url = str_replace('/admin', '', self::get_server_name()) . $id . '/admin';
@ -125,6 +120,7 @@ class Utils
/**
* This method pretty prints an object to the page framed by pre tags.
*
* @param mixed $object The object to print.
*/
public static function debug($object) {
@ -136,4 +132,33 @@ class Utils
public static function table($tableName) {
return TABLENAME_PREFIX . $tableName;
}
public static function markdown($md) {
preg_match_all('/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/', $md, $md_a_img); // Markdown [![alt](src)](href)
preg_match_all('/!\[(.*?)\]\((.*?)\)/', $md, $md_img); // Markdown ![alt](src)
preg_match_all('/\[(.*?)\]\((.*?)\)/', $md, $md_a); // Markdown [text](href)
if (isset($md_a_img[2][0]) && $md_a_img[2][0] != '' && isset($md_a_img[3][0]) && $md_a_img[3][0] != '') { // [![alt](src)](href)
$text = stripslashes($md_a_img[1][0]);
$html = '<a href="' . $md_a_img[3][0] . '"><img src="' . $md_a_img[2][0] . '" class="img-responsive" alt="' . $text . '" title="' . $text . '" /></a>';
} elseif (isset($md_img[2][0]) && $md_img[2][0] != '') { // ![alt](src)
$text = stripslashes($md_img[1][0]);
$html = '<img src="' . $md_img[2][0] . '" class="img-responsive" alt="' . $text . '" title="' . $text . '" />';
} elseif (isset($md_a[2][0]) && $md_a[2][0] != '') { // [text](href)
$text = stripslashes($md_a[1][0]);
$html = '<a href="' . $md_a[2][0] . '">' . $text . '</a>';
} else { // text only
$text = stripslashes($md);
$html = $text;
}
return $html;
}
}

View File

@ -45,6 +45,7 @@ $smarty->assign('langs', $ALLOWED_LANGUAGES);
$smarty->assign('date_format', $date_format);
function smarty_modifier_poll_url($poll_id, $admin=false){return Utils::getUrlSondage($poll_id, $admin);}
function smarty_modifier_markdown($md) {return Utils::markdown($md);}
// End- Smarty
if (session_id() == '') {

View File

@ -25,7 +25,7 @@
<tr>
<th role="presentation"></th>
{foreach $slots as $id=>$slot}
<th class="bg-info" id="H{$id}">{$slot->title}</th>
<th class="bg-info" id="H{$id}">{$slot->title|markdown}</th>
{/foreach}
<th></th>
</tr>