preprocess.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Generate html file for the visualisation
  38. posthtml(
  39. expressions({
  40. locals: {
  41. javascript: path.join(config.path.js, `${config.main}${config[target].infix}.js`),
  42. stylesheet: path.join(config.path.css, `${config.main}${config[target].infix}.css`)
  43. }
  44. })
  45. ).process(visualisation)
  46. .then((result) => {
  47. fs.writeFileSync(
  48. path.join(
  49. '../',
  50. config.path.output,
  51. config.html.visualisation.name
  52. ), result.html
  53. );
  54. });
  55. }
  56. function createContextHtml() {
  57. // Load context from markdown file
  58. const md = fs.readFileSync(
  59. path.join(
  60. '../',
  61. config.path.input,
  62. config.path.html,
  63. config.path.markdown,
  64. config.context
  65. ), 'utf8');
  66. // Convert markdown to html
  67. var converted = marked(md);
  68. // Load html skeleton
  69. const skeleton = fs.readFileSync(
  70. path.join(
  71. '../',
  72. config.path.input,
  73. config.path.html,
  74. config.path.skeletons,
  75. config.html.index.skeleton
  76. ), 'utf8');
  77. // Compile html
  78. // 1: Assign variables to iframe in context
  79. // 2: Place generated html from (1) into skeleton
  80. // write file
  81. posthtml(
  82. expressions({
  83. locals: {
  84. d3: config.html.visualisation.name,
  85. width: `${config.width}px`,
  86. height: `${config.height}px`
  87. }
  88. })
  89. ).process(converted)
  90. .then((result) => posthtml(expressions({locals: { content: result.html }}))
  91. .process(skeleton)
  92. .then((result) => {
  93. fs.writeFileSync(
  94. path.join(
  95. '../',
  96. config.path.output,
  97. config.html.index.name
  98. ), result.html
  99. );
  100. }));
  101. }
  102. function createHtml() {
  103. createVisualisationHtml();
  104. createContextHtml();
  105. }
  106. createDirectory(path.join('../', config.path.output));
  107. createHtml();