Skip to content

Commit

Permalink
update naming and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Apr 27, 2020
1 parent 3f2673d commit 85eaf3b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
16 changes: 12 additions & 4 deletions docs/mode-reference.rst
Expand Up @@ -62,7 +62,7 @@ Regular expression starting a mode. For example a single quote for strings or tw
If absent, ``begin`` defaults to a regexp that matches anything, so the mode starts immediately.


after:begin
on:begin
^^^^^^^^^^^

**type**: callback (matchData, response)
Expand All @@ -72,6 +72,8 @@ This callback is triggered the moment a begin match is detected. ``matchData`` i
- ``response.data`` - a simple object data store. Can be used for building more complex rules where the end rule is dependent on the content of begin, etc.
- ``response.ignoreMatch()`` - pretend as if this match never happened. The mode is not entered. Continues trying subsequent modes in the current mode's ``contains`` list

For an example of usage see ``END_SAME_AS_BEGIN`` in ``modes.js``.


end
^^^
Expand All @@ -90,7 +92,7 @@ Sometimes a mode can end not by itself but implicitly with its containing (paren
This is achieved with :ref:`endsWithParent <endsWithParent>` attribute.


before:end
on:end
^^^^^^^^^^^

**type**: callback (matchData, response)
Expand All @@ -100,6 +102,8 @@ This callback is triggered the moment an end match is detected. ``matchData`` in
- ``response.data`` - a simple object data store. Can be used for building more complex rules where the end rule is dependent on the content of begin, etc.
- ``response.ignoreMatch()`` - pretend as if this match never happened. The mode is not entered. Continues trying subsequent modes in the current mode's ``contains`` list

For an example of usage see ``END_SAME_AS_BEGIN`` in ``modes.js``.


beginKeywords
^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -204,8 +208,12 @@ tell it to end the function definition after itself:

.. _endSameAsBegin:

endSameAsBegin
^^^^^^^^^^^^^^
endSameAsBegin (deprecated as of 10.1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

**Deprecated:** *This attribute has been deprecated.* You should instead use the
``END_SAME_AS_BEGIN`` mode or use the ``on:begin`` and ``on:end`` attributes to
build more complex paired matchers.

**type**: boolean

Expand Down
16 changes: 8 additions & 8 deletions src/highlight.js
Expand Up @@ -203,9 +203,9 @@ const HLJS = function(hljs) {
let matched = regex.startsWith(mode.endRe, matchPlusRemainder);

if (matched) {
if (mode["before:end"]) {
if (mode["on:end"]) {
let resp = new Response(mode);
mode["before:end"](match, resp);
mode["on:end"](match, resp);
if (resp.ignore)
matched = false;
}
Expand All @@ -217,7 +217,7 @@ const HLJS = function(hljs) {
return mode;
}
}
// even if before:end fires an `ignore` it's still possible
// even if on:end fires an `ignore` it's still possible
// that we might trigger the end node because of a parent mode
if (mode.endsWithParent) {
return endOfMode(mode.parent, match, matchPlusRemainder);
Expand Down Expand Up @@ -245,7 +245,7 @@ const HLJS = function(hljs) {

let resp = new Response(new_mode);
// first internal before callbacks, then the public ones
let beforeCallbacks = [new_mode.__beforeBegin, new_mode["before:begin"]];
let beforeCallbacks = [new_mode.__beforeBegin, new_mode["on:begin"]];
for (let cb of beforeCallbacks) {
if (!cb) continue;
cb(match, resp);
Expand All @@ -268,10 +268,10 @@ const HLJS = function(hljs) {
}
}
mode = startNewMode(new_mode);
if (mode["after:begin"]) {
let resp = new Response(mode);
mode["after:begin"](match, resp);
}
// if (mode["after:begin"]) {
// let resp = new Response(mode);
// mode["after:begin"](match, resp);
// }
return new_mode.returnBegin ? 0 : lexeme.length;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/modes.js
Expand Up @@ -121,7 +121,7 @@ export const METHOD_GUARD = {
export const END_SAME_AS_BEGIN = function(mode) {
return Object.assign(mode,
{
'after:begin': (m, resp) => { resp.data._beginMatch = m[1]; },
'before:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch() }
'on:begin': (m, resp) => { resp.data._beginMatch = m[1]; },
'on:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch() }
});
};

0 comments on commit 85eaf3b

Please sign in to comment.