preprocess.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Generate html files, target-dependent
  3. * Context for embedding page is loaded from markdown file
  4. */
  5. const process = require('process');
  6. const path = require('path');
  7. const fs = require('fs');
  8. const posthtml = require('posthtml');
  9. const expressions = require('posthtml-expressions');
  10. const marked = require('marked');
  11. const config = require('../build.json');
  12. const target = process.env.NODE_ENV || 'development';
  13. // Change current directory to script location so that
  14. // the relative paths of the resources can be resolved
  15. process.chdir(path.dirname(process.argv[1]));
  16. function createDirectory(dir) {
  17. fs.mkdirSync(
  18. dir,
  19. { recursive: true },
  20. (error) => {
  21. if (error !== null) {
  22. console.error('Error while creating directory', error);
  23. }
  24. }
  25. );
  26. }
  27. function createVisualisationHtml() {
  28. const visualisation = fs.readFileSync(
  29. path.join(
  30. '../',
  31. config.path.input,
  32. config.path.html,
  33. config.path.skeletons,
  34. config.html.visualisation.skeleton
  35. ), 'utf8'
  36. );
  37. // "D-dis is insanity, Max!"
  38. // Generate html file for the visualisation
  39. posthtml(
  40. expressions({
  41. locals: {
  42. javascript: path.join(config.path.js, `${config.main}${config[target].infix}.js`),
  43. stylesheet: path.join(config.path.css, `${config.main}${config[target].infix}.css`)
  44. }
  45. })
  46. ).process(visualisation)
  47. .then((result) => {
  48. fs.writeFileSync(
  49. path.join(
  50. '../',
  51. config.path.build,
  52. config.html.visualisation.name
  53. ), result.html
  54. );
  55. });
  56. }
  57. function createContextHtml() {
  58. // Load context from markdown file
  59. const md = fs.readFileSync(
  60. path.join(
  61. '../',
  62. config.path.input,
  63. config.path.html,
  64. config.path.markdown,
  65. config.context
  66. ), 'utf8');
  67. // Convert markdown to html
  68. var converted = marked(md);
  69. // Load html skeleton
  70. const skeleton = fs.readFileSync(
  71. path.join(
  72. '../',
  73. config.path.input,
  74. config.path.html,
  75. config.path.skeletons,
  76. config.html.index.skeleton
  77. ), 'utf8');
  78. // Compile html
  79. // 1: Assign variables to iframe in context
  80. // 2: Place generated html from (1) into skeleton
  81. // write file
  82. posthtml(
  83. expressions({
  84. locals: {
  85. d3: config.html.visualisation.name,
  86. width: `${config.width}px`,
  87. height: `${config.height}px`
  88. }
  89. })
  90. ).process(converted)
  91. .then((result) => posthtml(expressions({locals: { content: result.html }}))
  92. .process(skeleton)
  93. .then((result) => {
  94. fs.writeFileSync(
  95. path.join(
  96. '../',
  97. config.path.build,
  98. config.html.index.name
  99. ), result.html
  100. );
  101. }));
  102. }
  103. function createHtml() {
  104. createVisualisationHtml();
  105. createContextHtml();
  106. }
  107. createDirectory(path.join('../', config.path.build));
  108. createDirectory(path.join('../', config.path.output));
  109. createHtml();