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.

parse-style.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. const reNewLine = /(?:\r?\n|\r)/gm;
  3. const Input = require("postcss/lib/input");
  4. const Document = require("./document");
  5. const getSyntax = require("./get-syntax");
  6. const patch = require("./patch-postcss");
  7. class LocalFixer {
  8. constructor (lines, style) {
  9. let line = 0;
  10. let column = style.startIndex;
  11. lines.some((lineEndIndex, lineNumber) => {
  12. if (lineEndIndex >= style.startIndex) {
  13. line = lineNumber--;
  14. if (lineNumber in lines) {
  15. column = style.startIndex - lines[lineNumber] - 1;
  16. }
  17. return true;
  18. }
  19. });
  20. this.line = line;
  21. this.column = column;
  22. this.style = style;
  23. }
  24. object (object) {
  25. if (object) {
  26. if (object.line === 1) {
  27. object.column += this.column;
  28. }
  29. object.line += this.line;
  30. }
  31. }
  32. node (node) {
  33. this.object(node.source.start);
  34. this.object(node.source.end);
  35. }
  36. root (root) {
  37. this.node(root);
  38. root.walk(node => {
  39. this.node(node);
  40. });
  41. }
  42. error (error) {
  43. if (error && error.name === "CssSyntaxError") {
  44. this.object(error);
  45. this.object(error.input);
  46. error.message = error.message.replace(/:\d+:\d+:/, ":" + error.line + ":" + error.column + ":");
  47. }
  48. return error;
  49. }
  50. parse (opts) {
  51. const style = this.style;
  52. const syntax = style.syntax || getSyntax(style.lang, opts);
  53. let root = style.root;
  54. try {
  55. root = syntax.parse(style.content, Object.assign({}, opts, {
  56. map: false,
  57. }, style.opts));
  58. } catch (error) {
  59. if (style.ignoreErrors) {
  60. return;
  61. } else if (!style.skipConvert) {
  62. this.error(error);
  63. }
  64. throw error;
  65. }
  66. if (!style.skipConvert) {
  67. this.root(root);
  68. }
  69. root.source.inline = Boolean(style.inline);
  70. root.source.lang = style.lang;
  71. root.source.syntax = syntax;
  72. return root;
  73. }
  74. }
  75. function docFixer (source, opts) {
  76. let match;
  77. const lines = [];
  78. reNewLine.lastIndex = 0;
  79. while ((match = reNewLine.exec(source))) {
  80. lines.push(match.index);
  81. }
  82. lines.push(source.length);
  83. return function parseStyle (style) {
  84. return new LocalFixer(lines, style).parse(opts);
  85. };
  86. }
  87. function parseStyle (source, opts, styles) {
  88. patch(Document);
  89. const document = new Document();
  90. let index = 0;
  91. if (styles.length) {
  92. const parseStyle = docFixer(source, opts);
  93. styles.sort((a, b) => (
  94. a.startIndex - b.startIndex
  95. )).forEach(style => {
  96. const root = parseStyle(style);
  97. if (root) {
  98. root.raws.beforeStart = source.slice(index, style.startIndex);
  99. if (style.endIndex) {
  100. index = style.endIndex;
  101. } else {
  102. index = style.startIndex + (style.content || root.source.input.css).length;
  103. }
  104. root.document = document;
  105. document.nodes.push(root);
  106. }
  107. });
  108. }
  109. document.raws.afterEnd = index ? source.slice(index) : source;
  110. document.source = {
  111. input: new Input(source, opts),
  112. start: {
  113. line: 1,
  114. column: 1,
  115. },
  116. opts,
  117. };
  118. return document;
  119. }
  120. module.exports = parseStyle;