Skip to content

Extract examples Improve example checking and generate example files #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ GEM
hamster (3.0.0)
concurrent-ruby (~> 1.0)
htmlentities (4.3.4)
i18n (1.0.1)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
json-ld (2.2.1)
json-ld (3.0.0)
multi_json (~> 1.12)
rdf (>= 2.2.8, < 4.0)
json-ld-preloaded (2.2.3)
json-ld-preloaded (3.0.0)
json-ld (>= 2.2, < 4.0)
multi_json (~> 1.12)
rdf (>= 2.2, < 4.0)
rdf (~> 3.0)
ld-patch (0.3.3)
ebnf (~> 1.1)
rdf (>= 2.2, < 4.0)
Expand Down Expand Up @@ -65,11 +65,11 @@ GEM
multi_json (1.13.1)
net-http-persistent (3.0.0)
connection_pool (~> 2.2)
nokogiri (1.8.2)
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
nokogumbo (1.5.0)
nokogiri
public_suffix (3.0.2)
public_suffix (3.0.3)
rake (12.3.1)
rdf (3.0.2)
hamster (~> 3.0)
Expand Down Expand Up @@ -120,7 +120,7 @@ GEM
rdf-turtle (3.0.1)
ebnf (~> 1.1)
rdf (~> 3.0)
rdf-vocab (3.0.2)
rdf-vocab (3.0.3)
rdf (~> 3.0)
rdf-xsd (3.0.0)
rdf (~> 3.0)
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ end

