fixing internal links in table

This commit is contained in:
2014-08-30 12:04:57 +02:00
parent 7adbc1c356
commit 28f2dabde5
5 changed files with 37 additions and 12 deletions

View File

@@ -1146,7 +1146,7 @@ class Databap extends PhpObject
$asTable['title'] = self::getTableFormat($asTable['title']); $asTable['title'] = self::getTableFormat($asTable['title']);
$asTable['description'] = self::getDescriptionFormat($asTable['description']); $asTable['description'] = self::getDescriptionFormat($asTable['description']);
$asTable['led'] = self::getDateFormat($asTable['led']); $asTable['led'] = self::getDateFormat($asTable['led']);
$asTable['formatted_keywords'] = ToolBox::findReplaceLinks($asTable['keywords']); $asTable['formatted_keywords'] = str_replace("\n", '<br />', ToolBox::findReplaceLinks($asTable['keywords']));
} }
return $asTable; return $asTable;
} }

View File

@@ -139,7 +139,7 @@ class Reader extends PhpObject
$sCode = $this->convText2Html($this->sCode); $sCode = $this->convText2Html($this->sCode);
//$sServName = $_GET['serv_name']; //$sServName = $_GET['serv_name'];
$asLines = explode("\n", str_replace("\r\n", "\n", $sCode)); $asLines = explode("\n", ToolBox::fixEOL($sCode));
$asLineClasses = array(); $asLineClasses = array();
$iOffSet = 0; $iOffSet = 0;

View File

