Skip to content

Commit

Permalink
Fix a bug in the prevChar method of the PGML parser.
Browse files Browse the repository at this point in the history
This was an error in conversion from the Perl `substr` method to the
JavaScript `substring` method.  This caused emphasis to not work
correctly.
  • Loading branch information
drgrice1 committed Nov 23, 2024
1 parent eca5ac6 commit 80a7d91
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 58 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openwebwork/codemirror-lang-pg",
"version": "0.0.1-beta.14",
"version": "0.0.1-beta.15",
"description": "PG language support for CodeMirror",
"author": "The WeBWorK Project",
"license": "MIT",
Expand Down
18 changes: 10 additions & 8 deletions src/pgml-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,19 @@ export class PGMLParse {
}

nextChar(defaultNext?: string) {
if (typeof defaultNext === 'undefined') defaultNext = '';
return (this.split[this.i] ?? this.split[this.i + 1] ?? defaultNext).substring(0, 1);
return (
this.split[this.i] ??
this.split[this.i + 1] ??
(typeof defaultNext === 'undefined' ? '' : defaultNext)
).substring(0, 1);
}

prevChar(defaultPrev?: string) {
if (typeof defaultPrev === 'undefined') defaultPrev = '';
let i2 = this.i - 2;
if (i2 < 0) i2 = 0;
let i3 = this.i - 3;
if (i3 < 0) i3 = 0;
return (this.split[i2] ?? this.split[i3] ?? defaultPrev).substring(-1, 1);
return (
this.split[this.i - 2] ??
this.split[this.i - 3] ??
(typeof defaultPrev === 'undefined' ? '' : defaultPrev)
).slice(-1);
}

Parse(split: (string | undefined)[]) {
Expand Down
102 changes: 58 additions & 44 deletions test/pgml-formatting.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
# bold text

BEGIN_PGML
*bold text with math [`x + 3`]*
The following is *bold text with math [`x + 3`]*
END_PGML

==>

{
"name": "Program",
"from": 0,
"to": 51,
"to": 68,
"children": [
{
"name": "PGMLBlock",
"from": 0,
"to": 51,
"to": 68,
"children": [
{ "name": "BeginPG", "from": 0, "to": 10 },
{
"name": "PGMLContent",
"from": 10,
"to": 43,
"to": 60,
"children": [
{
"name": "Paragraph",
"from": 10,
"to": 43,
"to": 60,
"children": [
{
"name": "StrongEmphasis",
"from": 11,
"to": 42,
"from": 28,
"to": 59,
"children": [
{ "name": "EmphasisMark", "from": 11, "to": 12 },
{ "name": "EmphasisMark", "from": 28, "to": 29 },
{
"name": "MathMode",
"from": 32,
"to": 41,
"from": 49,
"to": 58,
"children": [
{ "name": "MathModeMark", "from": 32, "to": 34 },
{ "name": "MathModeMark", "from": 39, "to": 41 }
{ "name": "MathModeMark", "from": 49, "to": 51 },
{ "name": "MathModeMark", "from": 56, "to": 58 }
]
},
{ "name": "EmphasisMark", "from": 41, "to": 42 }
{ "name": "EmphasisMark", "from": 58, "to": 59 }
]
}
]
}
]
},
{ "name": "EndPG", "from": 43, "to": 51 }
{ "name": "EndPG", "from": 60, "to": 68 }
]
}
]
Expand All @@ -59,51 +59,58 @@ END_PGML

from: '0'
parseAll: 'true'
to: '32'
to: '49'
type: 'root'
stack: [
[ # 0
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '0'
indent: '0'
to: '32'
to: '49'
type: 'indent'
stack: [
[ # 0
combine: { text: 'type' }
from: '0'
to: '31'
to: '17'
type: 'text'
stack: ['The following is ']
]
[ # 1
from: '17'
to: '48'
token: '*'
type: 'bold'
stack: [
[ # 0
combine: { text: 'type' }
from: '1'
to: '21'
from: '18'
to: '38'
type: 'text'
stack: ['bold text with math ']
]
[ # 1
children: [
{
combine: { text: 'type' }
from: '23'
to: '28'
from: '40'
to: '45'
type: 'text'
stack: ['x + 3']
}
]
from: '21'
from: '38'
terminator: '`]'
to: '30'
to: '47'
token: '[`'
type: 'math'
]
]
]
[ # 1
[ # 2
combine: { text: 'type' }
from: '31'
to: '32'
from: '48'
to: '49'
type: 'text'
stack: ['\n']
]
Expand All @@ -114,46 +121,46 @@ stack: [
# italic text

BEGIN_PGML
_italic text_
The following is _italic text_
END_PGML

==>

{
"name": "Program",
"from": 0,
"to": 33,
"to": 50,
"children": [
{
"name": "PGMLBlock",
"from": 0,
"to": 33,
"to": 50,
"children": [
{ "name": "BeginPG", "from": 0, "to": 10 },
{
"name": "PGMLContent",
"from": 10,
"to": 25,
"to": 42,
"children": [
{
"name": "Paragraph",
"from": 10,
"to": 25,
"to": 42,
"children": [
{
"name": "Emphasis",
"from": 11,
"to": 24,
"from": 28,
"to": 41,
"children": [
{ "name": "EmphasisMark", "from": 11, "to": 12 },
{ "name": "EmphasisMark", "from": 23, "to": 24 }
{ "name": "EmphasisMark", "from": 28, "to": 29 },
{ "name": "EmphasisMark", "from": 40, "to": 41 }
]
}
]
}
]
},
{ "name": "EndPG", "from": 25, "to": 33 }
{ "name": "EndPG", "from": 42, "to": 50 }
]
}
]
Expand All @@ -163,35 +170,42 @@ END_PGML

from: '0'
parseAll: 'true'
to: '14'
to: '31'
type: 'root'
stack: [
[ # 0
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '0'
indent: '0'
to: '14'
to: '31'
type: 'indent'
stack: [
[ # 0
combine: { text: 'type' }
from: '0'
to: '13'
to: '17'
type: 'text'
stack: ['The following is ']
]
[ # 1
from: '17'
to: '30'
token: '_'
type: 'italic'
stack: [
[ # 0
combine: { text: 'type' }
from: '1'
to: '12'
from: '18'
to: '29'
type: 'text'
stack: ['italic text']
]
]
]
[ # 1
[ # 2
combine: { text: 'type' }
from: '13'
to: '14'
from: '30'
to: '31'
type: 'text'
stack: ['\n']
]
Expand Down

0 comments on commit 80a7d91

Please sign in to comment.