Skip to content

Commit

Permalink
Support alternative example sources for forks and local repos
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed Nov 14, 2023
1 parent 9a49606 commit 0a4298b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/js/musicxml-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const options = {
},
'validate': {
type: 'boolean'
},
'source': {
type: 'string',
short: 's'
}
}
const { values: args } = (function() {
Expand All @@ -55,12 +59,15 @@ const { values: args } = (function() {
process.exit(1)
}
})()
if (!('source' in args)) {
args['source'] = URL_EXAMPLES_ROOT
}

if ('help' in args) {
console.log(`
Usage: musicxml-examples v${version} [--output|-o /path/to/output] [--example|-e example-slug] [--xml|-x] [--validate] [--version|-v] [--help|-h]
Usage: musicxml-examples v${version} [--output|-o /path/to/output] [--example|-e example-slug] [--source|-s url/or/path/to/examples/] [--xml|-x] [--validate] [--version|-v] [--help|-h]
Extracts MusicXML examples from ${URL_EXAMPLES_ROOT}.
Extracts MusicXML examples from ${args['source']}.
Use --xml to recreate a valid MusicXML structure around examples that lack it.
`.trim())
process.exit(0)
Expand All @@ -77,19 +84,17 @@ if (output !== '' && !fs.existsSync(output)) {
process.exit(1)
}

const response = await fetch(URL_EXAMPLES_ROOT)
const main = await response.text()
const main = await fetchPage(args['source'])
const $ = cheerio.load(main)
for (const example of $('body').find('a:has(img)')) {
const href = $(example).prop('href')
if ('example' in args && args['example'] !== href.replace('/', '')) continue
console.error(`Extracting ${href}...`)
await extractMusicXml(URL_EXAMPLES_ROOT + href, href.replace('/', ''))
await extractMusicXml(args['source'] + href, href.replace('/', ''))
}

async function extractMusicXml(page, slug) {
const response = await fetch(page)
const body = await response.text()
const body = await fetchPage(page)
const $ = cheerio.load(body)
const musicxml = scaffoldMusicXml($('.xmlmarkup').text())

Expand All @@ -108,6 +113,14 @@ async function extractMusicXml(page, slug) {
}
}

async function fetchPage(url) {
if (/^http/.test(url)) {
const response = await fetch(url)
return await response.text()
}
return fs.readFileSync(path.join(url, 'index.html'), { encoding: 'utf-8' })
}

function scaffoldMusicXml(xml) {
if (!('xml' in args)) {
return `<?xml version="1.0" encoding="utf-8"?>\n${xml}`
Expand Down

0 comments on commit 0a4298b

Please sign in to comment.