Skip to content

Commit

Permalink
feat: support for UR tags, including multi-lines UR tags
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Jun 24, 2020
1 parent 9c7aece commit b098ca6
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 21 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ parse

## Conversion Table

| Tag | Key | Description |
|:----|:-----|:---------------|
| TY | type | Reference type |
| Tag | Key | Description |
|:----|:--------|:---------------|
| TY | type | Reference type |
| KW | keyword | Keywords |
| UR | url | Web/URL |

## Development

Expand Down
19 changes: 17 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ function id(x) { return x[0]; }
{"name": "RECORD$ebnf$1", "symbols": ["RECORD$ebnf$1", "OTHER_TAG"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "RECORD", "symbols": ["RTYPE", "RECORD$ebnf$1", "EOR"], "postprocess": ast => ast.filter(x => x !== null)},
{"name": "RTYPE", "symbols": [(lexer.has("TY") ? {type: "TY"} : TY), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("TY_VAL") ? {type: "TY_VAL"} : TY_VAL), "__"], "postprocess": ([,,{value}]) => ({key: 'type', value})},
{"name": "OTHER_TAG", "symbols": ["KEYWORD"], "postprocess": ([d]) => d},
{"name": "OTHER_TAG$subexpression$1", "symbols": ["KEYWORD"]},
{"name": "OTHER_TAG$subexpression$1", "symbols": ["URL"]},
{"name": "OTHER_TAG", "symbols": ["OTHER_TAG$subexpression$1"], "postprocess": ([[d]]) => d},
{"name": "KEYWORD$ebnf$1", "symbols": ["LINE"]},
{"name": "KEYWORD$ebnf$1", "symbols": ["KEYWORD$ebnf$1", "LINE"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "KEYWORD", "symbols": [(lexer.has("KW") ? {type: "KW"} : KW), (lexer.has("SEP") ? {type: "SEP"} : SEP), "KEYWORD$ebnf$1"], "postprocess": ([,,lines]) => ({key: 'keyword', value: lines.join(' ')})},
{"name": "KEYWORD", "symbols": [(lexer.has("KW") ? {type: "KW"} : KW), (lexer.has("SEP") ? {type: "SEP"} : SEP), "KEYWORD$ebnf$1"], "postprocess": ([,,lines]) =>
( { key: 'keyword'
, value: lines.join(' ')
}
)
},
{"name": "URL$ebnf$1", "symbols": ["LINE"]},
{"name": "URL$ebnf$1", "symbols": ["URL$ebnf$1", "LINE"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "URL", "symbols": [(lexer.has("UR") ? {type: "UR"} : UR), (lexer.has("SEP") ? {type: "SEP"} : SEP), "URL$ebnf$1"], "postprocess": ([,,lines]) =>
( { key: 'url'
, value: lines.flatMap(line => line.split(';').map(url => url.trim()).filter(Boolean))
}
)
},
{"name": "LINE", "symbols": [(lexer.has("CONTENT") ? {type: "CONTENT"} : CONTENT), "__"], "postprocess": ([{value}]) => value},
{"name": "EOR", "symbols": [(lexer.has("ER") ? {type: "ER"} : ER), (lexer.has("SEP") ? {type: "SEP"} : SEP), "_"], "postprocess": () => null}
]
Expand Down
19 changes: 16 additions & 3 deletions grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@ RECORD -> RTYPE OTHER_TAG:* EOR
RTYPE -> %TY %SEP %TY_VAL __
{% ([,,{value}]) => ({key: 'type', value}) %}

OTHER_TAG -> KEYWORD
{% ([d]) => d %}
OTHER_TAG -> (KEYWORD | URL)
{% ([[d]]) => d %}

KEYWORD -> %KW %SEP LINE:+
{% ([,,lines]) => ({key: 'keyword', value: lines.join(' ')}) %}
{% ([,,lines]) =>
( { key: 'keyword'
, value: lines.join(' ')
}
)
%}

URL -> %UR %SEP LINE:+
{% ([,,lines]) =>
( { key: 'url'
, value: lines.flatMap(line => line.split(';').map(url => url.trim()).filter(Boolean))
}
)
%}

LINE -> %CONTENT __
{% ([{value}]) => value %}
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const to_record =
, acc.keyword.push(value)
, acc
)
, 'url' , (acc, {value}) =>
( acc.url = (acc.url || []).concat(value)
, acc
)
, acc => acc
);

Expand Down
3 changes: 2 additions & 1 deletion lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const lexer =
, SEP: " - "
, TY: "TY"
, KW: "KW"
, UR: "UR"
, ER: "ER"
, TY_VAL: [ "ABST" , "ADVS" , "AGGR"
, "ANCIENT", "ART" , "BILL"
Expand All @@ -31,7 +32,7 @@ const lexer =
, "STAT" , "THES" , "UNBILL"
, "UNPD" , "VIDEO"
]
, CONTENT: /[a-zA-Z0-9 \-\.']+/
, CONTENT: /[a-zA-Z0-9 \-\.':/;]+/
}
);

Expand Down
33 changes: 30 additions & 3 deletions ris-parser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,39 @@ Scenario Outline: Field mapping
TY - JOUR
KW - foo
KW - bar
UR - http://example.com
ER -
"""
When I parse the file
Then I will find a reference where '<field>' is set to '<value>'

Examples:
| field | value |
| type | JOUR |
| keyword | ["foo", "bar"] |
| field | value |
| type | JOUR |
| keyword | ["foo", "bar"] |
| url | ["http://example.com"] |

Scenario: URLs
Given I have this RIS file
"""
TY - JOUR
KW - foo
UR - url1
UR - url2; url3
KW - bar
UR - url4;
url5;
url6;
ER -
"""
When I parse the file
Then I will find a reference where 'url' is set to
"""
[ "url1"
, "url2"
, "url3"
, "url4"
, "url5"
, "url6"
]
"""
21 changes: 12 additions & 9 deletions sample.ris
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
TY - JOUR
KW - cortical contusion
KW - blood-brain barrier
KW - horseradish peroxidase
KW - hippocampus
KW - rat
UR - http://example.com
UR - http://example.net
KW - foo
KW - bar
KW - baz
ER -

TY - BOOK
KW - Epidemiology
KW - U.S. Gov't
KW - foo
KW - bar
UR - http://example.com; http://example.org;
http://example.net
ER -

TY - CHAP
KW - HIV
KW - AIDS
UR - http://example.net
KW - foo
UR - http://example.com; http://example.org
ER -
10 changes: 10 additions & 0 deletions steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ defineStep('I will find a reference where {string} is set to {string}', function
expected_value = value; // it's a string
}
assert.deepStrictEqual(this.list[0][field], expected_value);
});

defineStep('I will find a reference where {string} is set to', function (field, value) {
let expected_value;
try {
expected_value = JSON.parse(value);
} catch (e) {
expected_value = value; // it's a string
}
assert.deepStrictEqual(this.list[0][field], expected_value);
});

0 comments on commit b098ca6

Please sign in to comment.