|
| 1 | +/* Web Payments Community Group common spec JavaScript */ |
| 2 | +var jsonld = { |
| 3 | + // Add as the respecConfig localBiblio variable |
| 4 | + // Extend or override global respec references |
| 5 | + localBiblio: { |
| 6 | + "JSON-LD11": { |
| 7 | + title: "JSON-LD 1.1", |
| 8 | + href: "https://w3c.github.io/json-ld-syntax/", |
| 9 | + authors: ["Gregg Kellogg"], |
| 10 | + publisher: "W3C", |
| 11 | + status: 'ED' |
| 12 | + }, |
| 13 | + "JSON-LD11-API": { |
| 14 | + title: "JSON-LD 1.1 Processing Algorithms and API", |
| 15 | + href: "https://w3c.github.io/json-ld-api/", |
| 16 | + authors: ["Gregg Kellogg"], |
| 17 | + publisher: "W3C", |
| 18 | + status: 'ED' |
| 19 | + }, |
| 20 | + "JSON-LD11-FRAMING": { |
| 21 | + title: "JSON-LD 1.1 Framing", |
| 22 | + href: "https://w3c.github.io/json-ld-framing/", |
| 23 | + authors: ["Gregg Kellogg"], |
| 24 | + publisher: "W3C", |
| 25 | + status: 'ED' |
| 26 | + }, |
| 27 | + "JSON-LD-TESTS": { |
| 28 | + title: "JSON-LD 1.1 Test Suite", |
| 29 | + href: "https://json-ld.org/test-suite/", |
| 30 | + authors: ["Gregg Kellogg"], |
| 31 | + publisher: "Linking Data in JSON Community Group" |
| 32 | + }, |
| 33 | + // aliases to known references |
| 34 | + "IEEE-754-2008": { |
| 35 | + title: "IEEE 754-2008 Standard for Floating-Point Arithmetic", |
| 36 | + href: "http://standards.ieee.org/findstds/standard/754-2008.html", |
| 37 | + publisher: "Institute of Electrical and Electronics Engineers", |
| 38 | + date: "2008" |
| 39 | + }, |
| 40 | + "PROMISES": { |
| 41 | + title: 'Promise Objects', |
| 42 | + href: 'https://github.com/domenic/promises-unwrapping', |
| 43 | + authors: ['Domenic Denicola'], |
| 44 | + status: 'unofficial', |
| 45 | + date: 'January 2014' |
| 46 | + }, |
| 47 | + "MICROFORMATS": { |
| 48 | + title: "Microformats", |
| 49 | + href: "http://microformats.org" |
| 50 | + } |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +// We should be able to remove terms that are not actually |
| 55 | +// referenced from the common definitions |
| 56 | +// |
| 57 | +// Add class "preserve" to a definition to ensure it is not removed. |
| 58 | +// |
| 59 | +// the termlist is in a block of class "termlist", so make sure that |
| 60 | +// has an ID and put that ID into the termLists array so we can |
| 61 | +// interrogate all of the included termlists later. |
| 62 | +var termNames = [] ; |
| 63 | +var termLists = [] ; |
| 64 | +var termsReferencedByTerms = [] ; |
| 65 | + |
| 66 | +function restrictReferences(utils, content) { |
| 67 | + var base = document.createElement("div"); |
| 68 | + base.innerHTML = content; |
| 69 | + |
| 70 | + // New new logic: |
| 71 | + // |
| 72 | + // 1. build a list of all term-internal references |
| 73 | + // 2. When ready to process, for each reference INTO the terms, |
| 74 | + // remove any terms they reference from the termNames array too. |
| 75 | + $.each(base.querySelectorAll("dfn:not(.preserve)"), function(i, item) { |
| 76 | + var $t = $(item) ; |
| 77 | + var titles = $t.getDfnTitles(); |
| 78 | + var n = $t.makeID("dfn", titles[0]); |
| 79 | + if (n) { |
| 80 | + termNames[n] = $t.parent() ; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + var $container = $(".termlist", base) ; |
| 85 | + var containerID = $container.makeID("", "terms") ; |
| 86 | + termLists.push(containerID) ; |
| 87 | + return (base.innerHTML); |
| 88 | +} |
| 89 | + |
| 90 | +// add a handler to come in after all the definitions are resolved |
| 91 | +// |
| 92 | +// New logic: If the reference is within a 'dl' element of |
| 93 | +// class 'termlist', and if the target of that reference is |
| 94 | +// also within a 'dl' element of class 'termlist', then |
| 95 | +// consider it an internal reference and ignore it. |
| 96 | + |
| 97 | +function internalizeTermListReferences() { |
| 98 | + // all definitions are linked; find any internal references |
| 99 | + $(".termlist a.internalDFN").each(function() { |
| 100 | + var $r = $(this); |
| 101 | + var id = $r.attr('href'); |
| 102 | + var idref = id.replace(/^#/,"") ; |
| 103 | + if (termNames[idref]) { |
| 104 | + // this is a reference to another term |
| 105 | + // what is the idref of THIS term? |
| 106 | + var $def = $r.closest('dd') ; |
| 107 | + if ($def.length) { |
| 108 | + var $p = $def.prev('dt').find('dfn') ; |
| 109 | + var tid = $p.attr('id') ; |
| 110 | + if (tid) { |
| 111 | + if (termsReferencedByTerms[tid]) { |
| 112 | + termsReferencedByTerms[tid].push(idref); |
| 113 | + } else { |
| 114 | + termsReferencedByTerms[tid] = [] ; |
| 115 | + termsReferencedByTerms[tid].push(idref); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + // clearRefs is recursive. Walk down the tree of |
| 123 | + // references to ensure that all references are resolved. |
| 124 | + var clearRefs = function(theTerm) { |
| 125 | + if ( termsReferencedByTerms[theTerm] ) { |
| 126 | + $.each(termsReferencedByTerms[theTerm], function(i, item) { |
| 127 | + if (termNames[item]) { |
| 128 | + delete termNames[item]; |
| 129 | + clearRefs(item); |
| 130 | + } |
| 131 | + }); |
| 132 | + }; |
| 133 | + // make sure this term doesn't get removed |
| 134 | + if (termNames[theTerm]) { |
| 135 | + delete termNames[theTerm]; |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + // now termsReferencedByTerms has ALL terms that |
| 140 | + // reference other terms, and a list of the |
| 141 | + // terms that they reference |
| 142 | + $("a.internalDFN").each(function () { |
| 143 | + var $item = $(this) ; |
| 144 | + var t = $item.attr('href'); |
| 145 | + var r = t.replace(/^#/,"") ; |
| 146 | + if (r === 'dictionary') { |
| 147 | + var rr = r; |
| 148 | + } |
| 149 | + // if the item is outside the term list |
| 150 | + if ( ! $item.closest('dl.termlist').length ) { |
| 151 | + clearRefs(r); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + // delete any terms that were not referenced. |
| 156 | + Object.keys(termNames).forEach(function(term) { |
| 157 | + var $p = $("#"+term) ; |
| 158 | + if ($p) { |
| 159 | + var tList = $p.getDfnTitles(); |
| 160 | + $p.parent().next().remove(); |
| 161 | + $p.remove() ; |
| 162 | + tList.forEach(function( item ) { |
| 163 | + if (respecConfig.definitionMap[item]) { |
| 164 | + delete respecConfig.definitionMap[item]; |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + }); |
| 169 | +} |
| 170 | + |
| 171 | +function _esc(s) { |
| 172 | + s = s.replace(/&/g,'&'); |
| 173 | + s = s.replace(/>/g,'>'); |
| 174 | + s = s.replace(/"/g,'"'); |
| 175 | + s = s.replace(/</g,'<'); |
| 176 | + return s; |
| 177 | +} |
| 178 | + |
| 179 | +function updateExample(doc, content) { |
| 180 | + // perform transformations to make it render and prettier |
| 181 | + content = unComment(doc, content); |
| 182 | + content = _esc(content); |
| 183 | + content = content.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="hl-bold">$1</span>'); |
| 184 | + content = content.replace(/####([^#]*)####/g, '<span class="comment">$1</span>'); |
| 185 | + return content ; |
| 186 | +} |
| 187 | + |
| 188 | + |
| 189 | +function unComment(doc, content) { |
| 190 | + // perform transformations to make it render and prettier |
| 191 | + content = content.replace(/<!--/, ''); |
| 192 | + content = content.replace(/-->/, ''); |
| 193 | + return content ; |
| 194 | +} |
0 commit comments