silverstripe-docsviewer/code/DocumentationHelper.php

126 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/**
* Collection of static helper methods for managing the documentation
*
* @package docsviewer
*/
2015-11-21 07:25:41 +01:00
class DocumentationHelper
{
/**
2017-08-08 05:50:24 +02:00
* String helper for cleaning a file name to a readable version.
2015-11-21 07:25:41 +01:00
*
* @param string $name to convert
*
* @return string $name output
*/
public static function clean_page_name($name)
{
$name = self::trim_extension_off($name);
$name = self::trim_sort_number($name);
2015-11-21 07:25:41 +01:00
$name = str_replace(array('-', '_'), ' ', $name);
2015-11-21 07:25:41 +01:00
return ucfirst(trim($name));
}
2015-11-21 07:25:41 +01:00
/**
* String helper for cleaning a file name to a URL safe version.
*
* @param string $name to convert
*
* @return string $name output
*/
public static function clean_page_url($name)
{
$name = str_replace(array(' '), '_', $name);
2015-11-21 07:25:41 +01:00
$name = self::trim_extension_off($name);
$name = self::trim_sort_number($name);
2015-11-21 07:25:41 +01:00
if (preg_match('/^[\/]?index[\/]?/', $name)) {
return '';
}
2015-11-21 07:25:41 +01:00
return strtolower($name);
}
2015-11-21 07:25:41 +01:00
/**
* Removes leading numbers from pages (used to control sort order).
*
* @param string
*
* @return string
*/
public static function trim_sort_number($name)
{
$name = preg_replace("/^[0-9]*[_-]+/", '', $name);
2015-11-21 07:25:41 +01:00
return $name;
}
2015-11-21 07:25:41 +01:00
/**
* Helper function to strip the extension off and return the name without
* the extension.
*
* @param string
*
* @return string
*/
public static function trim_extension_off($name)
{
if (strrpos($name, '.') !== false) {
return substr($name, 0, strrpos($name, '.'));
}
return $name;
}
2015-11-21 07:25:41 +01:00
/**
* Helper function to get the extension of the filename.
*
* @param string
*
* @return string
*/
public static function get_extension($name)
{
if (preg_match('/\.[a-z]+$/', $name)) {
return substr($name, strrpos($name, '.') + 1);
}
2015-11-21 07:25:41 +01:00
return null;
}
/**
* Helper function to normalize paths to unix style directory separators
2017-08-08 05:50:24 +02:00
*
* @param string
2017-08-08 05:50:24 +02:00
*
* @return string
*/
public static function normalizePath($path)
{
if (DIRECTORY_SEPARATOR != '/') {
return str_replace(DIRECTORY_SEPARATOR, '/', $path);
}
return $path;
}
/**
* Helper function to make normalized paths relative
2017-08-08 05:50:24 +02:00
*
* @param string
2017-08-08 05:50:24 +02:00
*
* @return string
*/
public static function relativePath($path)
{
$base = self::normalizePath(Director::baseFolder());
return substr($path, strlen($base));
}
2014-12-07 22:42:25 +01:00
}