<commit_before>/**
 *	ACCENT.JS: multi language code highlighting library
 *	
 *	Description:
 *		implemented in javascript, this project aims to be a simple multi language code highlighting library,
 *		taking stylistic influence from various popular text editors
 *
 *		I do NOT intend to implement highlighting for all languages or even most
 **/


// TODO generalize language object to parse regexp patterns with generic function
;( function() {

	'use strict';

	// this allows for a more functionally pure iterations on Dom nodes
	NodeList.prototype.forEach =
		HTMLCollection.prototype.forEach = 
			Array.prototype.forEach;

	// helper methods for parsers of language features
	var _util = {
		
		// remove previous parser nodes
		// highlight with syntax class
		highlightIgnoreRest: function(syntaxClass) {
			return function(match) {	
				var escaped = match.replace(/\<span\s+class=acc-js.*?>(.*?)<\/span>/g, '$1');
				return '<span class='.concat(syntaxClass,'>',escaped,'</span>');
			};
		},

		// escape regexp match with a replacement string
		escapeWith: function(replacement) {
			return function() {
				return replacement;
			};
		}
	},

	/**
	 *	supported languages namespace
	 *
	 *	return expressions and corresponding identifier for each feature of supported language
	 * 	e.g. 'lolcode' -> { 'hai' : [_expression, _identifier], ... }
	 **/
	_language = {};

	_language['javascript'] = {

		// escape open html tag
		'escapeHTMLOpen': [
			/</g,
			_util.escapeWith('&lt;')
		],

		// common language operators such as conditionals and loops
		'operation': [
			/\b(if|else|continue|switch|case|default|break|return|for|try|catch|throw)(?=[^\w])/g, 
			'<span class=acc-js-operation>$1</span>'
		],

		// variable assignment keywords
		'declaration': [
			/(\bfunction|var|const|in|new|this|prototype)(?=[^\w])/g, 
			'<span class=acc-js-declaration>$1</span>'
		],

		// frequently used methods
		'specials': [
			/(\.\bgetElementById|getElementsBy(ClassName|TagName|Name)|(type|instance)of|hasOwnProperty)/g, 
			'<span class=acc-js-special>$1</span>'
		],

		// common dom methods
		'dom methods': [
			/(\.\binnerHTML|createElement|parentNode|innerHTML|(append|replace)Child)(?=[^\w])/g,
			'<span class=acc-js-dom>$1</span>'
		],

		// globals [window]
		'global': [
			/\b(window|console|document)/g,
			'<span class=acc-js-global>$1</span>'
		],

		// basic types and special type checking keywords
		'types': [
			/([^\w])\b(Array|String|Function|Object|Number|Date|Boolean|Error|RegExp|Math|null|undefined|true|false)(?=[^\w])/g, 
			'$1<span class=acc-js-type>$2</span>'
		],

		// numeric values (including hexadecimal)
		'number': [
			/(-{0,1}\d+\.{0,1}\d+|-{0,1}0x\w+)(?=[^\w])/g, 
			'<span class=acc-js-numeric>$1</span>'
		],

		// double/single qouted string
		'string': [
			/(".*?"|'.*?')/g, 
			_util.highlightIgnoreRest('acc-js-string')
		],

		// comments
		'inlineCom': [
			/(\/{2}.*?\n+|\/\*(.|[\r\n])*\*\/)/g,
			_util.highlightIgnoreRest('acc-js-comment')
		],

		// regexp literals
		'regExp': [
			/[^<](\/.+?\/(\s|,|\]|;|\/|g|i|m|y))/g,
			_util.highlightIgnoreRest('acc-js-regexp')
		],
	};

	// formats the code content with a default theme
	var _format = ( function() {
		
		// formats dom node by wrapping it with a HTML pre node
		var _wrapWith = function(domNode, wrapper) {
			var parent = domNode.parentNode,
				pre = document.createElement(wrapper);

				parent.replaceChild(pre, domNode);
				pre.appendChild(domNode);
		};

		return function(domNode, theme) {

			// wrap with pre formatter tag
			_wrapWith(domNode, 'pre');

			// add theme to pre wrapper
			domNode.parentNode.className = 'acc-'.concat(theme);

		};

	} )(),

	// parses element text body according to the selected language property rules (see language)
	_parse = ( function() {

		var _byFeature = function(textBody, langFeature) {
			return textBody.replace(langFeature[0], langFeature[1]);
		};

		
		// return highlighted text of given dom node for given language
		return function(domNode, lang) {
			
			var textBody = domNode.innerHTML;
			
			// apply language highlighting rules to text
			for(var feature in lang) {
				if(lang.hasOwnProperty(feature)) {	
					textBody = _byFeature(textBody, lang[feature]);
				}
			}
			
			// replace plain node text body with highlighted text body
			domNode.innerHTML = textBody;
		};

	} )(),


	// outward facing reference
	// clean up closure functionality, implment a less general condition evaluation function
	accent = ( function(format, parse, language) {

		var _defaultTheme = ['dark', 'light'],

		_errors = {
			_illegalArgumentsError:'both identifier and language parameters must be strings',
			_unknownThemeError: 'theme must be a known preset'.concat(' [', _defaultTheme, ']')
		},
		
		// reliable way to check type of accent function arguments
		_is = function(obj, type) {
			var clas = Object.prototype.toString.call(obj).slice(8, -1);
			return clas === type;
		};

		// add option param
		return function(identifier, lang, props) {

			var config = {},

			// basic input validation of function params

			isidentifierString = _is(identifier, 'String'),
			isLangString = _is(lang, 'String');
			
			if(isidentifierString && isLangString) {
				config.elems = document.getElementsByClassName(identifier);
				config.lang = language[lang];
			} else {
				throw new Error(_errors._illegalArgumentsError);<commit_msg>fixed accent variable name<commit_after>/**
 *	ACCENT.JS: multi language code highlighting library
 *	
 *	Description:
 *		implemented in javascript, this project aims to be a simple multi language code highlighting library,
 *		taking stylistic influence from various popular text editors
 *
 *		I do NOT intend to implement highlighting for all languages or even most
 **/


// TODO generalize language object to parse regexp patterns with generic function
;( function() {

	'use strict';

	// this allows for a more functionally pure iterations on Dom nodes
	NodeList.prototype.forEach =
		HTMLCollection.prototype.forEach = 
			Array.prototype.forEach;

	// helper methods for parsers of language features
	var _util = {
		
		// remove previous parser nodes
		// highlight with syntax class
		highlightIgnoreRest: function(syntaxClass) {
			return function(match) {	
				var escaped = match.replace(/\<span\s+class=acc-js.*?>(.*?)<\/span>/g, '$1');
				return '<span class='.concat(syntaxClass,'>',escaped,'</span>');
			};
		},

		// escape regexp match with a replacement string
		escapeWith: function(replacement) {
			return function() {
				return replacement;
			};
		}
	},

	/**
	 *	supported languages namespace
	 *
	 *	return expressions and corresponding identifier for each feature of supported language
	 * 	e.g. 'lolcode' -> { 'hai' : [_expression, _identifier], ... }
	 **/
	_language = {};

	_language['javascript'] = {

		// escape open html tag
		'escapeHTMLOpen': [
			/</g,
			_util.escapeWith('&lt;')
		],

		// common language operators such as conditionals and loops
		'operation': [
			/\b(if|else|continue|switch|case|default|break|return|for|try|catch|throw)(?=[^\w])/g, 
			'<span class=acc-js-operation>$1</span>'
		],

		// variable assignment keywords
		'declaration': [
			/(\bfunction|var|const|in|new|this|prototype)(?=[^\w])/g, 
			'<span class=acc-js-declaration>$1</span>'
		],

		// frequently used methods
		'specials': [
			/(\.\bgetElementById|getElementsBy(ClassName|TagName|Name)|(type|instance)of|hasOwnProperty)/g, 
			'<span class=acc-js-special>$1</span>'
		],

		// common dom methods
		'dom methods': [
			/(\.\binnerHTML|createElement|parentNode|innerHTML|(append|replace)Child)(?=[^\w])/g,
			'<span class=acc-js-dom>$1</span>'
		],

		// globals [window]
		'global': [
			/\b(window|console|document)/g,
			'<span class=acc-js-global>$1</span>'
		],

		// basic types and special type checking keywords
		'types': [
			/([^\w])\b(Array|String|Function|Object|Number|Date|Boolean|Error|RegExp|Math|null|undefined|true|false)(?=[^\w])/g, 
			'$1<span class=acc-js-type>$2</span>'
		],

		// numeric values (including hexadecimal)
		'number': [
			/(-{0,1}\d+\.{0,1}\d+|-{0,1}0x\w+)(?=[^\w])/g, 
			'<span class=acc-js-numeric>$1</span>'
		],

		// double/single qouted string
		'string': [
			/(".*?"|'.*?')/g, 
			_util.highlightIgnoreRest('acc-js-string')
		],

		// comments
		'inlineCom': [
			/(\/{2}.*?\n+|\/\*(.|[\r\n])*\*\/)/g,
			_util.highlightIgnoreRest('acc-js-comment')
		],

		// regexp literals
		'regExp': [
			/[^<](\/.+?\/(\s|,|\]|;|\/|g|i|m|y))/g,
			_util.highlightIgnoreRest('acc-js-regexp')
		],
	};

	// formats the code content with a default theme
	var _format = ( function() {
		
		// formats dom node by wrapping it with a HTML pre node
		var _wrapWith = function(domNode, wrapper) {
			var parent = domNode.parentNode,
				pre = document.createElement(wrapper);

				parent.replaceChild(pre, domNode);
				pre.appendChild(domNode);
		};

		return function(domNode, theme) {

			// wrap with pre formatter tag
			_wrapWith(domNode, 'pre');

			// add theme to pre wrapper
			domNode.parentNode.className = 'acc-'.concat(theme);

		};

	} )(),

	// parses element text body according to the selected language property rules (see language)
	_parse = ( function() {

		var _byFeature = function(textBody, langFeature) {
			return textBody.replace(langFeature[0], langFeature[1]);
		};

		
		// return highlighted text of given dom node for given language
		return function(domNode, lang) {
			
			var textBody = domNode.innerHTML;
			
			// apply language highlighting rules to text
			for(var feature in lang) {
				if(lang.hasOwnProperty(feature)) {	
					textBody = _byFeature(textBody, lang[feature]);
				}
			}
			
			// replace plain node text body with highlighted text body
			domNode.innerHTML = textBody;
		};

	} )(),


	// outward facing reference
	// clean up closure functionality, implment a less general condition evaluation function
	accent = ( function(format, parse, language) {

		var _defaultTheme = ['dark', 'light'],

		_errors = {
			_illegalArgumentsError:'both identifier and language parameters must be strings',
			_unknownThemeError: 'theme must be a known preset'.concat(' [', _defaultTheme, ']')
		},
		
		// reliable way to check type of accent function arguments
		_is = function(obj, type) {
			var clas = Object.prototype.toString.call(obj).slice(8, -1);
			return clas === type;
		};

		// add option param
		return function(identifier, lang, theme) {

			var config = {},

			// basic input validation of function params

			isidentifierString = _is(identifier, 'String'),
			isLangString = _is(lang, 'String');
			
			if(isidentifierString && isLangString) {
				config.elems = document.getElementsByClassName(identifier);
				config.lang = language[lang];
			} else {
				throw new Error(_errors._illegalArgumentsError);
{'fraction_non_alphanumeric': 0.1576572428400405, 'fraction_numerical': 0.003867759462197256, 'mean_word_length': 3.4913151364764268, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 41, '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': '848245', 'n_tokens_mistral': 3978, 'n_tokens_neox': 3399, 'n_words': 838}