Skip to content

Commit

Permalink
Great refactor and tidy-up.
Browse files Browse the repository at this point in the history
  • Loading branch information
obuchtala committed Apr 26, 2019
1 parent 1061c60 commit 71f7bbc
Show file tree
Hide file tree
Showing 22 changed files with 1,322 additions and 14,987 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
/.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# texture-xmlschema
# texture-xml-utils

Toolset for working with xml schemas.
35 changes: 35 additions & 0 deletions bundler/compileSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path')
const fs = require('fs')
const xmlSchemaToMarkdown = require('./xmlSchemaToMarkdown')

// ATTENTION: this is still very experimental and should be improved if
// we are going to use it more widely
module.exports = function compileSchema (b, name, rngFile, searchDirs = [], deps = [], options = {}) {
const DEST = `tmp/${name}.data.js`
const ISSUES = `tmp/${name}.issues.txt`
const SCHEMA = `tmp/${name}.schema.md`
const rngDir = path.dirname(rngFile)
const entry = path.basename(rngFile)
searchDirs.unshift(rngDir)
b.custom(`Compiling schema '${name}'...`, {
rngFile: [rngFile].concat(deps),
dest: DEST,
execute () {
return new Promise((resolve, reject) => {
setTimeout(() => {
// TODO: make sure that the rngFile exists
const { compileRNG, checkSchema, serializeXMLSchema } = require('..')
const xmlSchema = compileRNG(fs, searchDirs, entry)
b.writeFileSync(DEST, `export default ${serializeXMLSchema(xmlSchema)}`)
b.writeFileSync(SCHEMA, xmlSchemaToMarkdown(xmlSchema))
if (options.debug) {
const issues = checkSchema(xmlSchema)
const issuesData = [`${issues.length} issues:`, ''].concat(issues).join('\n')
b.writeFileSync(ISSUES, issuesData)
}
resolve()
}, 250)
})
}
})
}
67 changes: 67 additions & 0 deletions bundler/xmlSchemaToMarkdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module.exports = function xmlSchemaToMD (xmlSchema) {
const { _analyzeSchema } = require('..')
const PRE_START = '<pre style="white-space:pre-wrap;">'
const PRE_END = '</pre>'

let result = []
let elementSchemas = xmlSchema._elementSchemas
let elementNames = Object.keys(elementSchemas)
_analyzeSchema(elementSchemas)

elementNames.sort()
let notImplemented = []
result.push('# Texture Article')
result.push('')
result.push('This schema defines a strict sub-set of JATS-archiving 1.1 .')
result.push('')
result.push('## Supported Elements')
result.push('')
elementNames.forEach(name => {
let elementSchema = elementSchemas[name]
if (elementSchema.type === 'not-implemented') {
notImplemented.push(elementSchema)
return
}
result.push('### `<' + elementSchema.name + '>`')
if (elementSchema.type === 'not-implemented') {
result.push('Not implemented.')
} else {
let attributes = elementSchema.attributes
let elementSpec = elementSchema.expr.toString()
if (elementSpec.startsWith('(') && elementSpec.endsWith(')')) {
elementSpec = elementSpec.slice(1, -1)
}
if (/^\s*$/.exec(elementSpec)) elementSpec = 'EMPTY'

let parents = Object.keys(elementSchema.parents)
if (parents.length === 0 && xmlSchema.getStartElement() !== name) {
console.error('Warning: element <%s> is not used.', name)
}

result.push('')
result.push('**Attributes**:')
result.push(PRE_START)
result.push(Object.keys(attributes).join(', '))
result.push(PRE_END)
result.push('**Contains**:')
result.push(PRE_START)
result.push(elementSpec)
result.push(PRE_END)
if (parents.length > 0) {
result.push('**This element may be contained in:**')
result.push(PRE_START)
result.push(parents.join(', '))
result.push(PRE_END)
}
result.push('')
}
})
if (notImplemented.length > 0) {
result.push('## Not Implemented')
notImplemented.forEach(elementSchema => {
result.push('- ' + elementSchema.name)
})
}

return result.join('\n')
}
1 change: 1 addition & 0 deletions index.es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/index.js'
Loading

0 comments on commit 71f7bbc

Please sign in to comment.