Skip to content

Commit

Permalink
Merge pull request #4 from williamtroup/0.4.0
Browse files Browse the repository at this point in the history
0.4.0
  • Loading branch information
William Troup authored Oct 30, 2023
2 parents ac2185f + e3056d4 commit f125164
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 189 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
Syntax.js

[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Syntax.js%2C%20a%20free%20JavaScript%20syntax%20highlighter&url=https://github.com/williamtroup/Syntax.js&hashtags=javascript,syntax,highlighter)
[![npm](https://img.shields.io/badge/npmjs-v0.4.0-blue)](https://www.npmjs.com/package/jsyntax.js)
[![license](https://img.shields.io/badge/license-MIT-green)](https://github.com/williamtroup/Syntax.js/blob/main/LICENSE.txt)
[![discussions Welcome](https://img.shields.io/badge/discussions-Welcome-red)](https://github.com/williamtroup/Syntax.js/discussions)
[![coded by William Troup](https://img.shields.io/badge/coded_by-William_Troup-yellow)](https://github.com/williamtroup)
</h1>
<p align="center">An easy to use JavaScript syntax highlighter!</p>
<p align="center">v0.3.0</p>

> <p align="center">An easy to use JavaScript syntax highlighter!</p>
> <p align="center">v0.4.0</p>
<br />
![Syntax.js](docs/images/main.png)
Expand Down
2 changes: 1 addition & 1 deletion dist/languages/syntax.c-sharp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Syntax.js v0.3.0 | (c) Bunoon | MIT License */
/*! Syntax.js v0.4.0 | (c) Bunoon | MIT License */
$syntax.addLanguage( "c-sharp", {
keywords: [
"abstract",
Expand Down
32 changes: 32 additions & 0 deletions dist/languages/syntax.go.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*! Syntax.js v0.4.0 | (c) Bunoon | MIT License */
$syntax.addLanguage( "go", {
keywords: [
"break",
"default",
"func",
"interface",
"select",
"case",
"defer",
"go",
"map",
"struct",
"chan",
"else",
"goto",
"package",
"switch",
"const",
"fallthrough",
"if",
"range",
"type",
"continue",
"for",
"import",
"return",
"var"
],
comment: "//",
multiLineComment: [ "/*", "*/" ]
} );
41 changes: 41 additions & 0 deletions dist/languages/syntax.python.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*! Syntax.js v0.4.0 | (c) Bunoon | MIT License */
$syntax.addLanguage( "python", {
keywords: [
"False",
"await",
"else",
"import",
"pass",
"None",
"break",
"except",
"in",
"raise",
"True",
"class",
"finally",
"is",
"return",
"and",
"continue",
"for",
"lambda",
"try",
"as",
"def",
"from",
"nonlocal",
"while",
"assert",
"del",
"global",
"not",
"with",
"async",
"elif",
"if",
"or",
"yield"
],
comment: "#"
} );
43 changes: 43 additions & 0 deletions dist/languages/syntax.ruby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*! Syntax.js v0.4.0 | (c) Bunoon | MIT License */
$syntax.addLanguage( "ruby", {
keywords: [
"BEGIN",
"END",
"alias",
"and",
"begin",
"break",
"case",
"class",
"def",
"module",
"next",
"nil",
"not",
"or",
"redo",
"rescue",
"retry",
"return",
"elsif",
"end",
"false",
"ensure",
"for",
"if",
"true",
"undef",
"unless",
"do",
"else",
"super",
"then",
"until",
"when",
"while",
"defined?",
"self"
],
comment: "#",
multiLineComment: [ "=begin", "=end" ]
} );
91 changes: 37 additions & 54 deletions dist/syntax.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Syntax.js v0.3.0 | (c) Bunoon | MIT License */
/*! Syntax.js v0.4.0 | (c) Bunoon | MIT License */
(function() {
function render() {
var domElements = _parameter_Document.getElementsByTagName("*");
Expand All @@ -15,7 +15,7 @@
if (isDefined(syntaxLanguage) && _languages.hasOwnProperty(syntaxLanguage)) {
var innerHTML = element.innerHTML;
var syntaxOptions = getObjectFromString(element.getAttribute("data-syntax-options"));
syntaxOptions = buildDefaultOptions(syntaxOptions);
syntaxOptions = buildAttributeOptions(syntaxOptions);
if (element.children.length > 0) {
innerHTML = element.children[0].innerHTML;
}
Expand All @@ -37,7 +37,7 @@
syntax.appendChild(copyButton);
copyButton.onclick = function() {
_parameter_Navigator.clipboard.writeText(innerHTMLCopy);
fireCustomTrigger("onCopy", innerHTMLCopy);
fireCustomTrigger(syntaxOptions, "onCopy", innerHTMLCopy);
};
}
innerHTML = renderElementCommentVariables(innerHTML, syntaxLanguage);
Expand All @@ -48,7 +48,7 @@
innerHTML = renderElementCommentsFromVariables(innerHTML);
innerHTML = renderElementStringQuotesFromVariables(innerHTML);
renderElementCompletedHTML(number, syntax, innerHTML);
fireCustomTrigger("onRender", element);
fireCustomTrigger(syntaxOptions, "onRender", element);
}
}
}
Expand All @@ -70,23 +70,25 @@
}
function renderElementMultiLineCommentVariables(innerHTML, syntaxLanguage) {
var lookup = _languages[syntaxLanguage].multiLineComment;
var startIndex = 0;
var endIndex = 0;
for (; startIndex >= 0 && endIndex >= 0;) {
startIndex = innerHTML.indexOf(lookup[0], endIndex);
if (startIndex > -1) {
endIndex = innerHTML.indexOf(lookup[1], startIndex + lookup[0].length);
if (endIndex > -1) {
var comment = innerHTML.substring(startIndex, endIndex + lookup[1].length);
var commentLines = comment.split(_string.newLine);
var commentLinesLength = commentLines.length;
var commentLineIndex = 0;
for (; commentLineIndex < commentLinesLength; commentLineIndex++) {
var commentVariable = "$C{" + _comments_Cached_Count.toString() + "}";
var commentLine = commentLines[commentLineIndex];
_comments_Cached[commentVariable] = '<span class="comment">' + commentLine + "</span>";
_comments_Cached_Count++;
innerHTML = innerHTML.replace(commentLine, commentVariable);
if (isDefinedArray(lookup) && lookup.length === 2) {
var startIndex = 0;
var endIndex = 0;
for (; startIndex >= 0 && endIndex >= 0;) {
startIndex = innerHTML.indexOf(lookup[0], endIndex);
if (startIndex > -1) {
endIndex = innerHTML.indexOf(lookup[1], startIndex + lookup[0].length);
if (endIndex > -1) {
var comment = innerHTML.substring(startIndex, endIndex + lookup[1].length);
var commentLines = comment.split(_string.newLine);
var commentLinesLength = commentLines.length;
var commentLineIndex = 0;
for (; commentLineIndex < commentLinesLength; commentLineIndex++) {
var commentVariable = "$C{" + _comments_Cached_Count.toString() + "}";
var commentLine = commentLines[commentLineIndex];
_comments_Cached[commentVariable] = '<span class="comment">' + commentLine + "</span>";
_comments_Cached_Count++;
innerHTML = innerHTML.replace(commentLine, commentVariable);
}
}
}
}
Expand Down Expand Up @@ -151,6 +153,12 @@
syntax.appendChild(syntaxCode);
}
}
function buildAttributeOptions(newOptions) {
var options = !isDefinedObject(newOptions) ? {} : newOptions;
options.showCopyButton = getDefaultBoolean(options.showCopyButton, true);
options.copyButtonText = getDefaultString(options.copyButtonText, "Copy");
return options;
}
function isDefined(value) {
return value !== null && value !== undefined && value !== _string.empty;
}
Expand All @@ -166,6 +174,9 @@
function isDefinedFunction(object) {
return isDefined(object) && isFunction(object);
}
function isDefinedArray(object) {
return isDefinedObject(object) && object instanceof Array;
}
function isFunction(object) {
return typeof object === "function";
}
Expand All @@ -182,17 +193,14 @@
}
return result;
}
function isCustomTriggerSet(name) {
return isDefinedFunction(_options[name]);
}
function fireCustomTrigger(name) {
function fireCustomTrigger(options, name) {
var result = null;
var newArguments = [].slice.call(arguments, 1);
var newArguments = [].slice.call(arguments, 2);
if (newArguments.length > 0) {
result = false;
}
if (_options !== null && isCustomTriggerSet(name)) {
result = _options[name].apply(null, newArguments);
if (options !== null && isDefinedFunction(options[name])) {
result = options[name].apply(null, newArguments);
}
return result;
}
Expand All @@ -218,19 +226,9 @@
}
return result;
}
function buildDefaultOptions(newOptions) {
var options = !isDefinedObject(newOptions) ? {} : newOptions;
options.showCopyButton = getDefaultBoolean(options.showCopyButton, true);
return setTranslationStringOptions(options);
}
function setTranslationStringOptions(newOptions) {
newOptions.copyButtonText = getDefaultString(newOptions.copyButtonText, "Copy");
return newOptions;
}
var _parameter_Document = null;
var _parameter_Navigator = null;
var _string = {empty:"", space:" ", newLine:"\n"};
var _options = {};
var _elements_Type = {};
var _strings_Cached = {};
var _strings_Cached_Count = 0;
Expand All @@ -254,27 +252,12 @@
}
return added;
};
this.setOptions = function(newOptions, triggerEvent) {
_options = !isDefinedObject(newOptions) ? {} : newOptions;
triggerEvent = !isDefinedBoolean(triggerEvent) ? true : triggerEvent;
var propertyName;
for (propertyName in newOptions) {
if (newOptions.hasOwnProperty(propertyName)) {
_options[propertyName] = newOptions[propertyName];
}
}
if (triggerEvent) {
fireCustomTrigger("onOptionsUpdated", _options);
}
return this;
};
this.getVersion = function() {
return "0.3.0";
return "0.4.0";
};
(function(documentObject, navigatorObject, windowObject) {
_parameter_Document = documentObject;
_parameter_Navigator = navigatorObject;
_options = buildDefaultOptions();
_parameter_Document.addEventListener("DOMContentLoaded", function() {
render();
});
Expand Down
2 changes: 1 addition & 1 deletion dist/syntax.js.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Syntax.js Library v0.3.0
* Syntax.js Library v0.4.0
*
* Copyright 2023 Bunoon
* Released under the MIT License
Expand Down
2 changes: 1 addition & 1 deletion dist/syntax.js.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f125164

Please sign in to comment.