-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
119 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
const { compileRNG } = require('..') | ||
const path = require('path') | ||
const { _compileRNG } = require('..') | ||
|
||
module.exports = function _compileSchema (rngFile, searchDirs, options = {}) { | ||
const rngDir = path.dirname(rngFile) | ||
// Note: instead of using the absolute path to the rngFile | ||
// we register the dir as first search directory and use the | ||
// the basename (without dir) of the rng file relying on the rng lookup mechanism | ||
searchDirs.unshift(rngDir) | ||
const rngFileBase = path.basename(rngFile) | ||
return compileRNG(fs, searchDirs, rngFileBase) | ||
return _compileRNG(fs, path, rngFile, searchDirs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import { DefaultDOMElement, isArray } from 'substance' | ||
import _expandIncludes from './_expandIncludes' | ||
import _lookupRNG from './_lookupRNG' | ||
|
||
/* | ||
Loads a RNG with all dependencies into a DOM element | ||
*/ | ||
export default function _loadRNG (fs, searchDirs, rngFile) { | ||
export default function _loadRNG (fs, path, rngFile, searchDirs) { | ||
if (!isArray(searchDirs)) searchDirs = [searchDirs] | ||
let rngPath = _lookupRNG(fs, searchDirs, rngFile) | ||
if (!rngPath) throw new Error('Could not resolve RNG: ' + rngFile) | ||
let rngStr = fs.readFileSync(rngPath, 'utf8') | ||
let rngDir = path.dirname(rngFile) | ||
let rngStr = fs.readFileSync(rngFile, 'utf8') | ||
const rng = DefaultDOMElement.parseXML(rngStr, 'full-doc') | ||
// first pull in all includes (recursively) | ||
while (_expandIncludes(fs, searchDirs, rng)) { /* nothing */ } | ||
const grammarEl = rng.find('grammar') | ||
_expandIncludes(fs, path, rngDir, searchDirs, grammarEl) | ||
return rng | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,28 @@ | ||
export default function _lookupRNG (fs, searchDirs, file) { | ||
/** | ||
* Look up a RNG file from the current directory or a list of search directories. | ||
* | ||
* @param {*} fs | ||
* @param {*} rngFileName | ||
* @param {*} currentDir | ||
* @param {*} searchDirs | ||
*/ | ||
export default function _lookupRNG (fs, rngFileName, currentDir, searchDirs) { | ||
let rngPath | ||
// 1. Try if the file can be found directly | ||
rngPath = rngFileName | ||
if (fs.existsSync(rngPath)) { | ||
return rngPath | ||
} | ||
// 2. Try the current directory | ||
rngPath = currentDir + '/' + rngFileName | ||
if (fs.existsSync(rngPath)) { | ||
return rngPath | ||
} | ||
// 3. Try the search directories | ||
for (let i = 0; i < searchDirs.length; i++) { | ||
let absPath = searchDirs[i] + '/' + file | ||
if (fs.existsSync(absPath)) { | ||
return absPath | ||
rngPath = searchDirs[i] + '/' + rngFileName | ||
if (fs.existsSync(rngPath)) { | ||
return rngPath | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters