<commit_before><?php

class Almagesq {


	/*************************************************************************
	  CONSTANTS
	 *************************************************************************/
	const MAX_DEPTH = 2;
	const SETTINGS_PATH = '/../settings';


	/*************************************************************************
	  ATTRIBUTES		   
	 *************************************************************************/
	public $themePath = '/../settings/themes';
	public $patternPath;
	public $menus = array( );
	public $currentMenus = array( );
	public $patterns = array( );
	public $currentPattern;
	public $themes = array( );
	public $currentTheme;
	public $settings;


	/*************************************************************************
	  STATIC METHODS				   
	 *************************************************************************/
	public static function hasPatterns( $menu ) {
		return ( is_array( $menu ) && ! empty( $menu ) && is_numeric( current( array_keys( $menu ) ) ) );
	}
	public static function FileHumanName( $file ) {
		$fileName = static::FileName( $file );
		if ( strpos( $fileName, '-' ) !== FALSE ) {
			$prefix = substr( $fileName, 0, strpos( $fileName, '-' ) );
			if ( is_numeric( $prefix ) ) {
			  $fileName = substr( $fileName, strlen( $prefix ) + 1 );
			}
		}
		$fileName = ucfirst( str_replace( '_', ' ', $fileName ) );
  		return $fileName;
	}
	public static function FileName( $file ) {
		$fileName = basename( $file );
		if ( $pos = strpos( $file, '.' ) ) {
			$fileName = substr( $fileName, 0, $pos );
		}
		return $fileName;
	}


	/*************************************************************************
	  PUBLIC METHODS				   
	 *************************************************************************/
	public function getMenuPath( $menus ) {
		$path = $this->patternPath . '/' . implode( $menus, '/' );
		return realpath( $path );
	}
	public function getPatternPath( $pattern ) {
		$path = $this->getMenuPath( $this->currentMenus ) . '/' . $pattern;
		return realpath( $path );
	}
	public function getPatternHtml( $pattern ) {
		if ( $path = $this->getPatternPath( $pattern ) ) {
			return file_get_contents( $path );
		}
	}
	public function getCurrentPatternHtml( ) {
		return $this->getPatternHtml( $this->currentPattern );
	}
	public function getTitle( ) {
		$title = 'Style Guide';
		if ( isset( $this->settings[ 'nav-bar' ][ 'title' ] ) ) {
			$title = $this->settings[ 'nav-bar' ][ 'title' ];
		}
		return $title;
	}
	public function getNavbarStyle( $property ) {
		$style = '';
		$styles = array( );
		if ( isset( $this->settings[ 'nav-bar' ][ 'style' ] ) ) {
			$styles = $this->settings[ 'nav-bar' ][ 'style' ];
		}
		if ( is_array( $styles ) && isset( $styles[ $property ] ) ) {
			$style = $styles[ $property ];
		}
		return $style;
	}
	public function getStyles( ) {
		$styles = array( );
		if ( isset( $this->settings[ 'pattern' ][ 'styles' ] ) ) {
			$styles = $this->settings[ 'pattern' ][ 'styles' ];
			if ( ! is_array( $styles ) ) {
				$styles = array( $styles );
			}
			foreach ( $styles as $key => $url ) {
				$pos = strpos( $url, '|' );
				if ( $pos === FALSE ) {
					$condition = '';
				} else {
					$condition = substr( $url, 0, $pos );
					$url = substr( $url, $pos+1 );
				}
				$styles[ $key ] = array( 'condition' => $condition, 'url' => $url );
			}
		}
		return $styles;
	}
	public function getScripts( ) {
		$scripts = array( );
		if ( isset( $this->settings[ 'pattern' ][ 'scripts' ] ) ) {
			$scripts = $this->settings[ 'pattern' ][ 'scripts' ];
			if ( ! is_array( $scripts ) ) {
				$scripts = array( $scripts );
			}
		}
		return $scripts;
	}
	public function getPatternBackground( ) {
		$settings = $this->getPatternSettings( );
		if ( isset( $settings[ 'background' ] ) ) {
			return $settings[ 'background' ];
		}
		return 'transparent';
	}
	public function getPatternBreakpoints( ) {
		$settings = $this->getPatternSettings( );
		if ( isset( $settings[ 'breakpoints' ] ) ) {
			return $settings[ 'breakpoints' ];
		}
		return array( );
	}
	public function getMenuHttpQuery( $menus = NULL ) {
		$compiledMenus = $this->currentMenus;
		if ( is_array( $menus ) ) {
			if ( isset( $menus[ 0 ] ) ) {
				$compiledMenus[ 0 ] = $menus[ 0 ];
				unset( $compiledMenus[ 1 ] );
			}
			if ( isset( $menus[ 1 ] ) ) {
				$compiledMenus[ 1 ] = $menus[ 1 ];
			}
			if ( ! isset( $menus[ 0 ] ) && ! isset( $menus[ 1 ] ) ) {
				$compiledMenus = array( );
			}
		}
		$query = '';
		if ( isset( $compiledMenus[ 0 ] ) ) {
			$query .= '&amp;menu[]=' . $compiledMenus[ 0 ];
			if ( isset( $compiledMenus[ 1 ] ) ) {
				$query .= '&amp;menu[]=' . $compiledMenus[ 1 ];
			}
		}
		return $query;
	}
	public function getThemeHttpQuery( $theme = NULL ) {
		if ( is_null( $theme ) ) {
			$theme = $this->currentTheme;
		}
		return 'theme=' . $theme;
	}
	public function getHttpQuery( $menus = NULL, $theme = NULL ) {
		return '?' . $this->getThemeHttpQuery( $theme ) . $this->getMenuHttpQuery( $menus );
	}


	/*************************************************************************
	  CONSTRUCTOR METHODS				   
	 *************************************************************************/
	public function __construct( $themePath = NULL ) {
		if ( ! is_null( $themePath ) ) {
			$this->themePath = $themePath;
		}
		$this->themes = $this->initThemes( );
		$this->settings = $this->initSettings( );
		$this->patternPath = $this->initPatternPath( );
		$this->menus = UFIle::folderTree( $this->patternPath, '*.html', static::MAX_DEPTH, UFile::FILE_FLAG );
		$this->currentMenus = $this->initCurrentMenus( );
		$this->patterns = $this->initPatterns( );
		$this->currentPattern = $this->initCurrentPattern( );
	}
	protected function initThemes( ) {
		$themes = \UFile::fileList( __DIR__ . $this->themePath, '*.ini' );
		if ( empty( $themes ) ) {
			if ( ! $themes = realpath( __DIR__ . static::SETTINGS_PATH . '/default.ini' ) ) {
				echo 'Settings file not found :\'(';
				die;
			}<commit_msg>Manage absolute & relative themes path<commit_after><?php

class Almagesq {


	/*************************************************************************
	  CONSTANTS
	 *************************************************************************/
	const MAX_DEPTH = 2;
	const SETTINGS_PATH = '/../settings';


	/*************************************************************************
	  ATTRIBUTES		   
	 *************************************************************************/
	public $themePath = '/../settings/themes';
	public $patternPath;
	public $menus = array( );
	public $currentMenus = array( );
	public $patterns = array( );
	public $currentPattern;
	public $themes = array( );
	public $currentTheme;
	public $settings;


	/*************************************************************************
	  STATIC METHODS				   
	 *************************************************************************/
	public static function hasPatterns( $menu ) {
		return ( is_array( $menu ) && ! empty( $menu ) && is_numeric( current( array_keys( $menu ) ) ) );
	}
	public static function FileHumanName( $file ) {
		$fileName = static::FileName( $file );
		if ( strpos( $fileName, '-' ) !== FALSE ) {
			$prefix = substr( $fileName, 0, strpos( $fileName, '-' ) );
			if ( is_numeric( $prefix ) ) {
			  $fileName = substr( $fileName, strlen( $prefix ) + 1 );
			}
		}
		$fileName = ucfirst( str_replace( '_', ' ', $fileName ) );
  		return $fileName;
	}
	public static function FileName( $file ) {
		$fileName = basename( $file );
		if ( $pos = strpos( $file, '.' ) ) {
			$fileName = substr( $fileName, 0, $pos );
		}
		return $fileName;
	}


	/*************************************************************************
	  PUBLIC METHODS				   
	 *************************************************************************/
	public function getMenuPath( $menus ) {
		$path = $this->patternPath . '/' . implode( $menus, '/' );
		return realpath( $path );
	}
	public function getPatternPath( $pattern ) {
		$path = $this->getMenuPath( $this->currentMenus ) . '/' . $pattern;
		return realpath( $path );
	}
	public function getPatternHtml( $pattern ) {
		if ( $path = $this->getPatternPath( $pattern ) ) {
			return file_get_contents( $path );
		}
	}
	public function getCurrentPatternHtml( ) {
		return $this->getPatternHtml( $this->currentPattern );
	}
	public function getTitle( ) {
		$title = 'Style Guide';
		if ( isset( $this->settings[ 'nav-bar' ][ 'title' ] ) ) {
			$title = $this->settings[ 'nav-bar' ][ 'title' ];
		}
		return $title;
	}
	public function getNavbarStyle( $property ) {
		$style = '';
		$styles = array( );
		if ( isset( $this->settings[ 'nav-bar' ][ 'style' ] ) ) {
			$styles = $this->settings[ 'nav-bar' ][ 'style' ];
		}
		if ( is_array( $styles ) && isset( $styles[ $property ] ) ) {
			$style = $styles[ $property ];
		}
		return $style;
	}
	public function getStyles( ) {
		$styles = array( );
		if ( isset( $this->settings[ 'pattern' ][ 'styles' ] ) ) {
			$styles = $this->settings[ 'pattern' ][ 'styles' ];
			if ( ! is_array( $styles ) ) {
				$styles = array( $styles );
			}
			foreach ( $styles as $key => $url ) {
				$pos = strpos( $url, '|' );
				if ( $pos === FALSE ) {
					$condition = '';
				} else {
					$condition = substr( $url, 0, $pos );
					$url = substr( $url, $pos+1 );
				}
				$styles[ $key ] = array( 'condition' => $condition, 'url' => $url );
			}
		}
		return $styles;
	}
	public function getScripts( ) {
		$scripts = array( );
		if ( isset( $this->settings[ 'pattern' ][ 'scripts' ] ) ) {
			$scripts = $this->settings[ 'pattern' ][ 'scripts' ];
			if ( ! is_array( $scripts ) ) {
				$scripts = array( $scripts );
			}
		}
		return $scripts;
	}
	public function getPatternBackground( ) {
		$settings = $this->getPatternSettings( );
		if ( isset( $settings[ 'background' ] ) ) {
			return $settings[ 'background' ];
		}
		return 'transparent';
	}
	public function getPatternBreakpoints( ) {
		$settings = $this->getPatternSettings( );
		if ( isset( $settings[ 'breakpoints' ] ) ) {
			return $settings[ 'breakpoints' ];
		}
		return array( );
	}
	public function getMenuHttpQuery( $menus = NULL ) {
		$compiledMenus = $this->currentMenus;
		if ( is_array( $menus ) ) {
			if ( isset( $menus[ 0 ] ) ) {
				$compiledMenus[ 0 ] = $menus[ 0 ];
				unset( $compiledMenus[ 1 ] );
			}
			if ( isset( $menus[ 1 ] ) ) {
				$compiledMenus[ 1 ] = $menus[ 1 ];
			}
			if ( ! isset( $menus[ 0 ] ) && ! isset( $menus[ 1 ] ) ) {
				$compiledMenus = array( );
			}
		}
		$query = '';
		if ( isset( $compiledMenus[ 0 ] ) ) {
			$query .= '&amp;menu[]=' . $compiledMenus[ 0 ];
			if ( isset( $compiledMenus[ 1 ] ) ) {
				$query .= '&amp;menu[]=' . $compiledMenus[ 1 ];
			}
		}
		return $query;
	}
	public function getThemeHttpQuery( $theme = NULL ) {
		if ( is_null( $theme ) ) {
			$theme = $this->currentTheme;
		}
		return 'theme=' . $theme;
	}
	public function getHttpQuery( $menus = NULL, $theme = NULL ) {
		return '?' . $this->getThemeHttpQuery( $theme ) . $this->getMenuHttpQuery( $menus );
	}


	/*************************************************************************
	  CONSTRUCTOR METHODS				   
	 *************************************************************************/
	public function __construct( $themePath = NULL ) {
		if ( ! is_null( $themePath ) ) {
			if ( $themePath[ 0 ] !== '/' ) {
				$themePath = __DIR__ . '/' . $this->themePath;
			}
			$this->themePath = $themePath;
		}
		$this->themes = $this->initThemes( );
		$this->settings = $this->initSettings( );
		$this->patternPath = $this->initPatternPath( );
		$this->menus = UFIle::folderTree( $this->patternPath, '*.html', static::MAX_DEPTH, UFile::FILE_FLAG );
		$this->currentMenus = $this->initCurrentMenus( );
		$this->patterns = $this->initPatterns( );
		$this->currentPattern = $this->initCurrentPattern( );
	}
	protected function initThemes( ) {
		$themes = \UFile::fileList( $this->themePath, '*.ini' );
		if ( empty( $themes ) ) {
			if ( ! $themes = realpath( __DIR__ . static::SETTINGS_PATH . '/default.ini' ) ) {
				echo 'Settings file not found :\'(';
				die;
			}
{'fraction_non_alphanumeric': 0.2867762451474354, 'fraction_numerical': 0.003221276947220616, 'mean_word_length': 3.588101553618795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2707721', 'n_tokens_mistral': 4468, 'n_tokens_neox': 3908, 'n_words': 855}