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['description'] = self::getDescriptionFormat($asTable['description']);
$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;
}

View File

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

View File

@@ -243,6 +243,9 @@ class ToolBox
//TODO implement link pattern
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);
//building all replacements possibilities
@@ -254,32 +257,38 @@ class ToolBox
$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();
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);
//Phase 2 : Identify remaining links (external)
$asPatterns = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`sui', '`((?<!//)(www\.\S+[[:alnum:]]/?))`sui');
//Phase 2: Identify remaining links (external)
$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>');
$sText = preg_replace($asPatterns, $asLinks, $sText);
//Phase 3: rebuild link
//Phase 3: Rebuild link
$asPatterns = array();
$sPreChar = '(^|\ |\*|\')';
$sPostChar = '(\ |\.|\:|$)';
foreach($asReplacements as $sHash=>$sPage)
{
if(mb_strlen($sHash)>1)
{
$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
$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);
@@ -287,6 +296,18 @@ class ToolBox
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)
{
$iTimeStamp = is_numeric($oTime)?$oTime:strtotime($oTime);