Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decode.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var entityMap = require("../maps/entities.json"),
  2. legacyMap = require("../maps/legacy.json"),
  3. xmlMap = require("../maps/xml.json"),
  4. decodeCodePoint = require("./decode_codepoint.js");
  5. var decodeXMLStrict = getStrictDecoder(xmlMap),
  6. decodeHTMLStrict = getStrictDecoder(entityMap);
  7. function getStrictDecoder(map) {
  8. var keys = Object.keys(map).join("|"),
  9. replace = getReplacer(map);
  10. keys += "|#[xX][\\da-fA-F]+|#\\d+";
  11. var re = new RegExp("&(?:" + keys + ");", "g");
  12. return function(str) {
  13. return String(str).replace(re, replace);
  14. };
  15. }
  16. var decodeHTML = (function() {
  17. var legacy = Object.keys(legacyMap).sort(sorter);
  18. var keys = Object.keys(entityMap).sort(sorter);
  19. for (var i = 0, j = 0; i < keys.length; i++) {
  20. if (legacy[j] === keys[i]) {
  21. keys[i] += ";?";
  22. j++;
  23. } else {
  24. keys[i] += ";";
  25. }
  26. }
  27. var re = new RegExp("&(?:" + keys.join("|") + "|#[xX][\\da-fA-F]+;?|#\\d+;?)", "g"),
  28. replace = getReplacer(entityMap);
  29. function replacer(str) {
  30. if (str.substr(-1) !== ";") str += ";";
  31. return replace(str);
  32. }
  33. //TODO consider creating a merged map
  34. return function(str) {
  35. return String(str).replace(re, replacer);
  36. };
  37. })();
  38. function sorter(a, b) {
  39. return a < b ? 1 : -1;
  40. }
  41. function getReplacer(map) {
  42. return function replace(str) {
  43. if (str.charAt(1) === "#") {
  44. if (str.charAt(2) === "X" || str.charAt(2) === "x") {
  45. return decodeCodePoint(parseInt(str.substr(3), 16));
  46. }
  47. return decodeCodePoint(parseInt(str.substr(2), 10));
  48. }
  49. return map[str.slice(1, -1)];
  50. };
  51. }
  52. module.exports = {
  53. XML: decodeXMLStrict,
  54. HTML: decodeHTML,
  55. HTMLStrict: decodeHTMLStrict
  56. };