syntax.js 859 B

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. const stringify = require("./stringify");
  3. const parseStyle = require("./parse-style");
  4. const normalOpts = require("./normal-opts");
  5. module.exports = (extract, lang) => {
  6. const defaultConfig = {
  7. postcss: "css",
  8. stylus: "css",
  9. babel: "jsx",
  10. xml: "html",
  11. };
  12. function parse (source, opts) {
  13. source = source.toString();
  14. opts = normalOpts(opts, this);
  15. const document = parseStyle(source, opts, extract(source, opts));
  16. document.source.lang = lang;
  17. return document;
  18. }
  19. function initSyntax (syntax) {
  20. syntax.stringify = stringify.bind(syntax);
  21. syntax.parse = parse.bind(syntax);
  22. syntax.extract = extract.bind(syntax);
  23. return syntax;
  24. }
  25. function syntax (config) {
  26. return initSyntax({
  27. config: Object.assign({}, defaultConfig, config),
  28. });
  29. }
  30. initSyntax(syntax);
  31. syntax.config = defaultConfig;
  32. return syntax;
  33. };