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; namespace Framadate;
class Utils class Utils {
{ public static function get_server_name() {
public static function get_server_name()
{
$scheme = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') ? 'https' : 'http'; $scheme = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') ? 'https' : 'http';
$port = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT']; $port = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT'];
$dirname = dirname($_SERVER['SCRIPT_NAME']); $dirname = dirname($_SERVER['SCRIPT_NAME']);
$dirname = $dirname === '\\' ? '/' : $dirname . '/'; $dirname = $dirname === '\\' ? '/' : $dirname . '/';
$server_name = $_SERVER['SERVER_NAME'] . $port . $dirname; $server_name = $_SERVER['SERVER_NAME'] . $port . $dirname;
return $scheme . '://' . str_replace('/admin','',str_replace('//','/',str_replace('///','/',$server_name)));
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; global $err;
if ($err == 0) { if ($err == 0) {
return false; return false;
@ -40,8 +38,7 @@ class Utils
return ($err & $cerr) != 0; 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']); return (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) || isset($_SESSION['nom']);
} }
@ -49,16 +46,15 @@ class Utils
* @param string $title * @param string $title
* @deprecated * @deprecated
*/ */
public static function print_header($title = '') public static function print_header($title = '') {
{
global $lang; global $lang;
echo '<!DOCTYPE html> echo '<!DOCTYPE html>
<html lang="'.$lang.'"> <html lang="' . $lang . '">
<head> <head>
<meta charset="utf-8">'; <meta charset="utf-8">';
if (! empty($title)) { if (!empty($title)) {
echo '<title>' . stripslashes($title) . ' - ' . NOMAPPLICATION . '</title>'; echo '<title>' . stripslashes($title) . ' - ' . NOMAPPLICATION . '</title>';
} else { } else {
echo '<title>' . NOMAPPLICATION . '</title>'; echo '<title>' . NOMAPPLICATION . '</title>';
@ -73,9 +69,9 @@ class Utils
<script type="text/javascript" src="' . self::get_server_name() . 'js/jquery-1.11.1.min.js"></script> <script type="text/javascript" src="' . self::get_server_name() . 'js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap.min.js"></script> <script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap.min.js"></script>
<script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap-datepicker.js"></script> <script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="' . self::get_server_name() . 'js/locales/bootstrap-datepicker.'.$lang.'.js"></script> <script type="text/javascript" src="' . self::get_server_name() . 'js/locales/bootstrap-datepicker.' . $lang . '.js"></script>
<script type="text/javascript" src="' . self::get_server_name() . 'js/core.js"></script>'; <script type="text/javascript" src="' . self::get_server_name() . 'js/core.js"></script>';
if (file_exists($_SERVER['DOCUMENT_ROOT']."/nav/nav.js")) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/nav/nav.js")) {
echo '<script src="/nav/nav.js" id="nav_js" type="text/javascript" charset="utf-8"></script><!-- /Framanav -->'; echo '<script src="/nav/nav.js" id="nav_js" type="text/javascript" charset="utf-8"></script><!-- /Framanav -->';
} }
@ -93,19 +89,18 @@ class Utils
* @return bool True if valid. False if not valid. * @return bool True if valid. False if not valid.
* @deprecated * @deprecated
*/ */
public static function isValidEmail($email) public static function isValidEmail($email) {
{
return filter_var($email, FILTER_VALIDATE_EMAIL); return filter_var($email, FILTER_VALIDATE_EMAIL);
} }
/** /**
* Fonction permettant de générer les URL pour les sondage * Fonction permettant de générer les URL pour les sondage
*
* @param string $id L'identifiant du 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 * @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 * @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 (URL_PROPRE) {
if ($admin === true) { if ($admin === true) {
$url = str_replace('/admin', '', self::get_server_name()) . $id . '/admin'; $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. * This method pretty prints an object to the page framed by pre tags.
*
* @param mixed $object The object to print. * @param mixed $object The object to print.
*/ */
public static function debug($object) { public static function debug($object) {
@ -136,4 +132,33 @@ class Utils
public static function table($tableName) { public static function table($tableName) {
return TABLENAME_PREFIX . $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); $smarty->assign('date_format', $date_format);
function smarty_modifier_poll_url($poll_id, $admin=false){return Utils::getUrlSondage($poll_id, $admin);} 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 // End- Smarty
if (session_id() == '') { if (session_id() == '') {

View File

@ -25,7 +25,7 @@
<tr> <tr>
<th role="presentation"></th> <th role="presentation"></th>
{foreach $slots as $id=>$slot} {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} {/foreach}
<th></th> <th></th>
</tr> </tr>