@@ -77,6 +77,54 @@ function readCitationFormat(configuration: vscode.WorkspaceConfiguration, exclud
77
77
return fields
78
78
}
79
79
80
+ export const bibTools = {
81
+ expandField,
82
+ deParenthesis,
83
+ parseAbbrevations
84
+ }
85
+
86
+ function expandField ( abbreviations : { [ key : string ] : string } , value : bibtexParser . FieldValue ) : string {
87
+ if ( value . kind === 'concat' ) {
88
+ const args = value . content as bibtexParser . FieldValue [ ]
89
+ return args . map ( arg => expandField ( abbreviations , arg ) ) . join ( ' ' )
90
+ }
91
+ if ( bibtexParser . isAbbreviationValue ( value ) ) {
92
+ if ( value . content in abbreviations ) {
93
+ return abbreviations [ value . content ]
94
+ }
95
+ return ''
96
+ }
97
+ return value . content
98
+ }
99
+
100
+ function deParenthesis ( str : string ) : string {
101
+ // Remove wrapping { }
102
+ // Extract the content of \url{}
103
+ return str . replace ( / \\ u r l { ( [ ^ \\ { } ] + ) } / g, '$1' ) . replace ( / { + ( [ ^ \\ { } ] + ) } + / g, '$1' )
104
+ }
105
+
106
+ function parseAbbrevations ( ast : bibtexParser . BibtexAst ) {
107
+ const abbreviations : { [ key : string ] : string } = { }
108
+ ast . content . filter ( bibtexParser . isStringEntry ) . forEach ( ( entry : bibtexParser . StringEntry ) => {
109
+ // @string {string1 = "Proceedings of the "}
110
+ // @string {string2 = string1 # "Foo"}
111
+ if ( typeof entry . value . content === 'string' ) {
112
+ abbreviations [ entry . abbreviation ] = entry . value . content
113
+ } else {
114
+ abbreviations [ entry . abbreviation ] =
115
+ ( entry . value . content as ( bibtexParser . AbbreviationValue | bibtexParser . TextStringValue ) [ ] ) . map ( subEntry => {
116
+ if ( bibtexParser . isAbbreviationValue ( subEntry ) ) {
117
+ return abbreviations [ subEntry . content ] ?? `undefined @string "${ subEntry . content } "`
118
+ } else {
119
+ return subEntry . content
120
+ }
121
+ } ) . join ( '' )
122
+ }
123
+ } )
124
+
125
+ return abbreviations
126
+ }
127
+
80
128
export class Citation implements IProvider {
81
129
/**
82
130
* Bib entries in each bib `file`.
@@ -261,23 +309,7 @@ export class Citation implements IProvider {
261
309
lw . eventBus . fire ( eventbus . FileParsed , fileName )
262
310
return
263
311
}
264
- const abbreviations : { [ key : string ] : string } = { }
265
- ast . content . filter ( bibtexParser . isStringEntry ) . forEach ( ( entry : bibtexParser . StringEntry ) => {
266
- // @string {string1 = "Proceedings of the "}
267
- // @string {string2 = string1 # "Foo"}
268
- if ( typeof entry . value . content === 'string' ) {
269
- abbreviations [ entry . abbreviation ] = entry . value . content
270
- } else {
271
- abbreviations [ entry . abbreviation ] =
272
- ( entry . value . content as ( bibtexParser . AbbreviationValue | bibtexParser . TextStringValue ) [ ] ) . map ( subEntry => {
273
- if ( bibtexParser . isAbbreviationValue ( subEntry ) ) {
274
- return abbreviations [ subEntry . content ] ?? `undefined @string "${ subEntry . content } "`
275
- } else {
276
- return subEntry . content
277
- }
278
- } ) . join ( '' )
279
- }
280
- } )
312
+ const abbreviations = parseAbbrevations ( ast )
281
313
ast . content
282
314
. filter ( bibtexParser . isEntry )
283
315
. forEach ( ( entry : bibtexParser . Entry ) => {
@@ -293,7 +325,7 @@ export class Citation implements IProvider {
293
325
fields : new Fields ( )
294
326
}
295
327
entry . content . forEach ( field => {
296
- const value = this . deParenthesis ( this . expandField ( abbreviations , field . value ) )
328
+ const value = deParenthesis ( expandField ( abbreviations , field . value ) )
297
329
item . fields . set ( field . name , value )
298
330
} )
299
331
newEntry . push ( item )
@@ -304,20 +336,6 @@ export class Citation implements IProvider {
304
336
lw . eventBus . fire ( eventbus . FileParsed , fileName )
305
337
}
306
338
307
- private expandField ( abbreviations : { [ key : string ] : string } , value : bibtexParser . FieldValue ) : string {
308
- if ( value . kind === 'concat' ) {
309
- const args = value . content as bibtexParser . FieldValue [ ]
310
- return args . map ( arg => this . expandField ( abbreviations , arg ) ) . join ( ' ' )
311
- }
312
- if ( bibtexParser . isAbbreviationValue ( value ) ) {
313
- if ( value . content in abbreviations ) {
314
- return abbreviations [ value . content ]
315
- }
316
- return ''
317
- }
318
- return value . content
319
- }
320
-
321
339
removeEntriesInFile ( file : string ) {
322
340
logger . log ( `Remove parsed bib entries for ${ file } ` )
323
341
this . bibEntries . delete ( file )
@@ -360,10 +378,4 @@ export class Citation implements IProvider {
360
378
}
361
379
return items
362
380
}
363
-
364
- private deParenthesis ( str : string ) : string {
365
- // Remove wrapping { }
366
- // Extract the content of \url{}
367
- return str . replace ( / \\ u r l { ( [ ^ \\ { } ] + ) } / g, '$1' ) . replace ( / { + ( [ ^ \\ { } ] + ) } + / g, '$1' )
368
- }
369
381
}
0 commit comments