Skip to content

Commit

Permalink
Fix the emphasis delimiter length for people that want to do stupid t…
Browse files Browse the repository at this point in the history
…hings.
  • Loading branch information
drgrice1 committed Nov 23, 2024
1 parent 80a7d91 commit 454d370
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 25 deletions.
4 changes: 2 additions & 2 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.15",
"version": "0.0.1-beta.16",
"description": "PG language support for CodeMirror",
"author": "The WeBWorK Project",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/pgml-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ export class PGMLParse {
const type = BlockDefs[token.substring(0, 1)]?.type;
let block = this.block;
if (block && block.type === type) {
block.to += 1;
block.to += block.token?.length ?? 1;
this.Terminate();
return;
}
while (block?.type !== 'root') {
if (block && block.prev?.type === type) {
this.End(`end of ${type ?? ''}`, block);
block.to += 1;
block.to += block.token?.length ?? 1;
this.Terminate();
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pgml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ const pgmlFormat = (block: Item, offset: number): Element<Type>[] => {
children.push(elt(Type.OptionMark, block.to - 1 + offset, block.to + offset));
return [elt(Type.Option, block.from + offset, block.to + offset, children)];
} else if (block.type === 'bold' || block.type === 'italic') {
children.unshift(elt(Type.EmphasisMark, block.from + offset, block.from + 1 + offset));
children.unshift(elt(Type.EmphasisMark, block.from + offset, block.from + (block.token?.length ?? 1) + offset));
const lastChild = block.stack?.at(-1);
if (lastChild instanceof Item && block.to === lastChild.to)
children.push(elt(Type.PGMLError, block.to + offset, block.to + offset));
else children.push(elt(Type.EmphasisMark, block.to - 1 + offset, block.to + offset));
else children.push(elt(Type.EmphasisMark, block.to - (block.token?.length ?? 1) + offset, block.to + offset));
return [
elt(
block.type === 'bold' ? Type.StrongEmphasis : Type.Emphasis,
Expand Down
148 changes: 130 additions & 18 deletions test/pgml-formatting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

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

The following is bold with too many delimiters ***bold text****
END_PGML

==>

{
"name": "Program",
"from": 0,
"to": 68,
"to": 133,
"children": [
{
"name": "PGMLBlock",
"from": 0,
"to": 68,
"to": 133,
"children": [
{ "name": "BeginPG", "from": 0, "to": 10 },
{
"name": "PGMLContent",
"from": 10,
"to": 60,
"to": 125,
"children": [
{
"name": "Paragraph",
"from": 10,
"to": 60,
"to": 59,
"children": [
{
"name": "StrongEmphasis",
Expand All @@ -46,10 +48,26 @@ END_PGML
]
}
]
},
{
"name": "Paragraph",
"from": 61,
"to": 124,
"children": [
{
"name": "StrongEmphasis",
"from": 108,
"to": 123,
"children": [
{ "name": "EmphasisMark", "from": 108, "to": 111 },
{ "name": "EmphasisMark", "from": 120, "to": 123 }
]
}
]
}
]
},
{ "name": "EndPG", "from": 60, "to": 68 }
{ "name": "EndPG", "from": 125, "to": 133 }
]
}
]
Expand All @@ -59,14 +77,14 @@ END_PGML

from: '0'
parseAll: 'true'
to: '49'
to: '114'
type: 'root'
stack: [
[ # 0
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '0'
indent: '0'
to: '49'
to: '48'
type: 'indent'
stack: [
[ # 0
Expand Down Expand Up @@ -107,10 +125,48 @@ stack: [
]
]
]
]
]
[ # 1
from: '48'
noIndent: '1'
to: '50'
token: '\n\n'
type: 'par'
]
[ # 2
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '50'
indent: '0'
to: '113'
type: 'indent'
stack: [
[ # 0
combine: { text: 'type' }
from: '50'
to: '97'
type: 'text'
stack: ['The following is bold with too many delimiters ']
]
[ # 1
from: '97'
to: '112'
token: '***'
type: 'bold'
stack: [
[ # 0
combine: { text: 'type' }
from: '100'
to: '109'
type: 'text'
stack: ['bold text']
]
]
]
[ # 2
combine: { text: 'type' }
from: '48'
to: '49'
from: '112'
to: '113'
type: 'text'
stack: ['\n']
]
Expand All @@ -122,30 +178,32 @@ stack: [

BEGIN_PGML
The following is _italic text_

The following is poor taste ____italic text___
END_PGML

==>

{
"name": "Program",
"from": 0,
"to": 50,
"to": 98,
"children": [
{
"name": "PGMLBlock",
"from": 0,
"to": 50,
"to": 98,
"children": [
{ "name": "BeginPG", "from": 0, "to": 10 },
{
"name": "PGMLContent",
"from": 10,
"to": 42,
"to": 91,
"children": [
{
"name": "Paragraph",
"from": 10,
"to": 42,
"to": 41,
"children": [
{
"name": "Emphasis",
Expand All @@ -157,10 +215,26 @@ END_PGML
]
}
]
},
{
"name": "Paragraph",
"from": 43,
"to": 91,
"children": [
{
"name": "Emphasis",
"from": 71,
"to": 90,
"children": [
{ "name": "EmphasisMark", "from": 71, "to": 75 },
{ "name": "EmphasisMark", "from": 86, "to": 90 }
]
}
]
}
]
},
{ "name": "EndPG", "from": 42, "to": 50 }
{ "name": "EndPG", "from": 90, "to": 98 }
]
}
]
Expand All @@ -170,14 +244,14 @@ END_PGML

from: '0'
parseAll: 'true'
to: '31'
to: '79'
type: 'root'
stack: [
[ # 0
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '0'
indent: '0'
to: '31'
to: '30'
type: 'indent'
stack: [
[ # 0
Expand All @@ -202,10 +276,48 @@ stack: [
]
]
]
]
]
[ # 1
from: '30'
noIndent: '1'
to: '32'
token: '\n\n'
type: 'par'
]
[ # 2
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '32'
indent: '0'
to: '80'
type: 'indent'
stack: [
[ # 0
combine: { text: 'type' }
from: '32'
to: '60'
type: 'text'
stack: ['The following is poor taste ']
]
[ # 1
from: '60'
to: '79'
token: '____'
type: 'italic'
stack: [
[ # 0
combine: { text: 'type' }
from: '64'
to: '75'
type: 'text'
stack: ['italic text']
]
]
]
[ # 2
combine: { text: 'type' }
from: '30'
to: '31'
from: '79'
to: '80'
type: 'text'
stack: ['\n']
]
Expand Down

0 comments on commit 454d370

Please sign in to comment.