Skip to content

Commit

Permalink
[input] Use plugin API for internal plugins
Browse files Browse the repository at this point in the history
Use the new plugin API for the internal plugins,
instead of registering each format's type parser
and data parsers individually. This way, formats
added by internal plugins are also tagged with
their appropiate plugin ref, to make it possible
to remove them easily.

See 493cc0415aee3473820d4286691516fd70ed89aa
Closes #106
  • Loading branch information
larsgw committed Nov 2, 2018
1 parent 2ca7632 commit 6063205
Show file tree
Hide file tree
Showing 27 changed files with 144 additions and 112 deletions.
17 changes: 10 additions & 7 deletions src/parse/modules/bibjson/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

import * as json from './json'

export const scope = '@bibjson'
export const ref = '@bibjson'
export const parsers = {json}
export const types = {
export const formats = {
'@bibjson/object': {
dataType: 'SimpleObject',
propertyConstraint: {
props: ['fulltext_html', 'fulltext_xml', 'fulltext_pdf'],
match: 'some',
value: val => val && Array.isArray(val.value)
parse: json.parse,
parseType: {
dataType: 'SimpleObject',
propertyConstraint: {
props: ['fulltext_html', 'fulltext_xml', 'fulltext_pdf'],
match: 'some',
value: val => val && Array.isArray(val.value)
}
}
}
}
2 changes: 0 additions & 2 deletions src/parse/modules/bibjson/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const parseContentMine = function (data) {
return res
}

export const scope = '@bibjson'
export const types = '@bibjson/object'
export {
parseContentMine as parse
}
2 changes: 0 additions & 2 deletions src/parse/modules/bibtex/bibtxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const parseBibTxtEntry = entry => {
*/
const parseBibTxt = src => src.trim().split(bibTxtRegex.splitEntries).map(parseBibTxtEntry)

export const scope = '@bibtxt'
export const types = '@bibtxt/text'
export {
parseBibTxt as parse,
parseBibTxt as text,
Expand Down
31 changes: 23 additions & 8 deletions src/parse/modules/bibtex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,34 @@ import * as prop from './prop'
import * as type from './type'
import * as bibtxt from './bibtxt'

export const scope = '@bibtex'
export const ref = '@bibtex'
export const parsers = {text, json, prop, type, bibtxt}
export const types = {
export const formats = {
'@bibtex/text': {
dataType: 'String',
predicate: /^(?:\s*@\s*[^@]+?\s*\{\s*[^@]+?\s*,\s*[^@]+\})+\s*$/
parse: text.parse,
parseType: {
dataType: 'String',
predicate: /^(?:\s*@\s*[^@]+?\s*\{\s*[^@]+?\s*,\s*[^@]+\})+\s*$/
}
},
'@bibtxt/text': {
dataType: 'String',
predicate: /^\s*(\[(?!\s*[{[]).*?\]\s*(\n\s*[^[]((?!:)\S)+\s*:\s*.+?\s*)*\s*)+$/
parse: bibtxt.parse,
parseType: {
dataType: 'String',
predicate: /^\s*(\[(?!\s*[{[]).*?\]\s*(\n\s*[^[]((?!:)\S)+\s*:\s*.+?\s*)*\s*)+$/
}
},
'@bibtex/object': {
dataType: 'SimpleObject',
propertyConstraint: {props: ['type', 'label', 'properties']}
parse: json.parse,
parseType: {
dataType: 'SimpleObject',
propertyConstraint: {props: ['type', 'label', 'properties']}
}
},
'@bibtex/prop': {
parse: prop.parse
},
'@bibtex/type': {
parse: type.parse
}
}
2 changes: 0 additions & 2 deletions src/parse/modules/bibtex/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ const parseBibTeXJSON = function (data) {
})
}

export const scope = '@bibtex'
export const types = '@bibtex/object'
export {
parseBibTeXJSON as parse,
parseBibTeXJSON as default
Expand Down
2 changes: 0 additions & 2 deletions src/parse/modules/bibtex/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ const parseBibTeXProp = function (name, value) {
return [cslProp, cslValue]
}

export const scope = '@bibtex'
export const types = '@bibtex/prop'
export {
parseBibTeXProp as parse,
parseBibTeXProp as default
Expand Down
2 changes: 0 additions & 2 deletions src/parse/modules/bibtex/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ const parseBibTeX = function (str) {
return entries
}

export const scope = '@bibtex'
export const types = '@bibtex/text'
export {
parseBibTeX as parse,
parseBibTeX as default
Expand Down
2 changes: 0 additions & 2 deletions src/parse/modules/bibtex/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ const parseBibTeXType = function (pubType) {
}
}

export const scope = '@bibtex'
export const types = '@bibtex/type'
export {
parseBibTeXType as parse,
parseBibTeXType as default
Expand Down
2 changes: 0 additions & 2 deletions src/parse/modules/doi/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ const fetchDoiApi = function (url) {
*/
const parseDoiApi = data => [].concat(data).map(fetchDoiApi).map(parseDoiJson)

export const scope = '@doi'
export const types = '@doi/api'
export {
parseDoiApi as parse,
parseDoiApiAsync as parseAsync
Expand Down
2 changes: 0 additions & 2 deletions src/parse/modules/doi/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const parseDoi = data => {
return list.map(doi => `https://doi.org/${doi}`)
}

export const scope = '@doi'
export const types = ['@doi/id', '@doi/list+text', '@doi/list+object']
export {
parseDoi as parse,
parseDoi as default
Expand Down
38 changes: 27 additions & 11 deletions src/parse/modules/doi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,40 @@ import * as api from './api'
import * as json from './json'
import * as type from './type'

export const scope = '@doi'
export const ref = '@doi'
export const parsers = {id, api, json, type}
export const types = {
export const formats = {
'@doi/api': {
dataType: 'String',
predicate: /^\s*(https?:\/\/(?:dx\.)?doi\.org\/(10.\d{4,9}\/[-._;()/:A-Z0-9]+))\s*$/i,
extends: '@else/url'
parse: api.parse,
parseAsync: api.parseAsync,
parseType: {
dataType: 'String',
predicate: /^\s*(https?:\/\/(?:dx\.)?doi\.org\/(10.\d{4,9}\/[-._;()/:A-Z0-9]+))\s*$/i,
extends: '@else/url'
}
},
'@doi/id': {
dataType: 'String',
predicate: /^\s*(10.\d{4,9}\/[-._;()/:A-Z0-9]+)\s*$/i
parse: id.parse,
parseType: {
dataType: 'String',
predicate: /^\s*(10.\d{4,9}\/[-._;()/:A-Z0-9]+)\s*$/i
}
},
'@doi/list+text': {
dataType: 'String',
predicate: /^\s*(?:(?:10.\d{4,9}\/[-._;()/:A-Z0-9]+)\s*)+$/i
parse: id.parse,
parseType: {
dataType: 'String',
predicate: /^\s*(?:(?:10.\d{4,9}\/[-._;()/:A-Z0-9]+)\s*)+$/i
}
},
'@doi/list+object': {
dataType: 'Array',
elementConstraint: '@doi/id'
parse: id.parse,
parseType: {
dataType: 'Array',
elementConstraint: '@doi/id'
}
},
'@doi/type': {
parse: type.parse
}
}
2 changes: 0 additions & 2 deletions src/parse/modules/doi/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ const parseDoiJson = function (data) {
return Object.assign({}, data, res)
}

export const scope = '@doi'
export const types = '@doi/object'
export {
parseDoiJson as parse,
parseDoiJson as default
Expand Down
2 changes: 0 additions & 2 deletions src/parse/modules/doi/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ const varDoiTypes = {
*/
const fetchDoiType = value => varDoiTypes[value] || value

export const scope = '@doi'
export const types = '@doi/type'
export {
fetchDoiType as parse,
fetchDoiType as default
Expand Down
14 changes: 3 additions & 11 deletions src/parse/modules/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import {addFormat as add} from '../interface/register'
import {addPlugin} from '../interface/register'
import * as modules from './modules'

for (const module in modules) {
const {types, parsers} = modules[module]

for (const type in types) {
add(type, {parseType: types[type]})
}

for (const parser in parsers) {
const {types, parse, parseAsync} = parsers[parser]
;[].concat(types).forEach(type => add(type, {parse, parseAsync}))
}
const {ref, formats, config} = modules[module]
addPlugin(ref, formats, config)
}
2 changes: 0 additions & 2 deletions src/parse/modules/other/empty.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const scope = '@empty'
export const types = ['@empty', '@empty/text', '@empty/whitespace+text']
export const parse = () => []
2 changes: 0 additions & 2 deletions src/parse/modules/other/html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const scope = '@else'
export const types = '@else/html'
export const parse = input => input.value || input.textContent
54 changes: 38 additions & 16 deletions src/parse/modules/other/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,57 @@ import * as json from './json'
import * as jquery from './jquery'
import * as html from './html'

export const scope = '@else'
export const ref = '@else'
export const parsers = {empty, url, json, jquery, html}
export const types = {
export const formats = {
'@empty/text': {
dataType: 'String',
predicate: input => input === ''
parse: empty.parse,
parseType: {
dataType: 'String',
predicate: input => input === ''
}
},
'@empty/whitespace+text': {
dataType: 'String',
predicate: /^\s+$/
parse: empty.parse,
parseType: {
dataType: 'String',
predicate: /^\s+$/
}
},
'@empty': {
dataType: 'Primitive',
predicate: input => input == null
parse: empty.parse,
parseType: {
dataType: 'Primitive',
predicate: input => input == null
}
},
'@else/json': {
dataType: 'String',
predicate: /^\s*(\{[\S\s]+\}|\[[\S\s]*\])\s*$/
parse: json.parse,
parseType: {
dataType: 'String',
predicate: /^\s*(\{[\S\s]+\}|\[[\S\s]*\])\s*$/
}
},
'@else/url': {
dataType: 'String',
predicate: /^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3})|localhost)(:\d+)?(\/[-a-z\d%_.~+:]*)*(\?[;&a-z\d%_.~+=-]*)?(#[-a-z\d_]*)?$/i
parse: url.parse,
parseAsync: url.parseAsync,
parseType: {
dataType: 'String',
predicate: /^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3})|localhost)(:\d+)?(\/[-a-z\d%_.~+:]*)*(\?[;&a-z\d%_.~+=-]*)?(#[-a-z\d_]*)?$/i
}
},
'@else/jquery': {
dataType: 'ComplexObject',
predicate: input => typeof jQuery !== 'undefined' && input instanceof jQuery
parse: jquery.parse,
parseType: {
dataType: 'ComplexObject',
predicate: input => typeof jQuery !== 'undefined' && input instanceof jQuery
}
},
'@else/html': {
dataType: 'ComplexObject',
predicate: input => typeof HTMLElement !== 'undefined' && input instanceof HTMLElement
parse: html.parse,
parseType: {
dataType: 'ComplexObject',
predicate: input => typeof HTMLElement !== 'undefined' && input instanceof HTMLElement
}
}
}
2 changes: 0 additions & 2 deletions src/parse/modules/other/jquery.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const scope = '@else'
export const types = '@else/jquery'
export const parse = input => input.val() || input.text() || input.html()
2 changes: 0 additions & 2 deletions src/parse/modules/other/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ const parseJSON = function (str) {
}
}

export const scope = '@else'
export const types = '@else/json'
export {
parseJSON as parse,
parseJSON as default
Expand Down
2 changes: 0 additions & 2 deletions src/parse/modules/other/url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export const scope = '@else'
export const types = '@else/url'
export {default as parse} from '../../../util/fetchFile'
export {default as parseAsync} from '../../../util/fetchFileAsync'
2 changes: 0 additions & 2 deletions src/parse/modules/wikidata/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
* @module input/wikidata
*/

export const scope = '@wikidata'
export const types = '@wikidata/api'
export {default as parse} from '../../../util/fetchFile'
export {default as parseAsync} from '../../../util/fetchFileAsync'

0 comments on commit 6063205

Please sign in to comment.