Skip to content

Commit

Permalink
add support for Dawkeye-style cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Oct 18, 2022
1 parent e394ea6 commit 5cf6b07
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/models/ContributionSurveyRowParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Data that constructs a raw contribution survey row.
*/
import cloneRegex from '../util/cloneRegex';

export interface RawContributionSurveyRow {
type: 'detailed' | 'pageonly';
/**
Expand Down Expand Up @@ -103,7 +105,11 @@ export default class ContributionSurveyRowParser {
throw new Error( 'dp-undetectable-page-name' );
}

let extras = this.eatUntil( /^\[\[Special:Diff\/\d+/, true );
let extras =
// [[Special:Diff/12345|6789]]
this.eatUntil( /^(?:'''?)?\[\[Special:Diff\/\d+/, true ) ??
// {{dif|12345|6789}}
this.eatUntil( /^(?:'''?)?{{dif\|\d+/, true );

// At this point, `extras` is either a string or `null`. If it's a string,
// extras exist, and we should add them. If not, there's likely no more
Expand All @@ -115,7 +121,11 @@ export default class ContributionSurveyRowParser {
const starting = this.current;
let diff: true | string = true;
while ( diff ) {
diff = this.eatExpression( /\[\[Special:Diff\/(\d+)(?:\|.*)?]]/g, 1 );
diff =
// [[Special:Diff/12345|6789]]
this.eatExpression( /(?:'''?)?\[\[Special:Diff\/(\d+)(?:\|.*)?]](?:'''?)?/g, 1 ) ??
// {{dif|12345|6789}}
this.eatExpression( /(?:'''?)?{{dif\|(\d+)\|[^}]+}}(?:'''?)?/g, 1 );
if ( diff != null ) {
revids.push( +diff );
}
Expand Down Expand Up @@ -215,7 +225,7 @@ export default class ContributionSurveyRowParser {
return consumed;
}
} else {
if ( pattern.test( this.current ) ) {
if ( cloneRegex( pattern ).test( this.current ) ) {
return consumed;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/ui/root/DeputyContributionSurveySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,12 @@ export default class DeputyContributionSurveySection implements DeputyUIElement
csr, rowElements[ csr.title.getPrefixedText() ], line, this
);
} catch ( e ) {
console.warn( 'Could not parse row.', line, e );
// For debugging and tests.
mw.hook( 'deputy.errors.cciRowParse' ).fire( { line, e } );
// Only trigger on actual bulleted lists.
if ( line.startsWith( '*' ) ) {
console.warn( 'Could not parse row.', line, e );
// For debugging and tests.
mw.hook( 'deputy.errors.cciRowParse' ).fire( { line, error: e } );
}
rowElement = line;
}
if ( typeof rowElement !== 'string' ) {
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/ContributionSurveyRowParserTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ describe( 'ContributionSurveyRowParser line parsing tests', () => {
} );
} );

test( '{{dif}}, pre-colon', () => {
// Dawkeye
const parser = new ContributionSurveyRowParser(
'* [[:Example]]: (1 edits, 1 major, +15927) \'\'\'{{dif|340829968|(+15927)}}\'\'\''
);
expect( parser.parse() ).toEqual( {
type: 'detailed',
bullet: '*',
creation: false,
page: ':Example',
extras: ': (1 edits, 1 major, +15927) ',
diffs: '\'\'\'{{dif|340829968|(+15927)}}\'\'\'',
comments: null,
revids: [ 340829968 ]
} );
} );

test( 'edge: pre-colon, no comment, no diffs', () => {
const parser = new ContributionSurveyRowParser(
'*[[:Example]]: (1 edit)'
Expand Down

0 comments on commit 5cf6b07

Please sign in to comment.