jinja_svg_catalog.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import fs from "fs";
  2. import { resolve, dirname } from "path";
  3. import { Edge } from 'edge.js';
  4. import { optimize as svgo } from "svgo";
  5. import { fileURLToPath } from 'url';
  6. const __dirname = dirname(fileURLToPath(import.meta.url));
  7. const __jinja_class_placeholder__ = "__jinja_class_placeholder__";
  8. // -- types
  9. /**
  10. * @typedef {object} IconSet - A set of icons
  11. * @property {object[]} set - Array of SVG icons, where property name is the
  12. * name of the icon and value is the src of the SVG (relative to base).
  13. * @property {string} base - Folder in which the SVG src files are located.
  14. * @property {import("svgo").Config} svgo_opts - svgo options for this set.
  15. */
  16. /**
  17. * @typedef {object} IconSVG - Mapping of icon name to SVG source file.
  18. * @property {string} name - Name of the icon isource file.
  19. * @property {string} src - Name of the destination file.
  20. * @property {import("svgo").Config} svgo_opts - Options passed to svgo.
  21. */
  22. /**
  23. * @typedef {object} JinjaMacro - Arguments to create a jinja macro
  24. * @property {string} name - Name of the jinja macro.
  25. * @property {string} class - SVG's class name (value of XML class attribute)
  26. */
  27. // -- functions
  28. /**
  29. * Generate a jinja template with a catalog of SVG icons that can be
  30. * used in in other HTML jinja templates.
  31. *
  32. * @param {string} dest - filename of the generate jinja template.
  33. * @param {JinjaMacro} macros - Jinja macros to create.
  34. * @param {IconSVG[]} items - Array of SVG items.
  35. */
  36. function jinja_svg_catalog(dest, macros, items) {
  37. const svg_catalog = {};
  38. const edge_template = resolve(__dirname, "jinja_svg_catalog.html.edge");
  39. items.forEach(
  40. (item) => {
  41. /** @type {import("svgo").Config} */
  42. const svgo_opts = JSON.parse(JSON.stringify(item.svgo_opts));
  43. svgo_opts.plugins.push({
  44. name: "addAttributesToSVGElement",
  45. params: {
  46. attributes: [{ "class": __jinja_class_placeholder__, }]
  47. }}
  48. );
  49. try {
  50. const raw = fs.readFileSync(item.src, "utf8");
  51. const opt = svgo(raw, svgo_opts);
  52. svg_catalog[item.name] = opt.data;
  53. } catch (err) {
  54. console.error(`ERROR: jinja_svg_catalog processing ${item.name} src: ${item.src} -- ${err}`);
  55. throw(err);
  56. }
  57. }
  58. );
  59. fs.mkdir(dirname(dest), { recursive: true }, (err) => {
  60. if (err) throw err;
  61. });
  62. const ctx = {
  63. svg_catalog: svg_catalog,
  64. macros: macros,
  65. edge_template: edge_template,
  66. __jinja_class_placeholder__: __jinja_class_placeholder__,
  67. // see https://github.com/edge-js/edge/issues/162
  68. open_curly_brace : "{{",
  69. close_curly_brace : "}}"
  70. };
  71. const jinjatmpl = Edge.create().renderRawSync(
  72. fs.readFileSync(edge_template, "utf-8"),
  73. ctx
  74. );
  75. fs.writeFileSync(dest, jinjatmpl);
  76. console.log(`[jinja_svg_catalog] created: ${dest}`);
  77. }
  78. /**
  79. * Calls jinja_svg_catalog for a collection of icon sets where each set has its
  80. * own parameters.
  81. *
  82. * @param {string} dest - filename of the generate jinja template.
  83. * @param {JinjaMacro} macros - Jinja macros to create.
  84. * @param {IconSet[]} sets - Array of SVG sets.
  85. */
  86. function jinja_svg_sets(dest, macros, sets) {
  87. /** @type IconSVG[] */
  88. const items = [];
  89. const all = [];
  90. for (const obj of sets) {
  91. for (const [name, file] of Object.entries(obj.set)) {
  92. if (all.includes(name)) {
  93. throw new Error(`ERROR: ${name} has already been defined`);
  94. }
  95. items.push({
  96. name: name,
  97. src: resolve(obj.base, file),
  98. svgo_opts: obj.svgo_opts,
  99. });
  100. }
  101. jinja_svg_catalog(dest, macros, items);
  102. }
  103. }
  104. // -- exports
  105. export {
  106. jinja_svg_sets,
  107. jinja_svg_catalog,
  108. };