Skip to content

Commit

Permalink
feat: implement parse.map for mapping RIS tags to human-friendly names
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Jul 28, 2020
1 parent 4d37e51 commit bf5df51
Show file tree
Hide file tree
Showing 12 changed files with 2,503 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ parse: grammar.js
grammar.js: grammar.ne lexer.js
yarn -s nearleyc $^ > $@

# Output Markdown table for all possible types
markdown-type:
@jq -M -S -r -f doc/type.jq type-map.json | awk -F"," -f doc/type.awk

# Output Markdown table for the tag map
markdown-tag:
@jq -M -S -r -f doc/tag.jq tag-map.json | awk -F"," -f doc/tag.awk

/tmp/ris.test: grammar.js index.js ris-parser.feature steps.js
yarn cucumber-js --require steps.js ris-parser.feature
touch $@
Expand Down
156 changes: 156 additions & 0 deletions README.md

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions doc/tag.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
NR == 1 {
printf "|"
for (i=1; i<NF; i=i+1) { printf "%s|", $i }
printf "\n"
printf "|"
for (i=1; i<NF; i=i+1) { printf ":--|", $i }
printf "\n"
next
}

{
printf "|"
for (i=1; i<NF; i=i+1) {
if ($i == "N/A") {
printf "_%s_|", $i
} else {
printf "%s|", $i
}
}
printf "\n"
}
10 changes: 10 additions & 0 deletions doc/tag.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(to_entries | map(.key | split(".") | last) | unique) as $all_tags
| to_entries
| group_by(.key | split(".") | first)
| map(reduce .[] as $item ({}; . + { TY: ($item.key | split(".") | first)
, ($item.key | split(".") | last): $item.value
}))
| sort_by(.TY)
| ([["Type", "TY"] + $all_tags]) + map([.TY, "@type"] + (. as $format | ($all_tags | map($format[.] // "N/A"))))
| map(join(","))
| join("\n")
18 changes: 18 additions & 0 deletions doc/type.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# INPUT
# A,1
# B,2
#
# OUTPUT
# | TY | Description |
# |:---|:------------|
# | A | 1 |
# | B | 2 |


BEGIN {
print "| TY | Description |"
print "|:---|:------------|"
}
{
print "|" $1 "|" $2 "|"
}
7 changes: 7 additions & 0 deletions doc/type.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# INPUT
# {"A": "1", "B": "2"}
# OUTPUT
# "A,1"
# "B,2"

to_entries | map("\(.key),\(.value)") | join("\n")
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

const nearley = require('nearley');
const grammar = require('./grammar.js');
const type_map = require('./type-map.json');
const tag_map = require('./tag-map.json');

const zip =
(keys, values) =>
Expand Down Expand Up @@ -157,4 +159,15 @@ const parse = text => {
return process_ast(parser.results[0]);
};

parse.map = text => {
const parsed = parse(text);
return parsed.map(
p => Object.keys(p).reduce(
(mapped, key) => {
const new_key = tag_map[`${p.TY}.${key}`] || (key === 'TY' ? '@type' : key);
mapped[new_key] = new_key !== '@type' ? p[key] : type_map[p.TY];
return mapped;
}, {}));
};

module.exports = parse;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"files": [
"index.js",
"grammar.js",
"lexer.js"
"lexer.js",
"tag-map.json",
"type-map.json"
],
"dependencies": {
"moo": "^0.5.1",
Expand Down
11 changes: 11 additions & 0 deletions ris-parser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,14 @@ Scenario Outline: Samples
Examples:
| ris | response |
| 01.ris | 01.json |

Scenario: Remapping tags
Given I have this RIS file
"""
TY - JOUR
AB - this is my abstract
ER -
"""
When I parse the file and rename the tags
Then I will find a reference where '@type' is set to 'Journal'
Then I will find a reference where 'abstract' is set to 'this is my abstract'
4 changes: 4 additions & 0 deletions steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ defineStep('I parse the file', function () {
this.list = sut(this.file);
});

defineStep('I parse the file and rename the tags', function () {
this.list = sut.map(this.file);
});

defineStep('I will get a list of {int} reference(s)', function (count) {
assert.equal(this.list.length, count);
});
Expand Down
Loading

0 comments on commit bf5df51

Please sign in to comment.