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 (CRAYON_RESOURCE_PHP);
class CrayonLangsResourceType
{
class CrayonLangsResourceType {
const EXTENSION = 0;
const ALIAS = 1;
const DELIMITER = 2;
}
/* 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 ===============================================
// CSS classes for known elements
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();
// Methods ================================================================
public function __construct()
{
public function __construct() {
$this->set_default(self::DEFAULT_LANG, self::DEFAULT_LANG_NAME);
$this->directory(CRAYON_LANG_PATH);
$this->relative_directory(CRAYON_LANG_DIR);
@ -50,35 +46,30 @@ class CrayonLangs extends CrayonUserResourceCollection
CrayonLog::debug($this->user_directory());
}
public function filename($id, $user = NULL)
{
public function filename($id, $user = NULL) {
return $id."/$id.".$this->extension();
}
// XXX Override
public function load_process()
{
public function load_process() {
parent::load_process();
$this->load_exts();
$this->load_aliases();
$this->load_delimiters(); // TODO check for setting?
}
public function load_resources($dir = NULL)
{
public function load_resources($dir = NULL) {
parent::load_resources($dir);
}
// 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);
}
// XXX Override
public function add_default()
{
public function add_default() {
$result = parent::add_default();
if ($this->is_state_loading() && !$result) {
// 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.
* Returns a CrayonLang object. */
public function detect($path, $fallback_id = NULL)
{
public function detect($path, $fallback_id = NULL) {
$this->load();
extract(pathinfo($path));
@ -117,8 +107,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* Load all extensions and add them into each language. */
private function load_exts()
{
private function load_exts() {
// Load only once
if (!$this->is_state_loading()) {
return;
@ -132,8 +121,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* Load all extensions and add them into each language. */
private function load_aliases()
{
private function load_aliases() {
// Load only once
if (!$this->is_state_loading()) {
return;
@ -147,8 +135,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* Load all extensions and add them into each language. */
private function load_delimiters()
{
private function load_delimiters() {
// Load only once
if (!$this->is_state_loading()) {
return;
@ -162,8 +149,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
// 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) {
$attributes = array(); // key = language id, value = array of attr
foreach ($lines as $line) {
@ -182,8 +168,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* Returns the CrayonLang for the given extension */
public function ext($ext)
{
public function ext($ext) {
$this->load();
foreach ($this->get() as $lang) {
if ($lang->has_ext($ext)) {
@ -194,8 +179,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* Returns the CrayonLang for the given alias */
public function alias($alias)
{
public function alias($alias) {
$this->load();
foreach ($this->get() as $lang) {
if ($lang->has_alias($alias)) {
@ -206,8 +190,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* 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();
if (!array_key_exists($type, self::$resource_cache) || $reload) {
@ -237,23 +220,19 @@ class CrayonLangs extends CrayonUserResourceCollection
return self::$resource_cache[$type];
}
public function extensions($reload = FALSE)
{
public function extensions($reload = FALSE) {
return $this->fetch(CrayonLangsResourceType::EXTENSION, $reload);
}
public function aliases($reload = FALSE)
{
public function aliases($reload = FALSE) {
return $this->fetch(CrayonLangsResourceType::ALIAS, $reload);
}
public function delimiters($reload = FALSE)
{
public function delimiters($reload = FALSE) {
return $this->fetch(CrayonLangsResourceType::DELIMITER, $reload);
}
public function extensions_inverted($reload = FALSE)
{
public function extensions_inverted($reload = FALSE) {
$extensions = $this->extensions($reload);
$inverted = array();
foreach ($extensions as $lang=>$exts) {
@ -264,8 +243,7 @@ class CrayonLangs extends CrayonUserResourceCollection
return $inverted;
}
public function ids_and_aliases($reload = FALSE)
{
public function ids_and_aliases($reload = FALSE) {
$fetch = $this->fetch(CrayonLangsResourceType::ALIAS, $reload, TRUE);
foreach ($fetch as $id=>$alias_array) {
$ids_and_aliases[] = $id;
@ -277,8 +255,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* 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) {
return 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 */
public static function is_known_element($name)
{
public static function is_known_element($name) {
return self::known_elements($name) !== FALSE;
}
/* Compare two languages by name */
public static function langcmp($a, $b)
{
public static function langcmp($a, $b) {
$a = strtolower($a->name());
$b = strtolower($b->name());
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
usort($langs, 'CrayonLangs::langcmp');
$sorted_lags = array();
@ -317,8 +291,7 @@ class CrayonLangs extends CrayonUserResourceCollection
return $sorted_lags;
}
public function is_parsed($id = NULL)
{
public function is_parsed($id = NULL) {
if ($id === NULL) {
// Determine if all langs are successfully parsed
foreach ($this->get() as $lang) {
@ -333,8 +306,7 @@ class CrayonLangs extends CrayonUserResourceCollection
return FALSE;
}
public function is_default($id)
{
public function is_default($id) {
if (($lang = $this->get($id)) != FALSE) {
return $lang->is_default();
}
@ -343,9 +315,7 @@ class CrayonLangs extends CrayonUserResourceCollection
}
/* Individual language. */
class CrayonLang extends CrayonVersionResource
{
class CrayonLang extends CrayonVersionResource {
private $ext = array();
private $aliases = array();
private $delimiters = '';
@ -359,21 +329,18 @@ class CrayonLang extends CrayonVersionResource
const UNPARSED = 0;
const PARSED_SUCCESS = 1;
function __construct($id, $name = NULL)
{
function __construct($id, $name = NULL) {
parent::__construct($id, $name);
$this->modes = CrayonParser::modes();
}
// Override
function clean_id($id)
{
function clean_id($id) {
$id = CrayonUtil::space_to_hyphen( strtolower(trim($id)) );
return preg_replace('/[^\w\-+#]/msi', '', $id);
}
function ext($ext = NULL)
{
function ext($ext = NULL) {
if ($ext === NULL) {
return $this->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);
}
function alias($alias = NULL)
{
function alias($alias = NULL) {
if ($alias === NULL) {
return $this->aliases;
} 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);
}
function delimiter($delim = NULL)
{
function delimiter($delim = NULL) {
if ($delim === NULL) {
return $this->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) {
$regexes = array();
foreach ($this->elements as $element) {
@ -444,8 +406,7 @@ class CrayonLang extends CrayonVersionResource
}
// Retrieve by element name or set by CrayonElement
function element($name, $element = NULL)
{
function element($name, $element = NULL) {
if (is_string($name)) {
$name = trim(strtoupper($name));
if (array_key_exists($name, $this->elements) && $element === NULL) {
@ -456,13 +417,11 @@ class CrayonLang extends CrayonVersionResource
}
}
function elements()
{
function elements() {
return $this->elements;
}
function mode($name = NULL, $value = NULL)
{
function mode($name = NULL, $value = NULL) {
if (is_string($name) && CrayonParser::is_mode($name)) {
$name = trim(strtoupper($name));
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) {
return $this->state;
} else if (is_int($state)) {
@ -494,8 +452,7 @@ class CrayonLang extends CrayonVersionResource
}
}
function state_info()
{
function state_info() {
switch ($this->state) {
case self::PARSED_ERRORS :
return 'Parsed With Errors';
@ -508,19 +465,16 @@ class CrayonLang extends CrayonVersionResource
}
}
function is_parsed()
{
function is_parsed() {
return ($this->state != self::UNPARSED);
}
function is_default()
{
function is_default() {
return $this->id() == CrayonLangs::DEFAULT_LANG;
}
}
class CrayonElement
{
class CrayonElement {
// The pure regex syntax without any modifiers or delimiters
private $name = '';
private $css = '';
@ -528,20 +482,17 @@ class CrayonElement
private $fallback = '';
private $path = '';
function __construct($name, $path, $regex = '')
{
function __construct($name, $path, $regex = '') {
$this->name($name);
$this->path($path);
$this->regex($regex);
}
function __toString()
{
function __toString() {
return $this->regex();
}
function name($name = NULL)
{
function name($name = NULL) {
if ($name == NULL) {
return $this->name;
} else if (is_string($name)) {
@ -554,8 +505,7 @@ class CrayonElement
}
}
function regex($regex = NULL)
{
function regex($regex = NULL) {
if ($regex == NULL) {
return $this->regex;
} else if (is_string($regex)) {
@ -568,8 +518,7 @@ class CrayonElement
}
// Expects: 'class1 class2 class3'
function css($css = NULL)
{
function css($css = NULL) {
if ($css == NULL) {
return $this->css;
} else if (is_string($css)) {
@ -577,8 +526,7 @@ class CrayonElement
}
}
function fallback($fallback = NULL)
{
function fallback($fallback = NULL) {
if ($fallback == NULL) {
return $this->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) {
return $this->path;
} else if (is_string($path) && @file_exists($path)) {