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.

extract.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. const htmlparser = require("htmlparser2");
  3. const loadSyntax = require("postcss-syntax/load-syntax");
  4. function iterateCode (source, onStyleTag, onStyleAttribute) {
  5. const openTag = {};
  6. let disable;
  7. let style;
  8. const parser = new htmlparser.Parser({
  9. oncomment: (data) => {
  10. if (!/(?:^|\s+)postcss-(\w+)(?:\s+|$)/i.test(data)) {
  11. return;
  12. }
  13. data = RegExp.$1.toLowerCase();
  14. if (data === "enable") {
  15. disable = false;
  16. } else if (data === "disable") {
  17. disable = true;
  18. }
  19. },
  20. onopentag (name, attribute) {
  21. openTag[name] = true;
  22. // Test if current tag is a valid <style> tag.
  23. if (!/^style$/i.test(name)) {
  24. return;
  25. }
  26. style = {
  27. inXsls: openTag["xsl:stylesheet"],
  28. inXslt: openTag["xsl:template"],
  29. inHtml: openTag.html,
  30. tagName: name,
  31. attribute,
  32. startIndex: parser.endIndex + 1,
  33. };
  34. },
  35. onclosetag (name) {
  36. openTag[name] = false;
  37. if (disable || !style || name !== style.tagName) {
  38. return;
  39. }
  40. let content = source.slice(style.startIndex, parser.startIndex);
  41. const firstNewLine = /^[ \t]*\r?\n/.exec(content);
  42. if (firstNewLine) {
  43. const offset = firstNewLine[0].length;
  44. style.startIndex += offset;
  45. content = content.slice(offset);
  46. }
  47. style.content = content.replace(/[ \t]*$/, "");
  48. onStyleTag(style);
  49. style = null;
  50. },
  51. onattribute (name, content) {
  52. if (disable || name !== "style") {
  53. return;
  54. }
  55. const endIndex = parser._tokenizer._index;
  56. const startIndex = endIndex - content.length;
  57. if (source[startIndex - 1] !== source[endIndex] || !/\S/.test(source[endIndex])) {
  58. return;
  59. }
  60. onStyleAttribute({
  61. content,
  62. startIndex,
  63. inline: true,
  64. inTemplate: openTag.template,
  65. });
  66. },
  67. });
  68. parser.parseComplete(source);
  69. }
  70. function getSubString (str, regexp) {
  71. const subStr = str && regexp.exec(str);
  72. if (subStr) {
  73. return subStr[1].toLowerCase();
  74. }
  75. }
  76. function getLang (attribute) {
  77. return getSubString(attribute.type, /^\w+\/(?:x-)?(\w+)$/i) || getSubString(attribute.lang, /^(\w+)(?:\?.+)?$/) || "css";
  78. }
  79. function htmlParser (source, opts, styles) {
  80. styles = styles || [];
  81. const standard = opts.from && /\.(?:\w*html?|xht|xslt?|jsp|aspx?|ejs|php\d*|twig|liquid|m(?:ark)?d(?:ow)?n|mk?d)$/i.test(opts.from);
  82. function onStyleTag (style) {
  83. if (!(style.inHtml || style.inXsls || style.inXslt || standard) && (style.attribute.src || style.attribute.href) && !style.content.trim()) {
  84. return;
  85. }
  86. style.lang = getLang(style.attribute);
  87. styles.push(style);
  88. }
  89. function onStyleAttribute (style) {
  90. if (/{[\s\S]*?}/g.test(style.content)) {
  91. style.syntax = loadSyntax(opts, __dirname);
  92. style.lang = "custom-template";
  93. } else {
  94. // style.ignoreErrors = opts.from && /\.(?:jsp|aspx?|ejs|php\d*|twig)$/i.test(opts.from);
  95. style.lang = "css";
  96. }
  97. styles.push(style);
  98. }
  99. iterateCode(source, onStyleTag, onStyleAttribute);
  100. return styles;
  101. }
  102. module.exports = htmlParser;