@@ -243,6 +243,9 @@ class ToolBox
//TODO implement link pattern //TODO implement link pattern
public static function findReplaceLinks($sText, $sLinkPattern='') public static function findReplaceLinks($sText, $sLinkPattern='')
{ {
//Fix Carriage Return / Line Feed OS issue
$sText = self::fixEOL($sText);
$sServerPath = substr(str_replace(array('http://', 'https://'), '', $_GET['serv_name']), 0, -1); $sServerPath = substr(str_replace(array('http://', 'https://'), '', $_GET['serv_name']), 0, -1);
//building all replacements possibilities //building all replacements possibilities
@@ -254,32 +257,38 @@ class ToolBox
$asReplacements[preg_quote(Databap::$PAGE_TITLES[$sPage])] = $sPage; $asReplacements[preg_quote(Databap::$PAGE_TITLES[$sPage])] = $sPage;
} }
//Phase 1 : Identify internal links : merge cases /#code-1 and code 1, destroy link. //Phase 1: Identify internal links : merge cases /#code-1 and code 1, destroy link.
$asPatterns = array(); $asPatterns = array();
foreach($asReplacements as $sHash=>$sPage) foreach($asReplacements as $sHash=>$sPage)
{ {
$asPatterns['`(^|\ )(https?://|)'.$sServerPath.'/?\#'.$sHash.'\-([\d]+)`sui'] = '$1$4'.$sPage.' $3'; //Type + id
$asPatterns['`(^|\ )(https?://|)'.$sServerPath.'/?\#'.$sHash.'\-([\d]+)`mui'] = '$1$4'.$sPage.' $3';
//Type + phrase
if($sPage=='table') $asPatterns['`(^|\ )(https?://|)'.$sServerPath.'/?\#(?i)'.$sHash.'(?-i)\-([_A-Z0-9]+)`mu'] = '$1$4'.$sPage.' $3';
} }
$sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText); $sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText);
//Phase 2 : Identify remaining links (external) //Phase 2: Identify remaining links (external)
$asPatterns = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`sui', '`((?<!//)(www\.\S+[[:alnum:]]/?))`sui'); $asPatterns = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`mui', '`((?<!//)(www\.\S+[[:alnum:]]/?))`mui');
$asLinks = array('<a href="$1" class="external_link" target="_blank"><i class="fa fa-inline fa-c-share"></i>$1</a> ', '<a href="http://$1" class="external_link" target="_blank"><i class="fa fa-inline fa-c-share"></i>$1</a>'); $asLinks = array('<a href="$1" class="external_link" target="_blank"><i class="fa fa-inline fa-c-share"></i>$1</a> ', '<a href="http://$1" class="external_link" target="_blank"><i class="fa fa-inline fa-c-share"></i>$1</a>');
$sText = preg_replace($asPatterns, $asLinks, $sText); $sText = preg_replace($asPatterns, $asLinks, $sText);
//Phase 3: rebuild link //Phase 3: Rebuild link
$asPatterns = array(); $asPatterns = array();
$sPreChar = '(^|\ |\*|\')';
$sPostChar = '(\ |\.|\:|$)';
foreach($asReplacements as $sHash=>$sPage) foreach($asReplacements as $sHash=>$sPage)
{ {
if(mb_strlen($sHash)>1) if(mb_strlen($sHash)>1)
{ {
$sTitle = Databap::$PAGE_TITLES[$sPage]; $sTitle = Databap::$PAGE_TITLES[$sPage];
//Type + phrase
if($sPage=='table') $asPatterns['`(^|\ |\*)(?i)'.$sHash.'(?-i)\ ([_A-Z0-9]+)(\ |$)`su'] = '$1<a href="#'.$sPage.'-$2" target="_blank" class="internal_link round"><i class="fa fa-inline fa-c-'.$sPage.'"></i><span class="type">'.$sTitle.'</span> <span class="item">$2</span></a>$3';
//Type + id //Type + id
$asPatterns['`(^|\ |\*)'.$sHash.'\ ([\d]+)(\ |$)`sui'] = '$1<a href="#'.$sPage.'-$2" target="_blank" class="internal_link round"><i class="fa fa-inline fa-c-'.$sPage.'"></i><span class="type">'.$sTitle.'</span> n°<span class="item">$2</span></a>$3'; $asPatterns['`'.$sPreChar.$sHash.'\ ([\d]+)'.$sPostChar.'`mui'] = '$1<a href="#'.$sPage.'-$2" target="_blank" class="internal_link round"><i class="fa fa-inline fa-c-'.$sPage.'"></i><span class="type">'.$sTitle.'</span> n°<span class="item">$2</span></a>$3';
//Type + phrase
if($sPage=='table') $asPatterns['`'.$sPreChar.'(?i)'.$sHash.'(?-i)\ ([_A-Z0-9]+)'.$sPostChar.'`mu'] = '$1<a href="#'.$sPage.'-$2" target="_blank" class="internal_link round"><i class="fa fa-inline fa-c-'.$sPage.'"></i><span class="type">'.$sTitle.'</span> <span class="item">$2</span></a>$3';
} }
} }
$sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText); $sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText);
@@ -287,6 +296,18 @@ class ToolBox
return $sText; return $sText;
} }
public static function fixEOL($sText)
{
//Normalize line endings
//Convert all line-endings to UNIX format
$sText = str_replace("\r\n", "\n", $sText); //Windows
$sText = str_replace("\r", "\n", $sText); //Mac
// Don't allow out-of-control blank lines
$sText = preg_replace("/\n{2,}/", "\n\n", $sText);
return $sText;
}
public static function getDateTimeDesc($oTime) public static function getDateTimeDesc($oTime)
{ {
$iTimeStamp = is_numeric($oTime)?$oTime:strtotime($oTime); $iTimeStamp = is_numeric($oTime)?$oTime:strtotime($oTime);

View File

@@ -101,7 +101,7 @@ Table.prototype.applyTableInfo = function()
{ {
if(sKey=='warning' && sValue!='') databap.addWarningBefore(sValue, tableself.sFeedBackBoxId); if(sKey=='warning' && sValue!='') databap.addWarningBefore(sValue, tableself.sFeedBackBoxId);
//debug('key='+sKey+', value='+sValue); //debug('key='+sKey+', value='+sValue);
$('#read_'+sKey).html(sValue.replace(/(?:\r\n|\r|\n)/g, '<br />')); $('#read_'+sKey).html(sValue); //sValue.replace(/(?:\r\n|\r|\n)/g, '<br />')
$('#'+sKey).val(sValue); $('#'+sKey).val(sValue);
}); });
tableself.setDisplay(tableself.DISPLAY_READ); tableself.setDisplay(tableself.DISPLAY_READ);

View File

@@ -2400,6 +2400,10 @@ vertical-align 0% -5% -10% -15% -20% -25% -30%
font-size: 0.9em; font-size: 0.9em;
} }
#table #read_formatted_keywords a.internal_link {
padding:0 0 0 2px;
}
#table input:disabled { #table input:disabled {
color:#D9E5F2; color:#D9E5F2;
background-color:#04357B; background-color:#04357B;