daux.io/libs/DauxHelper.php

442 lignes
16 KiB
PHP
Brut Vue normale Historique

<?php namespace Todaymade\Daux;
use Todaymade\Daux\Tree\Builder;
use Todaymade\Daux\Tree\Directory;
2015-04-23 00:32:30 +02:00
class DauxHelper
{
2015-07-20 15:59:52 +02:00
/**
* Set a new base_url for the configuration
*
* @param Config $config
* @param string $base_url
*/
2015-07-19 14:05:12 +02:00
public static function rebaseConfiguration(Config $config, $base_url)
{
// Avoid changing the url if it is already correct
2015-07-19 16:36:34 +02:00
if ($config['base_url'] == $base_url && !empty($config['theme'])) {
2015-07-19 14:05:12 +02:00
return;
}
// Change base url for all links on the pages
$config['base_url'] = $config['base_page'] = $base_url;
$config['theme'] = static::getTheme($config, $base_url);
$config['image'] = str_replace('<base_url>', $base_url, $config['image']);
}
public static function resolveVariant(Config $params)
{
if (array_key_exists('theme-variant', $params['html'])) {
return;
}
if (is_dir(realpath(($params->getThemesPath() . DIRECTORY_SEPARATOR . $params['html']['theme'])))) {
return;
}
$theme = explode('-', $params['html']['theme']);
// do we have a variant or only a theme ?
if (isset($theme[1])) {
$params['html']['theme-variant'] = array_pop($theme);
$params['html']['theme'] = implode('-', $theme);
} else {
$params['html']['theme'] = array_pop($theme);
}
if (!is_dir(realpath($params->getThemesPath() . DIRECTORY_SEPARATOR . $params['html']['theme']))) {
throw new \RuntimeException("Theme '{$params['html']['theme']}' not found");
}
}
2015-07-17 23:38:06 +02:00
/**
* @param Config $params
* @param string $current_url
* @return array
*/
public static function getTheme(Config $params, $current_url)
2015-04-23 00:32:30 +02:00
{
self::resolveVariant($params);
$theme_folder = $params->getThemesPath() . DIRECTORY_SEPARATOR . $params['html']['theme'];
$theme_url = $params['base_url'] . 'themes/' . $params['html']['theme'] . '/';
2016-07-27 21:32:51 +02:00
$theme = [];
if (is_file($theme_folder . DIRECTORY_SEPARATOR . 'config.json')) {
$theme = json_decode(file_get_contents($theme_folder . DIRECTORY_SEPARATOR . 'config.json'), true);
2015-04-23 00:32:30 +02:00
if (!$theme) {
2016-07-27 21:32:51 +02:00
$theme = [];
}
2015-04-23 00:32:30 +02:00
}
2015-04-23 00:32:30 +02:00
//Default parameters for theme
$theme += [
2015-07-19 16:36:34 +02:00
'name' => $params['html']['theme'],
2015-04-23 00:32:30 +02:00
'css' => [],
'js' => [],
'fonts' => [],
'favicon' => '<base_url>themes/daux/img/favicon.png',
2015-07-20 15:59:52 +02:00
'templates' => $theme_folder . DIRECTORY_SEPARATOR . 'templates',
'variants' => [],
2015-04-23 00:32:30 +02:00
];
if (array_key_exists('theme-variant', $params['html'])) {
$variant = $params['html']['theme-variant'];
if (!array_key_exists($variant, $theme['variants'])) {
throw new Exception("Variant '$variant' not found for theme '$theme[name]'");
}
// These will be replaced
foreach (['templates', 'favicon'] as $element) {
if (array_key_exists($element, $theme['variants'][$variant])) {
$theme[$element] = $theme['variants'][$variant][$element];
}
}
// These will be merged
foreach (['css', 'js', 'fonts'] as $element) {
if (array_key_exists($element, $theme['variants'][$variant])) {
$theme[$element] = array_merge($theme[$element], $theme['variants'][$variant][$element]);
}
}
}
$substitutions = [
'<local_base>' => $params['local_base'],
'<base_url>' => $current_url,
2016-07-27 21:32:51 +02:00
'<theme_url>' => $theme_url,
];
2015-04-23 00:32:30 +02:00
// Substitute some placeholders
2015-04-23 10:24:50 +02:00
$theme['templates'] = strtr($theme['templates'], $substitutions);
2015-04-23 00:32:30 +02:00
$theme['favicon'] = utf8_encode(strtr($theme['favicon'], $substitutions));
2015-07-17 23:38:06 +02:00
foreach (['css', 'js', 'fonts'] as $element) {
foreach ($theme[$element] as $key => $value) {
$theme[$element][$key] = utf8_encode(strtr($value, $substitutions));
}
}
2015-04-23 00:32:30 +02:00
return $theme;
}
2015-07-17 23:38:06 +02:00
/**
2016-07-29 23:46:57 +02:00
* Remove all '/./' and '/../' in a path, without actually checking the path
*
2015-07-17 23:38:06 +02:00
* @param string $path
* @return string
*/
public static function getCleanPath($path)
{
2016-07-27 21:32:51 +02:00
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
2016-07-27 21:32:51 +02:00
$absolutes = [];
foreach ($parts as $part) {
if ('.' == $part) {
continue;
}
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
2016-07-27 21:32:51 +02:00
return implode(DIRECTORY_SEPARATOR, $absolutes);
}
2016-07-29 23:46:57 +02:00
/**
* Get the possible output file names for a source file.
*
* @param Config $config
* @param string $part
* @return string[]
*/
public static function getFilenames(Config $config, $part)
{
2016-07-27 21:32:51 +02:00
$extensions = implode('|', array_map('preg_quote', $config['valid_content_extensions'])) . '|html';
2016-07-27 21:32:51 +02:00
$raw = preg_replace('/(.*)?\\.(' . $extensions . ')$/', '$1', $part);
$raw = Builder::removeSortingInformations($raw);
return ["$raw.html", $raw];
}
2015-07-17 23:38:06 +02:00
/**
* Locate a file in the tree. Returns the file if found or false
*
* @param Directory $tree
* @param string $request
2015-07-18 21:45:38 +02:00
* @return Tree\Content|Tree\Raw|false
2015-07-17 23:38:06 +02:00
*/
public static function getFile($tree, $request)
{
$request = explode('/', $request);
foreach ($request as $node) {
// If the element we're in currently is not a
// directory, we failed to find the requested file
if (!$tree instanceof Directory) {
return false;
}
// Some relative paths may start with ./
if ($node == '.') {
continue;
}
if ($node == '..') {
$tree = $tree->getParent();
continue;
}
$node = urldecode($node);
// if the node exists in the current request tree,
// change the $tree variable to reference the new
// node and proceed to the next url part
if (isset($tree->getEntries()[$node])) {
$tree = $tree->getEntries()[$node];
continue;
}
// if the node doesn't exist, we can try
// two variants of the requested file:
// with and w/o the .html extension
foreach (static::getFilenames($tree->getConfig(), $node) as $filename) {
if (isset($tree->getEntries()[$filename])) {
$tree = $tree->getEntries()[$filename];
continue 2;
}
}
// At this stage, we're in a directory, but no
// sub-item matches, so the current node must
// be an index page or we failed
if ($node !== 'index' && $node !== 'index.html') {
return false;
}
return $tree->getIndexPage();
}
// If the entry we found is not a directory, we're done
if (!$tree instanceof Directory) {
return $tree;
}
if ($index = $tree->getIndexPage()) {
return $index;
}
return false;
}
2015-07-17 18:30:10 +02:00
/**
* Generate a URL friendly "slug" from a given string.
*
* Taken from Stringy
*
* @param string $title
2015-07-17 18:30:10 +02:00
* @return string
*/
public static function slug($title)
{
foreach (static::charsArray() as $key => $value) {
$title = str_replace($value, $key, $title);
}
$title = preg_replace('/[^\x20-\x7E]/u', '', $title);
$separator = '_';
// Convert all dashes into underscores
2016-07-27 21:32:51 +02:00
$title = preg_replace('![' . preg_quote('-') . ']+!u', $separator, $title);
2015-07-17 23:38:06 +02:00
2015-07-17 18:30:10 +02:00
// Remove all characters that are not the separator, letters, numbers, or whitespace.
2015-07-17 23:38:06 +02:00
$title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', $title);
2015-07-17 18:30:10 +02:00
// Replace all separator characters and whitespace by a single separator
2015-07-17 23:38:06 +02:00
$title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
2015-07-17 18:30:10 +02:00
return trim($title, $separator);
}
/**
* Returns the replacements for the slug() method.
*
* Taken from Stringy
*
* @return array An array of replacements.
*/
public static function charsArray()
{
static $charsArray;
2015-07-17 23:38:06 +02:00
if (isset($charsArray)) {
return $charsArray;
}
2015-07-17 18:30:10 +02:00
2016-07-27 21:32:51 +02:00
return $charsArray = [
'a' => [
2015-07-17 18:30:10 +02:00
'à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ',
'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ä', 'ā', 'ą',
'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ',
'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ',
2016-07-27 21:32:51 +02:00
'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', ],
'b' => ['б', 'β', 'Ъ', 'Ь', 'ب'],
'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ'],
'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ',
'д', 'δ', 'د', 'ض', ],
'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ',
2015-07-17 18:30:10 +02:00
'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ',
'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э',
2016-07-27 21:32:51 +02:00
'є', 'ə', ],
'f' => ['ф', 'φ', 'ف'],
'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ج'],
'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه'],
'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į',
2015-07-17 18:30:10 +02:00
'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ',
'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ',
2016-07-27 21:32:51 +02:00
'ῗ', 'і', 'ї', 'и', ],
'j' => ['ĵ', 'ј', 'Ј'],
'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك'],
'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل'],
'm' => ['м', 'μ', 'م'],
'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن'],
'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ',
2015-07-17 18:30:10 +02:00
'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő',
'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό',
2016-07-27 21:32:51 +02:00
'ö', 'о', 'و', 'θ', ],
'p' => ['п', 'π'],
'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر'],
's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص'],
't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط'],
'u' => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ',
'ự', 'ü', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', ],
'v' => ['в'],
'w' => ['ŵ', 'ω', 'ώ'],
'x' => ['χ'],
'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ',
'ϋ', 'ύ', 'ΰ', 'ي', ],
'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز'],
'aa' => ['ع'],
'ae' => ['æ'],
'ch' => ['ч'],
'dj' => ['ђ', 'đ'],
'dz' => ['џ'],
'gh' => ['غ'],
'kh' => ['х', 'خ'],
'lj' => ['љ'],
'nj' => ['њ'],
'oe' => ['œ'],
'ps' => ['ψ'],
'sh' => ['ш'],
'shch' => ['щ'],
'ss' => ['ß'],
'th' => ['þ', 'ث', 'ذ', 'ظ'],
'ts' => ['ц'],
'ya' => ['я'],
'yu' => ['ю'],
'zh' => ['ж'],
'(c)' => ['©'],
'A' => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ',
2015-07-17 18:30:10 +02:00
'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', '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\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84",
"\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87",
"\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A",
2016-07-27 21:32:51 +02:00
"\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80", ],
];
2015-07-17 18:30:10 +02:00
}
2016-07-29 23:46:57 +02:00
/**
* @param string $from
* @param string $to
* @return string
*/
public static function getRelativePath($from, $to)
{
// some compatibility fixes for Windows paths
$from = is_dir($from) ? rtrim($from, '\/') . '/' : $from;
$to = is_dir($to) ? rtrim($to, '\/') . '/' : $to;
$from = str_replace('\\', '/', $from);
$to = str_replace('\\', '/', $to);
$from = explode('/', $from);
$to = explode('/', $to);
$relPath = $to;
foreach ($from as $depth => $dir) {
// find first non-matching dir
if ($dir === $to[$depth]) {
// ignore this directory
array_shift($relPath);
} else {
// get number of remaining dirs to $from
$remaining = count($from) - $depth;
if ($remaining > 1) {
// add traversals up to first matching dir
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
//$relPath[0] = './' . $relPath[0];
}
}
}
2016-07-27 21:32:51 +02:00
return implode('/', $relPath);
}
2015-04-23 00:32:30 +02:00
}