Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 10 misc chores and small breaking changes #2325

Merged
merged 10 commits into from
Jan 31, 2020
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ The table below shows the full list of supported languages (and corresponding cl
| Basic | basic | |
| BNF | bnf | |
| Brainfuck | brainfuck, bf | |
| C# | cs, csharp | |
| C++ | cpp, c, cc, h, c++, h++, hpp | |
| C# | csharp, cs | |
| C | h | |
| C++ | cpp, hpp, cc, hh, c++, h++, cxx, hxx | |
| C/AL | cal | |
| Cache Object Script | cos, cls | |
| CMake | cmake, cmake.in | |
Expand Down Expand Up @@ -139,6 +140,7 @@ The table below shows the full list of supported languages (and corresponding cl
| Java | java, jsp | |
| JavaScript | javascript, js, jsx | |
| Kotlin | kotlin, kt | |
| LaTeX | tex | |
| Leaf | leaf | |
| Lasso | lasso, ls, lassoscript | |
| Less | less | |
Expand All @@ -162,7 +164,7 @@ The table below shows the full list of supported languages (and corresponding cl
| N1QL | n1ql | |
| NSIS | nsis | |
| Nginx | nginx, nginxconf | |
| Nimrod | nimrod, nim | |
| Nim | nimrod | |
| Nix | nix | |
| OCaml | ocaml, ml | |
| Objective C | objectivec, mm, objc, obj-c | |
Expand Down Expand Up @@ -219,7 +221,6 @@ The table below shows the full list of supported languages (and corresponding cl
| Tcl | tcl, tk | |
| Terraform (HCL) | terraform, tf, hcl | [highlightjs-terraform](https://github.com/highlightjs/highlightjs-terraform) |
| Test Anything Protocol | tap | |
| TeX | tex | |
| Thrift | thrift | |
| TP | tp | |
| Twig | twig, craftcms | |
Expand Down Expand Up @@ -301,8 +302,8 @@ onmessage = (event) => {

## Node.js

You can use highlight.js with node to highlight content before sending it to the browser.
Make sure to use the `.value` property to get the formatted html.
You can use highlight.js with node to highlight content before sending it to the browser.
Make sure to use the `.value` property to get the formatted html.
For more info about the returned object refer to the api docs https://highlightjs.readthedocs.io/en/latest/api.html


Expand Down
30 changes: 30 additions & 0 deletions VERSION_10_BREAKING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Version 10 Breaking Changes

Incompatibilities:
- chore(parser): compressed version 9.x language definitions no longer supported (rebuild them minified) [Josh Goebel][]

Renamed Language Files:
- chore(parser): rename `nimrod.js` to `nim.js` [Josh Goebel][]
- chore(parser): rename `cs.js` to `csharp.js` [Josh Goebel][]
- chore(parser): rename `tex.js` to `latex.js` [Josh Goebel][]
- chore(parser): effectively rename `cpp.js` to `c-like.js` [Josh Goebel][]
- chore(parser): create new `c.js` (C), depends on `c-like` now [Josh Goebel][]
- chore(parser): create new `cpp.js` (C), depends on `c-like` now [Josh Goebel][]
- This will allow us to clean up C/C++ in the future without another breaking change
by getting this require change out of the way early.
(https://github.com/highlightjs/highlight.js/issues/2146)

Legacy Browser Issues:
- **We're now using ES2015 features in the codebase. Internet Explorer 11 is no longer supported.**
- In general legacy browsers are no longer supported.
- chore(parser): remove `load` listener in favor of only the newer `DOMContentLoaded` [Josh Goebel][]

Removed styles:
- chore(styles): darkula.css (use darcula.css instead) [Josh Goebel][]

[Josh Goebel]: https://github.com/yyyc514

---

Also see:
https://github.com/highlightjs/highlight.js/issues/2273
20 changes: 3 additions & 17 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,6 @@ https://highlightjs.org/
return [mode];
}

function restoreLanguageApi(obj) {
if(API_REPLACES && !obj.langApiRestored) {
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
obj.langApiRestored = true;
for(var key in API_REPLACES) {
if (obj[key]) {
obj[API_REPLACES[key]] = obj[key];
}
}
(obj.contains || []).concat(obj.variants || []).forEach(restoreLanguageApi);
}
}

function compileKeywords(rawKeywords, case_insensitive) {
var compiled_keywords = {};

Expand Down Expand Up @@ -321,7 +309,7 @@ https://highlightjs.org/
}

function commonKeyword(word) {
return COMMON_KEYWORDS.indexOf(word.toLowerCase()) != -1;
return COMMON_KEYWORDS.includes(word.toLowerCase());
}

function compileLanguage(language) {
Expand Down Expand Up @@ -500,7 +488,7 @@ https://highlightjs.org/
}

// self is not valid at the top-level
if (language.contains && language.contains.indexOf('self') != -1) {
if (language.contains && language.contains.includes('self')) {
if (!SAFE_MODE) {
throw new Error("ERR: contains `self` is not supported at the top-level of a language. See documentation.")
} else {
Expand Down Expand Up @@ -777,7 +765,7 @@ https://highlightjs.org/
top: top
};
} catch (err) {
if (err.message && err.message.indexOf('Illegal') !== -1) {
if (err.message && err.message.includes('Illegal')) {
return {
illegal: true,
relevance: 0,
Expand Down Expand Up @@ -935,7 +923,6 @@ https://highlightjs.org/
*/
function initHighlightingOnLoad() {
window.addEventListener('DOMContentLoaded', initHighlighting, false);
window.addEventListener('load', initHighlighting, false);
}

var PLAINTEXT_LANGUAGE = { disableAutodetect: true };
Expand All @@ -954,7 +941,6 @@ https://highlightjs.org/
lang = PLAINTEXT_LANGUAGE;
}
languages[name] = lang;
restoreLanguageApi(lang);
lang.rawDefinition = language.bind(null,hljs);

if (lang.aliases) {
Expand Down
236 changes: 236 additions & 0 deletions src/languages/c-like.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
Language: C-like foundation grammar for C/C++ grammars
Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Zaven Muradyan <megalivoithos@gmail.com>, Roel Deckers <admin@codingcat.nl>, Sam Wu <samsam2310@gmail.com>, Jordi Petit <jordi.petit@gmail.com>, Pieter Vantorre <pietervantorre@gmail.com>, Google Inc. (David Benjamin) <davidben@google.com>
Category: common, system
*/

/* In the future the intention is to split out the C/C++ grammars distinctly
since they are separate languages. They will likely share a common foundation
though, and this file sets the groundwork for that - so that we get the breaking
change in v10 and don't have to change the requirements again later.

See: https://github.com/highlightjs/highlight.js/issues/2146
*/

function(hljs) {
function optional(s) {
return '(?:' + s + ')?';
}
var DECLTYPE_AUTO_RE = 'decltype\\(auto\\)'
var NAMESPACE_RE = '[a-zA-Z_]\\w*::'
var TEMPLATE_ARGUMENT_RE = '<.*?>';
var FUNCTION_TYPE_RE = '(' +
DECLTYPE_AUTO_RE + '|' +
optional(NAMESPACE_RE) +'[a-zA-Z_]\\w*' + optional(TEMPLATE_ARGUMENT_RE) +
')';
var CPP_PRIMITIVE_TYPES = {
className: 'keyword',
begin: '\\b[a-z\\d_]*_t\\b'
};

// https://en.cppreference.com/w/cpp/language/escape
// \\ \x \xFF \u2837 \u00323747 \374
var CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)'
var STRINGS = {
className: 'string',
variants: [
{
begin: '(u8?|U|L)?"', end: '"',
illegal: '\\n',
contains: [hljs.BACKSLASH_ESCAPE]
},
{
begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + "|.)", end: '\'',
illegal: '.'
},
{ begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\((?:.|\n)*?\)\1"/ }
]
};

var NUMBERS = {
className: 'number',
variants: [
{ begin: '\\b(0b[01\']+)' },
{ begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)' },
{ begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
],
relevance: 0
};

var PREPROCESSOR = {
className: 'meta',
begin: /#\s*[a-z]+\b/, end: /$/,
keywords: {
'meta-keyword':
'if else elif endif define undef warning error line ' +
'pragma _Pragma ifdef ifndef include'
},
contains: [
{
begin: /\\\n/, relevance: 0
},
hljs.inherit(STRINGS, {className: 'meta-string'}),
{
className: 'meta-string',
begin: /<.*?>/, end: /$/,
illegal: '\\n',
},
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE
]
};

var TITLE_MODE = {
className: 'title',
begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
relevance: 0
};

var FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';

var CPP_KEYWORDS = {
keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
'unsigned long volatile static protected bool template mutable if public friend ' +
'do goto auto void enum else break extern using asm case typeid wchar_t' +
'short reinterpret_cast|10 default double register explicit signed typename try this ' +
'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
'concept co_await co_return co_yield requires ' +
'noexcept static_assert thread_local restrict final override ' +
'atomic_bool atomic_char atomic_schar ' +
'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
'atomic_ullong new throw return ' +
'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream ' +
'auto_ptr deque list queue stack vector map set bitset multiset multimap unordered_set ' +
'unordered_map unordered_multiset unordered_multimap array shared_ptr abort terminate abs acos ' +
'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp ' +
'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper ' +
'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow ' +
'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp ' +
'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan ' +
'vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary',
literal: 'true false nullptr NULL'
};

var EXPRESSION_CONTAINS = [
CPP_PRIMITIVE_TYPES,
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
NUMBERS,
STRINGS
];

var EXPRESSION_CONTEXT = {
// This mode covers expression context where we can't expect a function
// definition and shouldn't highlight anything that looks like one:
// `return some()`, `else if()`, `(x*sum(1, 2))`
variants: [
{begin: /=/, end: /;/},
{begin: /\(/, end: /\)/},
{beginKeywords: 'new throw return else', end: /;/}
],
keywords: CPP_KEYWORDS,
contains: EXPRESSION_CONTAINS.concat([
{
begin: /\(/, end: /\)/,
keywords: CPP_KEYWORDS,
contains: EXPRESSION_CONTAINS.concat(['self']),
relevance: 0
}
]),
relevance: 0
};

var FUNCTION_DECLARATION = {
className: 'function',
begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
returnBegin: true, end: /[{;=]/,
excludeEnd: true,
keywords: CPP_KEYWORDS,
illegal: /[^\w\s\*&:<>]/,
contains: [

{ // to prevent it from being confused as the function title
begin: DECLTYPE_AUTO_RE,
keywords: CPP_KEYWORDS,
relevance: 0,
},
{
begin: FUNCTION_TITLE, returnBegin: true,
contains: [TITLE_MODE],
relevance: 0
},
{
className: 'params',
begin: /\(/, end: /\)/,
keywords: CPP_KEYWORDS,
relevance: 0,
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
STRINGS,
NUMBERS,
CPP_PRIMITIVE_TYPES,
// Count matching parentheses.
{
begin: /\(/, end: /\)/,
keywords: CPP_KEYWORDS,
relevance: 0,
contains: [
'self',
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
STRINGS,
NUMBERS,
CPP_PRIMITIVE_TYPES
]
}
]
},
CPP_PRIMITIVE_TYPES,
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
PREPROCESSOR
]
};

return {
aliases: ['c', 'cc', 'h', 'c++', 'h++', 'hpp', 'hh', 'hxx', 'cxx'],
keywords: CPP_KEYWORDS,
// the base c-like language will NEVER be auto-detected, rather the
// derivitives: c, c++, arduino turn auto-detect back on for themselves
disableAutodetect: true,
illegal: '</',
contains: [].concat(
EXPRESSION_CONTEXT,
FUNCTION_DECLARATION,
EXPRESSION_CONTAINS,
[
PREPROCESSOR,
{
begin: '\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<', end: '>',
keywords: CPP_KEYWORDS,
contains: ['self', CPP_PRIMITIVE_TYPES]
},
{
begin: hljs.IDENT_RE + '::',
keywords: CPP_KEYWORDS
},
{
className: 'class',
beginKeywords: 'class struct', end: /[{;:]/,
contains: [
{begin: /</, end: />/, contains: ['self']}, // skip generic stuff
hljs.TITLE_MODE
]
}
]),
exports: {
preprocessor: PREPROCESSOR,
strings: STRINGS,
keywords: CPP_KEYWORDS
}
};
}
21 changes: 21 additions & 0 deletions src/languages/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Language: C
Category: common, system
Website: https://en.wikipedia.org/wiki/C_(programming_language)
Requires: c-like.js
*/

function(hljs) {

var lang = hljs.getLanguage('c-like').rawDefinition();
// Until C is actually different than C++ there is no reason to auto-detect C
// as it's own language since it would just fail auto-detect testing or
// simply match with C++.
//
// See further comments in c-like.js.

// lang.disableAutodetect = false;
lang.aliases = ['c', 'h'];
return lang;

}