revert revert

This commit is contained in:
Darko Luketic 2021-11-29 12:21:24 +01:00
parent 16bf4ac2de
commit 700e55c9c2

View File

@ -2,17 +2,14 @@
require_once ('global.php'); require_once ('global.php');
require_once (CRAYON_RESOURCE_PHP); require_once (CRAYON_RESOURCE_PHP);
class CrayonLangsResourceType class CrayonLangsResourceType {
{
const EXTENSION = 0; const EXTENSION = 0;
const ALIAS = 1; const ALIAS = 1;
const DELIMITER = 2; const DELIMITER = 2;
} }
/* Manages languages once they are loaded. The parser directly loads them, saves them here. */ /* Manages languages once they are loaded. The parser directly loads them, saves them here. */
class CrayonLangs extends CrayonUserResourceCollection {
class CrayonLangs extends CrayonUserResourceCollection
{
// Properties and Constants =============================================== // Properties and Constants ===============================================
// CSS classes for known elements // CSS classes for known elements
private static $known_elements = array('COMMENT' => 'c', 'PREPROCESSOR' => 'p', 'STRING' => 's', 'KEYWORD' => 'k', private static $known_elements = array('COMMENT' => 'c', 'PREPROCESSOR' => 'p', 'STRING' => 's', 'KEYWORD' => 'k',
@ -28,8 +25,7 @@ class CrayonLangs extends CrayonUserResourceCollection
private static $resource_cache = array(); private static $resource_cache = array();
// Methods ================================================================ // Methods ================================================================
public function __construct() public function __construct() {
{
$this->set_default(self::DEFAULT_LANG, self::DEFAULT_LANG_NAME); $this->set_default(self::DEFAULT_LANG, self::DEFAULT_LANG_NAME);
$this->directory(CRAYON_LANG_PATH); $this->directory(CRAYON_LANG_PATH);
$this->relative_directory(CRAYON_LANG_DIR); $this->relative_directory(CRAYON_LANG_DIR);
@ -50,35 +46,30 @@ class CrayonLangs extends CrayonUserResourceCollection
CrayonLog::debug($this->user_directory()); CrayonLog::debug($this->user_directory());
} }
public function filename($id, $user = NULL) public function filename($id, $user = NULL) {
{
return $id."/$id.".$this->extension(); return $id."/$id.".$this->extension();
} }
// XXX Override // XXX Override
public function load_process() public function load_process() {
{
parent::load_process(); parent::load_process();
$this->load_exts(); $this->load_exts();
$this->load_aliases(); $this->load_aliases();
$this->load_delimiters(); // TODO check for setting? $this->load_delimiters(); // TODO check for setting?
} }
public function load_resources($dir = NULL) public function load_resources($dir = NULL) {
{
parent::load_resources($dir); parent::load_resources($dir);
} }
// XXX Override // XXX Override
public function create_user_resource_instance($id, $name = NULL) public function create_user_resource_instance($id, $name = NULL) {
{
return new CrayonLang($id, $name); return new CrayonLang($id, $name);
} }
// XXX Override // XXX Override
public function add_default() public function add_default() {
{
$result = parent::add_default(); $result = parent::add_default();
if ($this->is_state_loading() && !$result) { if ($this->is_state_loading() && !$result) {
// Default not added, must already be loaded, ready to parse // Default not added, must already be loaded, ready to parse
@ -88,8 +79,7 @@ class CrayonLangs extends CrayonUserResourceCollection
/* Attempts to detect the language based on extension, otherwise falls back to fallback language given. /* Attempts to detect the language based on extension, otherwise falls back to fallback language given.
* Returns a CrayonLang object. */ * Returns a CrayonLang object. */
public function detect($path, $fallback_id = NULL) public function detect($path, $fallback_id = NULL) {
{
$this->load(); $this->load();
extract(pathinfo($path)); extract(pathinfo($path));
@ -117,8 +107,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Load all extensions and add them into each language. */ /* Load all extensions and add them into each language. */
private function load_exts() private function load_exts() {
{
// Load only once // Load only once
if (!$this->is_state_loading()) { if (!$this->is_state_loading()) {
return; return;
@ -132,8 +121,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Load all extensions and add them into each language. */ /* Load all extensions and add them into each language. */
private function load_aliases() private function load_aliases() {
{
// Load only once // Load only once
if (!$this->is_state_loading()) { if (!$this->is_state_loading()) {
return; return;
@ -147,8 +135,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Load all extensions and add them into each language. */ /* Load all extensions and add them into each language. */
private function load_delimiters() private function load_delimiters() {
{
// Load only once // Load only once
if (!$this->is_state_loading()) { if (!$this->is_state_loading()) {
return; return;
@ -162,8 +149,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
// Used to load aliases and extensions to languages // Used to load aliases and extensions to languages
private function load_attr_file($path) private function load_attr_file($path) {
{
if ( ($lines = CrayonUtil::lines($path, 'lwc')) !== FALSE) { if ( ($lines = CrayonUtil::lines($path, 'lwc')) !== FALSE) {
$attributes = array(); // key = language id, value = array of attr $attributes = array(); // key = language id, value = array of attr
foreach ($lines as $line) { foreach ($lines as $line) {
@ -182,8 +168,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Returns the CrayonLang for the given extension */ /* Returns the CrayonLang for the given extension */
public function ext($ext) public function ext($ext) {
{
$this->load(); $this->load();
foreach ($this->get() as $lang) { foreach ($this->get() as $lang) {
if ($lang->has_ext($ext)) { if ($lang->has_ext($ext)) {
@ -194,8 +179,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Returns the CrayonLang for the given alias */ /* Returns the CrayonLang for the given alias */
public function alias($alias) public function alias($alias) {
{
$this->load(); $this->load();
foreach ($this->get() as $lang) { foreach ($this->get() as $lang) {
if ($lang->has_alias($alias)) { if ($lang->has_alias($alias)) {
@ -206,8 +190,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Fetches a resource. Type is an int from CrayonLangsResourceType. */ /* Fetches a resource. Type is an int from CrayonLangsResourceType. */
public function fetch($type, $reload = FALSE, $keep_empty_fetches = FALSE) public function fetch($type, $reload = FALSE, $keep_empty_fetches = FALSE) {
{
$this->load(); $this->load();
if (!array_key_exists($type, self::$resource_cache) || $reload) { if (!array_key_exists($type, self::$resource_cache) || $reload) {
@ -237,23 +220,19 @@ class CrayonLangs extends CrayonUserResourceCollection
return self::$resource_cache[$type]; return self::$resource_cache[$type];
} }
public function extensions($reload = FALSE) public function extensions($reload = FALSE) {
{
return $this->fetch(CrayonLangsResourceType::EXTENSION, $reload); return $this->fetch(CrayonLangsResourceType::EXTENSION, $reload);
} }
public function aliases($reload = FALSE) public function aliases($reload = FALSE) {
{
return $this->fetch(CrayonLangsResourceType::ALIAS, $reload); return $this->fetch(CrayonLangsResourceType::ALIAS, $reload);
} }
public function delimiters($reload = FALSE) public function delimiters($reload = FALSE) {
{
return $this->fetch(CrayonLangsResourceType::DELIMITER, $reload); return $this->fetch(CrayonLangsResourceType::DELIMITER, $reload);
} }
public function extensions_inverted($reload = FALSE) public function extensions_inverted($reload = FALSE) {
{
$extensions = $this->extensions($reload); $extensions = $this->extensions($reload);
$inverted = array(); $inverted = array();
foreach ($extensions as $lang=>$exts) { foreach ($extensions as $lang=>$exts) {
@ -264,8 +243,7 @@ class CrayonLangs extends CrayonUserResourceCollection
return $inverted; return $inverted;
} }
public function ids_and_aliases($reload = FALSE) public function ids_and_aliases($reload = FALSE) {
{
$fetch = $this->fetch(CrayonLangsResourceType::ALIAS, $reload, TRUE); $fetch = $this->fetch(CrayonLangsResourceType::ALIAS, $reload, TRUE);
foreach ($fetch as $id=>$alias_array) { foreach ($fetch as $id=>$alias_array) {
$ids_and_aliases[] = $id; $ids_and_aliases[] = $id;
@ -277,8 +255,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Return the array of valid elements or a particular element value */ /* Return the array of valid elements or a particular element value */
public static function known_elements($name = NULL) public static function known_elements($name = NULL) {
{
if ($name === NULL) { if ($name === NULL) {
return self::$known_elements; return self::$known_elements;
} else if (is_string($name) && array_key_exists($name, self::$known_elements)) { } else if (is_string($name) && array_key_exists($name, self::$known_elements)) {
@ -289,14 +266,12 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Verify an element is valid */ /* Verify an element is valid */
public static function is_known_element($name) public static function is_known_element($name) {
{
return self::known_elements($name) !== FALSE; return self::known_elements($name) !== FALSE;
} }
/* Compare two languages by name */ /* Compare two languages by name */
public static function langcmp($a, $b) public static function langcmp($a, $b) {
{
$a = strtolower($a->name()); $a = strtolower($a->name());
$b = strtolower($b->name()); $b = strtolower($b->name());
if ($a == $b) { if ($a == $b) {
@ -306,8 +281,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
} }
public static function sort_by_name($langs) public static function sort_by_name($langs) {
{
// Sort by name // Sort by name
usort($langs, 'CrayonLangs::langcmp'); usort($langs, 'CrayonLangs::langcmp');
$sorted_lags = array(); $sorted_lags = array();
@ -317,8 +291,7 @@ class CrayonLangs extends CrayonUserResourceCollection
return $sorted_lags; return $sorted_lags;
} }
public function is_parsed($id = NULL) public function is_parsed($id = NULL) {
{
if ($id === NULL) { if ($id === NULL) {
// Determine if all langs are successfully parsed // Determine if all langs are successfully parsed
foreach ($this->get() as $lang) { foreach ($this->get() as $lang) {
@ -333,8 +306,7 @@ class CrayonLangs extends CrayonUserResourceCollection
return FALSE; return FALSE;
} }
public function is_default($id) public function is_default($id) {
{
if (($lang = $this->get($id)) != FALSE) { if (($lang = $this->get($id)) != FALSE) {
return $lang->is_default(); return $lang->is_default();
} }
@ -343,9 +315,7 @@ class CrayonLangs extends CrayonUserResourceCollection
} }
/* Individual language. */ /* Individual language. */
class CrayonLang extends CrayonVersionResource {
class CrayonLang extends CrayonVersionResource
{
private $ext = array(); private $ext = array();
private $aliases = array(); private $aliases = array();
private $delimiters = ''; private $delimiters = '';
@ -359,21 +329,18 @@ class CrayonLang extends CrayonVersionResource
const UNPARSED = 0; const UNPARSED = 0;
const PARSED_SUCCESS = 1; const PARSED_SUCCESS = 1;
function __construct($id, $name = NULL) function __construct($id, $name = NULL) {
{
parent::__construct($id, $name); parent::__construct($id, $name);
$this->modes = CrayonParser::modes(); $this->modes = CrayonParser::modes();
} }
// Override // Override
function clean_id($id) function clean_id($id) {
{
$id = CrayonUtil::space_to_hyphen( strtolower(trim($id)) ); $id = CrayonUtil::space_to_hyphen( strtolower(trim($id)) );
return preg_replace('/[^\w\-+#]/msi', '', $id); return preg_replace('/[^\w\-+#]/msi', '', $id);
} }
function ext($ext = NULL) function ext($ext = NULL) {
{
if ($ext === NULL) { if ($ext === NULL) {
return $this->ext; return $this->ext;
} else if (is_array($ext) && !empty($ext)) { } else if (is_array($ext) && !empty($ext)) {
@ -387,13 +354,11 @@ class CrayonLang extends CrayonVersionResource
} }
} }
function has_ext($ext) function has_ext($ext) {
{
return is_string($ext) && in_array($ext, $this->ext); return is_string($ext) && in_array($ext, $this->ext);
} }
function alias($alias = NULL) function alias($alias = NULL) {
{
if ($alias === NULL) { if ($alias === NULL) {
return $this->aliases; return $this->aliases;
} else if (is_array($alias) && !empty($alias)) { } else if (is_array($alias) && !empty($alias)) {
@ -406,13 +371,11 @@ class CrayonLang extends CrayonVersionResource
} }
} }
function has_alias($alias) function has_alias($alias) {
{
return is_string($alias) && in_array($alias, $this->aliases); return is_string($alias) && in_array($alias, $this->aliases);
} }
function delimiter($delim = NULL) function delimiter($delim = NULL) {
{
if ($delim === NULL) { if ($delim === NULL) {
return $this->delimiters; return $this->delimiters;
// Convert to regex for capturing delimiters // Convert to regex for capturing delimiters
@ -427,8 +390,7 @@ class CrayonLang extends CrayonVersionResource
} }
} }
function regex($element = NULL) function regex($element = NULL) {
{
if ($element == NULL) { if ($element == NULL) {
$regexes = array(); $regexes = array();
foreach ($this->elements as $element) { foreach ($this->elements as $element) {
@ -444,8 +406,7 @@ class CrayonLang extends CrayonVersionResource
} }
// Retrieve by element name or set by CrayonElement // Retrieve by element name or set by CrayonElement
function element($name, $element = NULL) function element($name, $element = NULL) {
{
if (is_string($name)) { if (is_string($name)) {
$name = trim(strtoupper($name)); $name = trim(strtoupper($name));
if (array_key_exists($name, $this->elements) && $element === NULL) { if (array_key_exists($name, $this->elements) && $element === NULL) {
@ -456,13 +417,11 @@ class CrayonLang extends CrayonVersionResource
} }
} }
function elements() function elements() {
{
return $this->elements; return $this->elements;
} }
function mode($name = NULL, $value = NULL) function mode($name = NULL, $value = NULL) {
{
if (is_string($name) && CrayonParser::is_mode($name)) { if (is_string($name) && CrayonParser::is_mode($name)) {
$name = trim(strtoupper($name)); $name = trim(strtoupper($name));
if ($value == NULL && array_key_exists($name, $this->modes)) { if ($value == NULL && array_key_exists($name, $this->modes)) {
@ -479,8 +438,7 @@ class CrayonLang extends CrayonVersionResource
} }
} }
function state($state = NULL) function state($state = NULL) {
{
if ($state === NULL) { if ($state === NULL) {
return $this->state; return $this->state;
} else if (is_int($state)) { } else if (is_int($state)) {
@ -494,8 +452,7 @@ class CrayonLang extends CrayonVersionResource
} }
} }
function state_info() function state_info() {
{
switch ($this->state) { switch ($this->state) {
case self::PARSED_ERRORS : case self::PARSED_ERRORS :
return 'Parsed With Errors'; return 'Parsed With Errors';
@ -508,19 +465,16 @@ class CrayonLang extends CrayonVersionResource
} }
} }
function is_parsed() function is_parsed() {
{
return ($this->state != self::UNPARSED); return ($this->state != self::UNPARSED);
} }
function is_default() function is_default() {
{
return $this->id() == CrayonLangs::DEFAULT_LANG; return $this->id() == CrayonLangs::DEFAULT_LANG;
} }
} }
class CrayonElement class CrayonElement {
{
// The pure regex syntax without any modifiers or delimiters // The pure regex syntax without any modifiers or delimiters
private $name = ''; private $name = '';
private $css = ''; private $css = '';
@ -528,20 +482,17 @@ class CrayonElement
private $fallback = ''; private $fallback = '';
private $path = ''; private $path = '';
function __construct($name, $path, $regex = '') function __construct($name, $path, $regex = '') {
{
$this->name($name); $this->name($name);
$this->path($path); $this->path($path);
$this->regex($regex); $this->regex($regex);
} }
function __toString() function __toString() {
{
return $this->regex(); return $this->regex();
} }
function name($name = NULL) function name($name = NULL) {
{
if ($name == NULL) { if ($name == NULL) {
return $this->name; return $this->name;
} else if (is_string($name)) { } else if (is_string($name)) {
@ -554,8 +505,7 @@ class CrayonElement
} }
} }
function regex($regex = NULL) function regex($regex = NULL) {
{
if ($regex == NULL) { if ($regex == NULL) {
return $this->regex; return $this->regex;
} else if (is_string($regex)) { } else if (is_string($regex)) {
@ -568,8 +518,7 @@ class CrayonElement
} }
// Expects: 'class1 class2 class3' // Expects: 'class1 class2 class3'
function css($css = NULL) function css($css = NULL) {
{
if ($css == NULL) { if ($css == NULL) {
return $this->css; return $this->css;
} else if (is_string($css)) { } else if (is_string($css)) {
@ -577,8 +526,7 @@ class CrayonElement
} }
} }
function fallback($fallback = NULL) function fallback($fallback = NULL) {
{
if ($fallback == NULL) { if ($fallback == NULL) {
return $this->fallback; return $this->fallback;
} else if (is_string($fallback) && CrayonLangs::is_known_element($fallback)) { } else if (is_string($fallback) && CrayonLangs::is_known_element($fallback)) {
@ -586,8 +534,7 @@ class CrayonElement
} }
} }
function path($path = NULL) function path($path = NULL) {
{
if ($path == NULL) { if ($path == NULL) {
return $this->path; return $this->path;
} else if (is_string($path) && @file_exists($path)) { } else if (is_string($path) && @file_exists($path)) {