desc "Extract Examples"
task :examples do
sh %(bundle exec common/extract-examples.rb --example-dir examples index.html)
sh %(rm -rf examples yaml trig)
sh %(bundle exec common/extract-examples.rb --example-dir examples --yaml-dir yaml --trig-dir trig index.html)
end
193 changes: 93 additions & 100 deletions common/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Web Payments Community Group common spec JavaScript */
var jsonld = {
const jsonld = {
// Add as the respecConfig localBiblio variable
// Extend or override global respec references
localBiblio: {
Expand Down Expand Up @@ -59,32 +59,33 @@ var jsonld = {
// the termlist is in a block of class "termlist", so make sure that
// has an ID and put that ID into the termLists array so we can
// interrogate all of the included termlists later.
var termNames = [] ;
var termLists = [] ;
var termsReferencedByTerms = [] ;
const termNames = [] ;
const termLists = [] ;
const termsReferencedByTerms = [] ;

function restrictReferences(utils, content) {
var base = document.createElement("div");
base.innerHTML = content;
const base = document.createElement("div");
base.innerHTML = content;

// New new logic:
//
// 1. build a list of all term-internal references
// 2. When ready to process, for each reference INTO the terms,
// remove any terms they reference from the termNames array too.
$.each(base.querySelectorAll("dfn:not(.preserve)"), function(i, item) {
var $t = $(item) ;
var titles = $t.getDfnTitles();
var n = $t.makeID("dfn", titles[0]);
if (n) {
termNames[n] = $t.parent() ;
}
});
// New new logic:
//
// 1. build a list of all term-internal references
// 2. When ready to process, for each reference INTO the terms,
// remove any terms they reference from the termNames array too.
const noPreserve = base.querySelectorAll("dfn:not(.preserve)");
for (const item of noPreserve) {
const $t = $(item) ;
const titles = $t.getDfnTitles();
const n = $t.makeID("dfn", titles[0]);
if (n) {
termNames[n] = $t.parent();
}
}

var $container = $(".termlist", base) ;
var containerID = $container.makeID("", "terms") ;
termLists.push(containerID) ;
return (base.innerHTML);
const $container = $(".termlist", base) ;
const containerID = $container.makeID("", "terms") ;
termLists.push(containerID) ;
return (base.innerHTML);
}

// add a handler to come in after all the definitions are resolved
Expand All @@ -95,100 +96,92 @@ function restrictReferences(utils, content) {
// consider it an internal reference and ignore it.

function internalizeTermListReferences() {
// all definitions are linked; find any internal references
$(".termlist a.internalDFN").each(function() {
var $r = $(this);
var id = $r.attr('href');
var idref = id.replace(/^#/,"") ;
if (termNames[idref]) {
// this is a reference to another term
// what is the idref of THIS term?
var $def = $r.closest('dd') ;
if ($def.length) {
var $p = $def.prev('dt').find('dfn') ;
var tid = $p.attr('id') ;
if (tid) {
if (termsReferencedByTerms[tid]) {
termsReferencedByTerms[tid].push(idref);
} else {
termsReferencedByTerms[tid] = [] ;
termsReferencedByTerms[tid].push(idref);
}
}
}
// all definitions are linked; find any internal references
const internalTerms = document.querySelectorAll(".termlist a.internalDFN");
for (const item of internalTerms) {
const idref = item.getAttribute('href').replace(/^#/,"") ;
if (termNames[idref]) {
// this is a reference to another term
// what is the idref of THIS term?
const def = item.closest('dd');
if (def) {
const tid = def.previousElementSibling
.querySelector('dfn')
.getAttribute('id');
if (tid) {
if (termsReferencedByTerms[tid]) {
termsReferencedByTerms[tid].push(idref);
} else {
termsReferencedByTerms[tid] = [] ;
termsReferencedByTerms[tid].push(idref);
}
}
});
}
}
}

// clearRefs is recursive. Walk down the tree of
// references to ensure that all references are resolved.
var clearRefs = function(theTerm) {
if ( termsReferencedByTerms[theTerm] ) {
$.each(termsReferencedByTerms[theTerm], function(i, item) {
if (termNames[item]) {
delete termNames[item];
clearRefs(item);
}
});
};
// make sure this term doesn't get removed
if (termNames[theTerm]) {
delete termNames[theTerm];
// clearRefs is recursive. Walk down the tree of
// references to ensure that all references are resolved.
const clearRefs = function(theTerm) {
if ( termsReferencedByTerms[theTerm] ) {
for (const item of termsReferencedByTerms[theTerm]) {
if (termNames[item]) {
delete termNames[item];
clearRefs(item);
}
}
};
// make sure this term doesn't get removed
if (termNames[theTerm]) {
delete termNames[theTerm];
}
};

// now termsReferencedByTerms has ALL terms that
// reference other terms, and a list of the
// terms that they reference
$("a.internalDFN").each(function () {
var $item = $(this) ;
var t = $item.attr('href');
var r = t.replace(/^#/,"") ;
if (r === 'dictionary') {
var rr = r;
}
// if the item is outside the term list
if ( ! $item.closest('dl.termlist').length ) {
clearRefs(r);
}
});
// now termsReferencedByTerms has ALL terms that
// reference other terms, and a list of the
// terms that they reference
const internalRefs = document.querySelectorAll("a.internalDFN");
for (const item of internalRefs) {
const idref = item.getAttribute('href').replace(/^#/,"") ;
// if the item is outside the term list
if ( !item.closest('dl.termlist') ) {
clearRefs(idref);
}
}

// delete any terms that were not referenced.
Object.keys(termNames).forEach(function(term) {
var $p = $("#"+term) ;
if ($p) {
var tList = $p.getDfnTitles();
$p.parent().next().remove();
$p.remove() ;
tList.forEach(function( item ) {
if (respecConfig.definitionMap[item]) {
delete respecConfig.definitionMap[item];
}
});
// delete any terms that were not referenced.
for (const term of Object.keys(termNames)) {
const $p = $("#"+term) ;
if ($p && !$p.empty()) {
const tList = $p.getDfnTitles();
$p.parent().next().remove();
$p.remove() ;
for (const item of tList) {
if (respecConfig.definitionMap[item]) {
delete respecConfig.definitionMap[item];
}
});
}
}
}
}

function _esc(s) {
s = s.replace(/&/g,'&amp;');
s = s.replace(/>/g,'&gt;');
s = s.replace(/"/g,'&quot;');
s = s.replace(/</g,'&lt;');
return s;
return s.replace(/&/g,'&amp;')
.replace(/>/g,'&gt;')
.replace(/"/g,'&quot;')
.replace(/</g,'&lt;');
}

function updateExample(doc, content) {
// perform transformations to make it render and prettier
content = unComment(doc, content);
content = _esc(content);
content = content.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="hl-bold">$1</span>');
content = content.replace(/####([^#]*)####/g, '<span class="comment">$1</span>');
return content ;
return _esc(unComment(doc, content))
.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="hl-bold">$1</span>')
.replace(/####([^#]*)####/g, '<span class="comment">$1</span>');
}


function unComment(doc, content) {
// perform transformations to make it render and prettier
content = content.replace(/<!--/, '');
content = content.replace(/-->/, '');
return content ;
return content.replace(/<!--/, '')
.replace(/-->/, '');
}
Loading