Skip to content

Commit

Permalink
Merge pull request #59 from brianorwhatever/fix-refs
Browse files Browse the repository at this point in the history
External Specification References
  • Loading branch information
csuwildcat authored Mar 31, 2024
2 parents b593633 + 6fd3d2c commit 1e46f99
Show file tree
Hide file tree
Showing 13 changed files with 766 additions and 3,490 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fonts

# Logs
logs
*.log
Expand Down
2 changes: 1 addition & 1 deletion assets/compiled/body.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/compiled/refs.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ document.querySelectorAll('.chartjs').forEach(chart => {
/* Tooltips */
let tipMap = new WeakMap();
delegateEvent('pointerover', '.term-reference, .spec-reference', (e, anchor) => {
let term = document.getElementById((anchor.getAttribute('href') || '').replace('#', ''));
const id = anchor.getAttribute('data-local-href') || anchor.getAttribute('href') || '';
let term = document.getElementById(id.replace('#', ''));
if (!term || tipMap.has(anchor)) return;
let container = term.closest('dt, td:first-child');
if (!container) return;
Expand Down
6 changes: 3 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ let compileLocation = 'assets/compiled';

async function fetchSpecRefs(){
return Promise.all([
axios.get('https://ghcdn.rawgit.org/tobie/specref/master/refs/ietf.json'),
axios.get('https://ghcdn.rawgit.org/tobie/specref/master/refs/w3c.json'),
axios.get('https://ghcdn.rawgit.org/tobie/specref/master/refs/whatwg.json')
axios.get('https://raw.githubusercontent.com/tobie/specref/master/refs/ietf.json'),
axios.get('https://raw.githubusercontent.com/tobie/specref/master/refs/w3c.json'),
axios.get('https://raw.githubusercontent.com/tobie/specref/master/refs/whatwg.json')
]).then(async results => {
let json = Object.assign(results[0].data, results[1].data, results[2].data);
return fs.outputFile(compileLocation + '/refs.json', JSON.stringify(json));
Expand Down
122 changes: 114 additions & 8 deletions index.html

Large diffs are not rendered by default.

74 changes: 50 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@

module.exports = function(options = {}) {

const fs = require('fs-extra');
const {
fetchExternalSpecs,
validateReferences,
findExternalSpecByKey
} = require('./references.js');
const gulp = require('gulp');
const fs = require('fs-extra');
const path = require('path');
const findPkgDir = require('find-pkg-dir');
const modulePath = findPkgDir(__dirname);
let config = fs.readJsonSync('./specs.json');
let assets = fs.readJsonSync(modulePath + '/src/asset-map.json');
let externalReferences;
let references = [];
let definitions = [];

const katexRules = ['math_block', 'math_inline']
const replacerRegex = /\[\[\s*([^\s\[\]:]+):?\s*([^\]\n]+)?\]\]/img;
Expand Down Expand Up @@ -75,7 +84,7 @@ module.exports = function(options = {}) {
};
const spaceRegex = /\s+/g;
const specNameRegex = /^spec$|^spec[-]*\w+$/i;
const terminologyRegex = /^def$|^ref/i;
const terminologyRegex = /^def$|^ref$|^xref/i;
const specCorpus = fs.readJsonSync(modulePath + '/assets/compiled/refs.json');
const containers = require('markdown-it-container');
const md = require('markdown-it')({
Expand All @@ -84,6 +93,28 @@ module.exports = function(options = {}) {
typographer: true
})
.use(require('./src/markdown-it-extensions.js'), [
{
filter: type => type.match(terminologyRegex),
parse(token, type, primary){
if (!primary) return;
if (type === 'def'){
definitions.push(token.info.args);
return token.info.args.reduce((acc, syn) => {
return `<span id="term:${syn.replace(spaceRegex, '-').toLowerCase()}">${acc}</span>`;
}, primary);
}
else if (type === 'xref') {
const url = findExternalSpecByKey(config, token.info.args[0]);
const term = token.info.args[1].replace(spaceRegex, '-').toLowerCase();
return `<a class="term-reference" data-local-href="#term:${token.info.args[0]}:${term}"
href="${url}#term:${term}">${token.info.args[1]}</a>`;
}
else {
references.push(primary);
return `<a class="term-reference" href="#term:${primary.replace(spaceRegex, '-').toLowerCase()}">${primary}</a>`;
}
}
},
{
filter: type => type.match(specNameRegex),
parse(token, type, name){
Expand All @@ -107,20 +138,6 @@ module.exports = function(options = {}) {
}
else return renderRefGroup(type);
}
},
{
filter: type => type.match(terminologyRegex),
parse(token, type, primary){
if (!primary) return;
if (type === 'def'){
return token.info.args.reduce((acc, syn) => {
return `<span id="term:${syn.replace(spaceRegex, '-').toLowerCase()}">${acc}</span>`;
}, primary);
}
else {
return `<a class="term-reference" href="#term:${primary.replace(spaceRegex, '-').toLowerCase()}">${primary}</a>`;
}
}
}
])
.use(require('markdown-it-attrs'))
Expand Down Expand Up @@ -177,13 +194,17 @@ module.exports = function(options = {}) {
specGroups = {};
console.log('Rendering: ' + spec.title);
return new Promise(async (resolve, reject) => {
Promise.all((spec.markdown_paths || ['spec.md']).map(path => {
return fs.readFile(spec.spec_directory + path, 'utf8').catch(e => reject(e))
Promise.all((spec.markdown_paths || ['spec.md']).map(_path => {
return fs.readFile(spec.spec_directory + _path, 'utf8').catch(e => reject(e))
})).then(async docs => {
const features = (({ source, logo }) => ({ source, logo }))(spec);
if (spec.external_specs && !externalReferences) {
externalReferences = await fetchExternalSpecs(spec);
}
let doc = docs.join("\n");
doc = applyReplacers(doc);
md[spec.katex ? "enable" : "disable"](katexRules);
const render = md.render(doc);
fs.writeFile(path.join(spec.destination, 'index.html'), `
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -217,7 +238,7 @@ module.exports = function(options = {}) {
</header>
<article id="content">
${md.render(doc)}
${render}
</article>
</main>
Expand Down Expand Up @@ -245,7 +266,9 @@ module.exports = function(options = {}) {
</slide-panel>
</slide-panels>
<div style="display: none;">
${externalReferences}
</div>
</body>
<script>window.specConfig = ${JSON.stringify(spec)}</script>
${assets.body}
Expand All @@ -257,7 +280,10 @@ module.exports = function(options = {}) {
else {
resolve();
}
});
});
validateReferences(references, definitions, render);
references = [];
definitions = [];
});
});
}
Expand Down Expand Up @@ -291,11 +317,11 @@ module.exports = function(options = {}) {
});

if (options.dev) {
assetTags.head = assets.head.css.map(path => `<link href="${path}" rel="stylesheet"/>`).join('') +
assetTags.head = assets.head.css.map(_path => `<link href="${_path}" rel="stylesheet"/>`).join('') +
customAssets.css +
assets.head.js.map(path => `<script src="${path}"></script>`).join('') +
assets.head.js.map(_path => `<script src="${_path}"></script>`).join('') +
customAssets.js.head;
assetTags.body = assets.body.js.map(path => `<script src="${path}" data-manual></script>`).join('') +
assetTags.body = assets.body.js.map(_path => `<script src="${_path}" data-manual></script>`).join('') +
customAssets.js.body;
}
else {
Expand Down
6 changes: 4 additions & 2 deletions multi-file-test/index.html

Large diffs are not rendered by default.

Loading

0 comments on commit 1e46f99

Please sign in to comment.